Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 3 | import android.annotation.TargetApi; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 4 | import android.content.ComponentName; |
| 5 | import android.content.Context; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 6 | import android.content.Intent; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 7 | import android.content.pm.ApplicationInfo; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 8 | import android.net.Uri; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 9 | import android.os.Build; |
| 10 | import android.os.Bundle; |
| 11 | import android.os.UserManager; |
| 12 | import android.util.AttributeSet; |
| 13 | import android.util.Pair; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 14 | import android.widget.Toast; |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 15 | |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 16 | import com.android.launcher3.compat.LauncherActivityInfoCompat; |
| 17 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 18 | import com.android.launcher3.compat.UserHandleCompat; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 19 | |
| 20 | public class UninstallDropTarget extends ButtonDropTarget { |
| 21 | |
| 22 | public UninstallDropTarget(Context context, AttributeSet attrs) { |
| 23 | this(context, attrs, 0); |
| 24 | } |
| 25 | |
| 26 | public UninstallDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 27 | super(context, attrs, defStyle); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | protected void onFinishInflate() { |
| 32 | super.onFinishInflate(); |
| 33 | // Get the hover color |
Sunny Goyal | 3a2698b | 2015-04-28 21:36:46 -0700 | [diff] [blame] | 34 | mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 35 | |
Sunny Goyal | 40b626b | 2015-06-04 22:40:14 -0700 | [diff] [blame] | 36 | setDrawable(R.drawable.ic_uninstall_launcher); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @Override |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 40 | protected boolean supportsDrop(DragSource source, ItemInfo info) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 41 | return supportsDrop(getContext(), info); |
| 42 | } |
| 43 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 44 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 45 | public static boolean supportsDrop(Context context, Object info) { |
Sunny Goyal | 9fc953b | 2015-08-17 12:24:25 -0700 | [diff] [blame] | 46 | if (Utilities.ATLEAST_JB_MR2) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 47 | UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 48 | Bundle restrictions = userManager.getUserRestrictions(); |
| 49 | if (restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false) |
| 50 | || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false)) { |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 55 | return getUninstallTarget(context, info) != null; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 59 | * @return the component name that should be uninstalled or null. |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 60 | */ |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 61 | private static ComponentName getUninstallTarget(Context context, Object item) { |
| 62 | Intent intent = null; |
| 63 | UserHandleCompat user = null; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 64 | if (item instanceof AppInfo) { |
| 65 | AppInfo info = (AppInfo) item; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 66 | intent = info.intent; |
| 67 | user = info.user; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 68 | } else if (item instanceof ShortcutInfo) { |
| 69 | ShortcutInfo info = (ShortcutInfo) item; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 70 | if (info.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) { |
| 71 | // Do not use restore/target intent here as we cannot uninstall an app which is |
| 72 | // being installed/restored. |
| 73 | intent = info.intent; |
| 74 | user = info.user; |
| 75 | } |
| 76 | } |
| 77 | if (intent != null) { |
| 78 | LauncherActivityInfoCompat info = LauncherAppsCompat.getInstance(context) |
| 79 | .resolveActivity(intent, user); |
| 80 | if (info != null |
| 81 | && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 82 | return info.getComponentName(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void onDrop(DragObject d) { |
| 90 | // Differ item deletion |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 91 | if (d.dragSource instanceof DropTargetSource) { |
| 92 | ((DropTargetSource) d.dragSource).deferCompleteDropAfterUninstallActivity(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 93 | } |
| 94 | super.onDrop(d); |
| 95 | } |
| 96 | |
| 97 | @Override |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame^] | 98 | public void completeDrop(final DragObject d) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 99 | DropTargetResultCallback callback = d.dragSource instanceof DropTargetResultCallback |
| 100 | ? (DropTargetResultCallback) d.dragSource : null; |
| 101 | startUninstallActivity(mLauncher, d.dragInfo, callback); |
Tony Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 104 | public static boolean startUninstallActivity(Launcher launcher, ItemInfo info) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 105 | return startUninstallActivity(launcher, info, null); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 108 | public static boolean startUninstallActivity( |
| 109 | final Launcher launcher, ItemInfo info, DropTargetResultCallback callback) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 110 | final ComponentName cn = getUninstallTarget(launcher, info); |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 111 | |
| 112 | final boolean isUninstallable; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 113 | if (cn == null) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 114 | // System applications cannot be installed. For now, show a toast explaining that. |
| 115 | // We may give them the option of disabling apps this way. |
| 116 | Toast.makeText(launcher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show(); |
| 117 | isUninstallable = false; |
| 118 | } else { |
| 119 | Intent intent = new Intent(Intent.ACTION_DELETE, |
| 120 | Uri.fromParts("package", cn.getPackageName(), cn.getClassName())) |
| 121 | .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 122 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 123 | info.user.addToIntent(intent, Intent.EXTRA_USER); |
| 124 | launcher.startActivity(intent); |
| 125 | isUninstallable = true; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 126 | } |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 127 | if (callback != null) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 128 | sendUninstallResult(launcher, isUninstallable, cn, info.user, callback); |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 129 | } |
| 130 | return isUninstallable; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Notifies the {@param callback} whether the uninstall was successful or not. |
| 135 | * |
| 136 | * Since there is no direct callback for an uninstall request, we check the package existence |
| 137 | * when the launch resumes next time. This assumes that the uninstall activity will finish only |
| 138 | * after the task is completed |
| 139 | */ |
| 140 | protected static void sendUninstallResult( |
| 141 | final Launcher launcher, boolean activityStarted, |
| 142 | final ComponentName cn, final UserHandleCompat user, |
| 143 | final DropTargetResultCallback callback) { |
| 144 | if (activityStarted) { |
| 145 | final Runnable checkIfUninstallWasSuccess = new Runnable() { |
| 146 | @Override |
| 147 | public void run() { |
| 148 | String packageName = cn.getPackageName(); |
| 149 | boolean uninstallSuccessful = !AllAppsList.packageHasActivities( |
| 150 | launcher, packageName, user); |
| 151 | callback.onDragObjectRemoved(uninstallSuccessful); |
| 152 | } |
| 153 | }; |
| 154 | launcher.addOnResumeCallback(checkIfUninstallWasSuccess); |
| 155 | } else { |
| 156 | callback.onDragObjectRemoved(false); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public interface DropTargetResultCallback { |
| 161 | /** |
| 162 | * A drag operation was complete. |
| 163 | * @param isRemoved true if the drag object should be removed, false otherwise. |
| 164 | */ |
| 165 | void onDragObjectRemoved(boolean isRemoved); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Interface defining an object that can provide uninstallable drag objects. |
| 170 | */ |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 171 | public interface DropTargetSource extends DropTargetResultCallback { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * Indicates that an uninstall request are made and the actual result may come |
| 175 | * after some time. |
| 176 | */ |
| 177 | void deferCompleteDropAfterUninstallActivity(); |
| 178 | } |
| 179 | } |