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 | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame^] | 15 | import android.view.View; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 16 | import android.widget.Toast; |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 17 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame^] | 18 | import com.android.launcher3.Launcher.OnResumeCallback; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 19 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame^] | 20 | import com.android.launcher3.dragndrop.DragOptions; |
| 21 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 22 | |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 23 | import java.net.URISyntaxException; |
| 24 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 25 | public class UninstallDropTarget extends ButtonDropTarget { |
| 26 | |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 27 | private static final String TAG = "UninstallDropTarget"; |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 28 | private static Boolean sUninstallDisabled; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 29 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 30 | public UninstallDropTarget(Context context, AttributeSet attrs) { |
| 31 | this(context, attrs, 0); |
| 32 | } |
| 33 | |
| 34 | public UninstallDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 35 | super(context, attrs, defStyle); |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | protected void onFinishInflate() { |
| 40 | super.onFinishInflate(); |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 41 | setupUi(); |
| 42 | } |
| 43 | |
| 44 | protected void setupUi() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 45 | // Get the hover color |
Sunny Goyal | 3a2698b | 2015-04-28 21:36:46 -0700 | [diff] [blame] | 46 | mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint); |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 47 | setDrawable(R.drawable.ic_uninstall_shadow); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 51 | protected boolean supportsDrop(ItemInfo info) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 52 | return supportsDrop(getContext(), info); |
| 53 | } |
| 54 | |
Sunny Goyal | 1fe0c2c | 2017-08-15 12:54:42 -0700 | [diff] [blame] | 55 | public static boolean supportsDrop(Context context, ItemInfo info) { |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 56 | if (sUninstallDisabled == null) { |
| 57 | UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 58 | Bundle restrictions = userManager.getUserRestrictions(); |
| 59 | sUninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false) |
| 60 | || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false); |
| 61 | } |
| 62 | if (sUninstallDisabled) { |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 63 | return false; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 66 | if (info instanceof AppInfo) { |
| 67 | AppInfo appInfo = (AppInfo) info; |
| 68 | if (appInfo.isSystemApp != AppInfo.FLAG_SYSTEM_UNKNOWN) { |
| 69 | return (appInfo.isSystemApp & AppInfo.FLAG_SYSTEM_NO) != 0; |
| 70 | } |
| 71 | } |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 72 | return getUninstallTarget(context, info) != null; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 76 | * @return the component name that should be uninstalled or null. |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 77 | */ |
Sunny Goyal | 1fe0c2c | 2017-08-15 12:54:42 -0700 | [diff] [blame] | 78 | private static ComponentName getUninstallTarget(Context context, ItemInfo item) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 79 | Intent intent = null; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 80 | UserHandle user = null; |
Sunny Goyal | 1fe0c2c | 2017-08-15 12:54:42 -0700 | [diff] [blame] | 81 | if (item != null && |
| 82 | item.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) { |
| 83 | intent = item.getIntent(); |
| 84 | user = item.user; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 85 | } |
| 86 | if (intent != null) { |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 87 | LauncherActivityInfo info = LauncherAppsCompat.getInstance(context) |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 88 | .resolveActivity(intent, user); |
| 89 | if (info != null |
| 90 | && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 91 | return info.getComponentName(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | @Override |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame^] | 98 | public void onDrop(DragObject d, DragOptions options) { |
| 99 | // Defer onComplete |
| 100 | if (options.deferCompleteForUninstall) { |
| 101 | d.dragSource = new DeferredOnComplete(d.dragSource, getContext()); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 102 | } |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame^] | 103 | super.onDrop(d, options); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 107 | public void completeDrop(final DragObject d) { |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame^] | 108 | ComponentName target = performDropAction(d); |
| 109 | if (d.dragSource instanceof DeferredOnComplete) { |
| 110 | DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource; |
| 111 | if (target != null) { |
| 112 | deferred.mPackageName = target.getPackageName(); |
| 113 | mLauncher.setOnResumeCallback(deferred); |
| 114 | } else { |
| 115 | deferred.sendFailure(); |
| 116 | } |
| 117 | } |
Tony Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame^] | 120 | /** |
| 121 | * Performs the drop action and returns the target component for the dragObject or null if |
| 122 | * the action was not performed. |
| 123 | */ |
| 124 | protected ComponentName performDropAction(DragObject d) { |
| 125 | return performDropAction(mLauncher, d.dragInfo); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame^] | 128 | /** |
| 129 | * Performs the drop action and returns the target component for the dragObject or null if |
| 130 | * the action was not performed. |
| 131 | */ |
| 132 | private static ComponentName performDropAction(Context context, ItemInfo info) { |
| 133 | ComponentName cn = getUninstallTarget(context, info); |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 134 | if (cn == null) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 135 | // System applications cannot be installed. For now, show a toast explaining that. |
| 136 | // We may give them the option of disabling apps this way. |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame^] | 137 | Toast.makeText(context, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show(); |
| 138 | return null; |
| 139 | } |
| 140 | try { |
| 141 | Intent i = Intent.parseUri(context.getString(R.string.delete_package_intent), 0) |
| 142 | .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName())) |
| 143 | .putExtra(Intent.EXTRA_USER, info.user); |
| 144 | context.startActivity(i); |
| 145 | return cn; |
| 146 | } catch (URISyntaxException e) { |
| 147 | Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info); |
| 148 | return null; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | public static boolean startUninstallActivity(Launcher launcher, ItemInfo info) { |
| 153 | return performDropAction(launcher, info) != null; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until |
| 158 | * {@link #onLauncherResume} |
| 159 | */ |
| 160 | private class DeferredOnComplete implements DragSource, OnResumeCallback { |
| 161 | |
| 162 | private final DragSource mOriginal; |
| 163 | private final Context mContext; |
| 164 | |
| 165 | private String mPackageName; |
| 166 | private DragObject mDragObject; |
| 167 | |
| 168 | public DeferredOnComplete(DragSource original, Context context) { |
| 169 | mOriginal = original; |
| 170 | mContext = context; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete, |
| 175 | boolean success) { |
| 176 | mDragObject = d; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void fillInLogContainerData(View v, ItemInfo info, Target target, |
| 181 | Target targetParent) { |
| 182 | mOriginal.fillInLogContainerData(v, info, target, targetParent); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void onLauncherResume() { |
| 187 | // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well. |
| 188 | if (LauncherAppsCompat.getInstance(mContext) |
| 189 | .getApplicationInfo(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 190 | mDragObject.dragInfo.user) == null) { |
| 191 | mDragObject.dragSource = mOriginal; |
| 192 | mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, false, true); |
| 193 | } else { |
| 194 | sendFailure(); |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 195 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 196 | } |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame^] | 197 | |
| 198 | public void sendFailure() { |
| 199 | mDragObject.dragSource = mOriginal; |
| 200 | mDragObject.cancelled = true; |
| 201 | mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, false, false); |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 202 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 203 | } |
| 204 | } |