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;
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.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;
private ProviderSettingsHelper() {
}
public static String getAction(ProviderSettingsStatus providerSettingsStatus) {
switch (providerSettingsStatus) {
case ACTION_APPLICATION_DETAILS_SETTINGS:
return "android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS";
case APPLICATION_DETAILS:
return ACTION_APPLICATION_DETAILS_SETTINGS;
case SETTINGS:
default:
return "android.provider.Settings";
return ACTION_SETTINGS;
}
}

View File

@ -6,7 +6,7 @@ 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);
Task<Boolean> showPermissionRequestDialog(Activity activity, String message, String cancel);
void showError(Activity activity, String message);
}

View File

@ -27,7 +27,7 @@ public class AppParams {
/* SafeMobile Dispatch */
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 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;
@ -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);
@ -2239,7 +2243,8 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
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;
@ -2276,23 +2281,38 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
getRadioActivity().allVehicle = getAllVehicle();
getRadioActivity().UpdateVehicle();
}
}
else {
} else {
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()));
}
}
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);
}
}
}

View File

@ -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<Boolean> showDialog(Activity activity, String title, String message, String cancel, String ok) {
@ -32,19 +29,22 @@ public class DialogService implements IDialogService {
}
@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));
public Task<Boolean> 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));
}
}

View File

@ -257,7 +257,11 @@
<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="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>