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 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 36 | import com.android.launcher3.Launcher.OnResumeCallback; |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 37 | import com.android.launcher3.config.FeatureFlags; |
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; |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 40 | import com.android.launcher3.logging.LoggerUtils; |
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; |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 46 | import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 47 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 48 | import com.android.launcher3.util.PackageManagerHelper; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 49 | import com.android.launcher3.util.Themes; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 50 | |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 51 | import java.net.URISyntaxException; |
Samuel Fufa | a579ddc | 2020-02-27 16:59:19 -0800 | [diff] [blame] | 52 | import java.util.ArrayList; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 53 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 54 | /** |
| 55 | * Drop target which provides a secondary option for an item. |
| 56 | * For app targets: shows as uninstall |
| 57 | * For configurable widgets: shows as setup |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 58 | * For predicted app icons: don't suggest app |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 59 | */ |
| 60 | public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 61 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 62 | private static final String TAG = "SecondaryDropTarget"; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 63 | |
| 64 | private static final long CACHE_EXPIRE_TIMEOUT = 5000; |
| 65 | private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 66 | private final StatsLogManager mStatsLogManager; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 67 | private final Alarm mCacheExpireAlarm; |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 68 | private boolean mHadPendingAlarm; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 69 | |
Mehdi Alizadeh | 8517b69 | 2018-04-23 15:55:26 -0700 | [diff] [blame] | 70 | protected int mCurrentAccessibilityAction = -1; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 71 | public SecondaryDropTarget(Context context, AttributeSet attrs) { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 72 | this(context, attrs, 0); |
| 73 | } |
| 74 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 75 | public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 76 | super(context, attrs, defStyle); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 77 | mCacheExpireAlarm = new Alarm(); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 78 | mStatsLogManager = StatsLogManager.newInstance(context); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 82 | protected void onAttachedToWindow() { |
| 83 | super.onAttachedToWindow(); |
| 84 | if (mHadPendingAlarm) { |
| 85 | mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 86 | mCacheExpireAlarm.setOnAlarmListener(this); |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 87 | mHadPendingAlarm = false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | protected void onDetachedFromWindow() { |
| 93 | super.onDetachedFromWindow(); |
| 94 | if (mCacheExpireAlarm.alarmPending()) { |
| 95 | mCacheExpireAlarm.cancelAlarm(); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 96 | mCacheExpireAlarm.setOnAlarmListener(null); |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 97 | mHadPendingAlarm = true; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 102 | protected void onFinishInflate() { |
| 103 | super.onFinishInflate(); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 104 | setupUi(UNINSTALL); |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Mehdi Alizadeh | 8517b69 | 2018-04-23 15:55:26 -0700 | [diff] [blame] | 107 | protected void setupUi(int action) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 108 | if (action == mCurrentAccessibilityAction) { |
| 109 | return; |
| 110 | } |
| 111 | mCurrentAccessibilityAction = action; |
| 112 | |
| 113 | if (action == UNINSTALL) { |
| 114 | mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint); |
| 115 | setDrawable(R.drawable.ic_uninstall_shadow); |
| 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) { |
| 118 | mHoverColor = Themes.getColorAccent(getContext()); |
Tony Wickham | bd9754d | 2020-03-27 20:11:16 -0700 | [diff] [blame] | 119 | setDrawable(R.drawable.ic_block_shadow); |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 120 | updateText(R.string.dismiss_prediction_label); |
| 121 | } else if (action == RECONFIGURE) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 122 | mHoverColor = Themes.getColorAccent(getContext()); |
| 123 | setDrawable(R.drawable.ic_setup_shadow); |
| 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 |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 139 | public Target getDropTargetForLogging() { |
| 140 | Target t = LoggerUtils.newTarget(Target.Type.CONTROL); |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 141 | if (mCurrentAccessibilityAction == UNINSTALL) { |
| 142 | t.controlType = ControlType.UNINSTALL_TARGET; |
| 143 | } else if (mCurrentAccessibilityAction == DISMISS_PREDICTION) { |
| 144 | t.controlType = ControlType.DISMISS_PREDICTION; |
| 145 | } else { |
| 146 | t.controlType = ControlType.SETTINGS_BUTTON; |
| 147 | } |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 148 | return t; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | protected boolean supportsDrop(ItemInfo info) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 153 | return supportsAccessibilityDrop(info, getViewUnderDrag(info)); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public boolean supportsAccessibilityDrop(ItemInfo info, View view) { |
| 158 | if (view instanceof AppWidgetHostView) { |
| 159 | if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) { |
| 160 | setupUi(RECONFIGURE); |
| 161 | return true; |
| 162 | } |
| 163 | return false; |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 164 | } else if (FeatureFlags.ENABLE_PREDICTION_DISMISS.get() && info.isPredictedItem()) { |
| 165 | setupUi(DISMISS_PREDICTION); |
| 166 | return true; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | setupUi(UNINSTALL); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 170 | Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user); |
| 171 | if (uninstallDisabled == null) { |
| 172 | UserManager userManager = |
| 173 | (UserManager) getContext().getSystemService(Context.USER_SERVICE); |
| 174 | Bundle restrictions = userManager.getUserRestrictions(info.user); |
| 175 | uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false) |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 176 | || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 177 | mUninstallDisabledCache.put(info.user, uninstallDisabled); |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 178 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 179 | // Cancel any pending alarm and set cache expiry after some time |
| 180 | mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 181 | mCacheExpireAlarm.setOnAlarmListener(this); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 182 | if (uninstallDisabled) { |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 183 | return false; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Sunny Goyal | 076839c | 2017-10-30 13:52:20 -0700 | [diff] [blame] | 186 | if (info instanceof ItemInfoWithIcon) { |
| 187 | ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info; |
| 188 | if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) { |
| 189 | return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0; |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 192 | return getUninstallTarget(info) != null; |
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 | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame^] | 223 | StatsLogger logger = mStatsLogManager.logger().withInstanceId(d.logInstanceId); |
| 224 | if (d.originalDragInfo != null) { |
| 225 | logger.withItemInfo(d.originalDragInfo); |
| 226 | } |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 227 | if (mCurrentAccessibilityAction == UNINSTALL) { |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame^] | 228 | logger.log(LAUNCHER_ITEM_DROPPED_ON_UNINSTALL); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 229 | } else if (mCurrentAccessibilityAction == DISMISS_PREDICTION) { |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame^] | 230 | logger.log(LAUNCHER_ITEM_DROPPED_ON_DONT_SUGGEST); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame] | 231 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 235 | public void completeDrop(final DragObject d) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 236 | ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 237 | if (d.dragSource instanceof DeferredOnComplete) { |
| 238 | DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource; |
| 239 | if (target != null) { |
| 240 | deferred.mPackageName = target.getPackageName(); |
Winson Chung | 0b70cd4 | 2019-06-17 22:34:33 -0700 | [diff] [blame] | 241 | mLauncher.addOnResumeCallback(deferred); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 242 | } else { |
| 243 | deferred.sendFailure(); |
| 244 | } |
| 245 | } |
Tony Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 248 | private View getViewUnderDrag(ItemInfo info) { |
| 249 | if (info instanceof LauncherAppWidgetInfo && info.container == CONTAINER_DESKTOP && |
| 250 | mLauncher.getWorkspace().getDragInfo() != null) { |
| 251 | return mLauncher.getWorkspace().getDragInfo().cell; |
| 252 | } |
| 253 | return null; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id, |
| 258 | * otherwise return {@code INVALID_APPWIDGET_ID} |
| 259 | */ |
| 260 | private int getReconfigurableWidgetId(View view) { |
| 261 | if (!(view instanceof AppWidgetHostView)) { |
| 262 | return INVALID_APPWIDGET_ID; |
| 263 | } |
| 264 | AppWidgetHostView hostView = (AppWidgetHostView) view; |
| 265 | AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo(); |
| 266 | if (widgetInfo == null || widgetInfo.configure == null) { |
| 267 | return INVALID_APPWIDGET_ID; |
| 268 | } |
| 269 | if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo) |
| 270 | .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) { |
| 271 | return INVALID_APPWIDGET_ID; |
| 272 | } |
| 273 | return hostView.getAppWidgetId(); |
| 274 | } |
| 275 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 276 | /** |
| 277 | * Performs the drop action and returns the target component for the dragObject or null if |
| 278 | * the action was not performed. |
| 279 | */ |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 280 | protected ComponentName performDropAction(View view, ItemInfo info) { |
| 281 | if (mCurrentAccessibilityAction == RECONFIGURE) { |
| 282 | int widgetId = getReconfigurableWidgetId(view); |
| 283 | if (widgetId != INVALID_APPWIDGET_ID) { |
| 284 | mLauncher.getAppWidgetHost().startConfigActivity(mLauncher, widgetId, -1); |
| 285 | } |
| 286 | return null; |
| 287 | } |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 288 | if (mCurrentAccessibilityAction == DISMISS_PREDICTION) { |
Sunny Goyal | 852537f | 2020-07-15 17:02:16 -0700 | [diff] [blame^] | 289 | // We sent the log event, nothing else left to do |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 290 | return null; |
| 291 | } |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 292 | // else: mCurrentAccessibilityAction == UNINSTALL |
| 293 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 294 | ComponentName cn = getUninstallTarget(info); |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 295 | if (cn == null) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 296 | // System applications cannot be installed. For now, show a toast explaining that. |
| 297 | // We may give them the option of disabling apps this way. |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 298 | 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] | 299 | return null; |
| 300 | } |
| 301 | try { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 302 | Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0) |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 303 | .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName())) |
| 304 | .putExtra(Intent.EXTRA_USER, info.user); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 305 | mLauncher.startActivity(i); |
Samuel Fufa | cc1e107 | 2019-09-06 16:19:11 -0700 | [diff] [blame] | 306 | FileLog.d(TAG, "start uninstall activity " + cn.getPackageName()); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 307 | return cn; |
| 308 | } catch (URISyntaxException e) { |
| 309 | Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info); |
| 310 | return null; |
| 311 | } |
| 312 | } |
| 313 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 314 | @Override |
| 315 | public void onAccessibilityDrop(View view, ItemInfo item) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 316 | performDropAction(view, item); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /** |
| 320 | * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until |
| 321 | * {@link #onLauncherResume} |
| 322 | */ |
| 323 | private class DeferredOnComplete implements DragSource, OnResumeCallback { |
| 324 | |
| 325 | private final DragSource mOriginal; |
| 326 | private final Context mContext; |
| 327 | |
| 328 | private String mPackageName; |
| 329 | private DragObject mDragObject; |
| 330 | |
| 331 | public DeferredOnComplete(DragSource original, Context context) { |
| 332 | mOriginal = original; |
| 333 | mContext = context; |
| 334 | } |
| 335 | |
| 336 | @Override |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 337 | public void onDropCompleted(View target, DragObject d, |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 338 | boolean success) { |
| 339 | mDragObject = d; |
| 340 | } |
| 341 | |
| 342 | @Override |
Samuel Fufa | a579ddc | 2020-02-27 16:59:19 -0800 | [diff] [blame] | 343 | public void fillInLogContainerData(ItemInfo childInfo, Target child, |
| 344 | ArrayList<Target> parents) { |
| 345 | mOriginal.fillInLogContainerData(childInfo, child, parents); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public void onLauncherResume() { |
| 350 | // 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] | 351 | if (new PackageManagerHelper(mContext).getApplicationInfo(mPackageName, |
| 352 | mDragObject.dragInfo.user, PackageManager.MATCH_UNINSTALLED_PACKAGES) == null) { |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 353 | mDragObject.dragSource = mOriginal; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 354 | mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true); |
thiruram | c6a38ba | 2020-06-16 18:58:13 -0700 | [diff] [blame] | 355 | mStatsLogManager.logger().withInstanceId(mDragObject.logInstanceId) |
| 356 | .log(LAUNCHER_ITEM_UNINSTALL_COMPLETED); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 357 | } else { |
| 358 | sendFailure(); |
thiruram | c6a38ba | 2020-06-16 18:58:13 -0700 | [diff] [blame] | 359 | mStatsLogManager.logger().withInstanceId(mDragObject.logInstanceId) |
| 360 | .log(LAUNCHER_ITEM_UNINSTALL_CANCELLED); |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 361 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 362 | } |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 363 | |
| 364 | public void sendFailure() { |
| 365 | mDragObject.dragSource = mOriginal; |
| 366 | mDragObject.cancelled = true; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 367 | mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false); |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 368 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 369 | } |
| 370 | } |