From b89467c4bb99545773e498fb12a1fffeb19ca8a4 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Wed, 18 May 2022 13:22:24 +0100 Subject: [PATCH] When we need permissions, we now pop up a dialog asking nicely and explaining why. Also instructions, since it may be unclear for new users. --- .../activities/ControlCenterv2.java | 74 +++++++++++++++---- app/src/main/res/values/strings.xml | 2 + 2 files changed, 62 insertions(+), 14 deletions(-) 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 e63c4d23e..9bd0768c1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java @@ -19,9 +19,12 @@ package nodomain.freeyourgadget.gadgetbridge.activities; import android.Manifest; import android.annotation.TargetApi; +import android.app.AlertDialog; +import android.app.Dialog; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; @@ -44,6 +47,7 @@ import androidx.core.app.NotificationManagerCompat; import androidx.core.content.ContextCompat; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; +import androidx.fragment.app.DialogFragment; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -243,12 +247,26 @@ public class ControlCenterv2 extends AppCompatActivity Set set = NotificationManagerCompat.getEnabledListenerPackages(this); if (pesterWithPermissions) { if (!set.contains(this.getPackageName())) { // If notification listener access hasn't been granted - Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); - startActivity(enableIntent); + // 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"); } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + /* In order to be able to set ringer mode to silent in GB's PhoneCallReceiver + the permission to access notifications is needed above Android M + ACCESS_NOTIFICATION_POLICY is also needed in the manifest */ + if (pesterWithPermissions) { + if (!((NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE)).isNotificationPolicyAccessGranted()) { + // 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"); + } + } + // Check all the other permissions that we need to for Android M + later checkAndRequestPermissions(); } @@ -472,18 +490,6 @@ public class ControlCenterv2 extends AppCompatActivity } } - /* In order to be able to set ringer mode to silent in GB's PhoneCallReceiver - the permission to access notifications is needed above Android M - ACCESS_NOTIFICATION_POLICY is also needed in the manifest */ - if (pesterWithPermissions) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (!((NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE)).isNotificationPolicyAccessGranted()) { - GB.toast(this, getString(R.string.permission_granting_mandatory), Toast.LENGTH_LONG, GB.ERROR); - startActivity(new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)); - } - } - } - // HACK: On Lineage we have to do this so that the permission dialog pops up if (fakeStateListener == null) { fakeStateListener = new PhoneStateListener(); @@ -533,4 +539,44 @@ public class ControlCenterv2 extends AppCompatActivity } } + + /// Called from onCreate - this puts up a dialog explaining we need permissions, and goes to the correct Activity + public static class NotifyPolicyPermissionsDialogFragment extends DialogFragment { + @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_notification_policy_access, + 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) { + startActivity(new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)); + } + }); + return builder.create(); + } + } + + /// Called from onCreate - this puts up a dialog explaining we need permissions, and goes to the correct Activity + public static class NotifyListenerPermissionsDialogFragment extends DialogFragment { + @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_notification_listener, + 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 enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); + startActivity(enableIntent); + } + }); + return builder.create(); + } + } + } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e35a6b21c..358377974 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1138,6 +1138,8 @@ 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 CAUTION: Error when checking version information! You should not continue! Saw version name \"%s\" Location must be enabled CompanionDevice Pairing