blob: 55cb6f2143ff6b215ff2ff3dcd81ce32c4048584 [file] [log] [blame]
Sunny Goyalfa401a12015-04-10 13:45:42 -07001package com.android.launcher3;
2
Winson Chung1054d4e2018-03-05 19:39:21 +00003import static android.appwidget.AppWidgetManager.INVALID_APPWIDGET_ID;
4import static android.appwidget.AppWidgetProviderInfo.WIDGET_FEATURE_RECONFIGURABLE;
Sunny Goyal076839c2017-10-30 13:52:20 -07005import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_MASK;
6import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_NO;
Winson Chung1054d4e2018-03-05 19:39:21 +00007import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP;
8import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.RECONFIGURE;
9import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.UNINSTALL;
Sunny Goyal076839c2017-10-30 13:52:20 -070010
Winson Chung1054d4e2018-03-05 19:39:21 +000011import android.appwidget.AppWidgetHostView;
Winson Chung1054d4e2018-03-05 19:39:21 +000012import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070013import android.content.ComponentName;
14import android.content.Context;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080015import android.content.Intent;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070016import android.content.pm.ApplicationInfo;
Sunny Goyal3e9be432017-01-05 15:22:41 -080017import android.content.pm.LauncherActivityInfo;
Sunny Goyalf2db2532017-03-01 17:27:16 -080018import android.content.pm.PackageManager;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080019import android.net.Uri;
Sunny Goyalfa401a12015-04-10 13:45:42 -070020import android.os.Bundle;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080021import android.os.UserHandle;
Sunny Goyalfa401a12015-04-10 13:45:42 -070022import android.os.UserManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070023import android.util.ArrayMap;
Sunny Goyalfa401a12015-04-10 13:45:42 -070024import android.util.AttributeSet;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070025import android.util.Log;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070026import android.view.View;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080027import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070028
Sunny Goyal3dce5f32017-10-05 11:40:05 -070029import com.android.launcher3.Launcher.OnResumeCallback;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070030import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070031import com.android.launcher3.dragndrop.DragOptions;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070032import com.android.launcher3.logging.LoggerUtils;
33import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070034import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Winson Chung1054d4e2018-03-05 19:39:21 +000035import com.android.launcher3.util.Themes;
Sunny Goyalfa401a12015-04-10 13:45:42 -070036
Jon Mirandac56e3ff2017-08-23 12:13:24 -070037import java.net.URISyntaxException;
38
Winson Chung1054d4e2018-03-05 19:39:21 +000039/**
40 * Drop target which provides a secondary option for an item.
41 * For app targets: shows as uninstall
42 * For configurable widgets: shows as setup
43 */
44public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070045
Winson Chung1054d4e2018-03-05 19:39:21 +000046 private static final String TAG = "SecondaryDropTarget";
Sunny Goyal0236d0b2017-10-24 14:54:30 -070047
48 private static final long CACHE_EXPIRE_TIMEOUT = 5000;
49 private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
50
51 private final Alarm mCacheExpireAlarm;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070052
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070053 protected int mCurrentAccessibilityAction = -1;
Winson Chung1054d4e2018-03-05 19:39:21 +000054 public SecondaryDropTarget(Context context, AttributeSet attrs) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070055 this(context, attrs, 0);
56 }
57
Winson Chung1054d4e2018-03-05 19:39:21 +000058 public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070059 super(context, attrs, defStyle);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070060
61 mCacheExpireAlarm = new Alarm();
62 mCacheExpireAlarm.setOnAlarmListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070063 }
64
65 @Override
66 protected void onFinishInflate() {
67 super.onFinishInflate();
Winson Chung1054d4e2018-03-05 19:39:21 +000068 setupUi(UNINSTALL);
Sunny Goyalda1dfa32017-04-26 22:34:49 -070069 }
70
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070071 protected void setupUi(int action) {
Winson Chung1054d4e2018-03-05 19:39:21 +000072 if (action == mCurrentAccessibilityAction) {
73 return;
74 }
75 mCurrentAccessibilityAction = action;
76
77 if (action == UNINSTALL) {
78 mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
79 setDrawable(R.drawable.ic_uninstall_shadow);
80 updateText(R.string.uninstall_drop_target_label);
81 } else {
82 mHoverColor = Themes.getColorAccent(getContext());
83 setDrawable(R.drawable.ic_setup_shadow);
84 updateText(R.string.gadget_setup_text);
85 }
Sunny Goyalfa401a12015-04-10 13:45:42 -070086 }
87
88 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070089 public void onAlarm(Alarm alarm) {
90 mUninstallDisabledCache.clear();
Sunny Goyal1a70cef2015-04-22 11:29:51 -070091 }
92
Sunny Goyal0236d0b2017-10-24 14:54:30 -070093 @Override
94 public int getAccessibilityAction() {
Winson Chung1054d4e2018-03-05 19:39:21 +000095 return mCurrentAccessibilityAction;
96 }
97
98 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070099 public Target getDropTargetForLogging() {
100 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
101 t.controlType = mCurrentAccessibilityAction == UNINSTALL ? ControlType.UNINSTALL_TARGET
102 : ControlType.SETTINGS_BUTTON;
103 return t;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700104 }
105
106 @Override
107 protected boolean supportsDrop(ItemInfo info) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000108 return supportsAccessibilityDrop(info, getViewUnderDrag(info));
109 }
110
111 @Override
112 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
113 if (view instanceof AppWidgetHostView) {
114 if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) {
115 setupUi(RECONFIGURE);
116 return true;
117 }
118 return false;
119 }
120
121 setupUi(UNINSTALL);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700122 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
123 if (uninstallDisabled == null) {
124 UserManager userManager =
125 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
126 Bundle restrictions = userManager.getUserRestrictions(info.user);
127 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700128 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700129 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700130 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700131 // Cancel any pending alarm and set cache expiry after some time
132 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
133 if (uninstallDisabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800134 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700135 }
136
Sunny Goyal076839c2017-10-30 13:52:20 -0700137 if (info instanceof ItemInfoWithIcon) {
138 ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
139 if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) {
140 return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0;
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700141 }
142 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700143 return getUninstallTarget(info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700144 }
145
146 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700147 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -0700148 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700149 private ComponentName getUninstallTarget(ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700150 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800151 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700152 if (item != null &&
Sunny Goyalc5939392018-12-07 11:43:47 -0800153 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700154 intent = item.getIntent();
155 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700156 }
157 if (intent != null) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700158 LauncherActivityInfo info = LauncherAppsCompat.getInstance(mLauncher)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700159 .resolveActivity(intent, user);
160 if (info != null
161 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
162 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700163 }
164 }
165 return null;
166 }
167
168 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700169 public void onDrop(DragObject d, DragOptions options) {
170 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700171 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700172 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700173 }
174
175 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800176 public void completeDrop(final DragObject d) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000177 ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700178 if (d.dragSource instanceof DeferredOnComplete) {
179 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
180 if (target != null) {
181 deferred.mPackageName = target.getPackageName();
Winson Chung0b70cd42019-06-17 22:34:33 -0700182 mLauncher.addOnResumeCallback(deferred);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700183 } else {
184 deferred.sendFailure();
185 }
186 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700187 }
188
Winson Chung1054d4e2018-03-05 19:39:21 +0000189 private View getViewUnderDrag(ItemInfo info) {
190 if (info instanceof LauncherAppWidgetInfo && info.container == CONTAINER_DESKTOP &&
191 mLauncher.getWorkspace().getDragInfo() != null) {
192 return mLauncher.getWorkspace().getDragInfo().cell;
193 }
194 return null;
195 }
196
197 /**
198 * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id,
199 * otherwise return {@code INVALID_APPWIDGET_ID}
200 */
201 private int getReconfigurableWidgetId(View view) {
202 if (!(view instanceof AppWidgetHostView)) {
203 return INVALID_APPWIDGET_ID;
204 }
205 AppWidgetHostView hostView = (AppWidgetHostView) view;
206 AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo();
207 if (widgetInfo == null || widgetInfo.configure == null) {
208 return INVALID_APPWIDGET_ID;
209 }
210 if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo)
211 .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) {
212 return INVALID_APPWIDGET_ID;
213 }
214 return hostView.getAppWidgetId();
215 }
216
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700217 /**
218 * Performs the drop action and returns the target component for the dragObject or null if
219 * the action was not performed.
220 */
Winson Chung1054d4e2018-03-05 19:39:21 +0000221 protected ComponentName performDropAction(View view, ItemInfo info) {
222 if (mCurrentAccessibilityAction == RECONFIGURE) {
223 int widgetId = getReconfigurableWidgetId(view);
224 if (widgetId != INVALID_APPWIDGET_ID) {
225 mLauncher.getAppWidgetHost().startConfigActivity(mLauncher, widgetId, -1);
226 }
227 return null;
228 }
229 // else: mCurrentAccessibilityAction == UNINSTALL
230
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700231 ComponentName cn = getUninstallTarget(info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700232 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800233 // System applications cannot be installed. For now, show a toast explaining that.
234 // We may give them the option of disabling apps this way.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700235 Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700236 return null;
237 }
238 try {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700239 Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700240 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
241 .putExtra(Intent.EXTRA_USER, info.user);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700242 mLauncher.startActivity(i);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700243 return cn;
244 } catch (URISyntaxException e) {
245 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
246 return null;
247 }
248 }
249
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700250 @Override
251 public void onAccessibilityDrop(View view, ItemInfo item) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000252 performDropAction(view, item);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700253 }
254
255 /**
256 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
257 * {@link #onLauncherResume}
258 */
259 private class DeferredOnComplete implements DragSource, OnResumeCallback {
260
261 private final DragSource mOriginal;
262 private final Context mContext;
263
264 private String mPackageName;
265 private DragObject mDragObject;
266
267 public DeferredOnComplete(DragSource original, Context context) {
268 mOriginal = original;
269 mContext = context;
270 }
271
272 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700273 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700274 boolean success) {
275 mDragObject = d;
276 }
277
278 @Override
279 public void fillInLogContainerData(View v, ItemInfo info, Target target,
280 Target targetParent) {
281 mOriginal.fillInLogContainerData(v, info, target, targetParent);
282 }
283
284 @Override
285 public void onLauncherResume() {
286 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
287 if (LauncherAppsCompat.getInstance(mContext)
288 .getApplicationInfo(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES,
289 mDragObject.dragInfo.user) == null) {
290 mDragObject.dragSource = mOriginal;
Winson Chung1054d4e2018-03-05 19:39:21 +0000291 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700292 } else {
293 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700294 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700295 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700296
297 public void sendFailure() {
298 mDragObject.dragSource = mOriginal;
299 mDragObject.cancelled = true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000300 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800301 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700302 }
303}