Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: User can select which apps can show shortcuts #2025

Merged
merged 8 commits into from
Dec 29, 2022
37 changes: 37 additions & 0 deletions app/src/main/java/fr/neamar/kiss/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class DataHandler extends BroadcastReceiver
final static private List<String> 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;
Expand Down Expand Up @@ -550,6 +553,15 @@ public Set<String> getExcludedFavorites() {
return excludedFavorites;
}

@NonNull
public Set<String> getExcludedShortcutApps() {
Set<String> 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()
Expand Down Expand Up @@ -584,6 +596,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<String> 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()
Expand Down Expand Up @@ -625,6 +648,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<String> 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)
*
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/fr/neamar/kiss/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ protected void onCreate(Bundle savedInstanceState) {
Runnable alwaysAsync = () -> {
SettingsActivity.this.addExcludedAppSettings();
SettingsActivity.this.addExcludedFromHistoryAppSettings();
SettingsActivity.this.addExcludedShortcutAppSettings();
};

reorderPreferencesWithDisplayDependency();
Expand Down Expand Up @@ -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");
marunjar marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected ArrayList<AppPojo> doInBackground(Void... params) {
Set<String> excludedAppList = KissApplication.getApplication(ctx).getDataHandler().getExcluded();
Set<String> excludedAppListFavorites = KissApplication.getApplication(ctx).getDataHandler().getExcludedFavorites();
Set<String> excludedFromHistoryAppList = KissApplication.getApplication(ctx).getDataHandler().getExcludedFromHistory();
Set<String> 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);
Expand All @@ -56,14 +57,17 @@ protected ArrayList<AppPojo> 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());

Expand All @@ -86,9 +90,10 @@ protected ArrayList<AppPojo> 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());

Expand Down
17 changes: 16 additions & 1 deletion app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected ArrayList<ShortcutPojo> doInBackground(Void... arg0) {
}
}

return pojos;
return filterOutExcludedApps(pojos, context);
}

private ShortcutPojo createPojo(ShortcutRecord shortcutRecord, TagsHandler tagsHandler, String componentName, boolean pinned, boolean dynamic) {
Expand All @@ -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<ShortcutPojo> filterOutExcludedApps(List<ShortcutPojo> allPojos, Context context) {
ArrayList<ShortcutPojo> result = new ArrayList<>();
Set<String> excludedApps = KissApplication.getApplication(context).getDataHandler().getExcludedShortcutApps();
for (ShortcutPojo pojo : allPojos) {
if (!excludedApps.contains(pojo.packageName)) {
marunjar marked this conversation as resolved.
Show resolved Hide resolved
result.add(pojo);
}
}
return result;
}
}
15 changes: 14 additions & 1 deletion app/src/main/java/fr/neamar/kiss/pojo/AppPojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/fr/neamar/kiss/utils/ShortcutUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

/**
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<string name="settings_excluded_apps">Excluded apps</string>
<string name="ui_excluded_apps">View or edit apps excluded from KISS</string>
<string name="ui_excluded_from_history_apps">View or edit apps excluded from history</string>
<string name="ui_excluded_from_shortcuts_apps">View or edit apps excluded from shortcuts</string>
marunjar marked this conversation as resolved.
Show resolved Hide resolved
<string name="ui_excluded_apps_dialog_title">Select apps to exclude</string>
<string name="reset_warn">Do you really want to reset your history?</string>
<string name="reset_favorites_name">Reset your list of favorites</string>
Expand Down
Loading