blob: 68a441ad9888c4c7bfa110b66652cfe18f7b9c53 [file] [log] [blame]
Sunny Goyalfa401a12015-04-10 13:45:42 -07001package com.android.launcher3;
2
Sunny Goyal076839c2017-10-30 13:52:20 -07003import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_MASK;
4import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_NO;
5
Sunny Goyalfa401a12015-04-10 13:45:42 -07006import android.content.ComponentName;
7import android.content.Context;
Sunny Goyald5bd67d2016-03-11 01:10:19 -08008import android.content.Intent;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07009import android.content.pm.ApplicationInfo;
Sunny Goyal3e9be432017-01-05 15:22:41 -080010import android.content.pm.LauncherActivityInfo;
Sunny Goyalf2db2532017-03-01 17:27:16 -080011import android.content.pm.PackageManager;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080012import android.net.Uri;
Sunny Goyalfa401a12015-04-10 13:45:42 -070013import android.os.Bundle;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080014import android.os.UserHandle;
Sunny Goyalfa401a12015-04-10 13:45:42 -070015import android.os.UserManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070016import android.util.ArrayMap;
Sunny Goyalfa401a12015-04-10 13:45:42 -070017import android.util.AttributeSet;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070018import android.util.Log;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070019import android.view.View;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080020import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070021
Sunny Goyal3dce5f32017-10-05 11:40:05 -070022import com.android.launcher3.Launcher.OnResumeCallback;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070023import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070024import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070025import com.android.launcher3.dragndrop.DragOptions;
26import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyalfa401a12015-04-10 13:45:42 -070027
Jon Mirandac56e3ff2017-08-23 12:13:24 -070028import java.net.URISyntaxException;
29
Sunny Goyal0236d0b2017-10-24 14:54:30 -070030public class UninstallDropTarget extends ButtonDropTarget implements OnAlarmListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070031
Jon Mirandac56e3ff2017-08-23 12:13:24 -070032 private static final String TAG = "UninstallDropTarget";
Sunny Goyal0236d0b2017-10-24 14:54:30 -070033
34 private static final long CACHE_EXPIRE_TIMEOUT = 5000;
35 private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
36
37 private final Alarm mCacheExpireAlarm;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070038
Sunny Goyalfa401a12015-04-10 13:45:42 -070039 public UninstallDropTarget(Context context, AttributeSet attrs) {
40 this(context, attrs, 0);
41 }
42
43 public UninstallDropTarget(Context context, AttributeSet attrs, int defStyle) {
44 super(context, attrs, defStyle);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070045
46 mCacheExpireAlarm = new Alarm();
47 mCacheExpireAlarm.setOnAlarmListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070048 }
49
50 @Override
51 protected void onFinishInflate() {
52 super.onFinishInflate();
Sunny Goyalda1dfa32017-04-26 22:34:49 -070053 setupUi();
54 }
55
56 protected void setupUi() {
Sunny Goyalfa401a12015-04-10 13:45:42 -070057 // Get the hover color
Sunny Goyal3a2698b2015-04-28 21:36:46 -070058 mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
Sunny Goyalda1dfa32017-04-26 22:34:49 -070059 setDrawable(R.drawable.ic_uninstall_shadow);
Sunny Goyalfa401a12015-04-10 13:45:42 -070060 }
61
62 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070063 public void onAlarm(Alarm alarm) {
64 mUninstallDisabledCache.clear();
Sunny Goyal1a70cef2015-04-22 11:29:51 -070065 }
66
Sunny Goyal0236d0b2017-10-24 14:54:30 -070067 @Override
68 public int getAccessibilityAction() {
69 return LauncherAccessibilityDelegate.UNINSTALL;
70 }
71
72 @Override
73 protected boolean supportsDrop(ItemInfo info) {
74 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
75 if (uninstallDisabled == null) {
76 UserManager userManager =
77 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
78 Bundle restrictions = userManager.getUserRestrictions(info.user);
79 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -070080 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070081 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -070082 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -070083 // Cancel any pending alarm and set cache expiry after some time
84 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
85 if (uninstallDisabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -080086 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -070087 }
88
Sunny Goyal076839c2017-10-30 13:52:20 -070089 if (info instanceof ItemInfoWithIcon) {
90 ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
91 if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) {
92 return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0;
Sunny Goyal6b0aa872017-09-29 07:54:37 -070093 }
94 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -070095 return getUninstallTarget(info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -070096 }
97
98 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070099 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -0700100 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700101 private ComponentName getUninstallTarget(ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700102 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800103 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700104 if (item != null &&
105 item.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) {
106 intent = item.getIntent();
107 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700108 }
109 if (intent != null) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700110 LauncherActivityInfo info = LauncherAppsCompat.getInstance(mLauncher)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700111 .resolveActivity(intent, user);
112 if (info != null
113 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
114 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700115 }
116 }
117 return null;
118 }
119
120 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700121 public void onDrop(DragObject d, DragOptions options) {
122 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700123 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700124 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700125 }
126
127 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800128 public void completeDrop(final DragObject d) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700129 ComponentName target = performDropAction(d.dragInfo);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700130 if (d.dragSource instanceof DeferredOnComplete) {
131 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
132 if (target != null) {
133 deferred.mPackageName = target.getPackageName();
134 mLauncher.setOnResumeCallback(deferred);
135 } else {
136 deferred.sendFailure();
137 }
138 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700139 }
140
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700141 /**
142 * Performs the drop action and returns the target component for the dragObject or null if
143 * the action was not performed.
144 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700145 protected ComponentName performDropAction(ItemInfo info) {
146 ComponentName cn = getUninstallTarget(info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700147 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800148 // System applications cannot be installed. For now, show a toast explaining that.
149 // We may give them the option of disabling apps this way.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700150 Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700151 return null;
152 }
153 try {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700154 Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700155 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
156 .putExtra(Intent.EXTRA_USER, info.user);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700157 mLauncher.startActivity(i);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700158 return cn;
159 } catch (URISyntaxException e) {
160 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
161 return null;
162 }
163 }
164
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700165 @Override
166 public void onAccessibilityDrop(View view, ItemInfo item) {
167 performDropAction(item);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700168 }
169
170 /**
171 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
172 * {@link #onLauncherResume}
173 */
174 private class DeferredOnComplete implements DragSource, OnResumeCallback {
175
176 private final DragSource mOriginal;
177 private final Context mContext;
178
179 private String mPackageName;
180 private DragObject mDragObject;
181
182 public DeferredOnComplete(DragSource original, Context context) {
183 mOriginal = original;
184 mContext = context;
185 }
186
187 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700188 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700189 boolean success) {
190 mDragObject = d;
191 }
192
193 @Override
194 public void fillInLogContainerData(View v, ItemInfo info, Target target,
195 Target targetParent) {
196 mOriginal.fillInLogContainerData(v, info, target, targetParent);
197 }
198
199 @Override
200 public void onLauncherResume() {
201 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
202 if (LauncherAppsCompat.getInstance(mContext)
203 .getApplicationInfo(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES,
204 mDragObject.dragInfo.user) == null) {
205 mDragObject.dragSource = mOriginal;
Sunny Goyal1797af42017-10-06 13:29:57 -0700206 mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, true);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700207 } else {
208 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700209 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700210 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700211
212 public void sendFailure() {
213 mDragObject.dragSource = mOriginal;
214 mDragObject.cancelled = true;
Sunny Goyal1797af42017-10-06 13:29:57 -0700215 mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800216 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700217 }
218}