Merge "Removing dependency on getApplicationInfo method added in N" into ub-launcher3-calgary

This commit is contained in:
Sunny Goyal
2016-04-08 17:58:30 +00:00
committed by Android (Google) Code Review
7 changed files with 90 additions and 94 deletions
+2 -1
View File
@@ -26,6 +26,7 @@ import com.android.launcher3.compat.LauncherActivityInfoCompat;
import com.android.launcher3.compat.UserHandleCompat; import com.android.launcher3.compat.UserHandleCompat;
import com.android.launcher3.compat.UserManagerCompat; import com.android.launcher3.compat.UserManagerCompat;
import com.android.launcher3.util.ComponentKey; import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.PackageManagerHelper;
import java.util.ArrayList; import java.util.ArrayList;
@@ -87,7 +88,7 @@ public class AppInfo extends ItemInfo {
this.componentName = info.getComponentName(); this.componentName = info.getComponentName();
this.container = ItemInfo.NO_ID; this.container = ItemInfo.NO_ID;
flags = initFlags(info); flags = initFlags(info);
if ((info.getApplicationInfo().flags & LauncherActivityInfoCompat.FLAG_SUSPENDED) != 0) { if (PackageManagerHelper.isAppSuspended(info.getApplicationInfo())) {
isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED; isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
} }
if (quietModeEnabled) { if (quietModeEnabled) {
+23 -17
View File
@@ -33,7 +33,6 @@ import android.content.pm.ResolveInfo;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.Looper; import android.os.Looper;
@@ -64,6 +63,7 @@ import com.android.launcher3.util.CursorIconInfo;
import com.android.launcher3.util.FlagOp; import com.android.launcher3.util.FlagOp;
import com.android.launcher3.util.LongArrayMap; import com.android.launcher3.util.LongArrayMap;
import com.android.launcher3.util.ManagedProfileHeuristic; import com.android.launcher3.util.ManagedProfileHeuristic;
import com.android.launcher3.util.PackageManagerHelper;
import com.android.launcher3.util.StringFilter; import com.android.launcher3.util.StringFilter;
import com.android.launcher3.util.Thunk; import com.android.launcher3.util.Thunk;
import com.android.launcher3.util.ViewOnDrawExecutor; import com.android.launcher3.util.ViewOnDrawExecutor;
@@ -1749,6 +1749,7 @@ public class LauncherModel extends BroadcastReceiver
long serialNumber; long serialNumber;
Intent intent; Intent intent;
UserHandleCompat user; UserHandleCompat user;
String targetPackage;
while (!mStopped && c.moveToNext()) { while (!mStopped && c.moveToNext()) {
try { try {
@@ -1767,6 +1768,7 @@ public class LauncherModel extends BroadcastReceiver
int promiseType = c.getInt(restoredIndex); int promiseType = c.getInt(restoredIndex);
int disabledState = 0; int disabledState = 0;
boolean itemReplaced = false; boolean itemReplaced = false;
targetPackage = null;
if (user == null) { if (user == null) {
// User has been deleted remove the item. // User has been deleted remove the item.
itemsToRemove.add(id); itemsToRemove.add(id);
@@ -1780,6 +1782,9 @@ public class LauncherModel extends BroadcastReceiver
cn.getPackageName(), user); cn.getPackageName(), user);
boolean validComponent = validPkg && boolean validComponent = validPkg &&
launcherApps.isActivityEnabledForProfile(cn, user); launcherApps.isActivityEnabledForProfile(cn, user);
if (validPkg) {
targetPackage = cn.getPackageName();
}
if (validComponent) { if (validComponent) {
if (restored) { if (restored) {
@@ -1787,13 +1792,8 @@ public class LauncherModel extends BroadcastReceiver
restoredRows.add(id); restoredRows.add(id);
restored = false; restored = false;
} }
boolean isSuspended = launcherApps.isPackageSuspendedForProfile(
cn.getPackageName(), user);
if (isSuspended) {
disabledState = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
}
if (quietMode.get(serialNumber)) { if (quietMode.get(serialNumber)) {
disabledState |= ShortcutInfo.FLAG_DISABLED_QUIET_USER; disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
} }
} else if (validPkg) { } else if (validPkg) {
intent = null; intent = null;
@@ -1859,9 +1859,8 @@ public class LauncherModel extends BroadcastReceiver
itemsToRemove.add(id); itemsToRemove.add(id);
continue; continue;
} }
} else if (launcherApps.isAppEnabled( } else if (PackageManagerHelper.isAppOnSdcard(
manager, cn.getPackageName(), manager, cn.getPackageName())) {
PackageManager.GET_UNINSTALLED_PACKAGES)) {
// Package is present but not available. // Package is present but not available.
allowMissingTarget = true; allowMissingTarget = true;
disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE; disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
@@ -1901,7 +1900,7 @@ public class LauncherModel extends BroadcastReceiver
if (itemReplaced) { if (itemReplaced) {
if (user.equals(UserHandleCompat.myUserHandle())) { if (user.equals(UserHandleCompat.myUserHandle())) {
info = getAppShortcutInfo(manager, intent, user, context, null, info = getAppShortcutInfo(intent, user, context, null,
cursorIconInfo.iconIndex, titleIndex, cursorIconInfo.iconIndex, titleIndex,
false, useLowResIcon); false, useLowResIcon);
} else { } else {
@@ -1921,12 +1920,17 @@ public class LauncherModel extends BroadcastReceiver
} }
} else if (itemType == } else if (itemType ==
LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
info = getAppShortcutInfo(manager, intent, user, context, c, info = getAppShortcutInfo(intent, user, context, c,
cursorIconInfo.iconIndex, titleIndex, cursorIconInfo.iconIndex, titleIndex,
allowMissingTarget, useLowResIcon); allowMissingTarget, useLowResIcon);
} else { } else {
info = getShortcutInfo(c, context, titleIndex, cursorIconInfo); info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
// Shortcuts are only available on the primary profile
if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
}
// App shortcuts that used to be automatically added to Launcher // App shortcuts that used to be automatically added to Launcher
// didn't always have the correct intent flags set, so do that // didn't always have the correct intent flags set, so do that
// here // here
@@ -1954,7 +1958,7 @@ public class LauncherModel extends BroadcastReceiver
if (info.promisedIntent != null) { if (info.promisedIntent != null) {
info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber); info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
} }
info.isDisabled = disabledState; info.isDisabled |= disabledState;
if (isSafeMode && !Utilities.isSystemApp(context, intent)) { if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE; info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
} }
@@ -2822,9 +2826,7 @@ public class LauncherModel extends BroadcastReceiver
packagesUnavailable.clear(); packagesUnavailable.clear();
for (String pkg : entry.getValue()) { for (String pkg : entry.getValue()) {
if (!launcherApps.isPackageEnabledForProfile(pkg, user)) { if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
boolean packageOnSdcard = launcherApps.isAppEnabled( if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
if (packageOnSdcard) {
packagesUnavailable.add(pkg); packagesUnavailable.add(pkg);
} else { } else {
packagesRemoved.add(pkg); packagesRemoved.add(pkg);
@@ -3310,7 +3312,7 @@ public class LauncherModel extends BroadcastReceiver
* *
* If c is not null, then it will be used to fill in missing data like the title and icon. * If c is not null, then it will be used to fill in missing data like the title and icon.
*/ */
public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent, public ShortcutInfo getAppShortcutInfo(Intent intent,
UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex, UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
boolean allowMissingTarget, boolean useLowResIcon) { boolean allowMissingTarget, boolean useLowResIcon) {
if (user == null) { if (user == null) {
@@ -3340,6 +3342,10 @@ public class LauncherModel extends BroadcastReceiver
info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon); info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
} }
if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
}
// from the db // from the db
if (TextUtils.isEmpty(info.title) && c != null) { if (TextUtils.isEmpty(info.title) && c != null) {
info.title = Utilities.trim(c.getString(titleIndex)); info.title = Utilities.trim(c.getString(titleIndex));
@@ -24,8 +24,6 @@ import android.graphics.drawable.Drawable;
public abstract class LauncherActivityInfoCompat { public abstract class LauncherActivityInfoCompat {
public static final int FLAG_SUSPENDED = 1<<30;
LauncherActivityInfoCompat() { LauncherActivityInfoCompat() {
} }
@@ -19,9 +19,6 @@ package com.android.launcher3.compat;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Bundle; import android.os.Bundle;
@@ -59,9 +56,7 @@ public abstract class LauncherAppsCompat {
public static LauncherAppsCompat getInstance(Context context) { public static LauncherAppsCompat getInstance(Context context) {
synchronized (sInstanceLock) { synchronized (sInstanceLock) {
if (sInstance == null) { if (sInstance == null) {
if (Utilities.isNycOrAbove()) { if (Utilities.ATLEAST_LOLLIPOP) {
sInstance = new LauncherAppsCompatVN(context.getApplicationContext());
} else if (Utilities.ATLEAST_LOLLIPOP) {
sInstance = new LauncherAppsCompatVL(context.getApplicationContext()); sInstance = new LauncherAppsCompatVL(context.getApplicationContext());
} else { } else {
sInstance = new LauncherAppsCompatV16(context.getApplicationContext()); sInstance = new LauncherAppsCompatV16(context.getApplicationContext());
@@ -84,13 +79,4 @@ public abstract class LauncherAppsCompat {
public abstract boolean isActivityEnabledForProfile(ComponentName component, public abstract boolean isActivityEnabledForProfile(ComponentName component,
UserHandleCompat user); UserHandleCompat user);
public abstract boolean isPackageSuspendedForProfile(String packageName, UserHandleCompat user); public abstract boolean isPackageSuspendedForProfile(String packageName, UserHandleCompat user);
public boolean isAppEnabled(PackageManager pm, String packageName, int flags) {
try {
ApplicationInfo info = pm.getApplicationInfo(packageName, flags);
return info != null && info.enabled;
} catch (NameNotFoundException e) {
return false;
}
}
} }
@@ -27,11 +27,11 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.graphics.Rect; import android.graphics.Rect;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings; import android.provider.Settings;
import com.android.launcher3.Utilities; import com.android.launcher3.Utilities;
import com.android.launcher3.util.PackageManagerHelper;
import com.android.launcher3.util.Thunk; import com.android.launcher3.util.Thunk;
import java.util.ArrayList; import java.util.ArrayList;
@@ -114,7 +114,7 @@ public class LauncherAppsCompatV16 extends LauncherAppsCompat {
} }
public boolean isPackageEnabledForProfile(String packageName, UserHandleCompat user) { public boolean isPackageEnabledForProfile(String packageName, UserHandleCompat user) {
return isAppEnabled(mPm, packageName, 0); return PackageManagerHelper.isAppEnabled(mPm, packageName);
} }
public boolean isActivityEnabledForProfile(ComponentName component, UserHandleCompat user) { public boolean isActivityEnabledForProfile(ComponentName component, UserHandleCompat user) {
@@ -1,57 +0,0 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.compat;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.LauncherApps;
import android.os.UserHandle;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//TODO: Once gogole3 SDK is updated to N, add @TargetApi(Build.VERSION_CODES.N)
public class LauncherAppsCompatVN extends LauncherAppsCompatVL {
private static final String TAG = "LauncherAppsCompatVN";
LauncherAppsCompatVN(Context context) {
super(context);
}
@Override
public boolean isPackageSuspendedForProfile(String packageName, UserHandleCompat user) {
if (user != null && packageName != null) {
try {
//TODO: Replace with proper API call once google3 SDK is updated.
Method getApplicationInfoMethod = LauncherApps.class.getMethod("getApplicationInfo",
String.class, int.class, UserHandle.class);
ApplicationInfo info = (ApplicationInfo) getApplicationInfoMethod.invoke(
mLauncherApps, packageName, 0, user.getUser());
if (info != null) {
return (info.flags & LauncherActivityInfoCompat.FLAG_SUSPENDED) != 0;
}
} catch (NoSuchMethodError | NoSuchMethodException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
Log.e(TAG, "Running on N without getApplicationInfo", e);
}
}
return false;
}
}
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.util;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
/**
* Utility methods using package manager
*/
public class PackageManagerHelper {
private static final int FLAG_SUSPENDED = 1<<30;
/**
* 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.
*/
public static boolean isAppOnSdcard(PackageManager pm, String packageName) {
return isAppEnabled(pm, packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
}
public static boolean isAppEnabled(PackageManager pm, String packageName) {
return isAppEnabled(pm, packageName, 0);
}
public static boolean isAppEnabled(PackageManager pm, String packageName, int flags) {
try {
ApplicationInfo info = pm.getApplicationInfo(packageName, flags);
return info != null && info.enabled;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
public static boolean isAppSuspended(PackageManager pm, String packageName) {
try {
ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
return info != null && isAppSuspended(info);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
public static boolean isAppSuspended(ApplicationInfo info) {
return (info.flags & FLAG_SUSPENDED) != 0;
}
}