From 81937a5e0fe6349fbeb75737da974ae418a9b628 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Thu, 24 Mar 2022 12:16:30 +0200 Subject: [PATCH] SD-188 - navigate to phone settings if permission is denied --- .../enums/ProviderSettingsStatus.java | 3 +- .../helpers/ProviderSettingsHelper.java | 20 +++++-- .../safemobile/interfaces/IDialogService.java | 2 +- .../java/com/safemobile/lib/AppParams.java | 2 +- .../dispatch/TabLayoutActivity.java | 56 +++++++++++++------ .../com/safemobile/lib/DialogService.java | 28 +++++----- safeDispatch/src/main/res/values/strings.xml | 6 +- 7 files changed, 75 insertions(+), 42 deletions(-) diff --git a/libSafeMobile/src/main/java/com/safemobile/enums/ProviderSettingsStatus.java b/libSafeMobile/src/main/java/com/safemobile/enums/ProviderSettingsStatus.java index bc7f2fb..61d6922 100644 --- a/libSafeMobile/src/main/java/com/safemobile/enums/ProviderSettingsStatus.java +++ b/libSafeMobile/src/main/java/com/safemobile/enums/ProviderSettingsStatus.java @@ -1,5 +1,6 @@ package com.safemobile.enums; public enum ProviderSettingsStatus { - ACTION_APPLICATION_DETAILS_SETTINGS + APPLICATION_DETAILS, + SETTINGS } diff --git a/libSafeMobile/src/main/java/com/safemobile/helpers/ProviderSettingsHelper.java b/libSafeMobile/src/main/java/com/safemobile/helpers/ProviderSettingsHelper.java index 60d2fa8..db91e0a 100644 --- a/libSafeMobile/src/main/java/com/safemobile/helpers/ProviderSettingsHelper.java +++ b/libSafeMobile/src/main/java/com/safemobile/helpers/ProviderSettingsHelper.java @@ -3,18 +3,26 @@ package com.safemobile.helpers; import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.provider.Settings; import com.safemobile.enums.ProviderSettingsStatus; 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){ - switch (providerSettingsStatus){ - case ACTION_APPLICATION_DETAILS_SETTINGS: - return "android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS"; + private ProviderSettingsHelper() { + + } + + public static String getAction(ProviderSettingsStatus providerSettingsStatus) { + switch (providerSettingsStatus) { + case APPLICATION_DETAILS: + return ACTION_APPLICATION_DETAILS_SETTINGS; + case SETTINGS: default: - return "android.provider.Settings"; + return ACTION_SETTINGS; } } diff --git a/libSafeMobile/src/main/java/com/safemobile/interfaces/IDialogService.java b/libSafeMobile/src/main/java/com/safemobile/interfaces/IDialogService.java index 6d0601b..c71fc61 100644 --- a/libSafeMobile/src/main/java/com/safemobile/interfaces/IDialogService.java +++ b/libSafeMobile/src/main/java/com/safemobile/interfaces/IDialogService.java @@ -6,7 +6,7 @@ import com.google.android.gms.tasks.Task; public interface IDialogService { Task showDialog(Activity activity, String title, String message, String cancel, String ok); - Task showPermissionRequestDialog(Activity activity, String title, String message, String cancel); + Task showPermissionRequestDialog(Activity activity, String message, String cancel); void showError(Activity activity, String message); } diff --git a/libSafeMobile/src/main/java/com/safemobile/lib/AppParams.java b/libSafeMobile/src/main/java/com/safemobile/lib/AppParams.java index d5fca8c..96b4dea 100644 --- a/libSafeMobile/src/main/java/com/safemobile/lib/AppParams.java +++ b/libSafeMobile/src/main/java/com/safemobile/lib/AppParams.java @@ -27,7 +27,7 @@ public class AppParams { /* SafeMobile Dispatch */ public static ArrayList allUsers = new ArrayList(); - public static Theme theme = Theme.SAFENET; // the Theme type + public static Theme theme = Theme.SAFEDISPATCH; // the Theme type /* ***************************************** */ diff --git a/safeDispatch/src/main/java/com/safemobile/dispatch/TabLayoutActivity.java b/safeDispatch/src/main/java/com/safemobile/dispatch/TabLayoutActivity.java index cd95b6c..df561d7 100644 --- a/safeDispatch/src/main/java/com/safemobile/dispatch/TabLayoutActivity.java +++ b/safeDispatch/src/main/java/com/safemobile/dispatch/TabLayoutActivity.java @@ -14,6 +14,7 @@ import java.util.Timer; import java.util.TimerTask; import com.google.android.gms.tasks.Task; +import com.google.android.gms.tasks.TaskCompletionSource; import com.safemobile.activities.AbstractEmptyActivity; import com.safemobile.activities.AbstractLiveActivity; import com.safemobile.activities.AbstractMessagesActivity; @@ -21,6 +22,8 @@ import com.safemobile.activities.AbstractRadioActivity; import com.safemobile.activities.AbstractSDParentActivity; import com.safemobile.enums.AuthorizationCode; import com.safemobile.enums.AuthorizationStatus; +import com.safemobile.enums.ProviderSettingsStatus; +import com.safemobile.helpers.ProviderSettingsHelper; import com.safemobile.interfaces.IPermissionModule; import com.safemobile.interfaces.ITCPListener; import com.safemobile.interfaces.TCPEvent; @@ -880,7 +883,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity { tabHost.setCurrentTabByTag(TEXT); AppParams.crtTab = AppParams.Tabs.message; Vehicle vehicle = getVehicle4Imei(getImei()); - if(vehicle != null) + if (vehicle != null) getMessageActivity().setScId(vehicle.sc_id); getMessageActivity().LASTMESSAGES = false; getMessageActivity().getLastSMS(); @@ -1057,7 +1060,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity { SM.Debug("########### UPDATE"); int scId = demoPosition % 3 == 0 ? 101 : 102; SuperVehicle superVehicle = (getSuperVehHash().get((long) scId)); - if(superVehicle != null) + if (superVehicle != null) superVehicle.SetNewPosition(crtPos.lat, crtPos.lng, Calendar.getInstance().getTime().getTime(), crtPos.speed); getLiveActivity().refreshMap(); } @@ -1334,7 +1337,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity { myHandler.post(updateResults); } else myHandler.post(() -> - createNotification(AppParams.messageNotif)); + createNotification(AppParams.messageNotif)); //list for SMS if (AppParams.crtTab == AppParams.Tabs.message && getMessageActivity() != null) { @@ -1782,7 +1785,8 @@ public class TabLayoutActivity extends AbstractSDParentActivity { Vehicle vehicle = getVehicle4Imei(getImei()); if (vehicle != null) 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())); // show layout layoutNewMessage.setVisibility(View.VISIBLE); @@ -2233,13 +2237,14 @@ public class TabLayoutActivity extends AbstractSDParentActivity { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); - SM.Debug("onRequestPermissionsResult", "[requestCode] " + requestCode ); + 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); + default: + throw new IllegalStateException("Unexpected value: " + requestCode); } AuthorizationStatus result; @@ -2252,13 +2257,13 @@ public class TabLayoutActivity extends AbstractSDParentActivity { if (permissions.length == 0) return; - if (permissions[0].equals(Manifest.permission.RECORD_AUDIO)){ + if (permissions[0].equals(Manifest.permission.RECORD_AUDIO)) { onAudioPermissionChanged(result); } } private void onAudioPermissionChanged(AuthorizationStatus result) { - if (result == AuthorizationStatus.AUTHORIZE){ + if (result == AuthorizationStatus.AUTHORIZE) { // select button buttonRadio.setSelected(true); // deselect other buttons @@ -2276,23 +2281,38 @@ public class TabLayoutActivity extends AbstractSDParentActivity { getRadioActivity().allVehicle = getAllVehicle(); getRadioActivity().UpdateVehicle(); } - } - else { + } else { DialogService dialogService = new DialogService(); - Task responseTask = dialogService.showPermissionRequestDialog(this, "title", "permissions denied", getString(R.string.cancel)); + Task 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())); } } private void waitForUserInput(Boolean result) { - if (result) - { - //navigate to phone's settings - //ToDo: make the navigation .. see ProviderSettingsHelper - } - else - { + if (result) { + final int requestCode = 1202; + //navigate to application's settings + String action = ProviderSettingsHelper.getAction(ProviderSettingsStatus.APPLICATION_DETAILS); + if (action == null || action == "") + 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); } } } diff --git a/safeDispatch/src/main/java/com/safemobile/lib/DialogService.java b/safeDispatch/src/main/java/com/safemobile/lib/DialogService.java index abd3cbe..a69aa08 100644 --- a/safeDispatch/src/main/java/com/safemobile/lib/DialogService.java +++ b/safeDispatch/src/main/java/com/safemobile/lib/DialogService.java @@ -2,15 +2,12 @@ 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 showDialog(Activity activity, String title, String message, String cancel, String ok) { @@ -20,7 +17,7 @@ public class DialogService implements IDialogService { .setMessage(message) .setCancelable(false) .setNegativeButton(cancel, (dialog, which) -> { - taskCompletionSource.trySetResult(false); + taskCompletionSource.trySetResult(false); dialog.dismiss(); }) .setPositiveButton(ok, (dialog, which) -> taskCompletionSource.trySetResult(true)); @@ -32,19 +29,22 @@ public class DialogService implements IDialogService { } @Override - public Task showPermissionRequestDialog(Activity activity, String title, String message, String cancel) { - return showDialog(activity,title,message,cancel, activity.getString(R.string.go_to_settings)); + public Task showPermissionRequestDialog(Activity activity, String message, String cancel) { + return showDialog( + activity, + activity.getString(R.string.permission_denied), + 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(); + showDialog( + activity, + activity.getString(R.string.DialogService_Info_ErrorMessage), + message, + null, + activity.getString(R.string.ok)); } } diff --git a/safeDispatch/src/main/res/values/strings.xml b/safeDispatch/src/main/res/values/strings.xml index 0c72035..3cfa5b7 100644 --- a/safeDispatch/src/main/res/values/strings.xml +++ b/safeDispatch/src/main/res/values/strings.xml @@ -257,7 +257,11 @@ %1$d mph - Oops, something happened + Oops, something happened Go to settings + Permission Denied + %1$s permission was not accepted. To be able to use the %2$s, please open application settings and grant the %3$s permission + Microphone + Radio Tab \ No newline at end of file