Skip to content

Commit

Permalink
Use component name for icons and hidden apps
Browse files Browse the repository at this point in the history
Since a package can have multiple components.
  • Loading branch information
markusfisch committed Jan 19, 2025
1 parent 194c073 commit bbe5e00
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.markusfisch.android.pielauncher.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LauncherActivityInfo;
Expand Down Expand Up @@ -90,26 +91,27 @@ private void initListView() {
listView.setOnItemClickListener((parent, view, position, id) -> {
HiddenAppsAdapter.HiddenApp hiddenApp = adapter.getItem(position);
if (hiddenApp != null) {
askToShowApp(hiddenApp.packageName);
askToShowApp(hiddenApp.componentName);
}
});
loadHiddenApps();
}

private void askToShowApp(String packageName) {
private void askToShowApp(ComponentName componentName) {
Dialog.newDialog(this)
.setTitle(R.string.unhide_app)
.setMessage(R.string.want_to_unhide_app)
.setPositiveButton(android.R.string.ok, (d, w) -> {
PieLauncherApp.appMenu.hiddenApps.removeAndStore(this,
packageName);
componentName.getPackageName());
PieLauncherApp.appMenu.updateIconsAsync(this);
loadHiddenApps();
})
.setNegativeButton(android.R.string.cancel, (d, w) -> {
})
.setNeutralButton(R.string.start_app, (d, w) ->
AppMenu.launchPackage(this, packageName))
AppMenu.launchPackage(this,
componentName.getPackageName()))
.show();
}

