SD-188 - navigate to phone settings if permission is denied

This commit is contained in:
Bogdan Ungureanu 2022-03-24 12:16:30 +02:00
parent 66625bcdd1
commit 81937a5e0f
7 changed files with 75 additions and 42 deletions

View File

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

View File

@ -3,18 +3,26 @@ package com.safemobile.helpers;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.provider.Settings;
import com.safemobile.enums.ProviderSettingsStatus; import com.safemobile.enums.ProviderSettingsStatus;
public class ProviderSettingsHelper { public class ProviderSettingsHelper {
private static final String PACKAGE = "package"; public static final String PACKAGE = "package";
public static final String ACTION_APPLICATION_DETAILS_SETTINGS = Settings.ACTION_APPLICATION_DETAILS_SETTINGS;
public static final String ACTION_SETTINGS = Settings.ACTION_SETTINGS;
public static String getAction(ProviderSettingsStatus providerSettingsStatus){ private ProviderSettingsHelper() {
switch (providerSettingsStatus){
case ACTION_APPLICATION_DETAILS_SETTINGS: }
return "android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS";
public static String getAction(ProviderSettingsStatus providerSettingsStatus) {
switch (providerSettingsStatus) {
case APPLICATION_DETAILS:
return ACTION_APPLICATION_DETAILS_SETTINGS;
case SETTINGS:
default: default:
return "android.provider.Settings"; return ACTION_SETTINGS;
} }
} }

View File

@ -6,7 +6,7 @@ import com.google.android.gms.tasks.Task;
public interface IDialogService { public interface IDialogService {
Task<Boolean> showDialog(Activity activity, String title, String message, String cancel, String ok); Task<Boolean> showDialog(Activity activity, String title, String message, String cancel, String ok);
Task<Boolean> showPermissionRequestDialog(Activity activity, String title, String message, String cancel); Task<Boolean> showPermissionRequestDialog(Activity activity, String message, String cancel);
void showError(Activity activity, String message); void showError(Activity activity, String message);
} }

View File

@ -27,7 +27,7 @@ public class AppParams {
/* SafeMobile Dispatch */ /* SafeMobile Dispatch */
public static ArrayList<User> allUsers = new ArrayList<User>(); public static ArrayList<User> allUsers = new ArrayList<User>();
public static Theme theme = Theme.SAFENET; // the Theme type public static Theme theme = Theme.SAFEDISPATCH; // the Theme type
/* ***************************************** */ /* ***************************************** */

View File

@ -14,6 +14,7 @@ import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
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;
@ -21,6 +22,8 @@ import com.safemobile.activities.AbstractRadioActivity;
import com.safemobile.activities.AbstractSDParentActivity; import com.safemobile.activities.AbstractSDParentActivity;
import com.safemobile.enums.AuthorizationCode; import com.safemobile.enums.AuthorizationCode;
import com.safemobile.enums.AuthorizationStatus; import com.safemobile.enums.AuthorizationStatus;
import com.safemobile.enums.ProviderSettingsStatus;
import com.safemobile.helpers.ProviderSettingsHelper;
import com.safemobile.interfaces.IPermissionModule; 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;
@ -880,7 +883,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
tabHost.setCurrentTabByTag(TEXT); tabHost.setCurrentTabByTag(TEXT);
AppParams.crtTab = AppParams.Tabs.message; AppParams.crtTab = AppParams.Tabs.message;
Vehicle vehicle = getVehicle4Imei(getImei()); Vehicle vehicle = getVehicle4Imei(getImei());
if(vehicle != null) if (vehicle != null)
getMessageActivity().setScId(vehicle.sc_id); getMessageActivity().setScId(vehicle.sc_id);
getMessageActivity().LASTMESSAGES = false; getMessageActivity().LASTMESSAGES = false;
getMessageActivity().getLastSMS(); getMessageActivity().getLastSMS();
@ -1057,7 +1060,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
SM.Debug("########### UPDATE"); SM.Debug("########### UPDATE");
int scId = demoPosition % 3 == 0 ? 101 : 102; int scId = demoPosition % 3 == 0 ? 101 : 102;
SuperVehicle superVehicle = (getSuperVehHash().get((long) scId)); SuperVehicle superVehicle = (getSuperVehHash().get((long) scId));
if(superVehicle != null) if (superVehicle != null)
superVehicle.SetNewPosition(crtPos.lat, crtPos.lng, Calendar.getInstance().getTime().getTime(), crtPos.speed); superVehicle.SetNewPosition(crtPos.lat, crtPos.lng, Calendar.getInstance().getTime().getTime(), crtPos.speed);
getLiveActivity().refreshMap(); getLiveActivity().refreshMap();
} }
@ -1782,7 +1785,8 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
Vehicle vehicle = getVehicle4Imei(getImei()); Vehicle vehicle = getVehicle4Imei(getImei());
if (vehicle != null) if (vehicle != null)
textViewNMFrom.setText(String.format("%s: %s", getString(R.string.from), vehicle.name)); textViewNMFrom.setText(String.format("%s: %s", getString(R.string.from), vehicle.name));
else textViewNMFrom.setText(String.format("%s: %s", getString(R.string.from), getImei())); else
textViewNMFrom.setText(String.format("%s: %s", getString(R.string.from), getImei()));
textViewNMMessage.setText(String.format("TYPE: %s", getMess())); textViewNMMessage.setText(String.format("TYPE: %s", getMess()));
// show layout // show layout
layoutNewMessage.setVisibility(View.VISIBLE); layoutNewMessage.setVisibility(View.VISIBLE);
@ -2233,13 +2237,14 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);
SM.Debug("onRequestPermissionsResult", "[requestCode] " + requestCode ); SM.Debug("onRequestPermissionsResult", "[requestCode] " + requestCode);
String permission = ""; String permission = "";
switch (requestCode) { switch (requestCode) {
case PermissionModule.RECORD_AUDIO_PERMISSION_REQUEST_CODE: case PermissionModule.RECORD_AUDIO_PERMISSION_REQUEST_CODE:
permission = Manifest.permission.RECORD_AUDIO; permission = Manifest.permission.RECORD_AUDIO;
break; break;
default: throw new IllegalStateException("Unexpected value: " + requestCode); default:
throw new IllegalStateException("Unexpected value: " + requestCode);
} }
AuthorizationStatus result; AuthorizationStatus result;
@ -2252,13 +2257,13 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
if (permissions.length == 0) if (permissions.length == 0)
return; return;
if (permissions[0].equals(Manifest.permission.RECORD_AUDIO)){ if (permissions[0].equals(Manifest.permission.RECORD_AUDIO)) {
onAudioPermissionChanged(result); onAudioPermissionChanged(result);
} }
} }
private void onAudioPermissionChanged(AuthorizationStatus result) { private void onAudioPermissionChanged(AuthorizationStatus result) {
if (result == AuthorizationStatus.AUTHORIZE){ if (result == AuthorizationStatus.AUTHORIZE) {
// select button // select button
buttonRadio.setSelected(true); buttonRadio.setSelected(true);
// deselect other buttons // deselect other buttons
@ -2276,23 +2281,38 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
getRadioActivity().allVehicle = getAllVehicle(); getRadioActivity().allVehicle = getAllVehicle();
getRadioActivity().UpdateVehicle(); getRadioActivity().UpdateVehicle();
} }
} } else {
else {
DialogService dialogService = new DialogService(); DialogService dialogService = new DialogService();
Task<Boolean> responseTask = dialogService.showPermissionRequestDialog(this, "title", "permissions denied", getString(R.string.cancel)); Task<Boolean> responseTask = dialogService.showPermissionRequestDialog(this,
getString(R.string.permissionBlocked, getString(R.string.microphone), getString(R.string.radio_tab), getString(R.string.microphone)),
getString(R.string.cancel));
responseTask.addOnCompleteListener(task -> waitForUserInput(task.getResult())); responseTask.addOnCompleteListener(task -> waitForUserInput(task.getResult()));
} }
} }
private void waitForUserInput(Boolean result) { private void waitForUserInput(Boolean result) {
if (result) if (result) {
{ final int requestCode = 1202;
//navigate to phone's settings //navigate to application's settings
//ToDo: make the navigation .. see ProviderSettingsHelper String action = ProviderSettingsHelper.getAction(ProviderSettingsStatus.APPLICATION_DETAILS);
} if (action == null || action == "")
else return;
{
boolean isActionApplicationDetailsSettings = action.equals(ProviderSettingsHelper.ACTION_APPLICATION_DETAILS_SETTINGS);
if (!ProviderSettingsHelper.canHandleAction(context, action))
action = ProviderSettingsHelper.getAction(ProviderSettingsStatus.SETTINGS);
Intent intent = new Intent(action);
if (isActionApplicationDetailsSettings) {
Uri uri = Uri.fromParts(ProviderSettingsHelper.PACKAGE, this.getPackageName(), null);
intent.setData(uri);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
} }
} }
} }

