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