Expand All @@ -118,13 +120,13 @@ private void loadHiddenApps() {
Executors.newSingleThreadExecutor().execute(() -> {
final ArrayList<HiddenAppsAdapter.HiddenApp> hiddenApps =
new ArrayList<>();
for (String packageName :
PieLauncherApp.appMenu.hiddenApps.packageNames) {
for (ComponentName componentName :
PieLauncherApp.appMenu.hiddenApps.componentNames) {
Pair<String, Drawable> nameAndIcon = getAppNameAndIcon(
this, packageName);
this, componentName.getPackageName());
if (nameAndIcon != null) {
hiddenApps.add(new HiddenAppsAdapter.HiddenApp(
packageName,
componentName,
nameAndIcon.first,
nameAndIcon.second));
}
Expand All @@ -137,7 +139,8 @@ private void loadHiddenApps() {
});
}

private static Pair<String, Drawable> getAppNameAndIcon(Context context, String packageName) {
private static Pair<String, Drawable> getAppNameAndIcon(Context context,
String packageName) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
LauncherApps la = (LauncherApps) context.getSystemService(
Context.LAUNCHER_APPS_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
Expand Down Expand Up @@ -37,7 +39,7 @@ public interface OnHideListener {
void onHide();
}

private static final String PACKAGE_NAME = "package_name";
private static final String COMPONENT_NAME = "package_name";

private final Handler handler = new Handler(Looper.getMainLooper());

Expand All @@ -50,24 +52,24 @@ public interface OnHideListener {
private ArrayList<String> drawableNames;
private PickIconAdapter iconAdapter;

public static void start(Context context, String packageName) {
public static void start(Context context, ComponentName componentName) {
Intent intent = new Intent(context, PickIconActivity.class);
intent.putExtra(PACKAGE_NAME, packageName);
intent.putExtra(COMPONENT_NAME, componentName);
context.startActivity(intent);
}

public static void askToHide(Context context, String packageName) {
askToHide(context, packageName, null);
public static void askToHide(Context context, ComponentName componentName) {
askToHide(context, componentName, null);
}

public static void askToHide(Context context, String packageName,
public static void askToHide(Context context, ComponentName componentName,
OnHideListener hideListener) {
Dialog.newDialog(context)
.setTitle(R.string.hide_app)
.setMessage(R.string.want_to_hide_app)
.setPositiveButton(android.R.string.ok, (d, w) -> {
PieLauncherApp.appMenu.hiddenApps.addAndStore(context,
packageName);
componentName);
PieLauncherApp.appMenu.updateIconsAsync(context);
if (hideListener != null) {
hideListener.onHide();
Expand All @@ -83,9 +85,9 @@ protected void onCreate(Bundle state) {
super.onCreate(state);

Intent intent = getIntent();
String packageName;
if (intent == null || (packageName = intent.getStringExtra(
PACKAGE_NAME)) == null) {
ComponentName componentName;
if (intent == null || (componentName =
getComponentNameFromIntent(intent)) == null) {
finish();
return;
}
Expand Down Expand Up @@ -113,10 +115,10 @@ protected void onCreate(Bundle state) {
return;
}

initGridView(packageName);
initGridView(componentName);
initSearch();
initHide(packageName);
initReset(packageName);
initHide(componentName);
initReset(componentName);
initSwitchPack();

Window window = getWindow();
Expand Down Expand Up @@ -148,12 +150,12 @@ public void onScroll(AbsListView view, int firstVisibleItem,
toolbarBackground.backgroundColor);
}

private void initGridView(String packageName) {
private void initGridView(ComponentName componentName) {
gridView = findViewById(R.id.icons);
gridView.setOnItemClickListener((parent, view, position, id) -> {
PieLauncherApp.iconPack.addMapping(
iconPackPackageName,
packageName,
componentName,
iconAdapter.getItem(position));
PieLauncherApp.iconPack.storeMappings(this);
PieLauncherApp.appMenu.updateIconsAsync(this);
Expand Down Expand Up @@ -194,23 +196,23 @@ public void afterTextChanged(Editable e) {
searchInput.post(searchInput::requestFocus);
}

private void initReset(String packageName) {
private void initReset(ComponentName componentName) {
View resetButton = findViewById(R.id.reset);
if (PieLauncherApp.iconPack.hasMapping(packageName)) {
if (PieLauncherApp.iconPack.hasMapping(componentName)) {
resetButton.setOnClickListener((v) -> {
askToRestore(packageName);
askToRestore(componentName);
});
} else {
resetButton.setVisibility(View.INVISIBLE);
}
}

private void askToRestore(String packageName) {
private void askToRestore(ComponentName componentName) {
new AlertDialog.Builder(this)
.setTitle(R.string.change_icon)
.setMessage(R.string.want_to_restore_icon)
.setPositiveButton(android.R.string.ok, (d, w) -> {
PieLauncherApp.iconPack.removeMapping(packageName);
PieLauncherApp.iconPack.removeMapping(componentName);
PieLauncherApp.iconPack.storeMappings(this);
PieLauncherApp.appMenu.updateIconsAsync(this);
finish();
Expand All @@ -226,13 +228,14 @@ private void askToRestore(String packageName) {
.show();
}

private void initHide(String packageName) {
private void initHide(ComponentName componentName) {
View hideButton = findViewById(R.id.hide_app);
if (PieLauncherApp.appMenu.isDrawerPackageName(packageName)) {
if (PieLauncherApp.appMenu.isDrawerPackageName(
componentName.getPackageName())) {
hideButton.setVisibility(View.INVISIBLE);
} else {
hideButton.setOnClickListener((v) -> askToHide(
this, packageName, this::finish));
this, componentName, this::finish));
}
}

Expand Down Expand Up @@ -279,4 +282,13 @@ private void loadPack(String packageName) {
});
});
}

private static ComponentName getComponentNameFromIntent(Intent intent) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
return intent.getParcelableExtra(COMPONENT_NAME);
} else {
return intent.getParcelableExtra(COMPONENT_NAME,
ComponentName.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.markusfisch.android.pielauncher.adapter;

import android.content.ComponentName;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
Expand All @@ -15,12 +16,12 @@

public class HiddenAppsAdapter extends ArrayAdapter<HiddenAppsAdapter.HiddenApp> {
public static class HiddenApp {
public final String packageName;
public final ComponentName componentName;
public final String name;
public final Drawable icon;

public HiddenApp(String packageName, String name, Drawable icon) {
this.packageName = packageName;
public HiddenApp(ComponentName componentName, String name, Drawable icon) {
this.componentName = componentName;
this.name = name;
this.icon = icon;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.concurrent.Executors;

import de.markusfisch.android.pielauncher.R;
import de.markusfisch.android.pielauncher.activity.HomeActivity;
import de.markusfisch.android.pielauncher.app.PieLauncherApp;
import de.markusfisch.android.pielauncher.graphics.CanvasPieMenu;
import de.markusfisch.android.pielauncher.graphics.Converter;
Expand Down Expand Up @@ -88,11 +89,15 @@ public interface UpdateListener {
private String drawerPackageName;
private boolean indexing = false;

public static ComponentName getLaunchComponentForPackageName(
Context context, String packageName) {
Intent intent = getLaunchIntent(context, packageName);
return intent != null ? intent.getComponent() : null;
}

public static void launchPackage(Context context, String packageName) {
PackageManager pm = context.getPackageManager();
Intent intent;
if (pm != null && (intent = pm.getLaunchIntentForPackage(
packageName)) != null) {
Intent intent = getLaunchIntent(context, packageName);
if (intent != null) {
context.startActivity(intent);
}
}
Expand Down Expand Up @@ -248,7 +253,8 @@ public boolean indexAppsAsync(Context context,
}
indexing = true;
hiddenApps.restore(context);
HashSet<String> hideApps = new HashSet<>(hiddenApps.packageNames);
HashSet<ComponentName> hideApps = new HashSet<>(
hiddenApps.componentNames);
Map<LauncherItemKey, AppIcon> newApps = new HashMap<>();
if (packageNameRestriction != null) {
// Copy apps since we're indexing just one app.
Expand Down Expand Up @@ -285,13 +291,13 @@ private static void indexApps(
Context context,
String packageNameRestriction,
UserHandle userHandleRestriction,
HashSet<String> hideApps,
HashSet<ComponentName> hideApps,
Map<LauncherItemKey, AppIcon> allApps) {
PackageManager pm = context.getPackageManager();
PieLauncherApp.iconPack.selectPack(pm,
PieLauncherApp.getPrefs(context).getIconPack());
PieLauncherApp.iconPack.restoreMappings(context);
hideApps.add(context.getPackageName());
hideApps.add(new ComponentName(context, HomeActivity.class));
if (HAS_LAUNCHER_APP) {
indexProfilesApps(
(LauncherApps) context.getSystemService(
Expand All @@ -309,24 +315,24 @@ private static void indexIntentsApps(
PackageManager pm,
Map<LauncherItemKey, AppIcon> allApps,
String packageNameRestriction,
Set<String> hideApps) {
Set<ComponentName> hideApps) {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
if (packageNameRestriction != null) {
intent.setPackage(packageNameRestriction);
}
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
for (ResolveInfo info : activities) {
String packageName = info.activityInfo.applicationInfo.packageName;
if (hideApps.contains(packageName)) {
ComponentName componentName = getComponentName(info.activityInfo);
if (hideApps.contains(componentName)) {
continue;
}
Drawable icon = PieLauncherApp.iconPack.getIcon(packageName);
Drawable icon = PieLauncherApp.iconPack.getIcon(componentName);
if (icon == null) {
icon = info.loadIcon(pm);
}
addApp(allApps,
getComponentName(info.activityInfo),
componentName,
info.loadLabel(pm).toString(),
icon,
null);
Expand All @@ -340,7 +346,7 @@ private static void indexProfilesApps(
Map<LauncherItemKey, AppIcon> allApps,
String packageNameRestriction,
UserHandle userHandleRestriction,
Set<String> hideApps) {
Set<ComponentName> hideApps) {
List<UserHandle> profiles =
packageNameRestriction != null && userHandleRestriction != null
? Collections.singletonList(userHandleRestriction)
Expand All @@ -350,18 +356,17 @@ private static void indexProfilesApps(
for (UserHandle profile : profiles) {
for (LauncherActivityInfo info :
la.getActivityList(packageNameRestriction, profile)) {
String packageName = info.getApplicationInfo().packageName;
if (hideApps.contains(packageName)) {
// Always skip this package.
ComponentName componentName = info.getComponentName();
if (hideApps.contains(componentName)) {
continue;
}
Drawable icon = PieLauncherApp.iconPack.getIcon(packageName);
Drawable icon = PieLauncherApp.iconPack.getIcon(componentName);
if (icon == null &&
(icon = getBadgedIcon(info)) == null) {
continue;
}
addApp(allApps,
info.getComponentName(),
componentName,
info.getLabel().toString(),
icon,
profile);
Expand Down Expand Up @@ -417,13 +422,15 @@ private AppMenu.Icon addDrawerIcon(Context context,
Map<LauncherItemKey, AppIcon> allApps) {
String appPackageName = context.getPackageName();
drawerPackageName = appPackageName + ".drawer";
Drawable icon = PieLauncherApp.iconPack.getIcon(drawerPackageName);
ComponentName componentName = new ComponentName(
drawerPackageName, "Drawer");
Drawable icon = PieLauncherApp.iconPack.getIcon(componentName);
if (icon == null) {
icon = Converter.getDrawable(
context.getResources(), R.drawable.ic_drawer);
}
return addApp(allApps,
new ComponentName(drawerPackageName, "Drawer"),
componentName,
"Drawer",
icon,
null);
Expand Down Expand Up @@ -560,6 +567,12 @@ private LauncherApps getLauncherApps(Context context) {
return launcherApps;
}

private static Intent getLaunchIntent(Context context,
String packageName) {
PackageManager pm = context.getPackageManager();
return pm != null ? pm.getLaunchIntentForPackage(packageName) : null;
}

private static String getSubject(int item, AppIcon appIcon,
Locale defaultLocale) {
if (item == Preferences.SEARCH_PARAMETER_PACKAGE_NAME) {
Expand Down
Loading

0 comments on commit bbe5e00

Please sign in to comment.