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