blob: 983c289956a0219a94d65f8d1c082a4e20425ef4 [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;
Samuel Fufaa579ddc2020-02-27 16:59:19 -080044import java.util.ArrayList;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070045
Winson Chung1054d4e2018-03-05 19:39:21 +000046/**
47 * Drop target which provides a secondary option for an item.
48 * For app targets: shows as uninstall
49 * For configurable widgets: shows as setup
Samuel Fufa5cf3e862020-02-03 20:22:54 -080050 * For predicted app icons: don't suggest app
Winson Chung1054d4e2018-03-05 19:39:21 +000051 */
52public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070053
Winson Chung1054d4e2018-03-05 19:39:21 +000054 private static final String TAG = "SecondaryDropTarget";
Sunny Goyal0236d0b2017-10-24 14:54:30 -070055
56 private static final long CACHE_EXPIRE_TIMEOUT = 5000;
57 private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
58
59 private final Alarm mCacheExpireAlarm;
vadimt3d64ffd2020-02-27 11:28:47 -080060 private boolean mHadPendingAlarm;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070061
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070062 protected int mCurrentAccessibilityAction = -1;
Winson Chung1054d4e2018-03-05 19:39:21 +000063 public SecondaryDropTarget(Context context, AttributeSet attrs) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070064 this(context, attrs, 0);
65 }
66
Winson Chung1054d4e2018-03-05 19:39:21 +000067 public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070068 super(context, attrs, defStyle);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070069
70 mCacheExpireAlarm = new Alarm();
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);
vadimt908e9bb2020-02-28 18:02:30 -080078 mCacheExpireAlarm.setOnAlarmListener(this);
vadimt3d64ffd2020-02-27 11:28:47 -080079 mHadPendingAlarm = false;
80 }
81 }
82
83 @Override
84 protected void onDetachedFromWindow() {
85 super.onDetachedFromWindow();
86 if (mCacheExpireAlarm.alarmPending()) {
87 mCacheExpireAlarm.cancelAlarm();
vadimt908e9bb2020-02-28 18:02:30 -080088 mCacheExpireAlarm.setOnAlarmListener(null);
vadimt3d64ffd2020-02-27 11:28:47 -080089 mHadPendingAlarm = true;
90 }
91 }
92
93 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -070094 protected void onFinishInflate() {
95 super.onFinishInflate();
Winson Chung1054d4e2018-03-05 19:39:21 +000096 setupUi(UNINSTALL);
Sunny Goyalda1dfa32017-04-26 22:34:49 -070097 }
98
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070099 protected void setupUi(int action) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000100 if (action == mCurrentAccessibilityAction) {
101 return;
102 }
103 mCurrentAccessibilityAction = action;
104
105 if (action == UNINSTALL) {
106 mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
107 setDrawable(R.drawable.ic_uninstall_shadow);
108 updateText(R.string.uninstall_drop_target_label);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800109 } else if (action == DISMISS_PREDICTION) {
110 mHoverColor = Themes.getColorAccent(getContext());
Tony Wickhambd9754d2020-03-27 20:11:16 -0700111 setDrawable(R.drawable.ic_block_shadow);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800112 updateText(R.string.dismiss_prediction_label);
113 } else if (action == RECONFIGURE) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000114 mHoverColor = Themes.getColorAccent(getContext());
115 setDrawable(R.drawable.ic_setup_shadow);
116 updateText(R.string.gadget_setup_text);
117 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700118 }
119
120 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700121 public void onAlarm(Alarm alarm) {
122 mUninstallDisabledCache.clear();
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700123 }
124
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700125 @Override
126 public int getAccessibilityAction() {
Winson Chung1054d4e2018-03-05 19:39:21 +0000127 return mCurrentAccessibilityAction;
128 }
129
130 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700131 public Target getDropTargetForLogging() {
132 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800133 if (mCurrentAccessibilityAction == UNINSTALL) {
134 t.controlType = ControlType.UNINSTALL_TARGET;
135 } else if (mCurrentAccessibilityAction == DISMISS_PREDICTION) {
136 t.controlType = ControlType.DISMISS_PREDICTION;
137 } else {
138 t.controlType = ControlType.SETTINGS_BUTTON;
139 }
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700140 return t;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700141 }
142
143 @Override
144 protected boolean supportsDrop(ItemInfo info) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000145 return supportsAccessibilityDrop(info, getViewUnderDrag(info));
146 }
147
148 @Override
149 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
150 if (view instanceof AppWidgetHostView) {
151 if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) {
152 setupUi(RECONFIGURE);
153 return true;
154 }
155 return false;
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800156 } else if (FeatureFlags.ENABLE_PREDICTION_DISMISS.get() && info.isPredictedItem()) {
157 setupUi(DISMISS_PREDICTION);
158 return true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000159 }
160
161 setupUi(UNINSTALL);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700162 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
163 if (uninstallDisabled == null) {
164 UserManager userManager =
165 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
166 Bundle restrictions = userManager.getUserRestrictions(info.user);
167 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700168 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700169 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700170 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700171 // Cancel any pending alarm and set cache expiry after some time
172 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
vadimt908e9bb2020-02-28 18:02:30 -0800173 mCacheExpireAlarm.setOnAlarmListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700174 if (uninstallDisabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800175 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700176 }
177
Sunny Goyal076839c2017-10-30 13:52:20 -0700178 if (info instanceof ItemInfoWithIcon) {
179 ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
180 if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) {
181 return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0;
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700182 }
183 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700184 return getUninstallTarget(info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700185 }
186
187 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700188 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -0700189 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700190 private ComponentName getUninstallTarget(ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700191 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800192 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700193 if (item != null &&
Sunny Goyalc5939392018-12-07 11:43:47 -0800194 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700195 intent = item.getIntent();
196 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700197 }
198 if (intent != null) {
Sunny Goyale7b00122019-10-02 16:13:34 -0700199 LauncherActivityInfo info = mLauncher.getSystemService(LauncherApps.class)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700200 .resolveActivity(intent, user);
201 if (info != null
202 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
203 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700204 }
205 }
206 return null;
207 }
208
209 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700210 public void onDrop(DragObject d, DragOptions options) {
211 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700212 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700213 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700214 }
215
216 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800217 public void completeDrop(final DragObject d) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000218 ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700219 if (d.dragSource instanceof DeferredOnComplete) {
220 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
221 if (target != null) {
222 deferred.mPackageName = target.getPackageName();
Winson Chung0b70cd42019-06-17 22:34:33 -0700223 mLauncher.addOnResumeCallback(deferred);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700224 } else {
225 deferred.sendFailure();
226 }
227 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700228 }
229
Winson Chung1054d4e2018-03-05 19:39:21 +0000230 private View getViewUnderDrag(ItemInfo info) {
231 if (info instanceof LauncherAppWidgetInfo && info.container == CONTAINER_DESKTOP &&
232 mLauncher.getWorkspace().getDragInfo() != null) {
233 return mLauncher.getWorkspace().getDragInfo().cell;
234 }
235 return null;
236 }
237
238 /**
239 * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id,
240 * otherwise return {@code INVALID_APPWIDGET_ID}
241 */
242 private int getReconfigurableWidgetId(View view) {
243 if (!(view instanceof AppWidgetHostView)) {
244 return INVALID_APPWIDGET_ID;
245 }
246 AppWidgetHostView hostView = (AppWidgetHostView) view;
247 AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo();
248 if (widgetInfo == null || widgetInfo.configure == null) {
249 return INVALID_APPWIDGET_ID;
250 }
251 if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo)
252 .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) {
253 return INVALID_APPWIDGET_ID;
254 }
255 return hostView.getAppWidgetId();
256 }
257
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700258 /**
259 * Performs the drop action and returns the target component for the dragObject or null if
260 * the action was not performed.
261 */
Winson Chung1054d4e2018-03-05 19:39:21 +0000262 protected ComponentName performDropAction(View view, ItemInfo info) {
263 if (mCurrentAccessibilityAction == RECONFIGURE) {
264 int widgetId = getReconfigurableWidgetId(view);
265 if (widgetId != INVALID_APPWIDGET_ID) {
266 mLauncher.getAppWidgetHost().startConfigActivity(mLauncher, widgetId, -1);
267 }
268 return null;
269 }
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800270 if (mCurrentAccessibilityAction == DISMISS_PREDICTION) {
271 AppLaunchTracker.INSTANCE.get(getContext()).onDismissApp(info.getTargetComponent(),
272 info.user, AppLaunchTracker.CONTAINER_PREDICTIONS);
273 return null;
274 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000275 // else: mCurrentAccessibilityAction == UNINSTALL
276
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700277 ComponentName cn = getUninstallTarget(info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700278 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800279 // System applications cannot be installed. For now, show a toast explaining that.
280 // We may give them the option of disabling apps this way.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700281 Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700282 return null;
283 }
284 try {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700285 Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700286 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
287 .putExtra(Intent.EXTRA_USER, info.user);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700288 mLauncher.startActivity(i);
Samuel Fufacc1e1072019-09-06 16:19:11 -0700289 FileLog.d(TAG, "start uninstall activity " + cn.getPackageName());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700290 return cn;
291 } catch (URISyntaxException e) {
292 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
293 return null;
294 }
295 }
296
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700297 @Override
298 public void onAccessibilityDrop(View view, ItemInfo item) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000299 performDropAction(view, item);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700300 }
301
302 /**
303 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
304 * {@link #onLauncherResume}
305 */
306 private class DeferredOnComplete implements DragSource, OnResumeCallback {
307
308 private final DragSource mOriginal;
309 private final Context mContext;
310
311 private String mPackageName;
312 private DragObject mDragObject;
313
314 public DeferredOnComplete(DragSource original, Context context) {
315 mOriginal = original;
316 mContext = context;
317 }
318
319 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700320 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700321 boolean success) {
322 mDragObject = d;
323 }
324
325 @Override
Samuel Fufaa579ddc2020-02-27 16:59:19 -0800326 public void fillInLogContainerData(ItemInfo childInfo, Target child,
327 ArrayList<Target> parents) {
328 mOriginal.fillInLogContainerData(childInfo, child, parents);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700329 }
330
331 @Override
332 public void onLauncherResume() {
333 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
Sunny Goyale7b00122019-10-02 16:13:34 -0700334 if (new PackageManagerHelper(mContext).getApplicationInfo(mPackageName,
335 mDragObject.dragInfo.user, PackageManager.MATCH_UNINSTALLED_PACKAGES) == null) {
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700336 mDragObject.dragSource = mOriginal;
Winson Chung1054d4e2018-03-05 19:39:21 +0000337 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700338 } else {
339 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700340 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700341 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700342
343 public void sendFailure() {
344 mDragObject.dragSource = mOriginal;
345 mDragObject.cancelled = true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000346 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800347 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700348 }
349}