SD-188 - request audio permission on radio tab select

This commit is contained in:
Bogdan Ungureanu 2022-03-23 19:50:15 +02:00
parent 34c578fdf5
commit 66625bcdd1
8 changed files with 187 additions and 3 deletions

View File

@ -0,0 +1,5 @@
package com.safemobile.enums;
public enum ProviderSettingsStatus {
ACTION_APPLICATION_DETAILS_SETTINGS
}

View File

@ -0,0 +1,29 @@
package com.safemobile.helpers;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import com.safemobile.enums.ProviderSettingsStatus;
public class ProviderSettingsHelper {
private static final String PACKAGE = "package";
public static String getAction(ProviderSettingsStatus providerSettingsStatus){
switch (providerSettingsStatus){
case ACTION_APPLICATION_DETAILS_SETTINGS:
return "android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS";
default:
return "android.provider.Settings";
}
}
public static boolean canHandleAction(Context context, String action) {
Intent intent = new Intent(action);
Uri uri = Uri.fromParts(PACKAGE, context.getPackageName(), null);
intent.setData(uri);
return context.getPackageManager() != null && intent.resolveActivity(context.getPackageManager()) != null;
}
}

View File

@ -0,0 +1,12 @@
package com.safemobile.interfaces;
import android.app.Activity;
import com.google.android.gms.tasks.Task;
public interface IDialogService {
Task<Boolean> showDialog(Activity activity, String title, String message, String cancel, String ok);
Task<Boolean> showPermissionRequestDialog(Activity activity, String title, String message, String cancel);
void showError(Activity activity, String message);
}

View File

