blob: 84d6a9b3480a97897402ac9fef8ea428c28e4f15 [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 Goyald5bd67d2016-03-11 01:10:19 -080015import android.widget.Toast;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070016
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070017import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyalfa401a12015-04-10 13:45:42 -070018
Jon Mirandac56e3ff2017-08-23 12:13:24 -070019import java.net.URISyntaxException;
20
Sunny Goyalfa401a12015-04-10 13:45:42 -070021public class UninstallDropTarget extends ButtonDropTarget {
22
Jon Mirandac56e3ff2017-08-23 12:13:24 -070023 private static final String TAG = "UninstallDropTarget";
24
Sunny Goyalfa401a12015-04-10 13:45:42 -070025 public UninstallDropTarget(Context context, AttributeSet attrs) {
26 this(context, attrs, 0);
27 }
28
29 public UninstallDropTarget(Context context, AttributeSet attrs, int defStyle) {
30 super(context, attrs, defStyle);
31 }
32
33 @Override
34 protected void onFinishInflate() {
35 super.onFinishInflate();
Sunny Goyalda1dfa32017-04-26 22:34:49 -070036 setupUi();
37 }
38
39 protected void setupUi() {
Sunny Goyalfa401a12015-04-10 13:45:42 -070040 // Get the hover color
Sunny Goyal3a2698b2015-04-28 21:36:46 -070041 mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
Sunny Goyalda1dfa32017-04-26 22:34:49 -070042 setDrawable(R.drawable.ic_uninstall_shadow);
Sunny Goyalfa401a12015-04-10 13:45:42 -070043 }
44
45 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -070046 protected boolean supportsDrop(DragSource source, ItemInfo info) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -070047 return supportsDrop(getContext(), info);
48 }
49
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -070050 public static boolean supportsDrop(Context context, ItemInfo info) {
Sunny Goyala52ecb02016-12-16 15:04:51 -080051 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
52 Bundle restrictions = userManager.getUserRestrictions();
53 if (restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
54 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false)) {
55 return false;
Sunny Goyalfa401a12015-04-10 13:45:42 -070056 }
57
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070058 return getUninstallTarget(context, info) != null;
Sunny Goyalfa401a12015-04-10 13:45:42 -070059 }
60
61 /**
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070062 * @return the component name that should be uninstalled or null.
Sunny Goyalfa401a12015-04-10 13:45:42 -070063 */
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -070064 private static ComponentName getUninstallTarget(Context context, ItemInfo item) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070065 Intent intent = null;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066 UserHandle user = null;
Sunny Goyal1fe0c2c2017-08-15 12:54:42 -070067 if (item != null &&
68 item.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) {
69 intent = item.getIntent();
70 user = item.user;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070071 }
72 if (intent != null) {
Sunny Goyal3e9be432017-01-05 15:22:41 -080073 LauncherActivityInfo info = LauncherAppsCompat.getInstance(context)
Sunny Goyal8e0e1d72016-10-10 10:41:41 -070074 .resolveActivity(intent, user);
75 if (info != null
76 && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
77 return info.getComponentName();
Sunny Goyalfa401a12015-04-10 13:45:42 -070078 }
79 }
80 return null;
81 }
82
83 @Override
84 public void onDrop(DragObject d) {
85 // Differ item deletion
Sunny Goyald5bd67d2016-03-11 01:10:19 -080086 if (d.dragSource instanceof DropTargetSource) {
87 ((DropTargetSource) d.dragSource).deferCompleteDropAfterUninstallActivity();
Sunny Goyalfa401a12015-04-10 13:45:42 -070088 }
89 super.onDrop(d);
90 }
91
92 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -080093 public void completeDrop(final DragObject d) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080094 DropTargetResultCallback callback = d.dragSource instanceof DropTargetResultCallback
95 ? (DropTargetResultCallback) d.dragSource : null;
96 startUninstallActivity(mLauncher, d.dragInfo, callback);
Tony Wickham734dfbe2015-09-16 16:22:36 -070097 }
98
Sunny Goyalaa8ef112015-06-12 20:04:41 -070099 public static boolean startUninstallActivity(Launcher launcher, ItemInfo info) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800100 return startUninstallActivity(launcher, info, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700101 }
102
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800103 public static boolean startUninstallActivity(
104 final Launcher launcher, ItemInfo info, DropTargetResultCallback callback) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700105 final ComponentName cn = getUninstallTarget(launcher, info);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800106
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700107 boolean canUninstall;
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700108 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800109 // System applications cannot be installed. For now, show a toast explaining that.
110 // We may give them the option of disabling apps this way.
111 Toast.makeText(launcher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700112 canUninstall = false;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800113 } else {
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700114 try {
115 Intent i = Intent.parseUri(launcher.getString(R.string.delete_package_intent), 0)
116 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
117 .putExtra(Intent.EXTRA_USER, info.user);
118 launcher.startActivity(i);
119 canUninstall = true;
120 } catch (URISyntaxException e) {
121 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
122 canUninstall = false;
123 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700124 }
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800125 if (callback != null) {
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700126 sendUninstallResult(launcher, canUninstall, cn, info.user, callback);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800127 }
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700128 return canUninstall;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800129 }
130
131 /**
132 * Notifies the {@param callback} whether the uninstall was successful or not.
133 *
134 * Since there is no direct callback for an uninstall request, we check the package existence
135 * when the launch resumes next time. This assumes that the uninstall activity will finish only
136 * after the task is completed
137 */
138 protected static void sendUninstallResult(
139 final Launcher launcher, boolean activityStarted,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800140 final ComponentName cn, final UserHandle user,
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800141 final DropTargetResultCallback callback) {
142 if (activityStarted) {
143 final Runnable checkIfUninstallWasSuccess = new Runnable() {
144 @Override
145 public void run() {
Sunny Goyalf2db2532017-03-01 17:27:16 -0800146 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
147 boolean uninstallSuccessful = LauncherAppsCompat.getInstance(launcher)
148 .getApplicationInfo(cn.getPackageName(),
149 PackageManager.MATCH_UNINSTALLED_PACKAGES, user) == null;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800150 callback.onDragObjectRemoved(uninstallSuccessful);
151 }
152 };
153 launcher.addOnResumeCallback(checkIfUninstallWasSuccess);
154 } else {
155 callback.onDragObjectRemoved(false);
156 }
157 }
158
159 public interface DropTargetResultCallback {
160 /**
161 * A drag operation was complete.
162 * @param isRemoved true if the drag object should be removed, false otherwise.
163 */
164 void onDragObjectRemoved(boolean isRemoved);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700165 }
166
167 /**
168 * Interface defining an object that can provide uninstallable drag objects.
169 */
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800170 public interface DropTargetSource extends DropTargetResultCallback {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700171
172 /**
173 * Indicates that an uninstall request are made and the actual result may come
174 * after some time.
175 */
176 void deferCompleteDropAfterUninstallActivity();
177 }
178}