blob: 114c49118f959be46af339c055a128df9c632353 [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;
Samuel Fufacc1e1072019-09-06 16:19:11 -07005
Sunny Goyal076839c2017-10-30 13:52:20 -07006import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_MASK;
7import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_NO;
Winson Chung1054d4e2018-03-05 19:39:21 +00008import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP;
Samuel Fufa5cf3e862020-02-03 20:22:54 -08009import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.DISMISS_PREDICTION;
Winson Chung1054d4e2018-03-05 19:39:21 +000010import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.RECONFIGURE;
11import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.UNINSTALL;
Sunny Goyal076839c2017-10-30 13:52:20 -070012
Winson Chung1054d4e2018-03-05 19:39:21 +000013import android.appwidget.AppWidgetHostView;
Winson Chung1054d4e2018-03-05 19:39:21 +000014import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070015import android.content.ComponentName;
16import android.content.Context;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080017import android.content.Intent;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070018import android.content.pm.ApplicationInfo;
Sunny Goyal3e9be432017-01-05 15:22:41 -080019import android.content.pm.LauncherActivityInfo;
Sunny Goyale7b00122019-10-02 16:13:34 -070020import android.content.pm.LauncherApps;
Sunny Goyalf2db2532017-03-01 17:27:16 -080021import android.content.pm.PackageManager;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080022import android.net.Uri;
Sunny Goyalfa401a12015-04-10 13:45:42 -070023import android.os.Bundle;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080024import android.os.UserHandle;
Sunny Goyalfa401a12015-04-10 13:45:42 -070025import android.os.UserManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070026import android.util.ArrayMap;
Sunny Goyalfa401a12015-04-10 13:45:42 -070027import android.util.AttributeSet;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070028import android.util.Log;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070029import android.view.View;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080030import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070031
Sunny Goyal3dce5f32017-10-05 11:40:05 -070032import com.android.launcher3.Launcher.OnResumeCallback;
Samuel Fufa5cf3e862020-02-03 20:22:54 -080033import com.android.launcher3.config.FeatureFlags;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070034import com.android.launcher3.dragndrop.DragOptions;
Samuel Fufacc1e1072019-09-06 16:19:11 -070035import com.android.launcher3.logging.FileLog;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070036import com.android.launcher3.logging.LoggerUtils;
Samuel Fufa5cf3e862020-02-03 20:22:54 -080037import com.android.launcher3.model.AppLaunchTracker;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070038import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070039import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyale7b00122019-10-02 16:13:34 -070040import com.android.launcher3.util.PackageManagerHelper;
Winson Chung1054d4e2018-03-05 19:39:21 +000041import com.android.launcher3.util.Themes;
Sunny Goyalfa401a12015-04-10 13:45:42 -070042
Jon Mirandac56e3ff2017-08-23 12:13:24 -070043import java.net.URISyntaxException;
44
Winson Chung1054d4e2018-03-05 19:39:21 +000045/**
46 * Drop target which provides a secondary option for an item.
47 * For app targets: shows as uninstall
48 * For configurable widgets: shows as setup
Samuel Fufa5cf3e862020-02-03 20:22:54 -080049 * For predicted app icons: don't suggest app
Winson Chung1054d4e2018-03-05 19:39:21 +000050 */
51public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070052
Winson Chung1054d4e2018-03-05 19:39:21 +000053 private static final String TAG = "SecondaryDropTarget";
Sunny Goyal0236d0b2017-10-24 14:54:30 -070054
55 private static final long CACHE_EXPIRE_TIMEOUT = 5000;
56 private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
57
58 private final Alarm mCacheExpireAlarm;
vadimt3d64ffd2020-02-27 11:28:47 -080059 private boolean mHadPendingAlarm;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070060
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070061 protected int mCurrentAccessibilityAction = -1;
Winson Chung1054d4e2018-03-05 19:39:21 +000062 public SecondaryDropTarget(Context context, AttributeSet attrs) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070063 this(context, attrs, 0);
64 }
65
Winson Chung1054d4e2018-03-05 19:39:21 +000066 public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070067 super(context, attrs, defStyle);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070068
69 mCacheExpireAlarm = new Alarm();
70 mCacheExpireAlarm.setOnAlarmListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070071 }
72
73 @Override
vadimt3d64ffd2020-02-27 11:28:47 -080074 protected void onAttachedToWindow() {
75 super.onAttachedToWindow();
76 if (mHadPendingAlarm) {
77 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
78 mHadPendingAlarm = false;
79 }
80 }
81
82 @Override
83 protected void onDetachedFromWindow() {
84 super.onDetachedFromWindow();
85 if (mCacheExpireAlarm.alarmPending()) {
86 mCacheExpireAlarm.cancelAlarm();
87 mHadPendingAlarm = true;
88 }
89 }
90
91 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -070092 protected void onFinishInflate() {
93 super.onFinishInflate();
Winson Chung1054d4e2018-03-05 19:39:21 +000094 setupUi(UNINSTALL);
Sunny Goyalda1dfa32017-04-26 22:34:49 -070095 }
96
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070097 protected void setupUi(int action) {
Winson Chung1054d4e2018-03-05 19:39:21 +000098 if (action == mCurrentAccessibilityAction) {
99 return;
100 }
101 mCurrentAccessibilityAction = action;
102
103 if (action == UNINSTALL) {
104 mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
105 setDrawable(R.drawable.ic_uninstall_shadow);
106 updateText(R.string.uninstall_drop_target_label);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800107 } else if (action == DISMISS_PREDICTION) {
108 mHoverColor = Themes.getColorAccent(getContext());
109 setDrawable(R.drawable.ic_block);
110 updateText(R.string.dismiss_prediction_label);
111 } else if (action == RECONFIGURE) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000112 mHoverColor = Themes.getColorAccent(getContext());
113 setDrawable(R.drawable.ic_setup_shadow);
114 updateText(R.string.gadget_setup_text);
115 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700116 }
117
118 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700119 public void onAlarm(Alarm alarm) {
120 mUninstallDisabledCache.clear();
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700121 }
122
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700123 @Override
124 public int getAccessibilityAction() {
Winson Chung1054d4e2018-03-05 19:39:21 +0000125 return mCurrentAccessibilityAction;
126 }
127
128 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700129 public Target getDropTargetForLogging() {
130 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800131 if (mCurrentAccessibilityAction == UNINSTALL) {
132 t.controlType = ControlType.UNINSTALL_TARGET;
133 } else if (mCurrentAccessibilityAction == DISMISS_PREDICTION) {
134 t.controlType = ControlType.DISMISS_PREDICTION;
135 } else {
136 t.controlType = ControlType.SETTINGS_BUTTON;
137 }
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700138 return t;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700139 }
140
141 @Override
142 protected boolean supportsDrop(ItemInfo info) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000143 return supportsAccessibilityDrop(info, getViewUnderDrag(info));
144 }
145
146 @Override
147 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
148 if (view instanceof AppWidgetHostView) {
149 if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) {
150 setupUi(RECONFIGURE);
151 return true;
152 }
153 return false;
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800154 } else if (FeatureFlags.ENABLE_PREDICTION_DISMISS.get() && info.isPredictedItem()) {
155 setupUi(DISMISS_PREDICTION);
156 return true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000157 }
158
159 setupUi(UNINSTALL);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700160 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
161 if (uninstallDisabled == null) {
162 UserManager userManager =
163 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
164 Bundle restrictions = userManager.getUserRestrictions(info.user);
165 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700166 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700167 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700168 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700169 // Cancel any pending alarm and set cache expiry after some time
170 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
171 if (uninstallDisabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800172 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700173 }
174
Sunny Goyal076839c2017-10-30 13:52:20 -0700175 if (info instanceof ItemInfoWithIcon) {
176 ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
177 if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) {
178 return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0;
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700179 }
180 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700181 return getUninstallTarget(info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700182 }
183
184 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700185 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -0700186 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700187 private ComponentName getUninstallTarget(ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700188 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800189 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700190 if (item != null &&
Sunny Goyalc5939392018-12-07 11:43:47 -0800191 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700192 intent = item.getIntent();
193 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700194 }
195 if (intent != null) {
Sunny Goyale7b00122019-10-02 16:13:34 -0700196 LauncherActivityInfo info = mLauncher.getSystemService(LauncherApps.class)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700197 .resolveActivity(intent, user);
198 if (info != null
199 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
200 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700201 }
202 }
203 return null;
204 }
205
206 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700207 public void onDrop(DragObject d, DragOptions options) {
208 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700209 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700210 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700211 }
212
213 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800214 public void completeDrop(final DragObject d) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000215 ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700216 if (d.dragSource instanceof DeferredOnComplete) {
217 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
218 if (target != null) {
219 deferred.mPackageName = target.getPackageName();
Winson Chung0b70cd42019-06-17 22:34:33 -0700220 mLauncher.addOnResumeCallback(deferred);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700221 } else {
222 deferred.sendFailure();
223 }
224 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700225 }
226
Winson Chung1054d4e2018-03-05 19:39:21 +0000227 private View getViewUnderDrag(ItemInfo info) {
228 if (info instanceof LauncherAppWidgetInfo && info.container == CONTAINER_DESKTOP &&
229 mLauncher.getWorkspace().getDragInfo() != null) {
230 return mLauncher.getWorkspace().getDragInfo().cell;
231 }
232 return null;
233 }
234
235 /**
236 * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id,
237 * otherwise return {@code INVALID_APPWIDGET_ID}
238 */
239 private int getReconfigurableWidgetId(View view) {
240 if (!(view instanceof AppWidgetHostView)) {
241 return INVALID_APPWIDGET_ID;
242 }
243 AppWidgetHostView hostView = (AppWidgetHostView) view;
244 AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo();
245 if (widgetInfo == null || widgetInfo.configure == null) {
246 return INVALID_APPWIDGET_ID;
247 }
248 if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo)
249 .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) {
250 return INVALID_APPWIDGET_ID;
251 }
252 return hostView.getAppWidgetId();
253 }
254
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700255 /**
256 * Performs the drop action and returns the target component for the dragObject or null if
257 * the action was not performed.
258 */
Winson Chung1054d4e2018-03-05 19:39:21 +0000259 protected ComponentName performDropAction(View view, ItemInfo info) {
260 if (mCurrentAccessibilityAction == RECONFIGURE) {
261 int widgetId = getReconfigurableWidgetId(view);
262 if (widgetId != INVALID_APPWIDGET_ID) {
263 mLauncher.getAppWidgetHost().startConfigActivity(mLauncher, widgetId, -1);
264 }
265 return null;
266 }
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800267 if (mCurrentAccessibilityAction == DISMISS_PREDICTION) {
268 AppLaunchTracker.INSTANCE.get(getContext()).onDismissApp(info.getTargetComponent(),
269 info.user, AppLaunchTracker.CONTAINER_PREDICTIONS);
270 return null;
271 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000272 // else: mCurrentAccessibilityAction == UNINSTALL
273
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700274 ComponentName cn = getUninstallTarget(info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700275 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800276 // System applications cannot be installed. For now, show a toast explaining that.
277 // We may give them the option of disabling apps this way.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700278 Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700279 return null;
280 }
281 try {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700282 Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700283 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
284 .putExtra(Intent.EXTRA_USER, info.user);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700285 mLauncher.startActivity(i);
Samuel Fufacc1e1072019-09-06 16:19:11 -0700286 FileLog.d(TAG, "start uninstall activity " + cn.getPackageName());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700287 return cn;
288 } catch (URISyntaxException e) {
289 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
290 return null;
291 }
292 }
293
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700294 @Override
295 public void onAccessibilityDrop(View view, ItemInfo item) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000296 performDropAction(view, item);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700297 }
298
299 /**
300 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
301 * {@link #onLauncherResume}
302 */
303 private class DeferredOnComplete implements DragSource, OnResumeCallback {
304
305 private final DragSource mOriginal;
306 private final Context mContext;
307
308 private String mPackageName;
309 private DragObject mDragObject;
310
311 public DeferredOnComplete(DragSource original, Context context) {
312 mOriginal = original;
313 mContext = context;
314 }
315
316 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700317 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700318 boolean success) {
319 mDragObject = d;
320 }
321
322 @Override
323 public void fillInLogContainerData(View v, ItemInfo info, Target target,
324 Target targetParent) {
325 mOriginal.fillInLogContainerData(v, info, target, targetParent);
326 }
327
328 @Override
329 public void onLauncherResume() {
330 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
Sunny Goyale7b00122019-10-02 16:13:34 -0700331 if (new PackageManagerHelper(mContext).getApplicationInfo(mPackageName,
332 mDragObject.dragInfo.user, PackageManager.MATCH_UNINSTALLED_PACKAGES) == null) {
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700333 mDragObject.dragSource = mOriginal;
Winson Chung1054d4e2018-03-05 19:39:21 +0000334 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700335 } else {
336 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700337 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700338 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700339
340 public void sendFailure() {
341 mDragObject.dragSource = mOriginal;
342 mDragObject.cancelled = true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000343 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800344 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700345 }
346}