blob: 8e83a30c0ad5372470d708ce2db6417a4ae2165d [file] [log] [blame]
Sunny Goyalfa401a12015-04-10 13:45:42 -07001package com.android.launcher3;
2
3import android.content.ComponentName;
4import android.content.Context;
Sunny Goyald5bd67d2016-03-11 01:10:19 -08005import android.content.Intent;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07006import android.content.pm.ApplicationInfo;
Sunny Goyal3e9be432017-01-05 15:22:41 -08007import android.content.pm.LauncherActivityInfo;
Sunny Goyalf2db2532017-03-01 17:27:16 -08008import android.content.pm.PackageManager;
Sunny Goyald5bd67d2016-03-11 01:10:19 -08009import android.net.Uri;
Sunny Goyalfa401a12015-04-10 13:45:42 -070010import android.os.Bundle;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080011import android.os.UserHandle;
Sunny Goyalfa401a12015-04-10 13:45:42 -070012import android.os.UserManager;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070013import android.util.ArrayMap;
Sunny Goyalfa401a12015-04-10 13:45:42 -070014import android.util.AttributeSet;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070015import android.util.Log;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070016import android.view.View;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080017import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070018
Sunny Goyal3dce5f32017-10-05 11:40:05 -070019import com.android.launcher3.Launcher.OnResumeCallback;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070020import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070021import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070022import com.android.launcher3.dragndrop.DragOptions;
23import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyalfa401a12015-04-10 13:45:42 -070024
Jon Mirandac56e3ff2017-08-23 12:13:24 -070025import java.net.URISyntaxException;
26
Sunny Goyal0236d0b2017-10-24 14:54:30 -070027public class UninstallDropTarget extends ButtonDropTarget implements OnAlarmListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070028
Jon Mirandac56e3ff2017-08-23 12:13:24 -070029 private static final String TAG = "UninstallDropTarget";
Sunny Goyal0236d0b2017-10-24 14:54:30 -070030
31 private static final long CACHE_EXPIRE_TIMEOUT = 5000;
32 private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
33
34 private final Alarm mCacheExpireAlarm;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070035
Sunny Goyalfa401a12015-04-10 13:45:42 -070036 public UninstallDropTarget(Context context, AttributeSet attrs) {
37 this(context, attrs, 0);
38 }
39
40 public UninstallDropTarget(Context context, AttributeSet attrs, int defStyle) {
41 super(context, attrs, defStyle);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070042
43 mCacheExpireAlarm = new Alarm();
44 mCacheExpireAlarm.setOnAlarmListener(this);
Sunny Goyalfa401a12015-04-10 13:45:42 -070045 }
46
47 @Override
48 protected void onFinishInflate() {
49 super.onFinishInflate();
Sunny Goyalda1dfa32017-04-26 22:34:49 -070050 setupUi();
51 }
52
53 protected void setupUi() {
Sunny Goyalfa401a12015-04-10 13:45:42 -070054 // Get the hover color
Sunny Goyal3a2698b2015-04-28 21:36:46 -070055 mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
Sunny Goyalda1dfa32017-04-26 22:34:49 -070056 setDrawable(R.drawable.ic_uninstall_shadow);
Sunny Goyalfa401a12015-04-10 13:45:42 -070057 }
58
59 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070060 public void onAlarm(Alarm alarm) {
61 mUninstallDisabledCache.clear();
Sunny Goyal1a70cef2015-04-22 11:29:51 -070062 }
63
Sunny Goyal0236d0b2017-10-24 14:54:30 -070064 @Override
65 public int getAccessibilityAction() {
66 return LauncherAccessibilityDelegate.UNINSTALL;
67 }
68
69 @Override
70 protected boolean supportsDrop(ItemInfo info) {
71 Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
72 if (uninstallDisabled == null) {
73 UserManager userManager =
74 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
75 Bundle restrictions = userManager.getUserRestrictions(info.user);
76 uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
Sunny Goyal6b0aa872017-09-29 07:54:37 -070077 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070078 mUninstallDisabledCache.put(info.user, uninstallDisabled);
Sunny Goyal6b0aa872017-09-29 07:54:37 -070079 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -070080 // Cancel any pending alarm and set cache expiry after some time
81 mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
82 if (uninstallDisabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -080083 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -070084 }
85
Sunny Goyal6b0aa872017-09-29 07:54:37 -070086 if (info instanceof AppInfo) {
87 AppInfo appInfo = (AppInfo) info;
88 if (appInfo.isSystemApp != AppInfo.FLAG_SYSTEM_UNKNOWN) {
89 return (appInfo.isSystemApp & AppInfo.FLAG_SYSTEM_NO) != 0;
90 }
91 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -070092 return getUninstallTarget(info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -070093 }
94
95 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070096 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -070097 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -070098 private ComponentName getUninstallTarget(ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070099 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800100 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -0700101 if (item != null &&
102 item.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) {
103 intent = item.getIntent();
104 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700105 }
106 if (intent != null) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700107 LauncherActivityInfo info = LauncherAppsCompat.getInstance(mLauncher)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700108 .resolveActivity(intent, user);
109 if (info != null
110 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
111 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700112 }
113 }
114 return null;
115 }
116
117 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700118 public void onDrop(DragObject d, DragOptions options) {
119 // Defer onComplete
Sunny Goyal1797af42017-10-06 13:29:57 -0700120 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700121 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700122 }
123
124 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800125 public void completeDrop(final DragObject d) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700126 ComponentName target = performDropAction(d.dragInfo);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700127 if (d.dragSource instanceof DeferredOnComplete) {
128 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
129 if (target != null) {
130 deferred.mPackageName = target.getPackageName();
131 mLauncher.setOnResumeCallback(deferred);
132 } else {
133 deferred.sendFailure();
134 }
135 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700136 }
137
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700138 /**
139 * Performs the drop action and returns the target component for the dragObject or null if
140 * the action was not performed.
141 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700142 protected ComponentName performDropAction(ItemInfo info) {
143 ComponentName cn = getUninstallTarget(info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700144 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800145 // System applications cannot be installed. For now, show a toast explaining that.
146 // We may give them the option of disabling apps this way.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700147 Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700148 return null;
149 }
150 try {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700151 Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700152 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
153 .putExtra(Intent.EXTRA_USER, info.user);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700154 mLauncher.startActivity(i);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700155 return cn;
156 } catch (URISyntaxException e) {
157 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
158 return null;
159 }
160 }
161
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700162 @Override
163 public void onAccessibilityDrop(View view, ItemInfo item) {
164 performDropAction(item);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700165 }
166
167 /**
168 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
169 * {@link #onLauncherResume}
170 */
171 private class DeferredOnComplete implements DragSource, OnResumeCallback {
172
173 private final DragSource mOriginal;
174 private final Context mContext;
175
176 private String mPackageName;
177 private DragObject mDragObject;
178
179 public DeferredOnComplete(DragSource original, Context context) {
180 mOriginal = original;
181 mContext = context;
182 }
183
184 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700185 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700186 boolean success) {
187 mDragObject = d;
188 }
189
190 @Override
191 public void fillInLogContainerData(View v, ItemInfo info, Target target,
192 Target targetParent) {
193 mOriginal.fillInLogContainerData(v, info, target, targetParent);
194 }
195
196 @Override
197 public void onLauncherResume() {
198 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
199 if (LauncherAppsCompat.getInstance(mContext)
200 .getApplicationInfo(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES,
201 mDragObject.dragInfo.user) == null) {
202 mDragObject.dragSource = mOriginal;
Sunny Goyal1797af42017-10-06 13:29:57 -0700203 mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, true);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700204 } else {
205 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700206 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700207 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700208
209 public void sendFailure() {
210 mDragObject.dragSource = mOriginal;
211 mDragObject.cancelled = true;
Sunny Goyal1797af42017-10-06 13:29:57 -0700212 mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800213 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700214 }
215}