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