Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
Sunny Goyal | 076839c | 2017-10-30 13:52:20 -0700 | [diff] [blame^] | 3 | import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_MASK; |
| 4 | import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_NO; |
| 5 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 6 | import android.content.ComponentName; |
| 7 | import android.content.Context; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 8 | import android.content.Intent; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 9 | import android.content.pm.ApplicationInfo; |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 10 | import android.content.pm.LauncherActivityInfo; |
Sunny Goyal | f2db253 | 2017-03-01 17:27:16 -0800 | [diff] [blame] | 11 | import android.content.pm.PackageManager; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 12 | import android.net.Uri; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 13 | import android.os.Bundle; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 14 | import android.os.UserHandle; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 15 | import android.os.UserManager; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 16 | import android.util.ArrayMap; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 17 | import android.util.AttributeSet; |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 18 | import android.util.Log; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 19 | import android.view.View; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 20 | import android.widget.Toast; |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 21 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 22 | import com.android.launcher3.Launcher.OnResumeCallback; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 23 | import com.android.launcher3.accessibility.LauncherAccessibilityDelegate; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 24 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 25 | import com.android.launcher3.dragndrop.DragOptions; |
| 26 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 27 | |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 28 | import java.net.URISyntaxException; |
| 29 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 30 | public class UninstallDropTarget extends ButtonDropTarget implements OnAlarmListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 31 | |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 32 | private static final String TAG = "UninstallDropTarget"; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 33 | |
| 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 Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 38 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 39 | 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 45 | |
| 46 | mCacheExpireAlarm = new Alarm(); |
| 47 | mCacheExpireAlarm.setOnAlarmListener(this); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected void onFinishInflate() { |
| 52 | super.onFinishInflate(); |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 53 | setupUi(); |
| 54 | } |
| 55 | |
| 56 | protected void setupUi() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 57 | // Get the hover color |
Sunny Goyal | 3a2698b | 2015-04-28 21:36:46 -0700 | [diff] [blame] | 58 | mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint); |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 59 | setDrawable(R.drawable.ic_uninstall_shadow); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 63 | public void onAlarm(Alarm alarm) { |
| 64 | mUninstallDisabledCache.clear(); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 67 | @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 Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 80 | || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 81 | mUninstallDisabledCache.put(info.user, uninstallDisabled); |
Sunny Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 82 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 83 | // Cancel any pending alarm and set cache expiry after some time |
| 84 | mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT); |
| 85 | if (uninstallDisabled) { |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 86 | return false; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Sunny Goyal | 076839c | 2017-10-30 13:52:20 -0700 | [diff] [blame^] | 89 | 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 Goyal | 6b0aa87 | 2017-09-29 07:54:37 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 95 | return getUninstallTarget(info) != null; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 99 | * @return the component name that should be uninstalled or null. |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 100 | */ |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 101 | private ComponentName getUninstallTarget(ItemInfo item) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 102 | Intent intent = null; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 103 | UserHandle user = null; |
Sunny Goyal | 1fe0c2c | 2017-08-15 12:54:42 -0700 | [diff] [blame] | 104 | if (item != null && |
| 105 | item.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) { |
| 106 | intent = item.getIntent(); |
| 107 | user = item.user; |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 108 | } |
| 109 | if (intent != null) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 110 | LauncherActivityInfo info = LauncherAppsCompat.getInstance(mLauncher) |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 111 | .resolveActivity(intent, user); |
| 112 | if (info != null |
| 113 | && (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 114 | return info.getComponentName(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | @Override |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 121 | public void onDrop(DragObject d, DragOptions options) { |
| 122 | // Defer onComplete |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 123 | d.dragSource = new DeferredOnComplete(d.dragSource, getContext()); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 124 | super.onDrop(d, options); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 128 | public void completeDrop(final DragObject d) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 129 | ComponentName target = performDropAction(d.dragInfo); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 130 | 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 Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Performs the drop action and returns the target component for the dragObject or null if |
| 143 | * the action was not performed. |
| 144 | */ |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 145 | protected ComponentName performDropAction(ItemInfo info) { |
| 146 | ComponentName cn = getUninstallTarget(info); |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 147 | if (cn == null) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 148 | // 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 150 | Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show(); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 151 | return null; |
| 152 | } |
| 153 | try { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 154 | Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0) |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 155 | .setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName())) |
| 156 | .putExtra(Intent.EXTRA_USER, info.user); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 157 | mLauncher.startActivity(i); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 158 | 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 165 | @Override |
| 166 | public void onAccessibilityDrop(View view, ItemInfo item) { |
| 167 | performDropAction(item); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 168 | } |
| 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 Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 188 | public void onDropCompleted(View target, DragObject d, |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 189 | 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 Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 206 | mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, true); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 207 | } else { |
| 208 | sendFailure(); |
Jon Miranda | c56e3ff | 2017-08-23 12:13:24 -0700 | [diff] [blame] | 209 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 210 | } |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 211 | |
| 212 | public void sendFailure() { |
| 213 | mDragObject.dragSource = mOriginal; |
| 214 | mDragObject.cancelled = true; |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 215 | mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, false); |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 216 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 217 | } |
| 218 | } |