blob: 024b4eb5931cadbecc44fe496c19edd7d2d4dcea [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;
11import static com.android.launcher3.userevent.nano.LauncherLogProto.ControlType.SETTINGS_BUTTON;
12import static com.android.launcher3.userevent.nano.LauncherLogProto.ControlType.UNINSTALL_TARGET;
Sunny Goyal076839c2017-10-30 13:52:20 -070013
Winson Chung1054d4e2018-03-05 19:39:21 +000014import android.appwidget.AppWidgetHostView;
15import android.appwidget.AppWidgetManager;
16import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070017import android.content.ComponentName;
18import android.content.Context;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080019import android.content.Intent;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070020import android.content.pm.ApplicationInfo;
Sunny Goyal3e9be432017-01-05 15:22:41 -080021import android.content.pm.LauncherActivityInfo;
Sunny Goyalf2db2532017-03-01 17:27:16 -080022import android.content.pm.PackageManager;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080023import android.net.Uri;
Sunny Goyalfa401a12015-04-10 13:45:42 -070024import android.os.Bundle;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080025import android.os.UserHandle;
Sunny Goyalfa401a12015-04-10 13:45:42 -070026import android.os.UserManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070027import android.util.ArrayMap;
Sunny Goyalfa401a12015-04-10 13:45:42 -070028import android.util.AttributeSet;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070029import android.util.Log;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070030import android.view.View;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080031import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070032
Sunny Goyal3dce5f32017-10-05 11:40:05 -070033import com.android.launcher3.Launcher.OnResumeCallback;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070034import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070035import com.android.launcher3.dragndrop.DragOptions;
36import 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
Winson Chung1054d4e2018-03-05 19:39:21 +000055 private int mCurrentAccessibilityAction = -1;
56 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
Winson Chung1054d4e2018-03-05 19:39:21 +000073 private void setupUi(int action) {
74 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
101 public int getControlTypeForLogging() {
102 return mCurrentAccessibilityAction == UNINSTALL ? UNINSTALL_TARGET : SETTINGS_BUTTON;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700103 }
104
105 @Override
106 protected boolean supportsDrop(ItemInfo info) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000107 return supportsAccessibilityDrop(info, getViewUnderDrag(info));
108 }
109
110 @Override
111 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
112 if (view instanceof AppWidgetHostView) {
113 if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) {
114 setupUi(RECONFIGURE);
115 return true;
116 }
117 return false;
118 }
119
120 setupUi(UNINSTALL);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700121 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
122 if (uninstallDisabled == null) {
123 UserManager userManager =
124 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
125 Bundle restrictions = userManager.getUserRestrictions(info.user);
126 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700127 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700128 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700129 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700130 // Cancel any pending alarm and set cache expiry after some time
131 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
132 if (uninstallDisabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800133 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700134 }
135
Sunny Goyal076839c2017-10-30 13:52:20 -0700136 if (info instanceof ItemInfoWithIcon) {
137 ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
138 if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) {
139 return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0;
Sunny Goyal6b0aa872017-09-29 07:54:37 -0700140 }
141 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700142 return getUninstallTarget(info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700143 }
144
145 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700146 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -0700147 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700148 private ComponentName getUninstallTarget(ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700149 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800150 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700151 if (item != null &&
152 item.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) {
153 intent = item.getIntent();
154 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700155 }
156 if (intent != null) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700157 LauncherActivityInfo info = LauncherAppsCompat.getInstance(mLauncher)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700158 .resolveActivity(intent, user);
159 if (info != null
160 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
161 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700162 }
163 }
164 return null;
165 }
166
167 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700168 public void onDrop(DragObject d, DragOptions options) {
169 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700170 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700171 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700172 }
173
174 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800175 public void completeDrop(final DragObject d) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000176 ComponentName target = performDropAction(getViewUnderDrag(d.dragInfo), d.dragInfo);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700177 if (d.dragSource instanceof DeferredOnComplete) {
178 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
179 if (target != null) {
180 deferred.mPackageName = target.getPackageName();
181 mLauncher.setOnResumeCallback(deferred);
182 } else {
183 deferred.sendFailure();
184 }
185 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700186 }
187
Winson Chung1054d4e2018-03-05 19:39:21 +0000188 private View getViewUnderDrag(ItemInfo info) {
189 if (info instanceof LauncherAppWidgetInfo && info.container == CONTAINER_DESKTOP &&
190 mLauncher.getWorkspace().getDragInfo() != null) {
191 return mLauncher.getWorkspace().getDragInfo().cell;
192 }
193 return null;
194 }
195
196 /**
197 * Verifies that the view is an reconfigurable widget and returns the corresponding widget Id,
198 * otherwise return {@code INVALID_APPWIDGET_ID}
199 */
200 private int getReconfigurableWidgetId(View view) {
201 if (!(view instanceof AppWidgetHostView)) {
202 return INVALID_APPWIDGET_ID;
203 }
204 AppWidgetHostView hostView = (AppWidgetHostView) view;
205 AppWidgetProviderInfo widgetInfo = hostView.getAppWidgetInfo();
206 if (widgetInfo == null || widgetInfo.configure == null) {
207 return INVALID_APPWIDGET_ID;
208 }
209 if ( (LauncherAppWidgetProviderInfo.fromProviderInfo(getContext(), widgetInfo)
210 .getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) == 0) {
211 return INVALID_APPWIDGET_ID;
212 }
213 return hostView.getAppWidgetId();
214 }
215
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700216 /**
217 * Performs the drop action and returns the target component for the dragObject or null if
218 * the action was not performed.
219 */
Winson Chung1054d4e2018-03-05 19:39:21 +0000220 protected ComponentName performDropAction(View view, ItemInfo info) {
221 if (mCurrentAccessibilityAction == RECONFIGURE) {
222 int widgetId = getReconfigurableWidgetId(view);
223 if (widgetId != INVALID_APPWIDGET_ID) {
224 mLauncher.getAppWidgetHost().startConfigActivity(mLauncher, widgetId, -1);
225 }
226 return null;
227 }
228 // else: mCurrentAccessibilityAction == UNINSTALL
229
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700230 ComponentName cn = getUninstallTarget(info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700231 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800232 // System applications cannot be installed. For now, show a toast explaining that.
233 // We may give them the option of disabling apps this way.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700234 Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700235 return null;
236 }
237 try {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700238 Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700239 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
240 .putExtra(Intent.EXTRA_USER, info.user);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700241 mLauncher.startActivity(i);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700242 return cn;
243 } catch (URISyntaxException e) {
244 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
245 return null;
246 }
247 }
248
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700249 @Override
250 public void onAccessibilityDrop(View view, ItemInfo item) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000251 performDropAction(view, item);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700252 }
253
254 /**
255 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
256 * {@link #onLauncherResume}
257 */
258 private class DeferredOnComplete implements DragSource, OnResumeCallback {
259
260 private final DragSource mOriginal;
261 private final Context mContext;
262
263 private String mPackageName;
264 private DragObject mDragObject;
265
266 public DeferredOnComplete(DragSource original, Context context) {
267 mOriginal = original;
268 mContext = context;
269 }
270
271 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700272 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700273 boolean success) {
274 mDragObject = d;
275 }
276
277 @Override
278 public void fillInLogContainerData(View v, ItemInfo info, Target target,
279 Target targetParent) {
280 mOriginal.fillInLogContainerData(v, info, target, targetParent);
281 }
282
283 @Override
284 public void onLauncherResume() {
285 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
286 if (LauncherAppsCompat.getInstance(mContext)
287 .getApplicationInfo(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES,
288 mDragObject.dragInfo.user) == null) {
289 mDragObject.dragSource = mOriginal;
Winson Chung1054d4e2018-03-05 19:39:21 +0000290 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, true);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700291 } else {
292 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700293 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700294 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700295
296 public void sendFailure() {
297 mDragObject.dragSource = mOriginal;
298 mDragObject.cancelled = true;
Winson Chung1054d4e2018-03-05 19:39:21 +0000299 mOriginal.onDropCompleted(SecondaryDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800300 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700301 }
302}