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