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