@ -13,21 +13,27 @@ import java.util.Objects;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import com.google.android.gms.tasks.Task;
import com.safemobile.activities.AbstractEmptyActivity; import com.safemobile.activities.AbstractEmptyActivity;
import com.safemobile.activities.AbstractLiveActivity; import com.safemobile.activities.AbstractLiveActivity;
import com.safemobile.activities.AbstractMessagesActivity; import com.safemobile.activities.AbstractMessagesActivity;
import com.safemobile.activities.AbstractRadioActivity; import com.safemobile.activities.AbstractRadioActivity;
import com.safemobile.activities.AbstractSDParentActivity; import com.safemobile.activities.AbstractSDParentActivity;
import com.safemobile.enums.AuthorizationCode;
import com.safemobile.enums.AuthorizationStatus;
import com.safemobile.interfaces.IPermissionModule;
import com.safemobile.interfaces.ITCPListener; import com.safemobile.interfaces.ITCPListener;
import com.safemobile.interfaces.TCPEvent; import com.safemobile.interfaces.TCPEvent;
import com.safemobile.lib.Alarm; import com.safemobile.lib.Alarm;
import com.safemobile.lib.AlarmMSG; import com.safemobile.lib.AlarmMSG;
import com.safemobile.lib.AppParams; import com.safemobile.lib.AppParams;
import com.safemobile.lib.DialogService;
import com.safemobile.lib.GPSmsg; import com.safemobile.lib.GPSmsg;
import com.safemobile.lib.HistCountmsg; import com.safemobile.lib.HistCountmsg;
import com.safemobile.lib.HistPos; import com.safemobile.lib.HistPos;
import com.safemobile.lib.HistPosmsg; import com.safemobile.lib.HistPosmsg;
import com.safemobile.lib.OperationCodes; import com.safemobile.lib.OperationCodes;
import com.safemobile.lib.PermissionModule;
import com.safemobile.lib.RadioMSG; import com.safemobile.lib.RadioMSG;
import com.safemobile.lib.LastPos; import com.safemobile.lib.LastPos;
import com.safemobile.lib.LastPosmsg; import com.safemobile.lib.LastPosmsg;
@ -46,6 +52,7 @@ import com.safemobile.services.TCPService;
import com.safemobile.services.TCPhandler; import com.safemobile.services.TCPhandler;
import com.safemobile.services.TCPService.TCPBinder; import com.safemobile.services.TCPService.TCPBinder;
import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Dialog; import android.app.Dialog;
import android.app.Notification; import android.app.Notification;
@ -57,6 +64,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
@ -81,6 +89,8 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.widget.TabHost.TabSpec; import android.widget.TabHost.TabSpec;
import androidx.annotation.NonNull;
public class TabLayoutActivity extends AbstractSDParentActivity { public class TabLayoutActivity extends AbstractSDParentActivity {
/* Misc */ /* Misc */
@ -562,6 +572,15 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
buttonRadio.setVisibility(View.GONE); buttonRadio.setVisibility(View.GONE);
buttonRadio.setOnClickListener(v -> { buttonRadio.setOnClickListener(v -> {
if (!buttonRadio.isSelected()) { if (!buttonRadio.isSelected()) {
// check audio permission
IPermissionModule permissionModule = new PermissionModule();
AuthorizationStatus audioAuthorizationStatus = permissionModule.getAuthorizationStatus(this, AuthorizationCode.RECORD_AUDIO);
if (audioAuthorizationStatus != AuthorizationStatus.AUTHORIZE) {
permissionModule.requestAuthorization(this, AuthorizationCode.RECORD_AUDIO);
return;
}
// select button // select button
buttonRadio.setSelected(true); buttonRadio.setSelected(true);
// deselect other buttons // deselect other buttons
@ -2209,4 +2228,71 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
intent.putExtra("extra", new SerializedObject(object, action)); intent.putExtra("extra", new SerializedObject(object, action));
getBaseContext().sendBroadcast(intent); getBaseContext().sendBroadcast(intent);
} }
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
SM.Debug("onRequestPermissionsResult", "[requestCode] " + requestCode );
String permission = "";
switch (requestCode) {
case PermissionModule.RECORD_AUDIO_PERMISSION_REQUEST_CODE:
permission = Manifest.permission.RECORD_AUDIO;
break;
default: throw new IllegalStateException("Unexpected value: " + requestCode);
}
AuthorizationStatus result;
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
result = AuthorizationStatus.AUTHORIZE;
} else {
result = AuthorizationStatus.DENIED;
}
if (permissions.length == 0)
return;
if (permissions[0].equals(Manifest.permission.RECORD_AUDIO)){
onAudioPermissionChanged(result);
}
}
private void onAudioPermissionChanged(AuthorizationStatus result) {
if (result == AuthorizationStatus.AUTHORIZE){
// select button
buttonRadio.setSelected(true);
// deselect other buttons
buttonAlarms.setSelected(false);
buttonLive.setSelected(false);
buttonRecordings.setSelected(false);
buttonHistory.setSelected(false);
buttonSetup.setSelected(false);
buttonText.setSelected(false);
// select tab
tabHost.setCurrentTabByTag(RADIO);
AppParams.crtTab = AppParams.Tabs.radio;
if (getRadioActivity() != null && getRadioActivity().allVehicle != null && getRadioActivity().allVehicle.isEmpty()) {
getRadioActivity().allVehicle = getAllVehicle();
getRadioActivity().UpdateVehicle();
}
}
else {
DialogService dialogService = new DialogService();
Task<Boolean> responseTask = dialogService.showPermissionRequestDialog(this, "title", "permissions denied", getString(R.string.cancel));
responseTask.addOnCompleteListener(task -> waitForUserInput(task.getResult()));
}
}
private void waitForUserInput(Boolean result) {
if (result)
{
//navigate to phone's settings
//ToDo: make the navigation .. see ProviderSettingsHelper
}
else
{
}
}
} }

View File

@ -0,0 +1,50 @@
package com.safemobile.lib;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.safemobile.dispatch.R;
import com.safemobile.interfaces.IDialogService;
import java.util.concurrent.atomic.AtomicBoolean;
public class DialogService implements IDialogService {
@Override
public Task<Boolean> showDialog(Activity activity, String title, String message, String cancel, String ok) {
TaskCompletionSource<Boolean> taskCompletionSource = new TaskCompletionSource<>();
AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
.setCancelable(false)
.setNegativeButton(cancel, (dialog, which) -> {
taskCompletionSource.trySetResult(false);
dialog.dismiss();
})
.setPositiveButton(ok, (dialog, which) -> taskCompletionSource.trySetResult(true));
AlertDialog dialog = builder.create();
dialog.show();
return taskCompletionSource.getTask();
}
@Override
public Task<Boolean> showPermissionRequestDialog(Activity activity, String title, String message, String cancel) {
return showDialog(activity,title,message,cancel, activity.getString(R.string.go_to_settings));
}
@Override
public void showError(Activity activity, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setTitle(R.string.DialoService_Info_ErrorMessage)
.setMessage(message)
.setCancelable(false)
.setNeutralButton(R.string.ok, (dialog, which) -> dialog.dismiss());
AlertDialog dialog = builder.create();
dialog.show();
}
}

View File

@ -13,7 +13,7 @@ import com.safemobile.interfaces.IPermissionModule;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
public class PermissionService implements IPermissionModule { public class PermissionModule implements IPermissionModule {
private static final String[] AUDIO_PERMISSIONS = {Manifest.permission.RECORD_AUDIO}; private static final String[] AUDIO_PERMISSIONS = {Manifest.permission.RECORD_AUDIO};
private static final String[] LOCATION_PERMISSIONS = { private static final String[] LOCATION_PERMISSIONS = {
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,

View File

@ -24,14 +24,14 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="8dp" android:paddingTop="8dp"
android:textSize="18dp" android:textSize="18dp"
android:textColor="#FFFFFF" android:textColor="@color/black"
/> />
<TextView android:id="@+id/text2" <TextView android:id="@+id/text2"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="8dp" android:paddingTop="8dp"
android:textSize="18dp" android:textSize="18dp"
android:textColor="#FFFFFF" android:textColor="@color/black"
/> />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -257,5 +257,7 @@
<string name="speedMph">%1$d mph</string> <string name="speedMph">%1$d mph</string>
<string name="DialoService_Info_ErrorMessage">Oops, something happened</string>
<string name="go_to_settings">Go to settings</string>
</resources> </resources>