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; |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 9 | import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.INVALID; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 10 | import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.RECONFIGURE; |
| 11 | import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.UNINSTALL; |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame] | 12 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_DISMISS_PREDICTION_UNDO; |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 13 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_DONT_SUGGEST; |
| 14 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_UNINSTALL; |
| 15 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_UNINSTALL_CANCELLED; |
| 16 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_UNINSTALL_COMPLETED; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 17 | import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_MASK; |
| 18 | import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_NO; |
Sunny Goyal | 076839c | 2017-10-30 13:52:20 -0700 | [diff] [blame] | 19 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 20 | import android.appwidget.AppWidgetHostView; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 21 | import android.appwidget.AppWidgetProviderInfo; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 24 | import android.content.Intent; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 25 | import android.content.pm.ApplicationInfo; |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 26 | import android.content.pm.LauncherActivityInfo; |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 27 | import android.content.pm.LauncherApps; |
Sunny Goyal | f2db253 | 2017-03-01 17:27:16 -0800 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 29 | import android.net.Uri; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 30 | import android.os.Bundle; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 31 | import android.os.UserHandle; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 32 | import android.os.UserManager; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 33 | import android.util.ArrayMap; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 34 | import android.util.AttributeSet; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 35 | import android.util.Log; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 36 | import android.view.View; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 37 | import android.widget.Toast; |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 38 | |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 39 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 40 | import com.android.launcher3.dragndrop.DragOptions; |
Samuel Fufa | cc1e107 | 2019-09-06 16:19:11 -0700 | [diff] [blame] | 41 | import com.android.launcher3.logging.FileLog; |
Sunny Goyal | 384b578 | 2021-02-09 22:50:02 -0800 | [diff] [blame] | 42 | import com.android.launcher3.logging.InstanceId; |
| 43 | import com.android.launcher3.logging.InstanceIdSequence; |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 44 | import com.android.launcher3.logging.StatsLogManager; |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame] | 45 | import com.android.launcher3.logging.StatsLogManager.StatsLogger; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 46 | import com.android.launcher3.model.data.ItemInfo; |
| 47 | import com.android.launcher3.model.data.ItemInfoWithIcon; |
| 48 | import com.android.launcher3.model.data.LauncherAppWidgetInfo; |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 49 | import com.android.launcher3.util.PackageManagerHelper; |
Yogisha Dixit | 4ff5ee3 | 2021-05-04 14:58:34 +0100 | [diff] [blame] | 50 | import com.android.launcher3.util.PendingRequestArgs; |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame] | 51 | import com.android.launcher3.views.Snackbar; |
Yogisha Dixit | 741fae9 | 2021-02-22 14:03:44 +0000 | [diff] [blame] | 52 | import com.android.launcher3.widget.LauncherAppWidgetProviderInfo; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 53 | |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 54 | import java.net.URISyntaxException; |
| 55 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 56 | /** |
| 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 Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 60 | * For predicted app icons: don't suggest app |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 61 | */ |
| 62 | public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 63 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 64 | private static final String TAG = "SecondaryDropTarget"; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 65 | |
| 66 | private static final long CACHE_EXPIRE_TIMEOUT = 5000; |
| 67 | private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 68 | private final StatsLogManager mStatsLogManager; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 69 | private final Alarm mCacheExpireAlarm; |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 70 | private boolean mHadPendingAlarm; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 71 | |
Mehdi Alizadeh | 8517b69 | 2018-04-23 15:55:26 -0700 | [diff] [blame] | 72 | protected int mCurrentAccessibilityAction = -1; |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 73 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 74 | public SecondaryDropTarget(Context context, AttributeSet attrs) { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 75 | this(context, attrs, 0); |
| 76 | } |
| 77 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 78 | public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 79 | super(context, attrs, defStyle); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 80 | mCacheExpireAlarm = new Alarm(); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 81 | mStatsLogManager = StatsLogManager.newInstance(context); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 85 | protected void onAttachedToWindow() { |
| 86 | super.onAttachedToWindow(); |
| 87 | if (mHadPendingAlarm) { |
| 88 | mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 89 | mCacheExpireAlarm.setOnAlarmListener(this); |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 90 | mHadPendingAlarm = false; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void onDetachedFromWindow() { |
| 96 | super.onDetachedFromWindow(); |
| 97 | if (mCacheExpireAlarm.alarmPending()) { |
| 98 | mCacheExpireAlarm.cancelAlarm(); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 99 | mCacheExpireAlarm.setOnAlarmListener(null); |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 100 | mHadPendingAlarm = true; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 105 | protected void onFinishInflate() { |
| 106 | super.onFinishInflate(); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 107 | setupUi(UNINSTALL); |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Mehdi Alizadeh | 8517b69 | 2018-04-23 15:55:26 -0700 | [diff] [blame] | 110 | protected void setupUi(int action) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 111 | if (action == mCurrentAccessibilityAction) { |
| 112 | return; |
| 113 | } |
| 114 | mCurrentAccessibilityAction = action; |
| 115 | |
| 116 | if (action == UNINSTALL) { |
Yogisha Dixit | a3a6f51 | 2021-04-28 13:45:37 +0100 | [diff] [blame] | 117 | setDrawable(R.drawable.ic_uninstall_no_shadow); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 118 | updateText(R.string.uninstall_drop_target_label); |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 119 | } else if (action == DISMISS_PREDICTION) { |
Yogisha Dixit | a3a6f51 | 2021-04-28 13:45:37 +0100 | [diff] [blame] | 120 | setDrawable(R.drawable.ic_block_no_shadow); |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 121 | updateText(R.string.dismiss_prediction_label); |
| 122 | } else if (action == RECONFIGURE) { |
Yogisha Dixit | a3a6f51 | 2021-04-28 13:45:37 +0100 | [diff] [blame] | 123 | setDrawable(R.drawable.ic_setting); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 124 | updateText(R.string.gadget_setup_text); |
| 125 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 129 | public void onAlarm(Alarm alarm) { |
| 130 | mUninstallDisabledCache.clear(); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public int getAccessibilityAction() { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 135 | return mCurrentAccessibilityAction; |
| 136 | } |
| 137 | |
| 138 | @Override |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 139 | 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 147 | protected boolean supportsDrop(ItemInfo info) { |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 148 | return getButtonType(info, getViewUnderDrag(info)) != INVALID; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean supportsAccessibilityDrop(ItemInfo info, View view) { |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 153 | return getButtonType(info, view) != INVALID; |
| 154 | } |
| 155 | |
| 156 | private int getButtonType(ItemInfo info, View view) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 157 | if (view instanceof AppWidgetHostView) { |
| 158 | if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) { |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 159 | return RECONFIGURE; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 160 | } |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 161 | return INVALID; |
Sunny Goyal | c5c7da9 | 2022-08-02 13:36:54 -0700 | [diff] [blame] | 162 | } else if (info.isPredictedItem()) { |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 163 | return DISMISS_PREDICTION; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 166 | 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 Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 172 | || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 173 | mUninstallDisabledCache.put(info.user, uninstallDisabled); |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 174 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 175 | // Cancel any pending alarm and set cache expiry after some time |
| 176 | mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 177 | mCacheExpireAlarm.setOnAlarmListener(this); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 178 | if (uninstallDisabled) { |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 179 | return INVALID; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Sunny Goyal | 076839c | 2017-10-30 13:52:20 -0700 | [diff] [blame] | 182 | if (info instanceof ItemInfoWithIcon) { |
| 183 | ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info; |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 184 | if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0 |
| 185 | && (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) == 0) { |
| 186 | return INVALID; |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 189 | if (getUninstallTarget(info) == null) { |
| 190 | return INVALID; |
| 191 | } |
| 192 | return UNINSTALL; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 196 | * @return the component name that should be uninstalled or null. |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 197 | */ |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 198 | private ComponentName getUninstallTarget(ItemInfo item) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 199 | Intent intent = null; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 200 | UserHandle user = null; |
Sunny Goyal | 1fe0c2c | 2017-08-15 12:54:42 -0700 | [diff] [blame] | 201 | if (item != null && |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 202 | item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Sunny Goyal | 1fe0c2c | 2017-08-15 12:54:42 -0700 | [diff] [blame] | 203 | intent = item.getIntent(); |
| 204 | user = item.user; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 205 | } |
| 206 | if (intent != null) { |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 207 | LauncherActivityInfo info = mLauncher.getSystemService(LauncherApps.class) |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 208 | .resolveActivity(intent, user); |
| 209 | if (info != null |
| 210 | && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 211 | return info.getComponentName(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | @Override |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 218 | public void onDrop(DragObject d, DragOptions options) { |
| 219 | // Defer onComplete |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 220 | d.dragSource = new DeferredOnComplete(d.dragSource, getContext()); |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame] | 221 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 222 | super.onDrop(d, options); |
Sunny Goyal | 384b578 | 2021-02-09 22:50:02 -0800 | [diff] [blame] | 223 | 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 Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame] | 230 | } |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 231 | if (mCurrentAccessibilityAction == UNINSTALL) { |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame] | 232 | logger.log(LAUNCHER_ITEM_DROPPED_ON_UNINSTALL); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 233 | } else if (mCurrentAccessibilityAction == DISMISS_PREDICTION) { |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame] | 234 | logger.log(LAUNCHER_ITEM_DROPPED_ON_DONT_SUGGEST); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 235 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 239 | public void completeDrop(final DragObject d) { |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame] | 240 | ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo, |
| 241 | d.logInstanceId); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 242 | if (d.dragSource instanceof DeferredOnComplete) { |
| 243 | DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource; |
| 244 | if (target != null) { |
| 245 | deferred.mPackageName = target.getPackageName(); |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 246 | mLauncher.addOnResumeCallback(deferred::onLauncherResume); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 247 | } else { |
| 248 | deferred.sendFailure(); |
| 249 | } |
| 250 | } |
Tony Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 253 | 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 Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 281 | /** |
| 282 | * Performs the drop action and returns the target component for the dragObject or null if |
| 283 | * the action was not performed. |
| 284 | */ |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame] | 285 | protected ComponentName performDropAction(View view, ItemInfo info, InstanceId instanceId) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 286 | if (mCurrentAccessibilityAction == RECONFIGURE) { |
| 287 | int widgetId = getReconfigurableWidgetId(view); |
| 288 | if (widgetId != INVALID_APPWIDGET_ID) { |
Yogisha Dixit | 4ff5ee3 | 2021-05-04 14:58:34 +0100 | [diff] [blame] | 289 | mLauncher.setWaitingForResult( |
| 290 | PendingRequestArgs.forWidgetInfo(widgetId, null, info)); |
Sihua Ma | 0593a0d | 2022-10-03 16:01:48 -0700 | [diff] [blame^] | 291 | mLauncher.getAppWidgetHolder().startConfigActivity(mLauncher, widgetId, |
Tony Wickham | 62dbe98 | 2021-05-12 16:19:58 -0700 | [diff] [blame] | 292 | REQUEST_RECONFIGURE_APPWIDGET); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 293 | } |
| 294 | return null; |
| 295 | } |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 296 | if (mCurrentAccessibilityAction == DISMISS_PREDICTION) { |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame] | 297 | 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 Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 307 | return null; |
| 308 | } |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 309 | // else: mCurrentAccessibilityAction == UNINSTALL |
| 310 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 311 | ComponentName cn = getUninstallTarget(info); |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 312 | if (cn == null) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 313 | // 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 315 | 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] | 316 | return null; |
| 317 | } |
| 318 | try { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 319 | Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0) |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 320 | .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName())) |
| 321 | .putExtra(Intent.EXTRA_USER, info.user); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 322 | mLauncher.startActivity(i); |
Samuel Fufa | cc1e107 | 2019-09-06 16:19:11 -0700 | [diff] [blame] | 323 | FileLog.d(TAG, "start uninstall activity " + cn.getPackageName()); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 324 | 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 331 | @Override |
| 332 | public void onAccessibilityDrop(View view, ItemInfo item) { |
Stefan Andonian | 510a818 | 2022-03-30 17:46:09 +0000 | [diff] [blame] | 333 | InstanceId instanceId = new InstanceIdSequence().newInstanceId(); |
| 334 | doLog(instanceId, item); |
| 335 | performDropAction(view, item, instanceId); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /** |
| 339 | * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until |
| 340 | * {@link #onLauncherResume} |
| 341 | */ |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 342 | private class DeferredOnComplete implements DragSource { |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 343 | |
| 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 Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 356 | public void onDropCompleted(View target, DragObject d, |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 357 | boolean success) { |
| 358 | mDragObject = d; |
| 359 | } |
| 360 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 361 | public void onLauncherResume() { |
| 362 | // 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] | 363 | if (new PackageManagerHelper(mContext).getApplicationInfo(mPackageName, |
| 364 | mDragObject.dragInfo.user, PackageManager.MATCH_UNINSTALLED_PACKAGES) == null) { |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 365 | mDragObject.dragSource = mOriginal; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 366 | mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true); |
thiruram | c6a38ba | 2020-06-16 18:58:13 -0700 | [diff] [blame] | 367 | mStatsLogManager.logger().withInstanceId(mDragObject.logInstanceId) |
| 368 | .log(LAUNCHER_ITEM_UNINSTALL_COMPLETED); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 369 | } else { |
| 370 | sendFailure(); |
thiruram | c6a38ba | 2020-06-16 18:58:13 -0700 | [diff] [blame] | 371 | mStatsLogManager.logger().withInstanceId(mDragObject.logInstanceId) |
| 372 | .log(LAUNCHER_ITEM_UNINSTALL_CANCELLED); |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 373 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 374 | } |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 375 | |
| 376 | public void sendFailure() { |
| 377 | mDragObject.dragSource = mOriginal; |
| 378 | mDragObject.cancelled = true; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 379 | mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false); |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 380 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 381 | } |
| 382 | } |