blob: 791cfff9b0f341a947135e8b82ccf6c5390197fe [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
Tony Wickham62dbe982021-05-12 16:19:58 -07006import static com.android.launcher3.Launcher.REQUEST_RECONFIGURE_APPWIDGET;
Winson Chung1054d4e2018-03-05 19:39:21 +00007import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP;
Samuel Fufa5cf3e862020-02-03 20:22:54 -08008import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.DISMISS_PREDICTION;
Alex Chau158caf42022-05-24 17:09:32 +01009import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.INVALID;
Winson Chung1054d4e2018-03-05 19:39:21 +000010import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.RECONFIGURE;
11import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.UNINSTALL;
Stefan Andonian510a8182022-03-30 17:46:09 +000012import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_DISMISS_PREDICTION_UNDO;
thiruramff484512020-05-11 11:19:58 -070013import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_DONT_SUGGEST;
14import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_UNINSTALL;
15import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_UNINSTALL_CANCELLED;
16import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_UNINSTALL_COMPLETED;
Sunny Goyale396abf2020-04-06 15:11:17 -070017import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_MASK;
18import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_NO;
Sunny Goyal076839c2017-10-30 13:52:20 -070019
Winson Chung1054d4e2018-03-05 19:39:21 +000020import android.appwidget.AppWidgetHostView;
Winson Chung1054d4e2018-03-05 19:39:21 +000021import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070022import android.content.ComponentName;
23import android.content.Context;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080024import android.content.Intent;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070025import android.content.pm.ApplicationInfo;
Sunny Goyal3e9be432017-01-05 15:22:41 -080026import android.content.pm.LauncherActivityInfo;
Sunny Goyale7b00122019-10-02 16:13:34 -070027import android.content.pm.LauncherApps;
Sunny Goyalf2db2532017-03-01 17:27:16 -080028import android.content.pm.PackageManager;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080029import android.net.Uri;
Sunny Goyalfa401a12015-04-10 13:45:42 -070030import android.os.Bundle;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080031import android.os.UserHandle;
Sunny Goyalfa401a12015-04-10 13:45:42 -070032import android.os.UserManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070033import android.util.ArrayMap;
Sunny Goyalfa401a12015-04-10 13:45:42 -070034import android.util.AttributeSet;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070035import android.util.Log;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070036import android.view.View;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080037import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070038
Samuel Fufa5cf3e862020-02-03 20:22:54 -080039import com.android.launcher3.config.FeatureFlags;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070040import com.android.launcher3.dragndrop.DragOptions;
Samuel Fufacc1e1072019-09-06 16:19:11 -070041import com.android.launcher3.logging.FileLog;
Sunny Goyal384b5782021-02-09 22:50:02 -080042import com.android.launcher3.logging.InstanceId;
43import com.android.launcher3.logging.InstanceIdSequence;
thiruramff484512020-05-11 11:19:58 -070044import com.android.launcher3.logging.StatsLogManager;
Sunny Goyal852537f2020-07-15 17:02:16 -070045import com.android.launcher3.logging.StatsLogManager.StatsLogger;
Sunny Goyale396abf2020-04-06 15:11:17 -070046import com.android.launcher3.model.data.ItemInfo;
47import com.android.launcher3.model.data.ItemInfoWithIcon;
48import com.android.launcher3.model.data.LauncherAppWidgetInfo;
Sunny Goyale7b00122019-10-02 16:13:34 -070049import com.android.launcher3.util.PackageManagerHelper;
Yogisha Dixit4ff5ee32021-05-04 14:58:34 +010050import com.android.launcher3.util.PendingRequestArgs;
Stefan Andonian510a8182022-03-30 17:46:09 +000051import com.android.launcher3.views.Snackbar;
Yogisha Dixit741fae92021-02-22 14:03:44 +000052import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070053
Jon Mirandac56e3ff2017-08-23 12:13:24 -070054import java.net.URISyntaxException;
55
Winson Chung1054d4e2018-03-05 19:39:21 +000056/**
57 * Drop target which provides a secondary option for an item.
58 * For app targets: shows as uninstall
59 * For configurable widgets: shows as setup
Samuel Fufa5cf3e862020-02-03 20:22:54 -080060 * For predicted app icons: don't suggest app
Winson Chung1054d4e2018-03-05 19:39:21 +000061 */
62public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070063
Winson Chung1054d4e2018-03-05 19:39:21 +000064 private static final String TAG = "SecondaryDropTarget";
Sunny Goyal0236d0b2017-10-24 14:54:30 -070065
66 private static final long CACHE_EXPIRE_TIMEOUT = 5000;
67 private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
thiruramff484512020-05-11 11:19:58 -070068 private final StatsLogManager mStatsLogManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070069 private final Alarm mCacheExpireAlarm;
vadimt3d64ffd2020-02-27 11:28:47 -080070 private boolean mHadPendingAlarm;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070071
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070072 protected int mCurrentAccessibilityAction = -1;
Alex Chau158caf42022-05-24 17:09:32 +010073
Winson Chung1054d4e2018-03-05 19:39:21 +000074 public SecondaryDropTarget(Context context, AttributeSet attrs) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070075 this(context, attrs, 0);
76 }
77
Winson Chung1054d4e2018-03-05 19:39:21 +000078 public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070079 super(context, attrs, defStyle);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070080 mCacheExpireAlarm = new Alarm();
thiruramff484512020-05-11 11:19:58 -070081 mStatsLogManager = StatsLogManager.newInstance(context);
Sunny Goyalfa401a12015-04-10 13:45:42 -070082 }
83
84 @Override
vadimt3d64ffd2020-02-27 11:28:47 -080085 protected void onAttachedToWindow() {
86 super.onAttachedToWindow();
87 if (mHadPendingAlarm) {
88 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
vadimt908e9bb2020-02-28 18:02:30 -080089 mCacheExpireAlarm.setOnAlarmListener(this);
vadimt3d64ffd2020-02-27 11:28:47 -080090 mHadPendingAlarm = false;
91 }
92 }
93
94 @Override
95 protected void onDetachedFromWindow() {
96 super.onDetachedFromWindow();
97 if (mCacheExpireAlarm.alarmPending()) {
98 mCacheExpireAlarm.cancelAlarm();
vadimt908e9bb2020-02-28 18:02:30 -080099 mCacheExpireAlarm.setOnAlarmListener(null);
vadimt3d64ffd2020-02-27 11:28:47 -0800100 mHadPendingAlarm = true;
101 }
102 }
103
104 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700105 protected void onFinishInflate() {
106 super.onFinishInflate();
Winson Chung1054d4e2018-03-05 19:39:21 +0000107 setupUi(UNINSTALL);
Sunny Goyalda1dfa32017-04-26 22:34:49 -0700108 }
109
Mehdi Alizadeh8517b692018-04-23 15:55:26 -0700110 protected void setupUi(int action) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000111 if (action == mCurrentAccessibilityAction) {
112 return;
113 }
114 mCurrentAccessibilityAction = action;
115
116 if (action == UNINSTALL) {
Yogisha Dixita3a6f512021-04-28 13:45:37 +0100117 setDrawable(R.drawable.ic_uninstall_no_shadow);
Winson Chung1054d4e2018-03-05 19:39:21 +0000118 updateText(R.string.uninstall_drop_target_label);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800119 } else if (action == DISMISS_PREDICTION) {
Yogisha Dixita3a6f512021-04-28 13:45:37 +0100120 setDrawable(R.drawable.ic_block_no_shadow);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800121 updateText(R.string.dismiss_prediction_label);
122 } else if (action == RECONFIGURE) {
Yogisha Dixita3a6f512021-04-28 13:45:37 +0100123 setDrawable(R.drawable.ic_setting);
Winson Chung1054d4e2018-03-05 19:39:21 +0000124 updateText(R.string.gadget_setup_text);
125 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700126 }
127
128 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700129 public void onAlarm(Alarm alarm) {
130 mUninstallDisabledCache.clear();
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700131 }
132
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700133 @Override
134 public int getAccessibilityAction() {
Winson Chung1054d4e2018-03-05 19:39:21 +0000135 return mCurrentAccessibilityAction;
136 }
137
138 @Override
Alex Chau158caf42022-05-24 17:09:32 +0100139 protected void setupItemInfo(ItemInfo info) {
140 int buttonType = getButtonType(info, getViewUnderDrag(info));
141 if (buttonType != INVALID) {
142 setupUi(buttonType);
143 }
144 }
145
146 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700147 protected boolean supportsDrop(ItemInfo info) {
Alex Chau158caf42022-05-24 17:09:32 +0100148 return getButtonType(info, getViewUnderDrag(info)) != INVALID;
Winson Chung1054d4e2018-03-05 19:39:21 +0000149 }
150
151 @Override
152 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Alex Chau158caf42022-05-24 17:09:32 +0100153 return getButtonType(info, view) != INVALID;
154 }
155
156 private int getButtonType(ItemInfo info, View view) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000157 if (view instanceof AppWidgetHostView) {
158 if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) {
Alex Chau158caf42022-05-24 17:09:32 +0100159 return RECONFIGURE;
Winson Chung1054d4e2018-03-05 19:39:21 +0000160 }
Alex Chau158caf42022-05-24 17:09:32 +0100161 return INVALID;
Sunny Goyalc5c7da92022-08-02 13:36:54 -0700162 } else if (info.isPredictedItem()) {
Alex Chau158caf42022-05-24 17:09:32 +0100163 return DISMISS_PREDICTION;
Winson Chung1054d4e2018-03-05 19:39:21 +0000164 }
165
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700166 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
167 if (uninstallDisabled == null) {
168 UserManager userManager =
169 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
170 Bundle restrictions = userManager.getUserRestrictions(info.user);
171 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700172 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700173 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700174 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700175 // Cancel any pending alarm and set cache expiry after some time
176 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
vadimt908e9bb2020-02-28 18:02:30 -0800177 mCacheExpireAlarm.setOnAlarmListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700178 if (uninstallDisabled) {
Alex Chau158caf42022-05-24 17:09:32 +0100179 return INVALID;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700180 }
181
Sunny Goyal076839c2017-10-30 13:52:20 -0700182 if (info instanceof ItemInfoWithIcon) {
183 ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
Alex Chau158caf42022-05-24 17:09:32 +0100184 if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0
185 && (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) == 0) {
186 return INVALID;
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700187 }
188 }
Alex Chau158caf42022-05-24 17:09:32 +0100189 if (getUninstallTarget(info) == null) {
190 return INVALID;
191 }
192 return UNINSTALL;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700193 }
194
195 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700196 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -0700197 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700198 private ComponentName getUninstallTarget(ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700199 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800200 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700201 if (item != null &&
Sunny Goyalc5939392018-12-07 11:43:47 -0800202 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700203 intent = item.getIntent();
204 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700205 }
206 if (intent != null) {
Sunny Goyale7b00122019-10-02 16:13:34 -0700207 LauncherActivityInfo info = mLauncher.getSystemService(LauncherApps.class)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700208 .resolveActivity(intent, user);
209 if (info != null
210 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
211 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700212 }
213 }
214 return null;
215 }
216
217 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700218 public void onDrop(DragObject d, DragOptions options) {
219 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700220 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal852537f2020-07-15 17:02:16 -0700221
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700222 super.onDrop(d, options);
Sunny Goyal384b5782021-02-09 22:50:02 -0800223 doLog(d.logInstanceId, d.originalDragInfo);
224 }
225
226 private void doLog(InstanceId logInstanceId, ItemInfo itemInfo) {
227 StatsLogger logger = mStatsLogManager.logger().withInstanceId(logInstanceId);
228 if (itemInfo != null) {
229 logger.withItemInfo(itemInfo);
Sunny Goyal852537f2020-07-15 17:02:16 -0700230 }
thiruramff484512020-05-11 11:19:58 -0700231 if (mCurrentAccessibilityAction == UNINSTALL) {
Sunny Goyal852537f2020-07-15 17:02:16 -0700232 logger.log(LAUNCHER_ITEM_DROPPED_ON_UNINSTALL);
thiruramff484512020-05-11 11:19:58 -0700233 } else if (mCurrentAccessibilityAction == DISMISS_PREDICTION) {
Sunny Goyal852537f2020-07-15 17:02:16 -0700234 logger.log(LAUNCHER_ITEM_DROPPED_ON_DONT_SUGGEST);
thiruramff484512020-05-11 11:19:58 -0700235 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700236 }
237
238 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800239 public void completeDrop(final DragObject d) {
Stefan Andonian510a8182022-03-30 17:46:09 +0000240 ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo,
241 d.logInstanceId);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700242 if (d.dragSource instanceof DeferredOnComplete) {
243 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
244 if (target != null) {
245 deferred.mPackageName = target.getPackageName();
Sunny Goyalb65d7662021-03-07 15:09:11 -0800246 mLauncher.addOnResumeCallback(deferred::onLauncherResume);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700247 } else {
248 deferred.sendFailure();
249 }
250 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700251 }
252
Winson Chung1054d4e2018-03-05 19:39:21 +0000253 private View getViewUnderDrag(ItemInfo info) {
254 if (info instanceof LauncherAppWidgetInfo && info.container == CONTAINER_DESKTOP &&
255 mLauncher.getWorkspace().getDragInfo() != null) {
256 return mLauncher.getWorkspace().getDragInfo().cell;
257 }
258 return null;
259 }
260
261 /**
262 * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id,
263 * otherwise return {@code INVALID_APPWIDGET_ID}
264 */
265 private int getReconfigurableWidgetId(View view) {
266 if (!(view instanceof AppWidgetHostView)) {
267 return INVALID_APPWIDGET_ID;
268 }
269 AppWidgetHostView hostView = (AppWidgetHostView) view;
270 AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo();
271 if (widgetInfo == null || widgetInfo.configure == null) {
272 return INVALID_APPWIDGET_ID;
273 }
274 if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo)
275 .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) {
276 return INVALID_APPWIDGET_ID;
277 }
278 return hostView.getAppWidgetId();
279 }
280
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700281 /**
282 * Performs the drop action and returns the target component for the dragObject or null if
283 * the action was not performed.
284 */
Stefan Andonian510a8182022-03-30 17:46:09 +0000285 protected ComponentName performDropAction(View view, ItemInfo info, InstanceId instanceId) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000286 if (mCurrentAccessibilityAction == RECONFIGURE) {
287 int widgetId = getReconfigurableWidgetId(view);
288 if (widgetId != INVALID_APPWIDGET_ID) {
Yogisha Dixit4ff5ee32021-05-04 14:58:34 +0100289 mLauncher.setWaitingForResult(
290 PendingRequestArgs.forWidgetInfo(widgetId, null, info));
Sihua Ma0593a0d2022-10-03 16:01:48 -0700291 mLauncher.getAppWidgetHolder().startConfigActivity(mLauncher, widgetId,
Tony Wickham62dbe982021-05-12 16:19:58 -0700292 REQUEST_RECONFIGURE_APPWIDGET);
Winson Chung1054d4e2018-03-05 19:39:21 +0000293 }
294 return null;
295 }
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800296 if (mCurrentAccessibilityAction == DISMISS_PREDICTION) {
Stefan Andonian510a8182022-03-30 17:46:09 +0000297 if (FeatureFlags.ENABLE_DISMISS_PREDICTION_UNDO.get()) {
298 mLauncher.getDragLayer()
299 .announceForAccessibility(getContext().getString(R.string.item_removed));
300 Snackbar.show(mLauncher, R.string.item_removed, R.string.undo, () -> { }, () -> {
301 mStatsLogManager.logger()
302 .withInstanceId(instanceId)
303 .withItemInfo(info)
304 .log(LAUNCHER_DISMISS_PREDICTION_UNDO);
305 });
306 }
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800307 return null;
308 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000309 // else: mCurrentAccessibilityAction == UNINSTALL
310
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700311 ComponentName cn = getUninstallTarget(info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700312 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800313 // System applications cannot be installed. For now, show a toast explaining that.
314 // We may give them the option of disabling apps this way.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700315 Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700316 return null;
317 }
318 try {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700319 Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700320 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
321 .putExtra(Intent.EXTRA_USER, info.user);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700322 mLauncher.startActivity(i);
Samuel Fufacc1e1072019-09-06 16:19:11 -0700323 FileLog.d(TAG, "start uninstall activity " + cn.getPackageName());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700324 return cn;
325 } catch (URISyntaxException e) {
326 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
327 return null;
328 }
329 }
330
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700331 @Override
332 public void onAccessibilityDrop(View view, ItemInfo item) {
Stefan Andonian510a8182022-03-30 17:46:09 +0000333 InstanceId instanceId = new InstanceIdSequence().newInstanceId();
334 doLog(instanceId, item);
335 performDropAction(view, item, instanceId);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700336 }
337
338 /**
339 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
340 * {@link #onLauncherResume}
341 */
Sunny Goyalb65d7662021-03-07 15:09:11 -0800342 private class DeferredOnComplete implements DragSource {
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700343
344 private final DragSource mOriginal;
345 private final Context mContext;
346
347 private String mPackageName;
348 private DragObject mDragObject;
349
350 public DeferredOnComplete(DragSource original, Context context) {
351 mOriginal = original;
352 mContext = context;
353 }
354
355 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700356 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700357 boolean success) {
358 mDragObject = d;
359 }
360
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700361 public void onLauncherResume() {
362 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
Sunny Goyale7b00122019-10-02 16:13:34 -0700363 if (new PackageManagerHelper(mContext).getApplicationInfo(mPackageName,
364 mDragObject.dragInfo.user, PackageManager.MATCH_UNINSTALLED_PACKAGES) == null) {
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700365 mDragObject.dragSource = mOriginal;
Winson Chung1054d4e2018-03-05 19:39:21 +0000366 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true);
thiruramc6a38ba2020-06-16 18:58:13 -0700367 mStatsLogManager.logger().withInstanceId(mDragObject.logInstanceId)
368 .log(LAUNCHER_ITEM_UNINSTALL_COMPLETED);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700369 } else {
370 sendFailure();
thiruramc6a38ba2020-06-16 18:58:13 -0700371 mStatsLogManager.logger().withInstanceId(mDragObject.logInstanceId)
372 .log(LAUNCHER_ITEM_UNINSTALL_CANCELLED);
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700373 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700374 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700375
376 public void sendFailure() {
377 mDragObject.dragSource = mOriginal;
378 mDragObject.cancelled = true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000379 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800380 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700381 }
382}