From e743989e6b76583fc1e16806fa77d8cf227ef8a6 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 12 Dec 2022 18:19:54 +0100 Subject: [PATCH 1/6] Add feature for user to exclude apps from showing shortcuts --- .../main/java/fr/neamar/kiss/DataHandler.java | 38 +++++++- .../java/fr/neamar/kiss/SettingsActivity.java | 32 +++++++ .../kiss/dataprovider/ShortcutsProvider.java | 5 +- .../fr/neamar/kiss/loader/LoadAppPojos.java | 15 ++- .../kiss/loader/LoadShortcutsPojos.java | 17 +++- .../java/fr/neamar/kiss/pojo/AppPojo.java | 15 ++- .../fr/neamar/kiss/utils/ShortcutUtil.java | 1 - app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/preferences.xml | 93 ++++++++++++------- 9 files changed, 169 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/fr/neamar/kiss/DataHandler.java b/app/src/main/java/fr/neamar/kiss/DataHandler.java index 211775ad6..7fda25f54 100644 --- a/app/src/main/java/fr/neamar/kiss/DataHandler.java +++ b/app/src/main/java/fr/neamar/kiss/DataHandler.java @@ -27,7 +27,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; - import fr.neamar.kiss.broadcast.ProfileChangedHandler; import fr.neamar.kiss.dataprovider.AppProvider; import fr.neamar.kiss.dataprovider.ContactsProvider; @@ -63,6 +62,9 @@ public class DataHandler extends BroadcastReceiver final static private List PROVIDER_NAMES = Arrays.asList( "app", "contacts", "shortcuts" ); + + final static private String PREF_KEY_EXCLUDED_SHORTCUT_APPS = "excluded-shortcut-apps"; + private TagsHandler tagsHandler; final private Context context; private String currentQuery; @@ -550,6 +552,15 @@ public Set getExcludedFavorites() { return excludedFavorites; } + @NonNull + public Set getExcludedShortcutApps() { + Set excluded = PreferenceManager.getDefaultSharedPreferences(context).getStringSet(PREF_KEY_EXCLUDED_SHORTCUT_APPS, null); + if (excluded == null) { + excluded = new HashSet<>(); + } + return excluded; + } + public void addToExcludedFromHistory(AppPojo app) { // The set needs to be cloned and then edited, // modifying in place is not supported by putStringSet() @@ -584,6 +595,17 @@ public void addToExcluded(AppPojo app) { removeShortcuts(app.packageName); } + /** Add app as an app which is not allowed to show shortcuts */ + public void addToExcludedShortcutApps(AppPojo app) { + // The set needs to be cloned and then edited, + // modifying in place is not supported by putStringSet() + Set excluded = new HashSet<>(getExcludedShortcutApps()); + excluded.add(app.packageName); + PreferenceManager.getDefaultSharedPreferences(context).edit().putStringSet(PREF_KEY_EXCLUDED_SHORTCUT_APPS, excluded).apply(); + app.setExcludedShortcuts(true); + reloadShortcuts(); + } + public void removeFromExcluded(AppPojo app) { // The set needs to be cloned and then edited, // modifying in place is not supported by putStringSet() @@ -625,6 +647,20 @@ public void removeFromExcluded(UserHandle user) { PreferenceManager.getDefaultSharedPreferences(context).edit().putStringSet("excluded-apps", newExcluded).apply(); } + /** + * Remove app from the apps which are not allowed to show shortcuts - + * that is to say, this app may show shortcuts + */ + public void removeFromExcludedShortcutApps(AppPojo app) { + // The set needs to be cloned and then edited, + // modifying in place is not supported by putStringSet() + Set excluded = new HashSet<>(getExcludedShortcutApps()); + excluded.remove(app.packageName); + PreferenceManager.getDefaultSharedPreferences(context).edit().putStringSet(PREF_KEY_EXCLUDED_SHORTCUT_APPS, excluded).apply(); + app.setExcludedShortcuts(false); + reloadShortcuts(); + } + /** * Return all applications (including excluded) * diff --git a/app/src/main/java/fr/neamar/kiss/SettingsActivity.java b/app/src/main/java/fr/neamar/kiss/SettingsActivity.java index 51dea5b5a..ea34ff022 100644 --- a/app/src/main/java/fr/neamar/kiss/SettingsActivity.java +++ b/app/src/main/java/fr/neamar/kiss/SettingsActivity.java @@ -153,6 +153,7 @@ protected void onCreate(Bundle savedInstanceState) { Runnable alwaysAsync = () -> { SettingsActivity.this.addExcludedAppSettings(); SettingsActivity.this.addExcludedFromHistoryAppSettings(); + SettingsActivity.this.addExcludedShortcutAppSettings(); }; reorderPreferencesWithDisplayDependency(); @@ -396,6 +397,37 @@ public void onIncluded(final @NonNull AppPojo app) { category.addPreference(excludedAppsScreen); } + private void addExcludedShortcutAppSettings() { + final DataHandler dataHandler = KissApplication.getApplication(this).getDataHandler(); + + PreferenceScreen excludedAppsScreen = ExcludePreferenceScreen.getInstance( + this, + new ExcludePreferenceScreen.IsExcludedCallback() { + @Override + public boolean isExcluded(@NonNull AppPojo app) { + return app.isExcludedShortcuts(); + } + }, + new ExcludePreferenceScreen.OnExcludedListener() { + @Override + public void onExcluded(final @NonNull AppPojo app) { + dataHandler.addToExcludedShortcutApps(app); + } + + @Override + public void onIncluded(final @NonNull AppPojo app) { + dataHandler.removeFromExcludedShortcutApps(app); + } + }, + R.string.ui_excluded_from_shortcuts_apps, + R.string.ui_excluded_apps_dialog_title + ); + + PreferenceGroup category = (PreferenceGroup) findPreference("search-providers"); + excludedAppsScreen.setOrder(4); + category.addPreference(excludedAppsScreen); + } + private void addCustomSearchProvidersPreferences(SharedPreferences prefs) { if (prefs.getStringSet("selected-search-provider-names", null) == null) { // If null, it means this setting has never been accessed before diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutsProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutsProvider.java index f70a00791..f469771f7 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutsProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutsProvider.java @@ -47,9 +47,8 @@ public void reload() { try { this.initialize(new LoadShortcutsPojos(this)); - } - catch(IllegalStateException e) { - if(!notifiedKissNotDefaultLauncher) { + } catch (IllegalStateException e) { + if (!notifiedKissNotDefaultLauncher) { // Only display this message once per process Toast.makeText(this, R.string.unable_to_initialize_shortcuts, Toast.LENGTH_LONG).show(); } diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java index 261378575..7fd431c0f 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java @@ -45,6 +45,7 @@ protected ArrayList doInBackground(Void... params) { Set excludedAppList = KissApplication.getApplication(ctx).getDataHandler().getExcluded(); Set excludedAppListFavorites = KissApplication.getApplication(ctx).getDataHandler().getExcludedFavorites(); Set excludedFromHistoryAppList = KissApplication.getApplication(ctx).getDataHandler().getExcludedFromHistory(); + Set excludedShortcutsAppList = KissApplication.getApplication(ctx).getDataHandler().getExcludedShortcutApps(); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { UserManager manager = (UserManager) ctx.getSystemService(Context.USER_SERVICE); @@ -56,14 +57,17 @@ protected ArrayList doInBackground(Void... params) { for (LauncherActivityInfo activityInfo : launcher.getActivityList(null, profile)) { ApplicationInfo appInfo = activityInfo.getApplicationInfo(); - String id = user.addUserSuffixToString(pojoScheme + appInfo.packageName + "/" + activityInfo.getName(), '/'); + String appPackage = appInfo.packageName; + String id = user.addUserSuffixToString(pojoScheme + appPackage + "/" + activityInfo.getName(), '/'); - boolean isExcluded = excludedAppList.contains(AppPojo.getComponentName(appInfo.packageName, activityInfo.getName(), user)); + String componentName = AppPojo.getComponentName(appPackage, activityInfo.getName(), user); + boolean isExcluded = excludedAppList.contains(componentName); isExcluded |= excludedAppListFavorites.contains(id); boolean isExcludedFromHistory = excludedFromHistoryAppList.contains(id); + boolean isExcludedShortcuts = excludedShortcutsAppList.contains(appPackage); - AppPojo app = new AppPojo(id, appInfo.packageName, activityInfo.getName(), user, - isExcluded, isExcludedFromHistory); + AppPojo app = new AppPojo(id, appPackage, activityInfo.getName(), user, + isExcluded, isExcludedFromHistory, isExcludedShortcuts); app.setName(activityInfo.getLabel().toString()); @@ -86,9 +90,10 @@ protected ArrayList doInBackground(Void... params) { ); isExcluded |= excludedAppListFavorites.contains(id); boolean isExcludedFromHistory = excludedFromHistoryAppList.contains(id); + boolean isExcludedShortcuts = excludedShortcutsAppList.contains(id); AppPojo app = new AppPojo(id, appInfo.packageName, info.activityInfo.name, new UserHandle(), - isExcluded, isExcludedFromHistory); + isExcluded, isExcludedFromHistory, isExcludedShortcuts); app.setName(info.loadLabel(manager).toString()); diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java index 61206b000..c3320781e 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java @@ -66,7 +66,7 @@ protected ArrayList doInBackground(Void... arg0) { } } - return pojos; + return filterOutExcludedApps(pojos, context); } private ShortcutPojo createPojo(ShortcutRecord shortcutRecord, TagsHandler tagsHandler, String componentName, boolean pinned, boolean dynamic) { @@ -92,4 +92,19 @@ private boolean shortcutVisible(Context context, ShortcutInfo shortcutInfo, Set< return false; } + /** + * @return a new list which filters out shortcuts which come from apps which the user + * has excluded shortcuts for. + * Does not modify the input list. + */ + private ArrayList filterOutExcludedApps(List allPojos, Context context) { + ArrayList result = new ArrayList<>(); + Set excludedApps = KissApplication.getApplication(context).getDataHandler().getExcludedShortcutApps(); + for (ShortcutPojo pojo : allPojos) { + if (!excludedApps.contains(pojo.packageName)) { + result.add(pojo); + } + } + return result; + } } diff --git a/app/src/main/java/fr/neamar/kiss/pojo/AppPojo.java b/app/src/main/java/fr/neamar/kiss/pojo/AppPojo.java index b2f52ad66..fbc9d3a89 100644 --- a/app/src/main/java/fr/neamar/kiss/pojo/AppPojo.java +++ b/app/src/main/java/fr/neamar/kiss/pojo/AppPojo.java @@ -17,10 +17,14 @@ public static String getComponentName(String packageName, String activityName, private boolean excluded; private boolean excludedFromHistory; + /** + * Whether shortcuts are excluded for this app + */ + private boolean excludedShortcuts; private long customIconId = 0; public AppPojo(String id, String packageName, String activityName, UserHandle userHandle, - boolean isExcluded, boolean isExcludedFromHistory) { + boolean isExcluded, boolean isExcludedFromHistory, boolean isExcludedShortcuts) { super(id); this.packageName = packageName; @@ -29,6 +33,7 @@ public AppPojo(String id, String packageName, String activityName, UserHandle us this.excluded = isExcluded; this.excludedFromHistory = isExcludedFromHistory; + this.excludedShortcuts = isExcludedShortcuts; } public String getComponentName() { @@ -51,6 +56,14 @@ public void setExcludedFromHistory(boolean excludedFromHistory) { this.excludedFromHistory = excludedFromHistory; } + public boolean isExcludedShortcuts() { + return excludedShortcuts; + } + + public void setExcludedShortcuts(boolean excludedShortcuts) { + this.excludedShortcuts = excludedShortcuts; + } + public void setCustomIconId(long iconId) { customIconId = iconId; } diff --git a/app/src/main/java/fr/neamar/kiss/utils/ShortcutUtil.java b/app/src/main/java/fr/neamar/kiss/utils/ShortcutUtil.java index a9f18a158..fdce1ea40 100644 --- a/app/src/main/java/fr/neamar/kiss/utils/ShortcutUtil.java +++ b/app/src/main/java/fr/neamar/kiss/utils/ShortcutUtil.java @@ -51,7 +51,6 @@ public static boolean areShortcutsEnabled(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && prefs.getBoolean("enable-shortcuts", true); - } /** diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0d4a2e34e..b2a571aa6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,6 +47,7 @@ Excluded apps View or edit apps excluded from KISS View or edit apps excluded from history + View or edit apps excluded from shortcuts Select apps to exclude Do you really want to reset your history? Reset your list of favorites diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 16ac89bd7..5932d03ed 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -89,8 +89,8 @@ android:title="@string/exclude_favorites_history" /> + android:title="@string/results_size" /> - + - + @@ -252,7 +256,9 @@ android:key="enable-suggestions-keyboard" android:title="@string/keyboard_suggestions" /> - + - + @@ -412,37 +420,47 @@ android:key="providers" android:summary="@string/providers_summary" android:title="@string/title_providers"> - - - - - - - + + + + + + - + - - + + - + Date: Mon, 12 Dec 2022 18:22:31 +0100 Subject: [PATCH 2/6] Undo lost line break --- app/src/main/java/fr/neamar/kiss/DataHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/fr/neamar/kiss/DataHandler.java b/app/src/main/java/fr/neamar/kiss/DataHandler.java index 7fda25f54..60be9de82 100644 --- a/app/src/main/java/fr/neamar/kiss/DataHandler.java +++ b/app/src/main/java/fr/neamar/kiss/DataHandler.java @@ -27,6 +27,7 @@ import java.util.Locale; import java.util.Map; import java.util.Set; + import fr.neamar.kiss.broadcast.ProfileChangedHandler; import fr.neamar.kiss.dataprovider.AppProvider; import fr.neamar.kiss.dataprovider.ContactsProvider; From 1855a11d779690a409003954256384ef7c5770c4 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 20 Dec 2022 17:38:05 +0100 Subject: [PATCH 3/6] Move checking of excluded shortcut apps to isShortcutVisible --- .../kiss/loader/LoadShortcutsPojos.java | 45 +++++++------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java index c3320781e..a493bd176 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java @@ -37,6 +37,7 @@ protected ArrayList doInBackground(Void... arg0) { DataHandler dataHandler = KissApplication.getApplication(context).getDataHandler(); TagsHandler tagsHandler = dataHandler.getTagsHandler(); Set excludedApps = dataHandler.getExcluded(); + Set excludedShortcutApps = dataHandler.getExcludedShortcutApps(); ArrayList pojos = new ArrayList<>(); @@ -56,7 +57,7 @@ protected ArrayList doInBackground(Void... arg0) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { List shortcutInfos = ShortcutUtil.getAllShortcuts(context); for (ShortcutInfo shortcutInfo : shortcutInfos) { - if (shortcutVisible(context, shortcutInfo, excludedApps, visibleShortcutIds)) { + if (isShortcutVisible(context, shortcutInfo, excludedApps, excludedShortcutApps, visibleShortcutIds)) { ShortcutRecord shortcutRecord = ShortcutUtil.createShortcutRecord(context, shortcutInfo, !shortcutInfo.isPinned()); if (shortcutRecord != null) { ShortcutPojo pojo = createPojo(shortcutRecord, tagsHandler, ShortcutUtil.getComponentName(context, shortcutInfo), shortcutInfo.isPinned(), shortcutInfo.isDynamic()); @@ -66,7 +67,7 @@ protected ArrayList doInBackground(Void... arg0) { } } - return filterOutExcludedApps(pojos, context); + return pojos; } private ShortcutPojo createPojo(ShortcutRecord shortcutRecord, TagsHandler tagsHandler, String componentName, boolean pinned, boolean dynamic) { @@ -77,34 +78,22 @@ private ShortcutPojo createPojo(ShortcutRecord shortcutRecord, TagsHandler tagsH } @RequiresApi(Build.VERSION_CODES.O) - private boolean shortcutVisible(Context context, ShortcutInfo shortcutInfo, Set excludedApps, Set visibleShortcutIds) { - if (shortcutInfo.isEnabled()) { - String componentName = ShortcutUtil.getComponentName(context, shortcutInfo); - // if related package is excluded from KISS then the shortcut must be excluded too - if (!excludedApps.contains(componentName)) { - if (shortcutInfo.isPinned()) { - return visibleShortcutIds.contains(shortcutInfo.getId()); - } else { - return true; - } - } + private boolean isShortcutVisible(Context context, ShortcutInfo shortcutInfo, Set excludedApps, Set excludedShortcutApps, Set visibleShortcutIds) { + if (!shortcutInfo.isEnabled()) { + return false; } - return false; - } + String packageName = shortcutInfo.getPackage(); + String componentName = ShortcutUtil.getComponentName(context, shortcutInfo); - /** - * @return a new list which filters out shortcuts which come from apps which the user - * has excluded shortcuts for. - * Does not modify the input list. - */ - private ArrayList filterOutExcludedApps(List allPojos, Context context) { - ArrayList result = new ArrayList<>(); - Set excludedApps = KissApplication.getApplication(context).getDataHandler().getExcludedShortcutApps(); - for (ShortcutPojo pojo : allPojos) { - if (!excludedApps.contains(pojo.packageName)) { - result.add(pojo); - } + // if related package is excluded from KISS then the shortcut must be excluded too + boolean isExcluded = excludedApps.contains(componentName) || excludedShortcutApps.contains(packageName); + if (isExcluded) { + return false; + } + + if (shortcutInfo.isPinned()) { + return visibleShortcutIds.contains(shortcutInfo.getId()); } - return result; + return true; } } From 29150e0bc42239217b6c4a0a8f7085da79ba9ce8 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 20 Dec 2022 17:52:53 +0100 Subject: [PATCH 4/6] Fix merge --- .../main/java/fr/neamar/kiss/loader/LoadAppPojos.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java index 354f0d7d8..16b8f3071 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java @@ -55,7 +55,7 @@ protected ArrayList doInBackground(Void... params) { UserHandle user = new UserHandle(manager.getSerialNumberForUser(profile), profile); for (LauncherActivityInfo activityInfo : launcher.getActivityList(null, profile)) { ApplicationInfo appInfo = activityInfo.getApplicationInfo(); - final AppPojo app = createPojo(user, appInfo.packageName, activityInfo.getName(), activityInfo.getLabel(), excludedAppList, excludedFromHistoryAppList); + final AppPojo app = createPojo(user, appInfo.packageName, activityInfo.getName(), activityInfo.getLabel(), excludedAppList, excludedFromHistoryAppList, excludedShortcutsAppList); apps.add(app); } } @@ -67,7 +67,7 @@ protected ArrayList doInBackground(Void... params) { for (ResolveInfo info : manager.queryIntentActivities(mainIntent, 0)) { ApplicationInfo appInfo = info.activityInfo.applicationInfo; - final AppPojo app = createPojo(new UserHandle(), appInfo.packageName, info.activityInfo.name, info.loadLabel(manager), excludedAppList, excludedFromHistoryAppList); + final AppPojo app = createPojo(new UserHandle(), appInfo.packageName, info.activityInfo.name, info.loadLabel(manager), excludedAppList, excludedFromHistoryAppList, excludedShortcutsAppList); apps.add(app); } } @@ -89,13 +89,15 @@ protected ArrayList doInBackground(Void... params) { return apps; } - private AppPojo createPojo(UserHandle userHandle, String packageName, String activityName, CharSequence label, Set excludedAppList, Set excludedFromHistoryAppList) { + private AppPojo createPojo(UserHandle userHandle, String packageName, String activityName, CharSequence label, Set excludedAppList, Set excludedFromHistoryAppList, Set excludedShortcutsAppList) { String id = userHandle.addUserSuffixToString(pojoScheme + packageName + "/" + activityName, '/'); boolean isExcluded = excludedAppList.contains(AppPojo.getComponentName(packageName, activityName, userHandle)); boolean isExcludedFromHistory = excludedFromHistoryAppList.contains(id); + // TODO: are we checking for 'packageName' or 'id'? Test + boolean isExcludedShortcuts = excludedShortcutsAppList.contains(packageName); - AppPojo app = new AppPojo(id, packageName, activityName, userHandle, isExcluded, isExcludedFromHistory); + AppPojo app = new AppPojo(id, packageName, activityName, userHandle, isExcluded, isExcludedFromHistory, isExcludedShortcuts); app.setName(label.toString()); From ce2ddf0b9479723f4f3fc08b7d99784f19e218b2 Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 23 Dec 2022 18:21:33 +0100 Subject: [PATCH 5/6] Only show shortcut related settings when the device actually supports shortcuts. Rework the exclude apps from shortcuts UI --- .../main/java/fr/neamar/kiss/DataHandler.java | 2 +- .../java/fr/neamar/kiss/SettingsActivity.java | 81 ++++++++++++++++++- .../ResetExcludedAppShortcutsPreference.java | 40 +++++++++ .../preference/ResetShortcutsPreference.java | 4 + .../fr/neamar/kiss/utils/ShortcutUtil.java | 14 +++- app/src/main/res/values/strings.xml | 6 +- app/src/main/res/xml/preferences.xml | 26 +++--- 7 files changed, 150 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/fr/neamar/kiss/preference/ResetExcludedAppShortcutsPreference.java diff --git a/app/src/main/java/fr/neamar/kiss/DataHandler.java b/app/src/main/java/fr/neamar/kiss/DataHandler.java index c558885f2..68ed39e76 100644 --- a/app/src/main/java/fr/neamar/kiss/DataHandler.java +++ b/app/src/main/java/fr/neamar/kiss/DataHandler.java @@ -64,7 +64,7 @@ public class DataHandler extends BroadcastReceiver "app", "contacts", "shortcuts" ); - final static private String PREF_KEY_EXCLUDED_SHORTCUT_APPS = "excluded-shortcut-apps"; + public final static String PREF_KEY_EXCLUDED_SHORTCUT_APPS = "excluded-shortcut-apps"; private TagsHandler tagsHandler; final private Context context; diff --git a/app/src/main/java/fr/neamar/kiss/SettingsActivity.java b/app/src/main/java/fr/neamar/kiss/SettingsActivity.java index ea34ff022..ba64f5619 100644 --- a/app/src/main/java/fr/neamar/kiss/SettingsActivity.java +++ b/app/src/main/java/fr/neamar/kiss/SettingsActivity.java @@ -45,10 +45,13 @@ import fr.neamar.kiss.pojo.TagDummyPojo; import fr.neamar.kiss.preference.ExcludePreferenceScreen; import fr.neamar.kiss.preference.PreferenceScreenHelper; +import fr.neamar.kiss.preference.ResetExcludedAppShortcutsPreference; +import fr.neamar.kiss.preference.ResetShortcutsPreference; import fr.neamar.kiss.preference.SwitchPreference; import fr.neamar.kiss.searcher.QuerySearcher; import fr.neamar.kiss.utils.PackageManagerUtils; import fr.neamar.kiss.utils.Permission; +import fr.neamar.kiss.utils.ShortcutUtil; @SuppressWarnings("FragmentInjection") public class SettingsActivity extends PreferenceActivity implements @@ -151,11 +154,25 @@ protected void onCreate(Bundle savedInstanceState) { // This is reaaally slow, and always need to run asynchronously Runnable alwaysAsync = () -> { + // TODO: Note that there is a bug here with all of these settings pages: + // These settings pages load the list of AppPojos from DataHandler only once. + // This means that the data shown in these settings pages will be stale if the AppPojo + // data stored in DataHandler is changed by elsewhere in the app. + // You can easily reproduce this bug by: + // 1. Open the 'apps excluded from KISS' page + // 2. Change some values from their defaults + // 3. Go back and use the 'reset apps excluded from KISS' button + // 4. Open the 'apps excluded from KISS' page again. The data shown will be incorrect, + // as it won't have refreshed for the user having reset the list. + // This list will refresh if the user closes and re-opens KISS settings. SettingsActivity.this.addExcludedAppSettings(); SettingsActivity.this.addExcludedFromHistoryAppSettings(); SettingsActivity.this.addExcludedShortcutAppSettings(); }; + addEnableShortcutsSwitch(); + addRegenerateShortcutsPreference(); + addResetExcludedAppShortcutsPreference(); reorderPreferencesWithDisplayDependency(); if (savedInstanceState == null) { @@ -397,7 +414,68 @@ public void onIncluded(final @NonNull AppPojo app) { category.addPreference(excludedAppsScreen); } + /** + * Adds the button for resetting the apps excluded from showing shortcuts, + * only if this device supports shortcuts + */ + private void addResetExcludedAppShortcutsPreference() { + if(!ShortcutUtil.canDeviceShowShortcuts()) { + return; + } + + ResetExcludedAppShortcutsPreference pref = new ResetExcludedAppShortcutsPreference(this); + pref.setKey("reset-excluded-app-shortcuts"); + pref.setOrder(59); + pref.setTitle(R.string.reset_excluded_app_shortcuts_name); + pref.setDialogMessage(R.string.reset_excluded_app_shortcuts_warn); + + PreferenceGroup category = (PreferenceGroup) findPreference("exclude_apps_category"); + category.addPreference(pref); + } + + /** + * Adds the switch for toggling whether shortcuts shown be shown, + * only if this device supports shortcuts + */ + private void addEnableShortcutsSwitch() { + if(!ShortcutUtil.canDeviceShowShortcuts()) { + return; + } + + SwitchPreference pref = new SwitchPreference(this); + pref.setDefaultValue(true); + pref.setKey("enable-shortcuts"); + pref.setTitle(R.string.shortcuts_name); + + PreferenceGroup category = (PreferenceGroup) findPreference("search-providers"); + pref.setOrder(3); + category.addPreference(pref); + } + + /** + * Adds the button for regenerating the list of shortcuts, + * only if this device supports shortcuts + */ + private void addRegenerateShortcutsPreference() { + if(!ShortcutUtil.canDeviceShowShortcuts()) { + return; + } + + ResetShortcutsPreference pref = new ResetShortcutsPreference(this); + pref.setDialogMessage(R.string.regenerate_shortcuts_desc); + pref.setKey("reset"); + pref.setTitle(R.string.regenerate_shortcuts); + + PreferenceGroup category = (PreferenceGroup) findPreference("search-providers"); + pref.setOrder(5); + category.addPreference(pref); + } + private void addExcludedShortcutAppSettings() { + if(!ShortcutUtil.canDeviceShowShortcuts()) { + return; + } + final DataHandler dataHandler = KissApplication.getApplication(this).getDataHandler(); PreferenceScreen excludedAppsScreen = ExcludePreferenceScreen.getInstance( @@ -423,8 +501,7 @@ public void onIncluded(final @NonNull AppPojo app) { R.string.ui_excluded_apps_dialog_title ); - PreferenceGroup category = (PreferenceGroup) findPreference("search-providers"); - excludedAppsScreen.setOrder(4); + PreferenceGroup category = (PreferenceGroup) findPreference("exclude_apps_category"); category.addPreference(excludedAppsScreen); } diff --git a/app/src/main/java/fr/neamar/kiss/preference/ResetExcludedAppShortcutsPreference.java b/app/src/main/java/fr/neamar/kiss/preference/ResetExcludedAppShortcutsPreference.java new file mode 100644 index 000000000..95f0d90e5 --- /dev/null +++ b/app/src/main/java/fr/neamar/kiss/preference/ResetExcludedAppShortcutsPreference.java @@ -0,0 +1,40 @@ +package fr.neamar.kiss.preference; + +import android.content.Context; +import android.content.DialogInterface; +import android.preference.DialogPreference; +import android.preference.PreferenceManager; +import android.util.AttributeSet; +import android.widget.Toast; + +import fr.neamar.kiss.DataHandler; +import fr.neamar.kiss.KissApplication; +import fr.neamar.kiss.R; + +public class ResetExcludedAppShortcutsPreference extends DialogPreference { + + public ResetExcludedAppShortcutsPreference(Context context) { + super(context, null); + } + + public ResetExcludedAppShortcutsPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + public void onClick(DialogInterface dialog, int which) { + super.onClick(dialog, which); + if (which == DialogInterface.BUTTON_POSITIVE) { + PreferenceManager.getDefaultSharedPreferences(getContext()).edit() + .putStringSet(DataHandler.PREF_KEY_EXCLUDED_SHORTCUT_APPS, null).apply(); + DataHandler dataHandler = KissApplication.getApplication(getContext()).getDataHandler(); + // Reload shortcuts to refresh the shortcuts shown in KISS + dataHandler.reloadShortcuts(); + // Reload apps since the `AppPojo.isExcludedShortcuts` value also needs to be refreshed + dataHandler.reloadApps(); + Toast.makeText(getContext(), R.string.excluded_app_list_erased, Toast.LENGTH_LONG).show(); + } + + } + +} diff --git a/app/src/main/java/fr/neamar/kiss/preference/ResetShortcutsPreference.java b/app/src/main/java/fr/neamar/kiss/preference/ResetShortcutsPreference.java index 402d60461..107e5577b 100644 --- a/app/src/main/java/fr/neamar/kiss/preference/ResetShortcutsPreference.java +++ b/app/src/main/java/fr/neamar/kiss/preference/ResetShortcutsPreference.java @@ -12,6 +12,10 @@ public class ResetShortcutsPreference extends DialogPreference { + public ResetShortcutsPreference(Context context) { + super(context, null); + } + public ResetShortcutsPreference(Context context, AttributeSet attrs) { super(context, attrs); } diff --git a/app/src/main/java/fr/neamar/kiss/utils/ShortcutUtil.java b/app/src/main/java/fr/neamar/kiss/utils/ShortcutUtil.java index fdce1ea40..477480af1 100644 --- a/app/src/main/java/fr/neamar/kiss/utils/ShortcutUtil.java +++ b/app/src/main/java/fr/neamar/kiss/utils/ShortcutUtil.java @@ -45,12 +45,20 @@ public static String generateShortcutId(ShortcutRecord shortcutRecord) { } /** - * @return true if shortcuts are enabled in settings and android version is higher or equals android 8 + * @return true if this device supports shortcuts, and shortcuts are enabled in settings */ public static boolean areShortcutsEnabled(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && - prefs.getBoolean("enable-shortcuts", true); + return canDeviceShowShortcuts() && prefs.getBoolean("enable-shortcuts", true); + } + + /** + * @return whether this device is running Android 8 (API 26) or higher. + * Officially shortcuts were first supported by Android 7.1 (API 25), + * but we use shortcut APIs only available in Android 8. + */ + public static boolean canDeviceShowShortcuts() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O; } /** diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dfe57a165..0ea59f094 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -44,10 +44,12 @@ Do you really want to reset your excluded app list? Reset list of apps excluded from history Do you really want to reset your excluded from history app list? - Excluded apps + Reset list of apps excluded from showing shortcuts + Do you really want to reset your list of apps excluded from showing shortcuts? + Excluded entries View or edit apps excluded from KISS View or edit apps excluded from history - View or edit apps excluded from shortcuts + View or edit apps excluded from showing shortcuts Select apps to exclude Do you really want to reset your history? Reset your list of favorites diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 5932d03ed..0408eb2b7 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -47,16 +47,20 @@ android:key="exclude_apps_category" android:order="53" android:title="@string/settings_excluded_apps"> - + + + + + @@ -433,16 +437,8 @@ android:key="enable-settings" android:order="2" android:title="@string/settings_name" /> - - + + Date: Tue, 27 Dec 2022 16:16:21 +0100 Subject: [PATCH 6/6] Remove todo, add docs --- app/src/main/java/fr/neamar/kiss/DataHandler.java | 4 ++++ app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/fr/neamar/kiss/DataHandler.java b/app/src/main/java/fr/neamar/kiss/DataHandler.java index 68ed39e76..411b0cd56 100644 --- a/app/src/main/java/fr/neamar/kiss/DataHandler.java +++ b/app/src/main/java/fr/neamar/kiss/DataHandler.java @@ -64,6 +64,10 @@ public class DataHandler extends BroadcastReceiver "app", "contacts", "shortcuts" ); + /** + * Key for a preference that holds a String set of apps which are excluded from showing shortcuts. + * Each string in the set is the packageName of an app which may not show shortcuts. + */ public final static String PREF_KEY_EXCLUDED_SHORTCUT_APPS = "excluded-shortcut-apps"; private TagsHandler tagsHandler; diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java index 16b8f3071..a4642d594 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java @@ -94,7 +94,6 @@ private AppPojo createPojo(UserHandle userHandle, String packageName, String act boolean isExcluded = excludedAppList.contains(AppPojo.getComponentName(packageName, activityName, userHandle)); boolean isExcludedFromHistory = excludedFromHistoryAppList.contains(id); - // TODO: are we checking for 'packageName' or 'id'? Test boolean isExcludedShortcuts = excludedShortcutsAppList.contains(packageName); AppPojo app = new AppPojo(id, packageName, activityName, userHandle, isExcluded, isExcludedFromHistory, isExcludedShortcuts);