From 04dc33c863d64e2d805127b257bda25ccb6c50bc Mon Sep 17 00:00:00 2001 From: Yash-Garg Date: Wed, 2 Nov 2022 15:02:40 +0530 Subject: [PATCH] directly show notification settings activity to approve launcher Signed-off-by: Yash-Garg --- .../e/blisslauncher/core/Preferences.java | 10 ++ app/src/main/AndroidManifest.xml | 1 + .../features/launcher/LauncherActivity.java | 103 ++++++++++++++---- app/src/main/res/values/strings.xml | 4 + 4 files changed, 95 insertions(+), 23 deletions(-) diff --git a/app/src/apiOreo/java/foundation/e/blisslauncher/core/Preferences.java b/app/src/apiOreo/java/foundation/e/blisslauncher/core/Preferences.java index 0d57cdac21..409beca403 100644 --- a/app/src/apiOreo/java/foundation/e/blisslauncher/core/Preferences.java +++ b/app/src/apiOreo/java/foundation/e/blisslauncher/core/Preferences.java @@ -42,6 +42,8 @@ public class Preferences { private static final String NOTIFICATION_ACCESS = "notification_access"; + private static final String NOTIFICATION_INFO = "notification_info"; + private static final String ENABLE_LOCATION = "enable_location"; private static final String ACTION_USAGE = "foundation.e.blisslauncher.ACTION_USAGE"; @@ -338,6 +340,14 @@ public class Preferences { getPrefs(context).edit().putBoolean(NOTIFICATION_ACCESS, false).apply(); } + public static void setNotToShowNotificationDialog(Context context) { + getPrefs(context).edit().putBoolean(NOTIFICATION_INFO, false).apply(); + } + + public static boolean shouldShowNotificationDialog(Context context) { + return getPrefs(context).getBoolean(NOTIFICATION_INFO, true); + } + public static int getCurrentMigrationVersion(Context context) { return getPrefs(context).getInt(CURRENT_MIGRATION_VERSION, 0); } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b962ea0a15..770fec5da1 100755 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -70,6 +70,7 @@ android:windowSoftInputMode="adjustPan"> + diff --git a/app/src/main/java/foundation/e/blisslauncher/features/launcher/LauncherActivity.java b/app/src/main/java/foundation/e/blisslauncher/features/launcher/LauncherActivity.java index 3707413adc..38b5a7cfe1 100755 --- a/app/src/main/java/foundation/e/blisslauncher/features/launcher/LauncherActivity.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/launcher/LauncherActivity.java @@ -18,11 +18,13 @@ import android.app.WallpaperManager; import android.app.usage.UsageStats; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProviderInfo; +import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.LauncherApps; import android.content.pm.PackageManager; import android.content.res.Configuration; @@ -92,7 +94,6 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import foundation.e.blisslauncher.BlissLauncher; -import foundation.e.blisslauncher.BuildConfig; import foundation.e.blisslauncher.R; import foundation.e.blisslauncher.core.Alarm; import foundation.e.blisslauncher.core.DeviceProfile; @@ -172,6 +173,14 @@ public class LauncherActivity extends AppCompatActivity private static final int REQUEST_LOCATION_SOURCE_SETTING = 267; private static final int STORAGE_PERMISSION_REQUEST_CODE = 586; public static final String ACTION_LAUNCHER_RESUME = "foundation.e.blisslauncher.LauncherActivity.LAUNCHER_RESUME"; + public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key"; + public static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args"; + public static final String NOTIFICATION_SETTING = "enabled_notification_listeners"; + + private ContentResolver contentResolver; + private String permissionString; + private BroadcastReceiver unlockReceiver; + private ComponentName notificationComponentName; public static boolean longPressed; private final Alarm mReorderAlarm = new Alarm(); @@ -273,6 +282,24 @@ public class LauncherActivity extends AppCompatActivity setTheme(themeRes); } + contentResolver = getContentResolver(); + permissionString = Settings.Secure.getString(contentResolver, NOTIFICATION_SETTING); + notificationComponentName = new ComponentName(this, NotificationService.class); + + // Broadcast receiver to handle notification badges permission when screen is + // unlocked + unlockReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + final String action = intent.getAction(); + + if (Intent.ACTION_USER_PRESENT.equalsIgnoreCase(action) + && Preferences.shouldShowNotificationDialog(context)) { + showNotifPermissionDeniedDialog(); + } + } + }; + oldConfig = new Configuration(getResources().getConfiguration()); mDeviceProfile = BlissLauncher.getApplication(this).getDeviceProfile(); @@ -291,28 +318,7 @@ public class LauncherActivity extends AppCompatActivity mProgressBar.setVisibility(View.VISIBLE); - if (Preferences.shouldAskForNotificationAccess(this)) { - ContentResolver cr = getContentResolver(); - String setting = "enabled_notification_listeners"; - String permissionString = Settings.Secure.getString(cr, setting); - if (permissionString == null || !permissionString.contains(getPackageName())) { - if (BuildConfig.DEBUG) { - startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")); - } else if (!Preferences.shouldAskForNotificationAccess(this)) { - ComponentName cn = new ComponentName(this, NotificationService.class); - if (permissionString == null) { - permissionString = ""; - } else { - permissionString += ":"; - } - permissionString += cn.flattenToString(); - boolean success = Settings.Secure.putString(cr, setting, permissionString); - if (success) { - Preferences.setNotToAskForNotificationAccess(this); - } - } - } - } + registerUnlockBroadcastReceiver(); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { @@ -342,6 +348,12 @@ public class LauncherActivity extends AppCompatActivity mInsetsController = new WindowInsetsControllerCompat(getWindow(), mLauncherView); } + public void registerUnlockBroadcastReceiver() { + IntentFilter unlockFilter = new IntentFilter(); + unlockFilter.addAction(Intent.ACTION_USER_PRESENT); + this.registerReceiver(unlockReceiver, unlockFilter); + } + public View getRootView() { return mLauncherView; } @@ -489,6 +501,23 @@ public class LauncherActivity extends AppCompatActivity protected void onResume() { super.onResume(); + if (!Preferences.shouldAskForNotificationAccess(this)) { + if (Preferences.shouldShowNotificationDialog(this)) { + showNotifPermissionDeniedDialog(); + if (unlockReceiver != null) { + this.unregisterReceiver(unlockReceiver); + } + } + + if (permissionString == null) { + permissionString = ""; + } else { + permissionString += ":"; + } + + permissionString += notificationComponentName.flattenToString(); + } + if (mDepthManager != null) { mDepthManager.updateDepth(); } @@ -1490,6 +1519,34 @@ public class LauncherActivity extends AppCompatActivity } } + private void showNotifPermissionDeniedDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.perm_required); + builder.setMessage(R.string.notification_badge_warning); + builder.setPositiveButton(R.string.grant, (dialog, button) -> { + Bundle showFragmentArgs = new Bundle(); + showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, notificationComponentName.flattenToString()); + if (permissionString == null || !permissionString.contains(getPackageName())) { + dialog.dismiss(); + // Show notification access setting and highlight launcher + startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS") + .putExtra(EXTRA_FRAGMENT_ARG_KEY, notificationComponentName.flattenToString()) + .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs)); + // Prevent the activity from being shown again + Preferences.setNotToAskForNotificationAccess(this); + Preferences.setNotToShowNotificationDialog(this); + } + }); + builder.setNegativeButton(R.string.ignore, (dialog, button) -> { + Preferences.setNotToShowNotificationDialog(this); + dialog.dismiss(); + }); + builder.setCancelable(false); + + AlertDialog dialog = builder.create(); + dialog.show(); + } + private void showLocationEnableDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Build and show the dialog diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ad6afc9791..01ff014d6d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -155,6 +155,7 @@ Allows an app to remove shortcuts without user intervention. + Permission required Add automatically Add to Home Screen Tap to setup App Suggestions @@ -178,4 +179,7 @@ Dock is already full No more room in page Maps + To see notification dots on the app icons you need to grant notification access to the launcher. + Grant + Ignore -- GitLab