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; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame^] | 10 | import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_MASK; |
| 11 | import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_SYSTEM_NO; |
Sunny Goyal | 076839c | 2017-10-30 13:52:20 -0700 | [diff] [blame] | 12 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 13 | import android.appwidget.AppWidgetHostView; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 14 | import android.appwidget.AppWidgetProviderInfo; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 15 | import android.content.ComponentName; |
| 16 | import android.content.Context; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 17 | import android.content.Intent; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 18 | import android.content.pm.ApplicationInfo; |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 19 | import android.content.pm.LauncherActivityInfo; |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 20 | import android.content.pm.LauncherApps; |
Sunny Goyal | f2db253 | 2017-03-01 17:27:16 -0800 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 22 | import android.net.Uri; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 23 | import android.os.Bundle; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 24 | import android.os.UserHandle; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 25 | import android.os.UserManager; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 26 | import android.util.ArrayMap; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 27 | import android.util.AttributeSet; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 28 | import android.util.Log; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 29 | import android.view.View; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 30 | import android.widget.Toast; |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 31 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 32 | import com.android.launcher3.Launcher.OnResumeCallback; |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 33 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 34 | import com.android.launcher3.dragndrop.DragOptions; |
Samuel Fufa | cc1e107 | 2019-09-06 16:19:11 -0700 | [diff] [blame] | 35 | import com.android.launcher3.logging.FileLog; |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 36 | import com.android.launcher3.logging.LoggerUtils; |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 37 | import com.android.launcher3.model.AppLaunchTracker; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame^] | 38 | import com.android.launcher3.model.data.ItemInfo; |
| 39 | import com.android.launcher3.model.data.ItemInfoWithIcon; |
| 40 | import com.android.launcher3.model.data.LauncherAppWidgetInfo; |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 41 | import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 42 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 43 | import com.android.launcher3.util.PackageManagerHelper; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 44 | import com.android.launcher3.util.Themes; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 45 | |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 46 | import java.net.URISyntaxException; |
Samuel Fufa | a579ddc | 2020-02-27 16:59:19 -0800 | [diff] [blame] | 47 | import java.util.ArrayList; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 48 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Drop target which provides a secondary option for an item. |
| 51 | * For app targets: shows as uninstall |
| 52 | * For configurable widgets: shows as setup |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 53 | * For predicted app icons: don't suggest app |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 54 | */ |
| 55 | public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 56 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 57 | private static final String TAG = "SecondaryDropTarget"; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 58 | |
| 59 | private static final long CACHE_EXPIRE_TIMEOUT = 5000; |
| 60 | private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1); |
| 61 | |
| 62 | private final Alarm mCacheExpireAlarm; |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 63 | private boolean mHadPendingAlarm; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 64 | |
Mehdi Alizadeh | 8517b69 | 2018-04-23 15:55:26 -0700 | [diff] [blame] | 65 | protected int mCurrentAccessibilityAction = -1; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 66 | public SecondaryDropTarget(Context context, AttributeSet attrs) { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 67 | this(context, attrs, 0); |
| 68 | } |
| 69 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 70 | public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 71 | super(context, attrs, defStyle); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 72 | |
| 73 | mCacheExpireAlarm = new Alarm(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 77 | protected void onAttachedToWindow() { |
| 78 | super.onAttachedToWindow(); |
| 79 | if (mHadPendingAlarm) { |
| 80 | mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 81 | mCacheExpireAlarm.setOnAlarmListener(this); |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 82 | mHadPendingAlarm = false; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | protected void onDetachedFromWindow() { |
| 88 | super.onDetachedFromWindow(); |
| 89 | if (mCacheExpireAlarm.alarmPending()) { |
| 90 | mCacheExpireAlarm.cancelAlarm(); |
vadimt | 908e9bb | 2020-02-28 18:02:30 -0800 | [diff] [blame] | 91 | mCacheExpireAlarm.setOnAlarmListener(null); |
vadimt | 3d64ffd | 2020-02-27 11:28:47 -0800 | [diff] [blame] | 92 | mHadPendingAlarm = true; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @Override |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 97 | protected void onFinishInflate() { |
| 98 | super.onFinishInflate(); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 99 | setupUi(UNINSTALL); |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Mehdi Alizadeh | 8517b69 | 2018-04-23 15:55:26 -0700 | [diff] [blame] | 102 | protected void setupUi(int action) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 103 | if (action == mCurrentAccessibilityAction) { |
| 104 | return; |
| 105 | } |
| 106 | mCurrentAccessibilityAction = action; |
| 107 | |
| 108 | if (action == UNINSTALL) { |
| 109 | mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint); |
| 110 | setDrawable(R.drawable.ic_uninstall_shadow); |
| 111 | updateText(R.string.uninstall_drop_target_label); |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 112 | } else if (action == DISMISS_PREDICTION) { |
| 113 | mHoverColor = Themes.getColorAccent(getContext()); |
Tony Wickham | bd9754d | 2020-03-27 20:11:16 -0700 | [diff] [blame] | 114 | setDrawable(R.drawable.ic_block_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) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 117 | mHoverColor = Themes.getColorAccent(getContext()); |
| 118 | setDrawable(R.drawable.ic_setup_shadow); |
| 119 | updateText(R.string.gadget_setup_text); |
| 120 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 124 | public void onAlarm(Alarm alarm) { |
| 125 | mUninstallDisabledCache.clear(); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 128 | @Override |
| 129 | public int getAccessibilityAction() { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 130 | return mCurrentAccessibilityAction; |
| 131 | } |
| 132 | |
| 133 | @Override |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 134 | public Target getDropTargetForLogging() { |
| 135 | Target t = LoggerUtils.newTarget(Target.Type.CONTROL); |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 136 | if (mCurrentAccessibilityAction == UNINSTALL) { |
| 137 | t.controlType = ControlType.UNINSTALL_TARGET; |
| 138 | } else if (mCurrentAccessibilityAction == DISMISS_PREDICTION) { |
| 139 | t.controlType = ControlType.DISMISS_PREDICTION; |
| 140 | } else { |
| 141 | t.controlType = ControlType.SETTINGS_BUTTON; |
| 142 | } |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 143 | return t; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | protected boolean supportsDrop(ItemInfo info) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 148 | return supportsAccessibilityDrop(info, getViewUnderDrag(info)); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean supportsAccessibilityDrop(ItemInfo info, View view) { |
| 153 | if (view instanceof AppWidgetHostView) { |
| 154 | if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) { |
| 155 | setupUi(RECONFIGURE); |
| 156 | return true; |
| 157 | } |
| 158 | return false; |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 159 | } else if (FeatureFlags.ENABLE_PREDICTION_DISMISS.get() && info.isPredictedItem()) { |
| 160 | setupUi(DISMISS_PREDICTION); |
| 161 | return true; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | setupUi(UNINSTALL); |
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) { |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 178 | return false; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Sunny Goyal | 076839c | 2017-10-30 13:52:20 -0700 | [diff] [blame] | 181 | if (info instanceof ItemInfoWithIcon) { |
| 182 | ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info; |
| 183 | if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) { |
| 184 | return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0; |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 187 | return getUninstallTarget(info) != null; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 191 | * @return the component name that should be uninstalled or null. |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 192 | */ |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 193 | private ComponentName getUninstallTarget(ItemInfo item) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 194 | Intent intent = null; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 195 | UserHandle user = null; |
Sunny Goyal | 1fe0c2c | 2017-08-15 12:54:42 -0700 | [diff] [blame] | 196 | if (item != null && |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 197 | item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Sunny Goyal | 1fe0c2c | 2017-08-15 12:54:42 -0700 | [diff] [blame] | 198 | intent = item.getIntent(); |
| 199 | user = item.user; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 200 | } |
| 201 | if (intent != null) { |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 202 | LauncherActivityInfo info = mLauncher.getSystemService(LauncherApps.class) |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 203 | .resolveActivity(intent, user); |
| 204 | if (info != null |
| 205 | && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 206 | return info.getComponentName(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | return null; |
| 210 | } |
| 211 | |
| 212 | @Override |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 213 | public void onDrop(DragObject d, DragOptions options) { |
| 214 | // Defer onComplete |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 215 | d.dragSource = new DeferredOnComplete(d.dragSource, getContext()); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 216 | super.onDrop(d, options); |
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(); |
Winson Chung | 0b70cd4 | 2019-06-17 22:34:33 -0700 | [diff] [blame] | 226 | mLauncher.addOnResumeCallback(deferred); |
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) { |
| 274 | AppLaunchTracker.INSTANCE.get(getContext()).onDismissApp(info.getTargetComponent(), |
| 275 | info.user, AppLaunchTracker.CONTAINER_PREDICTIONS); |
| 276 | return null; |
| 277 | } |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 278 | // else: mCurrentAccessibilityAction == UNINSTALL |
| 279 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 280 | ComponentName cn = getUninstallTarget(info); |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 281 | if (cn == null) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 282 | // System applications cannot be installed. For now, show a toast explaining that. |
| 283 | // We may give them the option of disabling apps this way. |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 284 | 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] | 285 | return null; |
| 286 | } |
| 287 | try { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 288 | Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0) |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 289 | .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName())) |
| 290 | .putExtra(Intent.EXTRA_USER, info.user); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 291 | mLauncher.startActivity(i); |
Samuel Fufa | cc1e107 | 2019-09-06 16:19:11 -0700 | [diff] [blame] | 292 | FileLog.d(TAG, "start uninstall activity " + cn.getPackageName()); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 293 | return cn; |
| 294 | } catch (URISyntaxException e) { |
| 295 | Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info); |
| 296 | return null; |
| 297 | } |
| 298 | } |
| 299 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 300 | @Override |
| 301 | public void onAccessibilityDrop(View view, ItemInfo 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 | */ |
| 309 | private class DeferredOnComplete implements DragSource, OnResumeCallback { |
| 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 | |
| 328 | @Override |
Samuel Fufa | a579ddc | 2020-02-27 16:59:19 -0800 | [diff] [blame] | 329 | public void fillInLogContainerData(ItemInfo childInfo, Target child, |
| 330 | ArrayList<Target> parents) { |
| 331 | mOriginal.fillInLogContainerData(childInfo, child, parents); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void onLauncherResume() { |
| 336 | // 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] | 337 | if (new PackageManagerHelper(mContext).getApplicationInfo(mPackageName, |
| 338 | mDragObject.dragInfo.user, PackageManager.MATCH_UNINSTALLED_PACKAGES) == null) { |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 339 | mDragObject.dragSource = mOriginal; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 340 | mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 341 | } else { |
| 342 | sendFailure(); |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 343 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 344 | } |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 345 | |
| 346 | public void sendFailure() { |
| 347 | mDragObject.dragSource = mOriginal; |
| 348 | mDragObject.cancelled = true; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 349 | mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false); |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 350 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 351 | } |
| 352 | } |