Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 3 | import static android.appwidget.AppWidgetManager.INVALID_APPWIDGET_ID; |
| 4 | import static android.appwidget.AppWidgetProviderInfo.WIDGET_FEATURE_RECONFIGURABLE; |
Samuel Fufa | cc1e107 | 2019-09-06 16:19:11 -0700 | [diff] [blame] | 5 | |
Tony Wickham | 62dbe98 | 2021-05-12 16:19:58 -0700 | [diff] [blame] | 6 | import static com.android.launcher3.Launcher.REQUEST_RECONFIGURE_APPWIDGET; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 7 | import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP; |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 8 | import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.DISMISS_PREDICTION; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 9 | import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.RECONFIGURE; |
| 10 | import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.UNINSTALL; |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame^] | 11 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_DISMISS_PREDICTION_UNDO; |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 12 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_DONT_SUGGEST; |
| 13 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_UNINSTALL; |
| 14 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_UNINSTALL_CANCELLED; |
| 15 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_UNINSTALL_COMPLETED; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 16 | import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_MASK; |
| 17 | import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_NO; |
Sunny Goyal | 076839c | 2017-10-30 13:52:20 -0700 | [diff] [blame] | 18 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 19 | import android.appwidget.AppWidgetHostView; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 20 | import android.appwidget.AppWidgetProviderInfo; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 24 | import android.content.pm.ApplicationInfo; |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 25 | import android.content.pm.LauncherActivityInfo; |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 26 | import android.content.pm.LauncherApps; |
Sunny Goyal | f2db253 | 2017-03-01 17:27:16 -0800 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 28 | import android.net.Uri; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 30 | import android.os.UserHandle; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 31 | import android.os.UserManager; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 32 | import android.util.ArrayMap; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 33 | import android.util.AttributeSet; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 34 | import android.util.Log; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 35 | import android.view.View; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 36 | import android.widget.Toast; |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 37 | |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 38 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 39 | import com.android.launcher3.dragndrop.DragOptions; |
Samuel Fufa | cc1e107 | 2019-09-06 16:19:11 -0700 | [diff] [blame] | 40 | import com.android.launcher3.logging.FileLog; |
Sunny Goyal | 384b578 | 2021-02-09 22:50:02 -0800 | [diff] [blame] | 41 | import com.android.launcher3.logging.InstanceId; |
| 42 | import com.android.launcher3.logging.InstanceIdSequence; |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 43 | import com.android.launcher3.logging.StatsLogManager; |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame] | 44 | import com.android.launcher3.logging.StatsLogManager.StatsLogger; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 45 | import com.android.launcher3.model.data.ItemInfo; |
| 46 | import com.android.launcher3.model.data.ItemInfoWithIcon; |
| 47 | import com.android.launcher3.model.data.LauncherAppWidgetInfo; |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 48 | import com.android.launcher3.util.PackageManagerHelper; |
Yogisha Dixit | 4ff5ee3 | 2021-05-04 14:58:34 +0100 | [diff] [blame] | 49 | import com.android.launcher3.util.PendingRequestArgs; |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame^] | 50 | import com.android.launcher3.views.Snackbar; |
Yogisha Dixit | 741fae9 | 2021-02-22 14:03:44 +0000 | [diff] [blame] | 51 | import com.android.launcher3.widget.LauncherAppWidgetProviderInfo; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 52 | |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 53 | import java.net.URISyntaxException; |
| 54 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 55 | /** |
| 56 | * Drop target which provides a secondary option for an item. |
| 57 | * For app targets: shows as uninstall |
| 58 | * For configurable widgets: shows as setup |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 59 | * For predicted app icons: don't suggest app |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 60 | */ |
| 61 | public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 62 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 63 | private static final String TAG = "SecondaryDropTarget"; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 64 | |
| 65 | private static final long CACHE_EXPIRE_TIMEOUT = 5000; |
| 66 | private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 67 | private final StatsLogManager mStatsLogManager; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 68 | private final Alarm mCacheExpireAlarm; |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 69 | private boolean mHadPendingAlarm; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 70 | |
Mehdi Alizadeh | 8517b69 | 2018-04-23 15:55:26 -0700 | [diff] [blame] | 71 | protected int mCurrentAccessibilityAction = -1; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 72 | public SecondaryDropTarget(Context context, AttributeSet attrs) { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 73 | this(context, attrs, 0); |
| 74 | } |
| 75 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 76 | public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 77 | super(context, attrs, defStyle); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 78 | mCacheExpireAlarm = new Alarm(); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 79 | mStatsLogManager = StatsLogManager.newInstance(context); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 83 | protected void onAttachedToWindow() { |
| 84 | super.onAttachedToWindow(); |
| 85 | if (mHadPendingAlarm) { |
| 86 | mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 87 | mCacheExpireAlarm.setOnAlarmListener(this); |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 88 | mHadPendingAlarm = false; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | protected void onDetachedFromWindow() { |
| 94 | super.onDetachedFromWindow(); |
| 95 | if (mCacheExpireAlarm.alarmPending()) { |
| 96 | mCacheExpireAlarm.cancelAlarm(); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 97 | mCacheExpireAlarm.setOnAlarmListener(null); |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 98 | mHadPendingAlarm = true; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 103 | protected void onFinishInflate() { |
| 104 | super.onFinishInflate(); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 105 | setupUi(UNINSTALL); |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Mehdi Alizadeh | 8517b69 | 2018-04-23 15:55:26 -0700 | [diff] [blame] | 108 | protected void setupUi(int action) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 109 | if (action == mCurrentAccessibilityAction) { |
| 110 | return; |
| 111 | } |
| 112 | mCurrentAccessibilityAction = action; |
| 113 | |
| 114 | if (action == UNINSTALL) { |
Yogisha Dixit | a3a6f51 | 2021-04-28 13:45:37 +0100 | [diff] [blame] | 115 | setDrawable(R.drawable.ic_uninstall_no_shadow); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 116 | updateText(R.string.uninstall_drop_target_label); |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 117 | } else if (action == DISMISS_PREDICTION) { |
Yogisha Dixit | a3a6f51 | 2021-04-28 13:45:37 +0100 | [diff] [blame] | 118 | setDrawable(R.drawable.ic_block_no_shadow); |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 119 | updateText(R.string.dismiss_prediction_label); |
| 120 | } else if (action == RECONFIGURE) { |
Yogisha Dixit | a3a6f51 | 2021-04-28 13:45:37 +0100 | [diff] [blame] | 121 | setDrawable(R.drawable.ic_setting); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 122 | updateText(R.string.gadget_setup_text); |
| 123 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 127 | public void onAlarm(Alarm alarm) { |
| 128 | mUninstallDisabledCache.clear(); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 131 | @Override |
| 132 | public int getAccessibilityAction() { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 133 | return mCurrentAccessibilityAction; |
| 134 | } |
| 135 | |
| 136 | @Override |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 137 | protected boolean supportsDrop(ItemInfo info) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 138 | return supportsAccessibilityDrop(info, getViewUnderDrag(info)); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public boolean supportsAccessibilityDrop(ItemInfo info, View view) { |
| 143 | if (view instanceof AppWidgetHostView) { |
| 144 | if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) { |
| 145 | setupUi(RECONFIGURE); |
| 146 | return true; |
| 147 | } |
| 148 | return false; |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 149 | } else if (FeatureFlags.ENABLE_PREDICTION_DISMISS.get() && info.isPredictedItem()) { |
| 150 | setupUi(DISMISS_PREDICTION); |
| 151 | return true; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | setupUi(UNINSTALL); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 155 | Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user); |
| 156 | if (uninstallDisabled == null) { |
| 157 | UserManager userManager = |
| 158 | (UserManager) getContext().getSystemService(Context.USER_SERVICE); |
| 159 | Bundle restrictions = userManager.getUserRestrictions(info.user); |
| 160 | uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false) |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 161 | || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 162 | mUninstallDisabledCache.put(info.user, uninstallDisabled); |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 163 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 164 | // Cancel any pending alarm and set cache expiry after some time |
| 165 | mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 166 | mCacheExpireAlarm.setOnAlarmListener(this); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 167 | if (uninstallDisabled) { |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 168 | return false; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Sunny Goyal | 076839c | 2017-10-30 13:52:20 -0700 | [diff] [blame] | 171 | if (info instanceof ItemInfoWithIcon) { |
| 172 | ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info; |
| 173 | if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) { |
| 174 | return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0; |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 177 | return getUninstallTarget(info) != null; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 181 | * @return the component name that should be uninstalled or null. |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 182 | */ |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 183 | private ComponentName getUninstallTarget(ItemInfo item) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 184 | Intent intent = null; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 185 | UserHandle user = null; |
Sunny Goyal | 1fe0c2c | 2017-08-15 12:54:42 -0700 | [diff] [blame] | 186 | if (item != null && |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 187 | item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Sunny Goyal | 1fe0c2c | 2017-08-15 12:54:42 -0700 | [diff] [blame] | 188 | intent = item.getIntent(); |
| 189 | user = item.user; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 190 | } |
| 191 | if (intent != null) { |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 192 | LauncherActivityInfo info = mLauncher.getSystemService(LauncherApps.class) |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 193 | .resolveActivity(intent, user); |
| 194 | if (info != null |
| 195 | && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 196 | return info.getComponentName(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | return null; |
| 200 | } |
| 201 | |
| 202 | @Override |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 203 | public void onDrop(DragObject d, DragOptions options) { |
| 204 | // Defer onComplete |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 205 | d.dragSource = new DeferredOnComplete(d.dragSource, getContext()); |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame] | 206 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 207 | super.onDrop(d, options); |
Sunny Goyal | 384b578 | 2021-02-09 22:50:02 -0800 | [diff] [blame] | 208 | doLog(d.logInstanceId, d.originalDragInfo); |
| 209 | } |
| 210 | |
| 211 | private void doLog(InstanceId logInstanceId, ItemInfo itemInfo) { |
| 212 | StatsLogger logger = mStatsLogManager.logger().withInstanceId(logInstanceId); |
| 213 | if (itemInfo != null) { |
| 214 | logger.withItemInfo(itemInfo); |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame] | 215 | } |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 216 | if (mCurrentAccessibilityAction == UNINSTALL) { |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame] | 217 | logger.log(LAUNCHER_ITEM_DROPPED_ON_UNINSTALL); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 218 | } else if (mCurrentAccessibilityAction == DISMISS_PREDICTION) { |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame] | 219 | logger.log(LAUNCHER_ITEM_DROPPED_ON_DONT_SUGGEST); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 220 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 224 | public void completeDrop(final DragObject d) { |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame^] | 225 | ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo, |
| 226 | d.logInstanceId); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 227 | if (d.dragSource instanceof DeferredOnComplete) { |
| 228 | DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource; |
| 229 | if (target != null) { |
| 230 | deferred.mPackageName = target.getPackageName(); |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 231 | mLauncher.addOnResumeCallback(deferred::onLauncherResume); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 232 | } else { |
| 233 | deferred.sendFailure(); |
| 234 | } |
| 235 | } |
Tony Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 238 | private View getViewUnderDrag(ItemInfo info) { |
| 239 | if (info instanceof LauncherAppWidgetInfo && info.container == CONTAINER_DESKTOP && |
| 240 | mLauncher.getWorkspace().getDragInfo() != null) { |
| 241 | return mLauncher.getWorkspace().getDragInfo().cell; |
| 242 | } |
| 243 | return null; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id, |
| 248 | * otherwise return {@code INVALID_APPWIDGET_ID} |
| 249 | */ |
| 250 | private int getReconfigurableWidgetId(View view) { |
| 251 | if (!(view instanceof AppWidgetHostView)) { |
| 252 | return INVALID_APPWIDGET_ID; |
| 253 | } |
| 254 | AppWidgetHostView hostView = (AppWidgetHostView) view; |
| 255 | AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo(); |
| 256 | if (widgetInfo == null || widgetInfo.configure == null) { |
| 257 | return INVALID_APPWIDGET_ID; |
| 258 | } |
| 259 | if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo) |
| 260 | .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) { |
| 261 | return INVALID_APPWIDGET_ID; |
| 262 | } |
| 263 | return hostView.getAppWidgetId(); |
| 264 | } |
| 265 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 266 | /** |
| 267 | * Performs the drop action and returns the target component for the dragObject or null if |
| 268 | * the action was not performed. |
| 269 | */ |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame^] | 270 | protected ComponentName performDropAction(View view, ItemInfo info, InstanceId instanceId) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 271 | if (mCurrentAccessibilityAction == RECONFIGURE) { |
| 272 | int widgetId = getReconfigurableWidgetId(view); |
| 273 | if (widgetId != INVALID_APPWIDGET_ID) { |
Yogisha Dixit | 4ff5ee3 | 2021-05-04 14:58:34 +0100 | [diff] [blame] | 274 | mLauncher.setWaitingForResult( |
| 275 | PendingRequestArgs.forWidgetInfo(widgetId, null, info)); |
Tony Wickham | 62dbe98 | 2021-05-12 16:19:58 -0700 | [diff] [blame] | 276 | mLauncher.getAppWidgetHost().startConfigActivity(mLauncher, widgetId, |
| 277 | REQUEST_RECONFIGURE_APPWIDGET); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 278 | } |
| 279 | return null; |
| 280 | } |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 281 | if (mCurrentAccessibilityAction == DISMISS_PREDICTION) { |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame^] | 282 | if (FeatureFlags.ENABLE_DISMISS_PREDICTION_UNDO.get()) { |
| 283 | mLauncher.getDragLayer() |
| 284 | .announceForAccessibility(getContext().getString(R.string.item_removed)); |
| 285 | Snackbar.show(mLauncher, R.string.item_removed, R.string.undo, () -> { }, () -> { |
| 286 | mStatsLogManager.logger() |
| 287 | .withInstanceId(instanceId) |
| 288 | .withItemInfo(info) |
| 289 | .log(LAUNCHER_DISMISS_PREDICTION_UNDO); |
| 290 | }); |
| 291 | } |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 292 | return null; |
| 293 | } |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 294 | // else: mCurrentAccessibilityAction == UNINSTALL |
| 295 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 296 | ComponentName cn = getUninstallTarget(info); |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 297 | if (cn == null) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 298 | // System applications cannot be installed. For now, show a toast explaining that. |
| 299 | // We may give them the option of disabling apps this way. |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 300 | Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show(); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 301 | return null; |
| 302 | } |
| 303 | try { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 304 | Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0) |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 305 | .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName())) |
| 306 | .putExtra(Intent.EXTRA_USER, info.user); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 307 | mLauncher.startActivity(i); |
Samuel Fufa | cc1e107 | 2019-09-06 16:19:11 -0700 | [diff] [blame] | 308 | FileLog.d(TAG, "start uninstall activity " + cn.getPackageName()); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 309 | return cn; |
| 310 | } catch (URISyntaxException e) { |
| 311 | Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info); |
| 312 | return null; |
| 313 | } |
| 314 | } |
| 315 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 316 | @Override |
| 317 | public void onAccessibilityDrop(View view, ItemInfo item) { |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame^] | 318 | InstanceId instanceId = new InstanceIdSequence().newInstanceId(); |
| 319 | doLog(instanceId, item); |
| 320 | performDropAction(view, item, instanceId); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
| 324 | * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until |
| 325 | * {@link #onLauncherResume} |
| 326 | */ |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 327 | private class DeferredOnComplete implements DragSource { |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 328 | |
| 329 | private final DragSource mOriginal; |
| 330 | private final Context mContext; |
| 331 | |
| 332 | private String mPackageName; |
| 333 | private DragObject mDragObject; |
| 334 | |
| 335 | public DeferredOnComplete(DragSource original, Context context) { |
| 336 | mOriginal = original; |
| 337 | mContext = context; |
| 338 | } |
| 339 | |
| 340 | @Override |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 341 | public void onDropCompleted(View target, DragObject d, |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 342 | boolean success) { |
| 343 | mDragObject = d; |
| 344 | } |
| 345 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 346 | public void onLauncherResume() { |
| 347 | // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well. |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 348 | if (new PackageManagerHelper(mContext).getApplicationInfo(mPackageName, |
| 349 | mDragObject.dragInfo.user, PackageManager.MATCH_UNINSTALLED_PACKAGES) == null) { |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 350 | mDragObject.dragSource = mOriginal; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 351 | mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true); |
thiruram | c6a38ba | 2020-06-16 18:58:13 -0700 | [diff] [blame] | 352 | mStatsLogManager.logger().withInstanceId(mDragObject.logInstanceId) |
| 353 | .log(LAUNCHER_ITEM_UNINSTALL_COMPLETED); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 354 | } else { |
| 355 | sendFailure(); |
thiruram | c6a38ba | 2020-06-16 18:58:13 -0700 | [diff] [blame] | 356 | mStatsLogManager.logger().withInstanceId(mDragObject.logInstanceId) |
| 357 | .log(LAUNCHER_ITEM_UNINSTALL_CANCELLED); |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 358 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 359 | } |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 360 | |
| 361 | public void sendFailure() { |
| 362 | mDragObject.dragSource = mOriginal; |
| 363 | mDragObject.cancelled = true; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 364 | mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false); |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 365 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 366 | } |
| 367 | } |