blob: 76e85e255b43316831e04674a35eb7aa7a5211ec [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;
5
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;
13import android.appwidget.AppWidgetManager;
14import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070015import android.content.ComponentName;
16import android.content.Context;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080017import android.content.Intent;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070018import android.content.pm.ApplicationInfo;
Sunny Goyal3e9be432017-01-05 15:22:41 -080019import android.content.pm.LauncherActivityInfo;
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 Goyal8e0e1d72016-10-10 10:41:41 -070032import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070033import com.android.launcher3.dragndrop.DragOptions;
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 &&
155 item.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) {
156 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();
184 mLauncher.setOnResumeCallback(deferred);
185 } 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);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700245 return cn;
246 } catch (URISyntaxException e) {
247 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
248 return null;
249 }
250 }
251
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700252 @Override
253 public void onAccessibilityDrop(View view, ItemInfo item) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000254 performDropAction(view, item);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700255 }
256
257 /**
258 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
259 * {@link #onLauncherResume}
260 */
261 private class DeferredOnComplete implements DragSource, OnResumeCallback {
262
263 private final DragSource mOriginal;
264 private final Context mContext;
265
266 private String mPackageName;
267 private DragObject mDragObject;
268
269 public DeferredOnComplete(DragSource original, Context context) {
270 mOriginal = original;
271 mContext = context;
272 }
273
274 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700275 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700276 boolean success) {
277 mDragObject = d;
278 }
279
280 @Override
281 public void fillInLogContainerData(View v, ItemInfo info, Target target,
282 Target targetParent) {
283 mOriginal.fillInLogContainerData(v, info, target, targetParent);
284 }
285
286 @Override
287 public void onLauncherResume() {
288 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
289 if (LauncherAppsCompat.getInstance(mContext)
290 .getApplicationInfo(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES,
291 mDragObject.dragInfo.user) == null) {
292 mDragObject.dragSource = mOriginal;
Winson Chung1054d4e2018-03-05 19:39:21 +0000293 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700294 } else {
295 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700296 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700297 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700298
299 public void sendFailure() {
300 mDragObject.dragSource = mOriginal;
301 mDragObject.cancelled = true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000302 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800303 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700304 }
305}