blob: 9dbb5fc675fc4aa4bba6ac67ed3b84d6d1f3ae07 [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
Winson Chung1054d4e2018-03-05 19:39:21 +00006import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP;
Samuel Fufa5cf3e862020-02-03 20:22:54 -08007import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.DISMISS_PREDICTION;
Winson Chung1054d4e2018-03-05 19:39:21 +00008import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.RECONFIGURE;
9import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.UNINSTALL;
Sunny Goyale396abf2020-04-06 15:11:17 -070010import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_MASK;
11import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_NO;
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;
Sunny Goyale396abf2020-04-06 15:11:17 -070038import com.android.launcher3.model.data.ItemInfo;
39import com.android.launcher3.model.data.ItemInfoWithIcon;
40import com.android.launcher3.model.data.LauncherAppWidgetInfo;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070041import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070042import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyale7b00122019-10-02 16:13:34 -070043import com.android.launcher3.util.PackageManagerHelper;
Winson Chung1054d4e2018-03-05 19:39:21 +000044import com.android.launcher3.util.Themes;
Sunny Goyalfa401a12015-04-10 13:45:42 -070045
Jon Mirandac56e3ff2017-08-23 12:13:24 -070046import java.net.URISyntaxException;
Samuel Fufaa579ddc2020-02-27 16:59:19 -080047import java.util.ArrayList;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070048
Winson Chung1054d4e2018-03-05 19:39:21 +000049/**
50 * Drop target which provides a secondary option for an item.
51 * For app targets: shows as uninstall
52 * For configurable widgets: shows as setup
Samuel Fufa5cf3e862020-02-03 20:22:54 -080053 * For predicted app icons: don't suggest app
Winson Chung1054d4e2018-03-05 19:39:21 +000054 */
55public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070056
Winson Chung1054d4e2018-03-05 19:39:21 +000057 private static final String TAG = "SecondaryDropTarget";
Sunny Goyal0236d0b2017-10-24 14:54:30 -070058
59 private static final long CACHE_EXPIRE_TIMEOUT = 5000;
60 private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
61
62 private final Alarm mCacheExpireAlarm;
vadimt3d64ffd2020-02-27 11:28:47 -080063 private boolean mHadPendingAlarm;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070064
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070065 protected int mCurrentAccessibilityAction = -1;
Winson Chung1054d4e2018-03-05 19:39:21 +000066 public SecondaryDropTarget(Context context, AttributeSet attrs) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070067 this(context, attrs, 0);
68 }
69
Winson Chung1054d4e2018-03-05 19:39:21 +000070 public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070071 super(context, attrs, defStyle);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070072
73 mCacheExpireAlarm = new Alarm();
Sunny Goyalfa401a12015-04-10 13:45:42 -070074 }
75
76 @Override
vadimt3d64ffd2020-02-27 11:28:47 -080077 protected void onAttachedToWindow() {
78 super.onAttachedToWindow();
79 if (mHadPendingAlarm) {
80 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
vadimt908e9bb2020-02-28 18:02:30 -080081 mCacheExpireAlarm.setOnAlarmListener(this);
vadimt3d64ffd2020-02-27 11:28:47 -080082 mHadPendingAlarm = false;
83 }
84 }
85
86 @Override
87 protected void onDetachedFromWindow() {
88 super.onDetachedFromWindow();
89 if (mCacheExpireAlarm.alarmPending()) {
90 mCacheExpireAlarm.cancelAlarm();
vadimt908e9bb2020-02-28 18:02:30 -080091 mCacheExpireAlarm.setOnAlarmListener(null);
vadimt3d64ffd2020-02-27 11:28:47 -080092 mHadPendingAlarm = true;
93 }
94 }
95
96 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -070097 protected void onFinishInflate() {
98 super.onFinishInflate();
Winson Chung1054d4e2018-03-05 19:39:21 +000099 setupUi(UNINSTALL);
Sunny Goyalda1dfa32017-04-26 22:34:49 -0700100 }
101
Mehdi Alizadeh8517b692018-04-23 15:55:26 -0700102 protected void setupUi(int action) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000103 if (action == mCurrentAccessibilityAction) {
104 return;
105 }
106 mCurrentAccessibilityAction = action;
107
108 if (action == UNINSTALL) {
109 mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
110 setDrawable(R.drawable.ic_uninstall_shadow);
111 updateText(R.string.uninstall_drop_target_label);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800112 } else if (action == DISMISS_PREDICTION) {
113 mHoverColor = Themes.getColorAccent(getContext());
Tony Wickhambd9754d2020-03-27 20:11:16 -0700114 setDrawable(R.drawable.ic_block_shadow);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800115 updateText(R.string.dismiss_prediction_label);
116 } else if (action == RECONFIGURE) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000117 mHoverColor = Themes.getColorAccent(getContext());
118 setDrawable(R.drawable.ic_setup_shadow);
119 updateText(R.string.gadget_setup_text);
120 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700121 }
122
123 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700124 public void onAlarm(Alarm alarm) {
125 mUninstallDisabledCache.clear();
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700126 }
127
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700128 @Override
129 public int getAccessibilityAction() {
Winson Chung1054d4e2018-03-05 19:39:21 +0000130 return mCurrentAccessibilityAction;
131 }
132
133 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700134 public Target getDropTargetForLogging() {
135 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800136 if (mCurrentAccessibilityAction == UNINSTALL) {
137 t.controlType = ControlType.UNINSTALL_TARGET;
138 } else if (mCurrentAccessibilityAction == DISMISS_PREDICTION) {
139 t.controlType = ControlType.DISMISS_PREDICTION;
140 } else {
141 t.controlType = ControlType.SETTINGS_BUTTON;
142 }
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700143 return t;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700144 }
145
146 @Override
147 protected boolean supportsDrop(ItemInfo info) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000148 return supportsAccessibilityDrop(info, getViewUnderDrag(info));
149 }
150
151 @Override
152 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
153 if (view instanceof AppWidgetHostView) {
154 if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) {
155 setupUi(RECONFIGURE);
156 return true;
157 }
158 return false;
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800159 } else if (FeatureFlags.ENABLE_PREDICTION_DISMISS.get() && info.isPredictedItem()) {
160 setupUi(DISMISS_PREDICTION);
161 return true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000162 }
163
164 setupUi(UNINSTALL);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700165 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
166 if (uninstallDisabled == null) {
167 UserManager userManager =
168 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
169 Bundle restrictions = userManager.getUserRestrictions(info.user);
170 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700171 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700172 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700173 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700174 // Cancel any pending alarm and set cache expiry after some time
175 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
vadimt908e9bb2020-02-28 18:02:30 -0800176 mCacheExpireAlarm.setOnAlarmListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700177 if (uninstallDisabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800178 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700179 }
180
Sunny Goyal076839c2017-10-30 13:52:20 -0700181 if (info instanceof ItemInfoWithIcon) {
182 ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
183 if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) {
184 return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0;
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700185 }
186 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700187 return getUninstallTarget(info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700188 }
189
190 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700191 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -0700192 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700193 private ComponentName getUninstallTarget(ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700194 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800195 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700196 if (item != null &&
Sunny Goyalc5939392018-12-07 11:43:47 -0800197 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700198 intent = item.getIntent();
199 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700200 }
201 if (intent != null) {
Sunny Goyale7b00122019-10-02 16:13:34 -0700202 LauncherActivityInfo info = mLauncher.getSystemService(LauncherApps.class)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700203 .resolveActivity(intent, user);
204 if (info != null
205 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
206 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700207 }
208 }
209 return null;
210 }
211
212 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700213 public void onDrop(DragObject d, DragOptions options) {
214 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700215 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700216 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700217 }
218
219 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800220 public void completeDrop(final DragObject d) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000221 ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700222 if (d.dragSource instanceof DeferredOnComplete) {
223 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
224 if (target != null) {
225 deferred.mPackageName = target.getPackageName();
Winson Chung0b70cd42019-06-17 22:34:33 -0700226 mLauncher.addOnResumeCallback(deferred);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700227 } else {
228 deferred.sendFailure();
229 }
230 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700231 }
232
Winson Chung1054d4e2018-03-05 19:39:21 +0000233 private View getViewUnderDrag(ItemInfo info) {
234 if (info instanceof LauncherAppWidgetInfo && info.container == CONTAINER_DESKTOP &&
235 mLauncher.getWorkspace().getDragInfo() != null) {
236 return mLauncher.getWorkspace().getDragInfo().cell;
237 }
238 return null;
239 }
240
241 /**
242 * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id,
243 * otherwise return {@code INVALID_APPWIDGET_ID}
244 */
245 private int getReconfigurableWidgetId(View view) {
246 if (!(view instanceof AppWidgetHostView)) {
247 return INVALID_APPWIDGET_ID;
248 }
249 AppWidgetHostView hostView = (AppWidgetHostView) view;
250 AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo();
251 if (widgetInfo == null || widgetInfo.configure == null) {
252 return INVALID_APPWIDGET_ID;
253 }
254 if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo)
255 .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) {
256 return INVALID_APPWIDGET_ID;
257 }
258 return hostView.getAppWidgetId();
259 }
260
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700261 /**
262 * Performs the drop action and returns the target component for the dragObject or null if
263 * the action was not performed.
264 */
Winson Chung1054d4e2018-03-05 19:39:21 +0000265 protected ComponentName performDropAction(View view, ItemInfo info) {
266 if (mCurrentAccessibilityAction == RECONFIGURE) {
267 int widgetId = getReconfigurableWidgetId(view);
268 if (widgetId != INVALID_APPWIDGET_ID) {
269 mLauncher.getAppWidgetHost().startConfigActivity(mLauncher, widgetId, -1);
270 }
271 return null;
272 }
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800273 if (mCurrentAccessibilityAction == DISMISS_PREDICTION) {
274 AppLaunchTracker.INSTANCE.get(getContext()).onDismissApp(info.getTargetComponent(),
275 info.user, AppLaunchTracker.CONTAINER_PREDICTIONS);
276 return null;
277 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000278 // else: mCurrentAccessibilityAction == UNINSTALL
279
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700280 ComponentName cn = getUninstallTarget(info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700281 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800282 // System applications cannot be installed. For now, show a toast explaining that.
283 // We may give them the option of disabling apps this way.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700284 Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700285 return null;
286 }
287 try {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700288 Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700289 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
290 .putExtra(Intent.EXTRA_USER, info.user);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700291 mLauncher.startActivity(i);
Samuel Fufacc1e1072019-09-06 16:19:11 -0700292 FileLog.d(TAG, "start uninstall activity " + cn.getPackageName());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700293 return cn;
294 } catch (URISyntaxException e) {
295 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
296 return null;
297 }
298 }
299
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700300 @Override
301 public void onAccessibilityDrop(View view, ItemInfo item) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000302 performDropAction(view, item);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700303 }
304
305 /**
306 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
307 * {@link #onLauncherResume}
308 */
309 private class DeferredOnComplete implements DragSource, OnResumeCallback {
310
311 private final DragSource mOriginal;
312 private final Context mContext;
313
314 private String mPackageName;
315 private DragObject mDragObject;
316
317 public DeferredOnComplete(DragSource original, Context context) {
318 mOriginal = original;
319 mContext = context;
320 }
321
322 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700323 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700324 boolean success) {
325 mDragObject = d;
326 }
327
328 @Override
Samuel Fufaa579ddc2020-02-27 16:59:19 -0800329 public void fillInLogContainerData(ItemInfo childInfo, Target child,
330 ArrayList<Target> parents) {
331 mOriginal.fillInLogContainerData(childInfo, child, parents);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700332 }
333
334 @Override
335 public void onLauncherResume() {
336 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
Sunny Goyale7b00122019-10-02 16:13:34 -0700337 if (new PackageManagerHelper(mContext).getApplicationInfo(mPackageName,
338 mDragObject.dragInfo.user, PackageManager.MATCH_UNINSTALLED_PACKAGES) == null) {
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700339 mDragObject.dragSource = mOriginal;
Winson Chung1054d4e2018-03-05 19:39:21 +0000340 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700341 } else {
342 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700343 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700344 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700345
346 public void sendFailure() {
347 mDragObject.dragSource = mOriginal;
348 mDragObject.cancelled = true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000349 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800350 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700351 }
352}