blob: 43938194e9dedf97d051aad56c06f9216762a20e [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
Sunny Goyal1797af42017-10-06 13:29:57 -0700100 d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700101 super.onDrop(d, options);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700102 }
103
104 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800105 public void completeDrop(final DragObject d) {
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700106 ComponentName target = performDropAction(d);
107 if (d.dragSource instanceof DeferredOnComplete) {
108 DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
109 if (target != null) {
110 deferred.mPackageName = target.getPackageName();
111 mLauncher.setOnResumeCallback(deferred);
112 } else {
113 deferred.sendFailure();
114 }
115 }
Tony Wickham734dfbe2015-09-16 16:22:36 -0700116 }
117
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700118 /**
119 * Performs the drop action and returns the target component for the dragObject or null if
120 * the action was not performed.
121 */
122 protected ComponentName performDropAction(DragObject d) {
123 return performDropAction(mLauncher, d.dragInfo);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700124 }
125
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700126 /**
127 * Performs the drop action and returns the target component for the dragObject or null if
128 * the action was not performed.
129 */
130 private static ComponentName performDropAction(Context context, ItemInfo info) {
131 ComponentName cn = getUninstallTarget(context, info);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700132 if (cn == null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800133 // System applications cannot be installed. For now, show a toast explaining that.
134 // We may give them the option of disabling apps this way.
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700135 Toast.makeText(context, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
136 return null;
137 }
138 try {
139 Intent i = Intent.parseUri(context.getString(R.string.delete_package_intent), 0)
140 .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
141 .putExtra(Intent.EXTRA_USER, info.user);
142 context.startActivity(i);
143 return cn;
144 } catch (URISyntaxException e) {
145 Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
146 return null;
147 }
148 }
149
150 public static boolean startUninstallActivity(Launcher launcher, ItemInfo info) {
151 return performDropAction(launcher, info) != null;
152 }
153
154 /**
155 * A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
156 * {@link #onLauncherResume}
157 */
158 private class DeferredOnComplete implements DragSource, OnResumeCallback {
159
160 private final DragSource mOriginal;
161 private final Context mContext;
162
163 private String mPackageName;
164 private DragObject mDragObject;
165
166 public DeferredOnComplete(DragSource original, Context context) {
167 mOriginal = original;
168 mContext = context;
169 }
170
171 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700172 public void onDropCompleted(View target, DragObject d,
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700173 boolean success) {
174 mDragObject = d;
175 }
176
177 @Override
178 public void fillInLogContainerData(View v, ItemInfo info, Target target,
179 Target targetParent) {
180 mOriginal.fillInLogContainerData(v, info, target, targetParent);
181 }
182
183 @Override
184 public void onLauncherResume() {
185 // We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
186 if (LauncherAppsCompat.getInstance(mContext)
187 .getApplicationInfo(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES,
188 mDragObject.dragInfo.user) == null) {
189 mDragObject.dragSource = mOriginal;
Sunny Goyal1797af42017-10-06 13:29:57 -0700190 mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, true);
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700191 } else {
192 sendFailure();
Jon Mirandac56e3ff2017-08-23 12:13:24 -0700193 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700194 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700195
196 public void sendFailure() {
197 mDragObject.dragSource = mOriginal;
198 mDragObject.cancelled = true;
Sunny Goyal1797af42017-10-06 13:29:57 -0700199 mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, false);
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800200 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700201 }
202}