diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java index 9bd0768c1..e11ce8ae6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java @@ -89,6 +89,8 @@ public class ControlCenterv2 extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, GBActivity { public static final int MENU_REFRESH_CODE = 1; + public static final String ACTION_REQUEST_PERMISSIONS + = "nodomain.freeyourgadget.gadgetbridge.activities.controlcenter.requestpermissions"; private static PhoneStateListener fakeStateListener; //needed for KK compatibility @@ -123,6 +125,9 @@ public class ControlCenterv2 extends AppCompatActivity case DeviceService.ACTION_REALTIME_SAMPLES: handleRealtimeSample(intent.getSerializableExtra(DeviceService.EXTRA_REALTIME_SAMPLE)); break; + case ACTION_REQUEST_PERMISSIONS: + checkAndRequestPermissions(false); + break; } } }; @@ -234,6 +239,7 @@ public class ControlCenterv2 extends AppCompatActivity filterLocal.addAction(GBApplication.ACTION_NEW_DATA); filterLocal.addAction(DeviceManager.ACTION_DEVICES_CHANGED); filterLocal.addAction(DeviceService.ACTION_REALTIME_SAMPLES); + filterLocal.addAction(ACTION_REQUEST_PERMISSIONS); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal); refreshPairedDevices(); @@ -250,7 +256,7 @@ public class ControlCenterv2 extends AppCompatActivity // Put up a dialog explaining why we need permissions (Polite, but also Play Store policy) // When accepted, we open the Activity for Notification access DialogFragment dialog = new NotifyListenerPermissionsDialogFragment(); - dialog.show(getSupportFragmentManager(), "PermissionsDialogFragment"); + dialog.show(getSupportFragmentManager(), "NotifyListenerPermissionsDialogFragment"); } } @@ -263,11 +269,11 @@ public class ControlCenterv2 extends AppCompatActivity // Put up a dialog explaining why we need permissions (Polite, but also Play Store policy) // When accepted, we open the Activity for Notification access DialogFragment dialog = new NotifyPolicyPermissionsDialogFragment(); - dialog.show(getSupportFragmentManager(), "PermissionsDialogFragment"); + dialog.show(getSupportFragmentManager(), "NotifyPolicyPermissionsDialogFragment"); } } // Check all the other permissions that we need to for Android M + later - checkAndRequestPermissions(); + checkAndRequestPermissions(true); } ChangeLog cl = createChangeLog(); @@ -405,7 +411,7 @@ public class ControlCenterv2 extends AppCompatActivity } @TargetApi(Build.VERSION_CODES.M) - private void checkAndRequestPermissions() { + private void checkAndRequestPermissions(boolean showDialogFirst) { List wantedPermissions = new ArrayList<>(); if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_DENIED) @@ -478,15 +484,20 @@ public class ControlCenterv2 extends AppCompatActivity } } wantedPermissions.removeAll(shouldNotAsk); - } else { + } else if (!showDialogFirst) { // Permissions have not been asked yet, but now will be prefs.getPreferences().edit().putBoolean("permissions_asked", true).apply(); } if (!wantedPermissions.isEmpty()) { - GB.toast(this, getString(R.string.permission_granting_mandatory), Toast.LENGTH_LONG, GB.ERROR); - ActivityCompat.requestPermissions(this, wantedPermissions.toArray(new String[0]), 0); - GB.toast(this, getString(R.string.permission_granting_mandatory), Toast.LENGTH_LONG, GB.ERROR); + if (showDialogFirst) { + // Show a dialog - thus will then call checkAndRequestPermissions(false) + DialogFragment dialog = new LocationPermissionsDialogFragment(); + dialog.show(getSupportFragmentManager(), "LocationPermissionsDialogFragment"); + } else { + GB.toast(this, getString(R.string.permission_granting_mandatory), Toast.LENGTH_LONG, GB.ERROR); + ActivityCompat.requestPermissions(this, wantedPermissions.toArray(new String[0]), 0); + } } } @@ -579,4 +590,25 @@ public class ControlCenterv2 extends AppCompatActivity } } + /// Called from checkAndRequestPermissions - this puts up a dialog explaining we need permissions, and then calls checkAndRequestPermissions (via an intent) when 'ok' pressed + public static class LocationPermissionsDialogFragment extends DialogFragment { + ControlCenterv2 controlCenter; + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + // Use the Builder class for convenient dialog construction + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + Context context = getContext(); + builder.setMessage(context.getString(R.string.permission_location, + getContext().getString(R.string.app_name), + getContext().getString(R.string.ok))) + .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + Intent intent = new Intent(ACTION_REQUEST_PERMISSIONS); + LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent); + } + }); + return builder.create(); + } + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e4c53899b..bfc343204 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1185,8 +1185,9 @@ Many thanks to all unlisted contributors for contributing code, translations, support, ideas, motivation, bug reports, money… ✊ Links All these permissions are required and instability might occur if not granted - %1$s needs access to Notifications in order to display them on your watch.\n\nPlease tap \'%2$s\' then \'%1$s\' and enable \'Allow Notification Access\', then tap \'Back\' to return to %1$s - %1$s needs access to Do Not Disturb settings in order to honour them on your watch.\n\nPlease tap \'%2$s\' then \'%1$s\' and enable \'Allow Do Not Disturb\', then tap \'Back\' to return to %1$s + %1$s needs access to Notifications in order to display them on your watch when your phone\'s screen is off.\n\nPlease tap \'%2$s\' then \'%1$s\' and enable \'Allow Notification Access\', then tap \'Back\' to return to %1$s + %1$s needs access to Do Not Disturb settings in order to honour them on your watch when your phone\'s screen is off.\n\nPlease tap \'%2$s\' then \'%1$s\' and enable \'Allow Do Not Disturb\', then tap \'Back\' to return to %1$s + %1$s needs access to your location in the background to allow it to stay connected to your watch even when your screen is off.\n\nPlease tap \'%2$s\' to agree. CAUTION: Error when checking version information! You should not continue! Saw version name \"%s\" Location must be enabled CompanionDevice Pairing