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