blob: 1362586c9e9e6b41f10cf9067d72dcf547ec4d5c [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
Samuel Fufa5cf3e862020-02-03 20:22:54 -08006import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.DISMISS_PREDICTION;
Alex Chau158caf42022-05-24 17:09:32 +01007import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.INVALID;
Winson Chung1054d4e2018-03-05 19:39:21 +00008import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.RECONFIGURE;
9import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.UNINSTALL;
Stefan Andonian510a8182022-03-30 17:46:09 +000010import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_DISMISS_PREDICTION_UNDO;
thiruramff484512020-05-11 11:19:58 -070011import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_DONT_SUGGEST;
12import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_UNINSTALL;
13import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_UNINSTALL_CANCELLED;
14import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_UNINSTALL_COMPLETED;
Sunny Goyale396abf2020-04-06 15:11:17 -070015import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_MASK;
16import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_NO;
Sunny Goyal076839c2017-10-30 13:52:20 -070017
Winson Chung1054d4e2018-03-05 19:39:21 +000018import android.appwidget.AppWidgetHostView;
Winson Chung1054d4e2018-03-05 19:39:21 +000019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070020import android.content.ComponentName;
21import android.content.Context;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080022import android.content.Intent;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070023import android.content.pm.ApplicationInfo;
Sunny Goyal3e9be432017-01-05 15:22:41 -080024import android.content.pm.LauncherActivityInfo;
Sunny Goyale7b00122019-10-02 16:13:34 -070025import android.content.pm.LauncherApps;
Sunny Goyalf2db2532017-03-01 17:27:16 -080026import android.content.pm.PackageManager;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080027import android.net.Uri;
Sunny Goyalfa401a12015-04-10 13:45:42 -070028import android.os.Bundle;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080029import android.os.UserHandle;
Sunny Goyalfa401a12015-04-10 13:45:42 -070030import android.os.UserManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070031import android.util.ArrayMap;
Sunny Goyalfa401a12015-04-10 13:45:42 -070032import android.util.AttributeSet;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070033import android.util.Log;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070034import android.view.View;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080035import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070036
Anushree Ganjam45ef5442024-01-26 14:08:07 -080037import androidx.annotation.Nullable;
38
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;
Anushree Ganjam45ef5442024-01-26 14:08:07 -080048import com.android.launcher3.pm.UserCache;
Sunny Goyale7b00122019-10-02 16:13:34 -070049import com.android.launcher3.util.PackageManagerHelper;
Yogisha Dixit741fae92021-02-22 14:03:44 +000050import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070051
Jon Mirandac56e3ff2017-08-23 12:13:24 -070052import java.net.URISyntaxException;
53
Winson Chung1054d4e2018-03-05 19:39:21 +000054/**
55 * Drop target which provides a secondary option for an item.
56 * For app targets: shows as uninstall
57 * For configurable widgets: shows as setup
Samuel Fufa5cf3e862020-02-03 20:22:54 -080058 * For predicted app icons: don't suggest app
Winson Chung1054d4e2018-03-05 19:39:21 +000059 */
60public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070061
Winson Chung1054d4e2018-03-05 19:39:21 +000062 private static final String TAG = "SecondaryDropTarget";
Sunny Goyal0236d0b2017-10-24 14:54:30 -070063
64 private static final long CACHE_EXPIRE_TIMEOUT = 5000;
65 private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
thiruramff484512020-05-11 11:19:58 -070066 private final StatsLogManager mStatsLogManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070067 private final Alarm mCacheExpireAlarm;
vadimt3d64ffd2020-02-27 11:28:47 -080068 private boolean mHadPendingAlarm;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070069
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070070 protected int mCurrentAccessibilityAction = -1;
Alex Chau158caf42022-05-24 17:09:32 +010071
Winson Chung1054d4e2018-03-05 19:39:21 +000072 public SecondaryDropTarget(Context context, AttributeSet attrs) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070073 this(context, attrs, 0);
74 }
75
Winson Chung1054d4e2018-03-05 19:39:21 +000076 public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070077 super(context, attrs, defStyle);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070078 mCacheExpireAlarm = new Alarm();
thiruramff484512020-05-11 11:19:58 -070079 mStatsLogManager = StatsLogManager.newInstance(context);
Sunny Goyalfa401a12015-04-10 13:45:42 -070080 }
81
82 @Override
vadimt3d64ffd2020-02-27 11:28:47 -080083 protected void onAttachedToWindow() {
84 super.onAttachedToWindow();
85 if (mHadPendingAlarm) {
86 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
vadimt908e9bb2020-02-28 18:02:30 -080087 mCacheExpireAlarm.setOnAlarmListener(this);
vadimt3d64ffd2020-02-27 11:28:47 -080088 mHadPendingAlarm = false;
89 }
90 }
91
92 @Override
93 protected void onDetachedFromWindow() {
94 super.onDetachedFromWindow();
95 if (mCacheExpireAlarm.alarmPending()) {
96 mCacheExpireAlarm.cancelAlarm();
vadimt908e9bb2020-02-28 18:02:30 -080097 mCacheExpireAlarm.setOnAlarmListener(null);
vadimt3d64ffd2020-02-27 11:28:47 -080098 mHadPendingAlarm = true;
99 }
100 }
101
102 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700103 protected void onFinishInflate() {
104 super.onFinishInflate();
Winson Chung1054d4e2018-03-05 19:39:21 +0000105 setupUi(UNINSTALL);
Sunny Goyalda1dfa32017-04-26 22:34:49 -0700106 }
107
Mehdi Alizadeh8517b692018-04-23 15:55:26 -0700108 protected void setupUi(int action) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000109 if (action == mCurrentAccessibilityAction) {
110 return;
111 }
112 mCurrentAccessibilityAction = action;
113
114 if (action == UNINSTALL) {
Yogisha Dixita3a6f512021-04-28 13:45:37 +0100115 setDrawable(R.drawable.ic_uninstall_no_shadow);
Winson Chung1054d4e2018-03-05 19:39:21 +0000116 updateText(R.string.uninstall_drop_target_label);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800117 } else if (action == DISMISS_PREDICTION) {
Yogisha Dixita3a6f512021-04-28 13:45:37 +0100118 setDrawable(R.drawable.ic_block_no_shadow);
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800119 updateText(R.string.dismiss_prediction_label);
120 } else if (action == RECONFIGURE) {
Yogisha Dixita3a6f512021-04-28 13:45:37 +0100121 setDrawable(R.drawable.ic_setting);
Winson Chung1054d4e2018-03-05 19:39:21 +0000122 updateText(R.string.gadget_setup_text);
123 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700124 }
125
126 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700127 public void onAlarm(Alarm alarm) {
128 mUninstallDisabledCache.clear();
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700129 }
130
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700131 @Override
132 public int getAccessibilityAction() {
Winson Chung1054d4e2018-03-05 19:39:21 +0000133 return mCurrentAccessibilityAction;
134 }
135
136 @Override
Alex Chau158caf42022-05-24 17:09:32 +0100137 protected void setupItemInfo(ItemInfo info) {
138 int buttonType = getButtonType(info, getViewUnderDrag(info));
139 if (buttonType != INVALID) {
140 setupUi(buttonType);
141 }
142 }
143
144 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700145 protected boolean supportsDrop(ItemInfo info) {
Alex Chau158caf42022-05-24 17:09:32 +0100146 return getButtonType(info, getViewUnderDrag(info)) != INVALID;
Winson Chung1054d4e2018-03-05 19:39:21 +0000147 }
148
149 @Override
150 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Alex Chau158caf42022-05-24 17:09:32 +0100151 return getButtonType(info, view) != INVALID;
152 }
153
154 private int getButtonType(ItemInfo info, View view) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000155 if (view instanceof AppWidgetHostView) {
156 if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) {
Alex Chau158caf42022-05-24 17:09:32 +0100157 return RECONFIGURE;
Winson Chung1054d4e2018-03-05 19:39:21 +0000158 }
Alex Chau158caf42022-05-24 17:09:32 +0100159 return INVALID;
Sunny Goyalc5c7da92022-08-02 13:36:54 -0700160 } else if (info.isPredictedItem()) {
Anushree Ganjam5e9b71e2024-01-11 14:15:54 -0800161 if (Flags.enableShortcutDontSuggestApp()) {
162 return INVALID;
163 }
Alex Chau158caf42022-05-24 17:09:32 +0100164 return DISMISS_PREDICTION;
Winson Chung1054d4e2018-03-05 19:39:21 +0000165 }
166
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700167 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
168 if (uninstallDisabled == null) {
169 UserManager userManager =
170 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
171 Bundle restrictions = userManager.getUserRestrictions(info.user);
172 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700173 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700174 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700175 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700176 // Cancel any pending alarm and set cache expiry after some time
177 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
vadimt908e9bb2020-02-28 18:02:30 -0800178 mCacheExpireAlarm.setOnAlarmListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700179 if (uninstallDisabled) {
Alex Chau158caf42022-05-24 17:09:32 +0100180 return INVALID;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700181 }
Anushree Ganjam45ef5442024-01-26 14:08:07 -0800182 if (Flags.enablePrivateSpace() && UserCache.getInstance(getContext()).getUserInfo(
183 info.user).isPrivate()) {
184 return INVALID;
185 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700186
Sunny Goyal076839c2017-10-30 13:52:20 -0700187 if (info instanceof ItemInfoWithIcon) {
188 ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
Alex Chau158caf42022-05-24 17:09:32 +0100189 if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0
190 && (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) == 0) {
191 return INVALID;
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700192 }
193 }
Anushree Ganjam45ef5442024-01-26 14:08:07 -0800194 if (getUninstallTarget(getContext(), info) == null) {
Alex Chau158caf42022-05-24 17:09:32 +0100195 return INVALID;
196 }
197 return UNINSTALL;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700198 }
199
200 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700201 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -0700202 */
Anushree Ganjam45ef5442024-01-26 14:08:07 -0800203 public static ComponentName getUninstallTarget(Context context, ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700204 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800205 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700206 if (item != null &&
Sunny Goyalc5939392018-12-07 11:43:47 -0800207 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700208 intent = item.getIntent();
209 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700210 }
211 if (intent != null) {
Anushree Ganjam45ef5442024-01-26 14:08:07 -0800212 LauncherActivityInfo info = context.getSystemService(LauncherApps.class)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700213 .resolveActivity(intent, user);
214 if (info != null
215 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
216 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700217 }
218 }
219 return null;
220 }
221
222 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700223 public void onDrop(DragObject d, DragOptions options) {
224 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700225 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal852537f2020-07-15 17:02:16 -0700226
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700227 super.onDrop(d, options);
Sunny Goyal384b5782021-02-09 22:50:02 -0800228 doLog(d.logInstanceId, d.originalDragInfo);
229 }
230
231 private void doLog(InstanceId logInstanceId, ItemInfo itemInfo) {
232 StatsLogger logger = mStatsLogManager.logger().withInstanceId(logInstanceId);
233 if (itemInfo != null) {
234 logger.withItemInfo(itemInfo);
Sunny Goyal852537f2020-07-15 17:02:16 -0700235 }
thiruramff484512020-05-11 11:19:58 -0700236 if (mCurrentAccessibilityAction == UNINSTALL) {
Sunny Goyal852537f2020-07-15 17:02:16 -0700237 logger.log(LAUNCHER_ITEM_DROPPED_ON_UNINSTALL);
thiruramff484512020-05-11 11:19:58 -0700238 } else if (mCurrentAccessibilityAction == DISMISS_PREDICTION) {
Sunny Goyal852537f2020-07-15 17:02:16 -0700239 logger.log(LAUNCHER_ITEM_DROPPED_ON_DONT_SUGGEST);
thiruramff484512020-05-11 11:19:58 -0700240 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700241 }
242
243 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800244 public void completeDrop(final DragObject d) {
Stefan Andonian510a8182022-03-30 17:46:09 +0000245 ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo,
246 d.logInstanceId);
Andrew Cole44898c32023-04-05 13:49:32 -0700247 mDropTargetHandler.onSecondaryTargetCompleteDrop(target, d);
Tony Wickham734dfbe2015-09-16 16:22:36 -0700248 }
249
Winson Chung1054d4e2018-03-05 19:39:21 +0000250 private View getViewUnderDrag(ItemInfo info) {
Andrew Cole44898c32023-04-05 13:49:32 -0700251 return mDropTargetHandler.getViewUnderDrag(info);
Winson Chung1054d4e2018-03-05 19:39:21 +0000252 }
253
254 /**
255 * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id,
256 * otherwise return {@code INVALID_APPWIDGET_ID}
257 */
258 private int getReconfigurableWidgetId(View view) {
259 if (!(view instanceof AppWidgetHostView)) {
260 return INVALID_APPWIDGET_ID;
261 }
262 AppWidgetHostView hostView = (AppWidgetHostView) view;
263 AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo();
264 if (widgetInfo == null || widgetInfo.configure == null) {
265 return INVALID_APPWIDGET_ID;
266 }
267 if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo)
268 .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) {
269 return INVALID_APPWIDGET_ID;
270 }
271 return hostView.getAppWidgetId();
272 }
273
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700274 /**
275 * Performs the drop action and returns the target component for the dragObject or null if
276 * the action was not performed.
277 */
Stefan Andonian510a8182022-03-30 17:46:09 +0000278 protected ComponentName performDropAction(View view, ItemInfo info, InstanceId instanceId) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000279 if (mCurrentAccessibilityAction == RECONFIGURE) {
280 int widgetId = getReconfigurableWidgetId(view);
281 if (widgetId != INVALID_APPWIDGET_ID) {
Andrew Cole44898c32023-04-05 13:49:32 -0700282 mDropTargetHandler.reconfigureWidget(widgetId, info);
Winson Chung1054d4e2018-03-05 19:39:21 +0000283 }
284 return null;
285 }
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800286 if (mCurrentAccessibilityAction == DISMISS_PREDICTION) {
Stefan Andonian510a8182022-03-30 17:46:09 +0000287 if (FeatureFlags.ENABLE_DISMISS_PREDICTION_UNDO.get()) {
Andrew Cole44898c32023-04-05 13:49:32 -0700288 CharSequence announcement = getContext().getString(R.string.item_removed);
289 mDropTargetHandler
Anushree Ganjam45ef5442024-01-26 14:08:07 -0800290 .dismissPrediction(announcement, () -> {
291 }, () -> {
292 mStatsLogManager.logger()
293 .withInstanceId(instanceId)
294 .withItemInfo(info)
295 .log(LAUNCHER_DISMISS_PREDICTION_UNDO);
296 });
Stefan Andonian510a8182022-03-30 17:46:09 +0000297 }
Samuel Fufa5cf3e862020-02-03 20:22:54 -0800298 return null;
299 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000300
Anushree Ganjam45ef5442024-01-26 14:08:07 -0800301 return performUninstall(getContext(), getUninstallTarget(getContext(), info), info);
302 }
303
304 /**
305 * Performs uninstall and returns the target component for the {@link ItemInfo} or null if
306 * the uninstall was not performed.
307 */
308 public static ComponentName performUninstall(Context context, @Nullable ComponentName cn,
309 ItemInfo info) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700310 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800311 // System applications cannot be installed. For now, show a toast explaining that.
312 // We may give them the option of disabling apps this way.
Andrew Cole44898c32023-04-05 13:49:32 -0700313 Toast.makeText(
Anushree Ganjam45ef5442024-01-26 14:08:07 -0800314 context,
Andrew Cole44898c32023-04-05 13:49:32 -0700315 R.string.uninstall_system_app_text,
316 Toast.LENGTH_SHORT
Anushree Ganjam45ef5442024-01-26 14:08:07 -0800317 ).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700318 return null;
319 }
320 try {
Anushree Ganjam45ef5442024-01-26 14:08:07 -0800321 Intent i = Intent.parseUri(context.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700322 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
323 .putExtra(Intent.EXTRA_USER, info.user);
Anushree Ganjam45ef5442024-01-26 14:08:07 -0800324 context.startActivity(i);
Samuel Fufacc1e1072019-09-06 16:19:11 -0700325 FileLog.d(TAG, "start uninstall activity " + cn.getPackageName());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700326 return cn;
327 } catch (URISyntaxException e) {
328 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
329 return null;
330 }
331 }
332
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700333 @Override
334 public void onAccessibilityDrop(View view, ItemInfo item) {
Stefan Andonian510a8182022-03-30 17:46:09 +0000335 InstanceId instanceId = new InstanceIdSequence().newInstanceId();
336 doLog(instanceId, item);
337 performDropAction(view, item, instanceId);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700338 }
339
340 /**
341 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
342 * {@link #onLauncherResume}
343 */
Andrew Cole44898c32023-04-05 13:49:32 -0700344 protected class DeferredOnComplete implements DragSource {
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700345
346 private final DragSource mOriginal;
347 private final Context mContext;
348
Andrew Cole44898c32023-04-05 13:49:32 -0700349 protected String mPackageName;
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700350 private DragObject mDragObject;
351
352 public DeferredOnComplete(DragSource original, Context context) {
353 mOriginal = original;
354 mContext = context;
355 }
356
357 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700358 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700359 boolean success) {
360 mDragObject = d;
361 }
362
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700363 public void onLauncherResume() {
364 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
Sunny Goyale7b00122019-10-02 16:13:34 -0700365 if (new PackageManagerHelper(mContext).getApplicationInfo(mPackageName,
366 mDragObject.dragInfo.user, PackageManager.MATCH_UNINSTALLED_PACKAGES) == null) {
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700367 mDragObject.dragSource = mOriginal;
Winson Chung1054d4e2018-03-05 19:39:21 +0000368 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true);
thiruramc6a38ba2020-06-16 18:58:13 -0700369 mStatsLogManager.logger().withInstanceId(mDragObject.logInstanceId)
370 .log(LAUNCHER_ITEM_UNINSTALL_COMPLETED);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700371 } else {
372 sendFailure();
thiruramc6a38ba2020-06-16 18:58:13 -0700373 mStatsLogManager.logger().withInstanceId(mDragObject.logInstanceId)
374 .log(LAUNCHER_ITEM_UNINSTALL_CANCELLED);
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700375 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700376 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700377
378 public void sendFailure() {
379 mDragObject.dragSource = mOriginal;
380 mDragObject.cancelled = true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000381 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800382 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700383 }
384}