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