View File

@ -2,15 +2,12 @@ package com.safemobile.lib;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource; import com.google.android.gms.tasks.TaskCompletionSource;
import com.safemobile.dispatch.R; import com.safemobile.dispatch.R;
import com.safemobile.interfaces.IDialogService; import com.safemobile.interfaces.IDialogService;
import java.util.concurrent.atomic.AtomicBoolean;
public class DialogService implements IDialogService { public class DialogService implements IDialogService {
@Override @Override
public Task<Boolean> showDialog(Activity activity, String title, String message, String cancel, String ok) { public Task<Boolean> showDialog(Activity activity, String title, String message, String cancel, String ok) {
@ -32,19 +29,22 @@ public class DialogService implements IDialogService {
} }
@Override @Override
public Task<Boolean> showPermissionRequestDialog(Activity activity, String title, String message, String cancel) { public Task<Boolean> showPermissionRequestDialog(Activity activity, String message, String cancel) {
return showDialog(activity,title,message,cancel, activity.getString(R.string.go_to_settings)); return showDialog(
activity,
activity.getString(R.string.permission_denied),
message,
cancel,
activity.getString(R.string.go_to_settings));
} }
@Override @Override
public void showError(Activity activity, String message) { public void showError(Activity activity, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity) showDialog(
.setTitle(R.string.DialoService_Info_ErrorMessage) activity,
.setMessage(message) activity.getString(R.string.DialogService_Info_ErrorMessage),
.setCancelable(false) message,
.setNeutralButton(R.string.ok, (dialog, which) -> dialog.dismiss()); null,
activity.getString(R.string.ok));
AlertDialog dialog = builder.create();
dialog.show();
} }
} }

View File

@ -257,7 +257,11 @@
<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="DialogService_Info_ErrorMessage">Oops, something happened</string>
<string name="go_to_settings">Go to settings</string> <string name="go_to_settings">Go to settings</string>
<string name="permission_denied">Permission Denied</string>
<string name="permissionBlocked">%1$s permission was not accepted. To be able to use the %2$s, please open application settings and grant the %3$s permission</string>
<string name="microphone">Microphone</string>
<string name="radio_tab">Radio Tab</string>
</resources> </resources>