blob: c8c590d9de81317bde5fd27162671adc8d8ad0f0 [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 Goyalf2db2532017-03-01 17:27:16 -080019import android.content.pm.PackageManager;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080020import android.net.Uri;
Sunny Goyalfa401a12015-04-10 13:45:42 -070021import android.os.Bundle;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080022import android.os.UserHandle;
Sunny Goyalfa401a12015-04-10 13:45:42 -070023import android.os.UserManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070024import android.util.ArrayMap;
Sunny Goyalfa401a12015-04-10 13:45:42 -070025import android.util.AttributeSet;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070026import android.util.Log;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070027import android.view.View;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080028import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070029
Sunny Goyal3dce5f32017-10-05 11:40:05 -070030import com.android.launcher3.Launcher.OnResumeCallback;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070031import com.android.launcher3.compat.LauncherAppsCompat;
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;
Winson Chung1054d4e2018-03-05 19:39:21 +000037import com.android.launcher3.util.Themes;
Sunny Goyalfa401a12015-04-10 13:45:42 -070038
Jon Mirandac56e3ff2017-08-23 12:13:24 -070039import java.net.URISyntaxException;
40
Winson Chung1054d4e2018-03-05 19:39:21 +000041/**
42 * Drop target which provides a secondary option for an item.
43 * For app targets: shows as uninstall
44 * For configurable widgets: shows as setup
45 */
46public class SecondaryDropTarget extends ButtonDropTarget implements OnAlarmListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070047
Winson Chung1054d4e2018-03-05 19:39:21 +000048 private static final String TAG = "SecondaryDropTarget";
Sunny Goyal0236d0b2017-10-24 14:54:30 -070049
50 private static final long CACHE_EXPIRE_TIMEOUT = 5000;
51 private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
52
53 private final Alarm mCacheExpireAlarm;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070054
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070055 protected int mCurrentAccessibilityAction = -1;
Winson Chung1054d4e2018-03-05 19:39:21 +000056 public SecondaryDropTarget(Context context, AttributeSet attrs) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070057 this(context, attrs, 0);
58 }
59
Winson Chung1054d4e2018-03-05 19:39:21 +000060 public SecondaryDropTarget(Context context, AttributeSet attrs, int defStyle) {
Sunny Goyalfa401a12015-04-10 13:45:42 -070061 super(context, attrs, defStyle);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070062
63 mCacheExpireAlarm = new Alarm();
64 mCacheExpireAlarm.setOnAlarmListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070065 }
66
67 @Override
68 protected void onFinishInflate() {
69 super.onFinishInflate();
Winson Chung1054d4e2018-03-05 19:39:21 +000070 setupUi(UNINSTALL);
Sunny Goyalda1dfa32017-04-26 22:34:49 -070071 }
72
Mehdi Alizadeh8517b692018-04-23 15:55:26 -070073 protected void setupUi(int action) {
Winson Chung1054d4e2018-03-05 19:39:21 +000074 if (action == mCurrentAccessibilityAction) {
75 return;
76 }
77 mCurrentAccessibilityAction = action;
78
79 if (action == UNINSTALL) {
80 mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
81 setDrawable(R.drawable.ic_uninstall_shadow);
82 updateText(R.string.uninstall_drop_target_label);
83 } else {
84 mHoverColor = Themes.getColorAccent(getContext());
85 setDrawable(R.drawable.ic_setup_shadow);
86 updateText(R.string.gadget_setup_text);
87 }
Sunny Goyalfa401a12015-04-10 13:45:42 -070088 }
89
90 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070091 public void onAlarm(Alarm alarm) {
92 mUninstallDisabledCache.clear();
Sunny Goyal1a70cef2015-04-22 11:29:51 -070093 }
94
Sunny Goyal0236d0b2017-10-24 14:54:30 -070095 @Override
96 public int getAccessibilityAction() {
Winson Chung1054d4e2018-03-05 19:39:21 +000097 return mCurrentAccessibilityAction;
98 }
99
100 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700101 public Target getDropTargetForLogging() {
102 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
103 t.controlType = mCurrentAccessibilityAction == UNINSTALL ? ControlType.UNINSTALL_TARGET
104 : ControlType.SETTINGS_BUTTON;
105 return t;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700106 }
107
108 @Override
109 protected boolean supportsDrop(ItemInfo info) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000110 return supportsAccessibilityDrop(info, getViewUnderDrag(info));
111 }
112
113 @Override
114 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
115 if (view instanceof AppWidgetHostView) {
116 if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) {
117 setupUi(RECONFIGURE);
118 return true;
119 }
120 return false;
121 }
122
123 setupUi(UNINSTALL);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700124 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
125 if (uninstallDisabled == null) {
126 UserManager userManager =
127 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
128 Bundle restrictions = userManager.getUserRestrictions(info.user);
129 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700130 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700131 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700132 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700133 // Cancel any pending alarm and set cache expiry after some time
134 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
135 if (uninstallDisabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800136 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700137 }
138
Sunny Goyal076839c2017-10-30 13:52:20 -0700139 if (info instanceof ItemInfoWithIcon) {
140 ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
141 if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) {
142 return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0;
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700143 }
144 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700145 return getUninstallTarget(info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700146 }
147
148 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700149 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -0700150 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700151 private ComponentName getUninstallTarget(ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700152 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800153 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700154 if (item != null &&
Sunny Goyalc5939392018-12-07 11:43:47 -0800155 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700156 intent = item.getIntent();
157 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700158 }
159 if (intent != null) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700160 LauncherActivityInfo info = LauncherAppsCompat.getInstance(mLauncher)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700161 .resolveActivity(intent, user);
162 if (info != null
163 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
164 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700165 }
166 }
167 return null;
168 }
169
170 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700171 public void onDrop(DragObject d, DragOptions options) {
172 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700173 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700174 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700175 }
176
177 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800178 public void completeDrop(final DragObject d) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000179 ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700180 if (d.dragSource instanceof DeferredOnComplete) {
181 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
182 if (target != null) {
183 deferred.mPackageName = target.getPackageName();
Winson Chung0b70cd42019-06-17 22:34:33 -0700184 mLauncher.addOnResumeCallback(deferred);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700185 } else {
186 deferred.sendFailure();
187 }
188 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700189 }
190
Winson Chung1054d4e2018-03-05 19:39:21 +0000191 private View getViewUnderDrag(ItemInfo info) {
192 if (info instanceof LauncherAppWidgetInfo && info.container == CONTAINER_DESKTOP &&
193 mLauncher.getWorkspace().getDragInfo() != null) {
194 return mLauncher.getWorkspace().getDragInfo().cell;
195 }
196 return null;
197 }
198
199 /**
200 * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id,
201 * otherwise return {@code INVALID_APPWIDGET_ID}
202 */
203 private int getReconfigurableWidgetId(View view) {
204 if (!(view instanceof AppWidgetHostView)) {
205 return INVALID_APPWIDGET_ID;
206 }
207 AppWidgetHostView hostView = (AppWidgetHostView) view;
208 AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo();
209 if (widgetInfo == null || widgetInfo.configure == null) {
210 return INVALID_APPWIDGET_ID;
211 }
212 if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo)
213 .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) {
214 return INVALID_APPWIDGET_ID;
215 }
216 return hostView.getAppWidgetId();
217 }
218
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700219 /**
220 * Performs the drop action and returns the target component for the dragObject or null if
221 * the action was not performed.
222 */
Winson Chung1054d4e2018-03-05 19:39:21 +0000223 protected ComponentName performDropAction(View view, ItemInfo info) {
224 if (mCurrentAccessibilityAction == RECONFIGURE) {
225 int widgetId = getReconfigurableWidgetId(view);
226 if (widgetId != INVALID_APPWIDGET_ID) {
227 mLauncher.getAppWidgetHost().startConfigActivity(mLauncher, widgetId, -1);
228 }
229 return null;
230 }
231 // else: mCurrentAccessibilityAction == UNINSTALL
232
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700233 ComponentName cn = getUninstallTarget(info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700234 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800235 // System applications cannot be installed. For now, show a toast explaining that.
236 // We may give them the option of disabling apps this way.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700237 Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700238 return null;
239 }
240 try {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700241 Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700242 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
243 .putExtra(Intent.EXTRA_USER, info.user);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700244 mLauncher.startActivity(i);
Samuel Fufacc1e1072019-09-06 16:19:11 -0700245 FileLog.d(TAG, "start uninstall activity " + cn.getPackageName());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700246 return cn;
247 } catch (URISyntaxException e) {
248 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
249 return null;
250 }
251 }
252
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700253 @Override
254 public void onAccessibilityDrop(View view, ItemInfo item) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000255 performDropAction(view, item);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700256 }
257
258 /**
259 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
260 * {@link #onLauncherResume}
261 */
262 private class DeferredOnComplete implements DragSource, OnResumeCallback {
263
264 private final DragSource mOriginal;
265 private final Context mContext;
266
267 private String mPackageName;
268 private DragObject mDragObject;
269
270 public DeferredOnComplete(DragSource original, Context context) {
271 mOriginal = original;
272 mContext = context;
273 }
274
275 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700276 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700277 boolean success) {
278 mDragObject = d;
279 }
280
281 @Override
282 public void fillInLogContainerData(View v, ItemInfo info, Target target,
283 Target targetParent) {
284 mOriginal.fillInLogContainerData(v, info, target, targetParent);
285 }
286
287 @Override
288 public void onLauncherResume() {
289 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
290 if (LauncherAppsCompat.getInstance(mContext)
291 .getApplicationInfo(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES,
292 mDragObject.dragInfo.user) == null) {
293 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}