Caching the uninstall disabled state for each userHandle

> Removing static access to varios drop targets
> Creating a cache at UI level with 5sec timeout

Bug: 67104426
Change-Id: Ide6e2e0c01606f9b5fb9281f95dc009873c18fb9
This commit is contained in:
Sunny Goyal committed 2017-10-24 14:55:54 -07:00
1 parent be93f264d7
commit 0236d0b0ba
6 files changed
+125 -82

No files matched your search

@@ -10,22 +10,28 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.ArrayMap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.android.launcher3.Launcher.OnResumeCallback;
import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
import com.android.launcher3.compat.LauncherAppsCompat;
import com.android.launcher3.dragndrop.DragOptions;
import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
import java.net.URISyntaxException;
public class UninstallDropTarget extends ButtonDropTarget {
public class UninstallDropTarget extends ButtonDropTarget implements OnAlarmListener {
private static final String TAG = "UninstallDropTarget";
private static Boolean sUninstallDisabled;
private static final long CACHE_EXPIRE_TIMEOUT = 5000;
private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
private final Alarm mCacheExpireAlarm;
public UninstallDropTarget(Context context, AttributeSet attrs) {
this(context, attrs, 0);
@@ -33,6 +39,9 @@ public class UninstallDropTarget extends ButtonDropTarget {
public UninstallDropTarget(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mCacheExpireAlarm = new Alarm();
mCacheExpireAlarm.setOnAlarmListener(this);
}
@Override
@@ -48,18 +57,29 @@ public class UninstallDropTarget extends ButtonDropTarget {
}
@Override
protected boolean supportsDrop(ItemInfo info) {
return supportsDrop(getContext(), info);
public void onAlarm(Alarm alarm) {
mUninstallDisabledCache.clear();
}
public static boolean supportsDrop(Context context, ItemInfo info) {
if (sUninstallDisabled == null) {
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
Bundle restrictions = userManager.getUserRestrictions();
sUninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
@Override
public int getAccessibilityAction() {
return LauncherAccessibilityDelegate.UNINSTALL;
}
@Override
protected boolean supportsDrop(ItemInfo info) {
Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
if (uninstallDisabled == null) {
UserManager userManager =
(UserManager) getContext().getSystemService(Context.USER_SERVICE);
Bundle restrictions = userManager.getUserRestrictions(info.user);
uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
|| restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
mUninstallDisabledCache.put(info.user, uninstallDisabled);
}
if (sUninstallDisabled) {
// Cancel any pending alarm and set cache expiry after some time
mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
if (uninstallDisabled) {
return false;
}
@@ -69,13 +89,13 @@ public class UninstallDropTarget extends ButtonDropTarget {
return (appInfo.isSystemApp & AppInfo.FLAG_SYSTEM_NO) != 0;
}
}
return getUninstallTarget(context, info) != null;
return getUninstallTarget(info) != null;
}
/**
* @return the component name that should be uninstalled or null.
*/
private static ComponentName getUninstallTarget(Context context, ItemInfo item) {
private ComponentName getUninstallTarget(ItemInfo item) {
Intent intent = null;
UserHandle user = null;
if (item != null &&
@@ -84,7 +104,7 @@ public class UninstallDropTarget extends ButtonDropTarget {
user = item.user;
}
if (intent != null) {
LauncherActivityInfo info = LauncherAppsCompat.getInstance(context)
LauncherActivityInfo info = LauncherAppsCompat.getInstance(mLauncher)
.resolveActivity(intent, user);
if (info != null
&& (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
@@ -103,7 +123,7 @@ public class UninstallDropTarget extends ButtonDropTarget {
@Override
public void completeDrop(final DragObject d) {
ComponentName target = performDropAction(d);
ComponentName target = performDropAction(d.dragInfo);
if (d.dragSource instanceof DeferredOnComplete) {
DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
if (target != null) {
@@ -119,27 +139,19 @@ public class UninstallDropTarget extends ButtonDropTarget {
* Performs the drop action and returns the target component for the dragObject or null if
* the action was not performed.
*/
protected ComponentName performDropAction(DragObject d) {
return performDropAction(mLauncher, d.dragInfo);
}
/**
* Performs the drop action and returns the target component for the dragObject or null if
* the action was not performed.
*/
private static ComponentName performDropAction(Context context, ItemInfo info) {
ComponentName cn = getUninstallTarget(context, info);
protected ComponentName performDropAction(ItemInfo info) {
ComponentName cn = getUninstallTarget(info);
if (cn == null) {
// System applications cannot be installed. For now, show a toast explaining that.
// We may give them the option of disabling apps this way.
Toast.makeText(context, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
return null;
}
try {
Intent i = Intent.parseUri(context.getString(R.string.delete_package_intent), 0)
Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
.setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
.putExtra(Intent.EXTRA_USER, info.user);
context.startActivity(i);
mLauncher.startActivity(i);
return cn;
} catch (URISyntaxException e) {
Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
@@ -147,8 +159,9 @@ public class UninstallDropTarget extends ButtonDropTarget {
}
}
public static boolean startUninstallActivity(Launcher launcher, ItemInfo info) {
return performDropAction(launcher, info) != null;
@Override
public void onAccessibilityDrop(View view, ItemInfo item) {
performDropAction(item);
}
/**