blob: e8c452877eba0f6bdc4090dbb27aace0782c523c [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;
13import android.util.AttributeSet;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070014import android.util.Log;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070015import android.view.View;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080016import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070017
Sunny Goyal3dce5f32017-10-05 11:40:05 -070018import com.android.launcher3.Launcher.OnResumeCallback;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070019import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070020import com.android.launcher3.dragndrop.DragOptions;
21import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyalfa401a12015-04-10 13:45:42 -070022
Jon Mirandac56e3ff2017-08-23 12:13:24 -070023import java.net.URISyntaxException;
24
Sunny Goyalfa401a12015-04-10 13:45:42 -070025public class UninstallDropTarget extends ButtonDropTarget {
26
Jon Mirandac56e3ff2017-08-23 12:13:24 -070027 private static final String TAG = "UninstallDropTarget";
Sunny Goyal6b0aa872017-09-29 07:54:37 -070028 private static Boolean sUninstallDisabled;
Jon Mirandac56e3ff2017-08-23 12:13:24 -070029
Sunny Goyalfa401a12015-04-10 13:45:42 -070030 public UninstallDropTarget(Context context, AttributeSet attrs) {
31 this(context, attrs, 0);
32 }
33
34 public UninstallDropTarget(Context context, AttributeSet attrs, int defStyle) {
35 super(context, attrs, defStyle);
36 }
37
38 @Override
39 protected void onFinishInflate() {
40 super.onFinishInflate();
Sunny Goyalda1dfa32017-04-26 22:34:49 -070041 setupUi();
42 }
43
44 protected void setupUi() {
Sunny Goyalfa401a12015-04-10 13:45:42 -070045 // Get the hover color
Sunny Goyal3a2698b2015-04-28 21:36:46 -070046 mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
Sunny Goyalda1dfa32017-04-26 22:34:49 -070047 setDrawable(R.drawable.ic_uninstall_shadow);
Sunny Goyalfa401a12015-04-10 13:45:42 -070048 }
49
50 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070051 protected boolean supportsDrop(ItemInfo info) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -070052 return supportsDrop(getContext(), info);
53 }
54
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -070055 public static boolean supportsDrop(Context context, ItemInfo info) {
Sunny Goyal6b0aa872017-09-29 07:54:37 -070056 if (sUninstallDisabled == null) {
57 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
58 Bundle restrictions = userManager.getUserRestrictions();
59 sUninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
60 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
61 }
62 if (sUninstallDisabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -080063 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -070064 }
65
Sunny Goyal6b0aa872017-09-29 07:54:37 -070066 if (info instanceof AppInfo) {
67 AppInfo appInfo = (AppInfo) info;
68 if (appInfo.isSystemApp != AppInfo.FLAG_SYSTEM_UNKNOWN) {
69 return (appInfo.isSystemApp & AppInfo.FLAG_SYSTEM_NO) != 0;
70 }
71 }
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070072 return getUninstallTarget(context, info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -070073 }
74
75 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070076 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -070077 */
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -070078 private static ComponentName getUninstallTarget(Context context, ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070079 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080080 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -070081 if (item != null &&
82 item.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) {
83 intent = item.getIntent();
84 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070085 }
86 if (intent != null) {
Sunny Goyal3e9be432017-01-05 15:22:41 -080087 LauncherActivityInfo info = LauncherAppsCompat.getInstance(context)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070088 .resolveActivity(intent, user);
89 if (info != null
90 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
91 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -070092 }
93 }
94 return null;
95 }
96
97 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -070098 public void onDrop(DragObject d, DragOptions options) {
99 // Defer onComplete
100 if (options.deferCompleteForUninstall) {
101 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyalfa401a12015-04-10 13:45:42 -0700102 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700103 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700104 }
105
106 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800107 public void completeDrop(final DragObject d) {
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700108 ComponentName target = performDropAction(d);
109 if (d.dragSource instanceof DeferredOnComplete) {
110 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
111 if (target != null) {
112 deferred.mPackageName = target.getPackageName();
113 mLauncher.setOnResumeCallback(deferred);
114 } else {
115 deferred.sendFailure();
116 }
117 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700118 }
119
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700120 /**
121 * Performs the drop action and returns the target component for the dragObject or null if
122 * the action was not performed.
123 */
124 protected ComponentName performDropAction(DragObject d) {
125 return performDropAction(mLauncher, d.dragInfo);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700126 }
127
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700128 /**
129 * Performs the drop action and returns the target component for the dragObject or null if
130 * the action was not performed.
131 */
132 private static ComponentName performDropAction(Context context, ItemInfo info) {
133 ComponentName cn = getUninstallTarget(context, info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700134 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800135 // System applications cannot be installed. For now, show a toast explaining that.
136 // We may give them the option of disabling apps this way.
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700137 Toast.makeText(context, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
138 return null;
139 }
140 try {
141 Intent i = Intent.parseUri(context.getString(R.string.delete_package_intent), 0)
142 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
143 .putExtra(Intent.EXTRA_USER, info.user);
144 context.startActivity(i);
145 return cn;
146 } catch (URISyntaxException e) {
147 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
148 return null;
149 }
150 }
151
152 public static boolean startUninstallActivity(Launcher launcher, ItemInfo info) {
153 return performDropAction(launcher, info) != null;
154 }
155
156 /**
157 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
158 * {@link #onLauncherResume}
159 */
160 private class DeferredOnComplete implements DragSource, OnResumeCallback {
161
162 private final DragSource mOriginal;
163 private final Context mContext;
164
165 private String mPackageName;
166 private DragObject mDragObject;
167
168 public DeferredOnComplete(DragSource original, Context context) {
169 mOriginal = original;
170 mContext = context;
171 }
172
173 @Override
174 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
175 boolean success) {
176 mDragObject = d;
177 }
178
179 @Override
180 public void fillInLogContainerData(View v, ItemInfo info, Target target,
181 Target targetParent) {
182 mOriginal.fillInLogContainerData(v, info, target, targetParent);
183 }
184
185 @Override
186 public void onLauncherResume() {
187 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
188 if (LauncherAppsCompat.getInstance(mContext)
189 .getApplicationInfo(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES,
190 mDragObject.dragInfo.user) == null) {
191 mDragObject.dragSource = mOriginal;
192 mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, false, true);
193 } else {
194 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700195 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700196 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700197
198 public void sendFailure() {
199 mDragObject.dragSource = mOriginal;
200 mDragObject.cancelled = true;
201 mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, false, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800202 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700203 }
204}