2015-04-10 13:45:42 -07:00
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
2017-10-30 13:52:20 -07:00
|
|
|
import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_MASK;
|
|
|
|
|
import static com.android.launcher3.ItemInfoWithIcon.FLAG_SYSTEM_NO;
|
|
|
|
|
|
2015-04-10 13:45:42 -07:00
|
|
|
import android.content.ComponentName;
|
|
|
|
|
import android.content.Context;
|
2016-03-11 01:10:19 -08:00
|
|
|
import android.content.Intent;
|
2016-10-10 10:41:41 -07:00
|
|
|
import android.content.pm.ApplicationInfo;
|
2017-01-05 15:22:41 -08:00
|
|
|
import android.content.pm.LauncherActivityInfo;
|
2017-03-01 17:27:16 -08:00
|
|
|
import android.content.pm.PackageManager;
|
2016-03-11 01:10:19 -08:00
|
|
|
import android.net.Uri;
|
2015-04-10 13:45:42 -07:00
|
|
|
import android.os.Bundle;
|
2016-12-15 15:53:17 -08:00
|
|
|
import android.os.UserHandle;
|
2015-04-10 13:45:42 -07:00
|
|
|
import android.os.UserManager;
|
2017-10-24 14:54:30 -07:00
|
|
|
import android.util.ArrayMap;
|
2015-04-10 13:45:42 -07:00
|
|
|
import android.util.AttributeSet;
|
2017-08-23 12:13:24 -07:00
|
|
|
import android.util.Log;
|
2017-10-05 11:40:05 -07:00
|
|
|
import android.view.View;
|
2016-03-11 01:10:19 -08:00
|
|
|
import android.widget.Toast;
|
2015-06-12 20:04:41 -07:00
|
|
|
|
2017-10-05 11:40:05 -07:00
|
|
|
import com.android.launcher3.Launcher.OnResumeCallback;
|
2018-03-05 18:06:52 +00:00
|
|
|
import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
|
2016-10-10 10:41:41 -07:00
|
|
|
import com.android.launcher3.compat.LauncherAppsCompat;
|
2017-10-05 11:40:05 -07:00
|
|
|
import com.android.launcher3.dragndrop.DragOptions;
|
|
|
|
|
import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2017-08-23 12:13:24 -07:00
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
|
2018-03-05 18:06:52 +00:00
|
|
|
public class UninstallDropTarget extends ButtonDropTarget implements OnAlarmListener {
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2018-03-05 18:06:52 +00:00
|
|
|
private static final String TAG = "UninstallDropTarget";
|
2017-10-24 14:54:30 -07:00
|
|
|
|
|
|
|
|
private static final long CACHE_EXPIRE_TIMEOUT = 5000;
|
|
|
|
|
private final ArrayMap<UserHandle, Boolean> mUninstallDisabledCache = new ArrayMap<>(1);
|
|
|
|
|
|
|
|
|
|
private final Alarm mCacheExpireAlarm;
|
2017-08-23 12:13:24 -07:00
|
|
|
|
2018-03-05 18:06:52 +00:00
|
|
|
public UninstallDropTarget(Context context, AttributeSet attrs) {
|
2015-04-10 13:45:42 -07:00
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 18:06:52 +00:00
|
|
|
public UninstallDropTarget(Context context, AttributeSet attrs, int defStyle) {
|
2015-04-10 13:45:42 -07:00
|
|
|
super(context, attrs, defStyle);
|
2017-10-24 14:54:30 -07:00
|
|
|
|
|
|
|
|
mCacheExpireAlarm = new Alarm();
|
|
|
|
|
mCacheExpireAlarm.setOnAlarmListener(this);
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
2018-03-05 18:06:52 +00:00
|
|
|
setupUi();
|
2017-04-26 22:34:49 -07:00
|
|
|
}
|
|
|
|
|
|
2018-03-05 18:06:52 +00:00
|
|
|
protected void setupUi() {
|
|
|
|
|
// Get the hover color
|
|
|
|
|
mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
|
|
|
|
|
setDrawable(R.drawable.ic_uninstall_shadow);
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-10-24 14:54:30 -07:00
|
|
|
public void onAlarm(Alarm alarm) {
|
|
|
|
|
mUninstallDisabledCache.clear();
|
2015-04-22 11:29:51 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-24 14:54:30 -07:00
|
|
|
@Override
|
|
|
|
|
public int getAccessibilityAction() {
|
2018-03-05 18:06:52 +00:00
|
|
|
return LauncherAccessibilityDelegate.UNINSTALL;
|
2017-10-24 14:54:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected boolean supportsDrop(ItemInfo info) {
|
|
|
|
|
Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
|
|
|
|
|
if (uninstallDisabled == null) {
|
|
|
|
|
UserManager userManager =
|
|
|
|
|
(UserManager) getContext().getSystemService(Context.USER_SERVICE);
|
|
|
|
|
Bundle restrictions = userManager.getUserRestrictions(info.user);
|
|
|
|
|
uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
|
2017-09-29 07:54:37 -07:00
|
|
|
|| restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
|
2017-10-24 14:54:30 -07:00
|
|
|
mUninstallDisabledCache.put(info.user, uninstallDisabled);
|
2017-09-29 07:54:37 -07:00
|
|
|
}
|
2017-10-24 14:54:30 -07:00
|
|
|
// Cancel any pending alarm and set cache expiry after some time
|
|
|
|
|
mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
|
|
|
|
|
if (uninstallDisabled) {
|
2016-12-16 15:04:51 -08:00
|
|
|
return false;
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-30 13:52:20 -07:00
|
|
|
if (info instanceof ItemInfoWithIcon) {
|
|
|
|
|
ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
|
|
|
|
|
if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0) {
|
|
|
|
|
return (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) != 0;
|
2017-09-29 07:54:37 -07:00
|
|
|
}
|
|
|
|
|
}
|
2017-10-24 14:54:30 -07:00
|
|
|
return getUninstallTarget(info) != null;
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-10-10 10:41:41 -07:00
|
|
|
* @return the component name that should be uninstalled or null.
|
2015-04-10 13:45:42 -07:00
|
|
|
*/
|
2017-10-24 14:54:30 -07:00
|
|
|
private ComponentName getUninstallTarget(ItemInfo item) {
|
2016-10-10 10:41:41 -07:00
|
|
|
Intent intent = null;
|
2016-12-15 15:53:17 -08:00
|
|
|
UserHandle user = null;
|
2017-08-15 12:54:42 -07:00
|
|
|
if (item != null &&
|
|
|
|
|
item.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) {
|
|
|
|
|
intent = item.getIntent();
|
|
|
|
|
user = item.user;
|
2016-10-10 10:41:41 -07:00
|
|
|
}
|
|
|
|
|
if (intent != null) {
|
2017-10-24 14:54:30 -07:00
|
|
|
LauncherActivityInfo info = LauncherAppsCompat.getInstance(mLauncher)
|
2016-10-10 10:41:41 -07:00
|
|
|
.resolveActivity(intent, user);
|
|
|
|
|
if (info != null
|
|
|
|
|
&& (info.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
|
|
|
|
return info.getComponentName();
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-10-05 11:40:05 -07:00
|
|
|
public void onDrop(DragObject d, DragOptions options) {
|
|
|
|
|
// Defer onComplete
|
2017-10-06 13:29:57 -07:00
|
|
|
d.dragSource = new DeferredOnComplete(d.dragSource, getContext());
|
2017-10-05 11:40:05 -07:00
|
|
|
super.onDrop(d, options);
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-12-13 19:37:10 -08:00
|
|
|
public void completeDrop(final DragObject d) {
|
2018-03-05 18:06:52 +00:00
|
|
|
ComponentName target = performDropAction(d.dragInfo);
|
2017-10-05 11:40:05 -07:00
|
|
|
if (d.dragSource instanceof DeferredOnComplete) {
|
|
|
|
|
DeferredOnComplete deferred = (DeferredOnComplete) d.dragSource;
|
|
|
|
|
if (target != null) {
|
|
|
|
|
deferred.mPackageName = target.getPackageName();
|
|
|
|
|
mLauncher.setOnResumeCallback(deferred);
|
|
|
|
|
} else {
|
|
|
|
|
deferred.sendFailure();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-11 01:10:19 -08:00
|
|
|
}
|
|
|
|
|
|
2017-10-05 11:40:05 -07:00
|
|
|
/**
|
|
|
|
|
* Performs the drop action and returns the target component for the dragObject or null if
|
|
|
|
|
* the action was not performed.
|
|
|
|
|
*/
|
2018-03-05 18:06:52 +00:00
|
|
|
protected ComponentName performDropAction(ItemInfo info) {
|
2017-10-24 14:54:30 -07:00
|
|
|
ComponentName cn = getUninstallTarget(info);
|
2016-10-10 10:41:41 -07:00
|
|
|
if (cn == null) {
|
2016-03-11 01:10:19 -08:00
|
|
|
// System applications cannot be installed. For now, show a toast explaining that.
|
|
|
|
|
// We may give them the option of disabling apps this way.
|
2017-10-24 14:54:30 -07:00
|
|
|
Toast.makeText(mLauncher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
|
2017-10-05 11:40:05 -07:00
|
|
|
return null;
|
2016-03-11 01:10:19 -08:00
|
|
|
}
|
2017-10-05 11:40:05 -07:00
|
|
|
try {
|
2017-10-24 14:54:30 -07:00
|
|
|
Intent i = Intent.parseUri(mLauncher.getString(R.string.delete_package_intent), 0)
|
2017-10-05 11:40:05 -07:00
|
|
|
.setData(Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
|
|
|
|
|
.putExtra(Intent.EXTRA_USER, info.user);
|
2017-10-24 14:54:30 -07:00
|
|
|
mLauncher.startActivity(i);
|
2017-10-05 11:40:05 -07:00
|
|
|
return cn;
|
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
|
Log.e(TAG, "Failed to parse intent to start uninstall activity for item=" + info);
|
|
|
|
|
return null;
|
2016-03-11 01:10:19 -08:00
|
|
|
}
|
2017-10-05 11:40:05 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-24 14:54:30 -07:00
|
|
|
@Override
|
|
|
|
|
public void onAccessibilityDrop(View view, ItemInfo item) {
|
2018-03-05 18:06:52 +00:00
|
|
|
performDropAction(item);
|
2016-03-11 01:10:19 -08:00
|
|
|
}
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2016-03-11 01:10:19 -08:00
|
|
|
/**
|
2017-10-05 11:40:05 -07:00
|
|
|
* A wrapper around {@link DragSource} which delays the {@link #onDropCompleted} action until
|
|
|
|
|
* {@link #onLauncherResume}
|
2016-03-11 01:10:19 -08:00
|
|
|
*/
|
2017-10-05 11:40:05 -07:00
|
|
|
private class DeferredOnComplete implements DragSource, OnResumeCallback {
|
|
|
|
|
|
|
|
|
|
private final DragSource mOriginal;
|
|
|
|
|
private final Context mContext;
|
|
|
|
|
|
|
|
|
|
private String mPackageName;
|
|
|
|
|
private DragObject mDragObject;
|
|
|
|
|
|
|
|
|
|
public DeferredOnComplete(DragSource original, Context context) {
|
|
|
|
|
mOriginal = original;
|
|
|
|
|
mContext = context;
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-05 11:40:05 -07:00
|
|
|
@Override
|
2017-10-06 13:29:57 -07:00
|
|
|
public void onDropCompleted(View target, DragObject d,
|
2017-10-05 11:40:05 -07:00
|
|
|
boolean success) {
|
|
|
|
|
mDragObject = d;
|
|
|
|
|
}
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2017-10-05 11:40:05 -07:00
|
|
|
@Override
|
|
|
|
|
public void fillInLogContainerData(View v, ItemInfo info, Target target,
|
|
|
|
|
Target targetParent) {
|
|
|
|
|
mOriginal.fillInLogContainerData(v, info, target, targetParent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onLauncherResume() {
|
|
|
|
|
// We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
|
|
|
|
|
if (LauncherAppsCompat.getInstance(mContext)
|
|
|
|
|
.getApplicationInfo(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES,
|
|
|
|
|
mDragObject.dragInfo.user) == null) {
|
|
|
|
|
mDragObject.dragSource = mOriginal;
|
2018-03-05 18:06:52 +00:00
|
|
|
mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, true);
|
2017-10-05 11:40:05 -07:00
|
|
|
} else {
|
|
|
|
|
sendFailure();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2017-10-05 11:40:05 -07:00
|
|
|
public void sendFailure() {
|
|
|
|
|
mDragObject.dragSource = mOriginal;
|
|
|
|
|
mDragObject.cancelled = true;
|
2018-03-05 18:06:52 +00:00
|
|
|
mOriginal.onDropCompleted(UninstallDropTarget.this, mDragObject, false);
|
2017-10-05 11:40:05 -07:00
|
|
|
}
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
}
|