Migrate PackageManagerHelper to MainThreadInitializedObject

- This is in preparation for other things that can be loaded with
  PMH initialization and prevents duplicate temporary helpers from
  loading this many times.
- Most calls in PMH can use the app context, but one call requires
  starting activities/showing toasts so that one needs to take the
  context and can be made static instead.

Bug: 323112914
Test: atest NexusLauncherTests
Change-Id: Id11c780955880cf49c022cbf2744c41e1b696355
This commit is contained in:
Winson Chung
2024-04-04 05:46:32 +00:00
parent 1ebeb1d1ae
commit bb32b7e0de
13 changed files with 47 additions and 34 deletions
@@ -56,10 +56,14 @@ import java.util.Objects;
/**
* Utility methods using package manager
*/
public class PackageManagerHelper {
public class PackageManagerHelper implements SafeCloseable{
private static final String TAG = "PackageManagerHelper";
@NonNull
public static final MainThreadInitializedObject<PackageManagerHelper> INSTANCE =
new MainThreadInitializedObject<>(PackageManagerHelper::new);
@NonNull
private final Context mContext;
@@ -75,6 +79,9 @@ public class PackageManagerHelper {
mLauncherApps = Objects.requireNonNull(context.getSystemService(LauncherApps.class));
}
@Override
public void close() { }
/**
* Returns true if the app can possibly be on the SDCard. This is just a workaround and doesn't
* guarantee that the app is on SD card.
@@ -170,10 +177,11 @@ public class PackageManagerHelper {
/**
* Starts the details activity for {@code info}
*/
public void startDetailsActivityForInfo(ItemInfo info, Rect sourceBounds, Bundle opts) {
public static void startDetailsActivityForInfo(Context context, ItemInfo info,
Rect sourceBounds, Bundle opts) {
if (info instanceof ItemInfoWithIcon appInfo
&& (appInfo.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0) {
mContext.startActivity(ApiWrapper.INSTANCE.get(mContext).getAppMarketActivityIntent(
context.startActivity(ApiWrapper.INSTANCE.get(context).getAppMarketActivityIntent(
appInfo.getTargetComponent().getPackageName(), Process.myUserHandle()));
return;
}
@@ -189,9 +197,10 @@ public class PackageManagerHelper {
}
if (componentName != null) {
try {
mLauncherApps.startAppDetailsActivity(componentName, info.user, sourceBounds, opts);
context.getSystemService(LauncherApps.class).startAppDetailsActivity(componentName,
info.user, sourceBounds, opts);
} catch (SecurityException | ActivityNotFoundException e) {
Toast.makeText(mContext, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Toast.makeText(context, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Log.e(TAG, "Unable to launch settings", e);
}
}