blob: 3c2ed725928875ec4f6e6980fbe7c80891c289c3 [file] [log] [blame]
Sunny Goyalfa401a12015-04-10 13:45:42 -07001package com.android.launcher3;
2
Winson Chung1054d4e2018-03-05 19:39:21 +00003import static android.appwidget.AppWidgetManager.INVALID_APPWIDGET_ID;
4import static android.appwidget.AppWidgetProviderInfo.WIDGET_FEATURE_RECONFIGURABLE;
Samuel Fufacc1e1072019-09-06 16:19:11 -07005
Sunny Goyal076839c2017-10-30 13:52:20 -07006import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_MASK;
7import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_NO;
Winson Chung1054d4e2018-03-05 19:39:21 +00008import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP;
9import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.RECONFIGURE;
10import static com.android.launcher3.accessibility.LauncherAccessibilityDelegate.UNINSTALL;
Sunny Goyal076839c2017-10-30 13:52:20 -070011
Winson Chung1054d4e2018-03-05 19:39:21 +000012import android.appwidget.AppWidgetHostView;
Winson Chung1054d4e2018-03-05 19:39:21 +000013import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070014import android.content.ComponentName;
15import android.content.Context;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080016import android.content.Intent;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070017import android.content.pm.ApplicationInfo;
Sunny Goyal3e9be432017-01-05 15:22:41 -080018import android.content.pm.LauncherActivityInfo;
Sunny Goyale7b00122019-10-02 16:13:34 -070019import android.content.pm.LauncherApps;
Sunny Goyalf2db2532017-03-01 17:27:16 -080020import android.content.pm.PackageManager;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080021import android.net.Uri;
Sunny Goyalfa401a12015-04-10 13:45:42 -070022import android.os.Bundle;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080023import android.os.UserHandle;
Sunny Goyalfa401a12015-04-10 13:45:42 -070024import android.os.UserManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070025import android.util.ArrayMap;
Sunny Goyalfa401a12015-04-10 13:45:42 -070026import android.util.AttributeSet;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070027import android.util.Log;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070028import android.view.View;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080029import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070030
Sunny Goyal3dce5f32017-10-05 11:40:05 -070031import com.android.launcher3.Launcher.OnResumeCallback;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070032import com.android.launcher3.dragndrop.DragOptions;
Samuel Fufacc1e1072019-09-06 16:19:11 -070033import com.android.launcher3.logging.FileLog;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070034import com.android.launcher3.logging.LoggerUtils;
35import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070036import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyale7b00122019-10-02 16:13:34 -070037import com.android.launcher3.util.PackageManagerHelper;
Winson Chung1054d4e2018-03-05 19:39:21 +000038import com.android.launcher3.util.Themes;
Sunny Goyalfa401a12015-04-10 13:45:42 -070039
Jon Mirandac56e3ff2017-08-23 12:13:24 -070040import java.net.URISyntaxException;
41
Winson Chung1054d4e2018-03-05 19:39:21 +000042/**
43 * Drop target which provides a secondary option for an item.
44 * For app targets: shows as uninstall
45 * For configurable widgets: shows as setup
46 */
47public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070048
Winson Chung1054d4e2018-03-05 19:39:21 +000049 private static final String TAG = "SecondaryDropTarget";
Sunny Goyal0236d0b2017-10-24 14:54:30 -070050
51 private static final long CACHE_EXPIRE_TIMEOUT = 5000;
52 private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
53
54 private final Alarm mCacheExpireAlarm;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070055
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070056 protected int mCurrentAccessibilityAction = -1;
Winson Chung1054d4e2018-03-05 19:39:21 +000057 public SecondaryDropTarget(Context context, AttributeSet attrs) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070058 this(context, attrs, 0);
59 }
60
Winson Chung1054d4e2018-03-05 19:39:21 +000061 public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070062 super(context, attrs, defStyle);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070063
64 mCacheExpireAlarm = new Alarm();
65 mCacheExpireAlarm.setOnAlarmListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070066 }
67
68 @Override
69 protected void onFinishInflate() {
70 super.onFinishInflate();
Winson Chung1054d4e2018-03-05 19:39:21 +000071 setupUi(UNINSTALL);
Sunny Goyalda1dfa32017-04-26 22:34:49 -070072 }
73
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070074 protected void setupUi(int action) {
Winson Chung1054d4e2018-03-05 19:39:21 +000075 if (action == mCurrentAccessibilityAction) {
76 return;
77 }
78 mCurrentAccessibilityAction = action;
79
80 if (action == UNINSTALL) {
81 mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
82 setDrawable(R.drawable.ic_uninstall_shadow);
83 updateText(R.string.uninstall_drop_target_label);
84 } else {
85 mHoverColor = Themes.getColorAccent(getContext());
86 setDrawable(R.drawable.ic_setup_shadow);
87 updateText(R.string.gadget_setup_text);
88 }
Sunny Goyalfa401a12015-04-10 13:45:42 -070089 }
90
91 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070092 public void onAlarm(Alarm alarm) {
93 mUninstallDisabledCache.clear();
Sunny Goyal1a70cef2015-04-22 11:29:51 -070094 }
95
Sunny Goyal0236d0b2017-10-24 14:54:30 -070096 @Override
97 public int getAccessibilityAction() {
Winson Chung1054d4e2018-03-05 19:39:21 +000098 return mCurrentAccessibilityAction;
99 }
100
101 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700102 public Target getDropTargetForLogging() {
103 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
104 t.controlType = mCurrentAccessibilityAction == UNINSTALL ? ControlType.UNINSTALL_TARGET
105 : ControlType.SETTINGS_BUTTON;
106 return t;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700107 }
108
109 @Override
110 protected boolean supportsDrop(ItemInfo info) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000111 return supportsAccessibilityDrop(info, getViewUnderDrag(info));
112 }
113
114 @Override
115 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
116 if (view instanceof AppWidgetHostView) {
117 if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) {
118 setupUi(RECONFIGURE);
119 return true;
120 }
121 return false;
122 }
123
124 setupUi(UNINSTALL);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700125 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
126 if (uninstallDisabled == null) {
127 UserManager userManager =
128 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
129 Bundle restrictions = userManager.getUserRestrictions(info.user);
130 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700131 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700132 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700133 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700134 // Cancel any pending alarm and set cache expiry after some time
135 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
136 if (uninstallDisabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800137 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700138 }
139
Sunny Goyal076839c2017-10-30 13:52:20 -0700140 if (info instanceof ItemInfoWithIcon) {
141 ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
142 if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) {
143 return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0;
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700144 }
145 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700146 return getUninstallTarget(info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700147 }
148
149 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700150 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -0700151 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700152 private ComponentName getUninstallTarget(ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700153 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800154 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700155 if (item != null &&
Sunny Goyalc5939392018-12-07 11:43:47 -0800156 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700157 intent = item.getIntent();
158 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700159 }
160 if (intent != null) {
Sunny Goyale7b00122019-10-02 16:13:34 -0700161 LauncherActivityInfo info = mLauncher.getSystemService(LauncherApps.class)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700162 .resolveActivity(intent, user);
163 if (info != null
164 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
165 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700166 }
167 }
168 return null;
169 }
170
171 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700172 public void onDrop(DragObject d, DragOptions options) {
173 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700174 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700175 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700176 }
177
178 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800179 public void completeDrop(final DragObject d) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000180 ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700181 if (d.dragSource instanceof DeferredOnComplete) {
182 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
183 if (target != null) {
184 deferred.mPackageName = target.getPackageName();
Winson Chung0b70cd42019-06-17 22:34:33 -0700185 mLauncher.addOnResumeCallback(deferred);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700186 } else {
187 deferred.sendFailure();
188 }
189 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700190 }
191
Winson Chung1054d4e2018-03-05 19:39:21 +0000192 private View getViewUnderDrag(ItemInfo info) {
193 if (info instanceof LauncherAppWidgetInfo && info.container == CONTAINER_DESKTOP &&
194 mLauncher.getWorkspace().getDragInfo() != null) {
195 return mLauncher.getWorkspace().getDragInfo().cell;
196 }
197 return null;
198 }
199
200 /**
201 * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id,
202 * otherwise return {@code INVALID_APPWIDGET_ID}
203 */
204 private int getReconfigurableWidgetId(View view) {
205 if (!(view instanceof AppWidgetHostView)) {
206 return INVALID_APPWIDGET_ID;
207 }
208 AppWidgetHostView hostView = (AppWidgetHostView) view;
209 AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo();
210 if (widgetInfo == null || widgetInfo.configure == null) {
211 return INVALID_APPWIDGET_ID;
212 }
213 if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo)
214 .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) {
215 return INVALID_APPWIDGET_ID;
216 }
217 return hostView.getAppWidgetId();
218 }
219
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700220 /**
221 * Performs the drop action and returns the target component for the dragObject or null if
222 * the action was not performed.
223 */
Winson Chung1054d4e2018-03-05 19:39:21 +0000224 protected ComponentName performDropAction(View view, ItemInfo info) {
225 if (mCurrentAccessibilityAction == RECONFIGURE) {
226 int widgetId = getReconfigurableWidgetId(view);
227 if (widgetId != INVALID_APPWIDGET_ID) {
228 mLauncher.getAppWidgetHost().startConfigActivity(mLauncher, widgetId, -1);
229 }
230 return null;
231 }
232 // else: mCurrentAccessibilityAction == UNINSTALL
233
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700234 ComponentName cn = getUninstallTarget(info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700235 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800236 // System applications cannot be installed. For now, show a toast explaining that.
237 // We may give them the option of disabling apps this way.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700238 Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700239 return null;
240 }
241 try {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700242 Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700243 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
244 .putExtra(Intent.EXTRA_USER, info.user);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700245 mLauncher.startActivity(i);
Samuel Fufacc1e1072019-09-06 16:19:11 -0700246 FileLog.d(TAG, "start uninstall activity " + cn.getPackageName());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700247 return cn;
248 } catch (URISyntaxException e) {
249 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
250 return null;
251 }
252 }
253
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700254 @Override
255 public void onAccessibilityDrop(View view, ItemInfo item) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000256 performDropAction(view, item);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700257 }
258
259 /**
260 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
261 * {@link #onLauncherResume}
262 */
263 private class DeferredOnComplete implements DragSource, OnResumeCallback {
264
265 private final DragSource mOriginal;
266 private final Context mContext;
267
268 private String mPackageName;
269 private DragObject mDragObject;
270
271 public DeferredOnComplete(DragSource original, Context context) {
272 mOriginal = original;
273 mContext = context;
274 }
275
276 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700277 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700278 boolean success) {
279 mDragObject = d;
280 }
281
282 @Override
283 public void fillInLogContainerData(View v, ItemInfo info, Target target,
284 Target targetParent) {
285 mOriginal.fillInLogContainerData(v, info, target, targetParent);
286 }
287
288 @Override
289 public void onLauncherResume() {
290 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
Sunny Goyale7b00122019-10-02 16:13:34 -0700291 if (new PackageManagerHelper(mContext).getApplicationInfo(mPackageName,
292 mDragObject.dragInfo.user, PackageManager.MATCH_UNINSTALLED_PACKAGES) == null) {
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700293 mDragObject.dragSource = mOriginal;
Winson Chung1054d4e2018-03-05 19:39:21 +0000294 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700295 } else {
296 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700297 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700298 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700299
300 public void sendFailure() {
301 mDragObject.dragSource = mOriginal;
302 mDragObject.cancelled = true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000303 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800304 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700305 }
306}