2015-04-10 13:45:42 -07:00
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
import android.util.AttributeSet;
|
2016-03-11 01:10:19 -08:00
|
|
|
import android.widget.Toast;
|
2015-06-12 20:04:41 -07:00
|
|
|
|
2016-10-10 10:41:41 -07:00
|
|
|
import com.android.launcher3.compat.LauncherAppsCompat;
|
2015-04-10 13:45:42 -07:00
|
|
|
|
|
|
|
|
public class UninstallDropTarget extends ButtonDropTarget {
|
|
|
|
|
|
|
|
|
|
public UninstallDropTarget(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UninstallDropTarget(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
// Get the hover color
|
2015-04-28 21:36:46 -07:00
|
|
|
mHoverColor = getResources().getColor(R.color.uninstall_target_hover_tint);
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2015-06-04 22:40:14 -07:00
|
|
|
setDrawable(R.drawable.ic_uninstall_launcher);
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-06-12 20:04:41 -07:00
|
|
|
protected boolean supportsDrop(DragSource source, ItemInfo info) {
|
2015-04-22 11:29:51 -07:00
|
|
|
return supportsDrop(getContext(), info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean supportsDrop(Context context, Object info) {
|
2016-12-16 15:04:51 -08:00
|
|
|
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
|
|
|
|
Bundle restrictions = userManager.getUserRestrictions();
|
|
|
|
|
if (restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
|
|
|
|
|
|| restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false)) {
|
|
|
|
|
return false;
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-10 10:41:41 -07:00
|
|
|
return getUninstallTarget(context, 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
|
|
|
*/
|
2016-10-10 10:41:41 -07:00
|
|
|
private static ComponentName getUninstallTarget(Context context, Object item) {
|
|
|
|
|
Intent intent = null;
|
2016-12-15 15:53:17 -08:00
|
|
|
UserHandle user = null;
|
2015-04-10 13:45:42 -07:00
|
|
|
if (item instanceof AppInfo) {
|
|
|
|
|
AppInfo info = (AppInfo) item;
|
2016-10-10 10:41:41 -07:00
|
|
|
intent = info.intent;
|
|
|
|
|
user = info.user;
|
2015-04-10 13:45:42 -07:00
|
|
|
} else if (item instanceof ShortcutInfo) {
|
|
|
|
|
ShortcutInfo info = (ShortcutInfo) item;
|
2016-10-10 10:41:41 -07:00
|
|
|
if (info.itemType == LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION) {
|
|
|
|
|
// Do not use restore/target intent here as we cannot uninstall an app which is
|
|
|
|
|
// being installed/restored.
|
|
|
|
|
intent = info.intent;
|
|
|
|
|
user = info.user;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (intent != null) {
|
2017-01-05 15:22:41 -08:00
|
|
|
LauncherActivityInfo info = LauncherAppsCompat.getInstance(context)
|
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
|
|
|
|
|
public void onDrop(DragObject d) {
|
|
|
|
|
// Differ item deletion
|
2016-03-11 01:10:19 -08:00
|
|
|
if (d.dragSource instanceof DropTargetSource) {
|
|
|
|
|
((DropTargetSource) d.dragSource).deferCompleteDropAfterUninstallActivity();
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
super.onDrop(d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-12-13 19:37:10 -08:00
|
|
|
public void completeDrop(final DragObject d) {
|
2016-03-11 01:10:19 -08:00
|
|
|
DropTargetResultCallback callback = d.dragSource instanceof DropTargetResultCallback
|
|
|
|
|
? (DropTargetResultCallback) d.dragSource : null;
|
|
|
|
|
startUninstallActivity(mLauncher, d.dragInfo, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean startUninstallActivity(Launcher launcher, ItemInfo info) {
|
|
|
|
|
return startUninstallActivity(launcher, info, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean startUninstallActivity(
|
|
|
|
|
final Launcher launcher, ItemInfo info, DropTargetResultCallback callback) {
|
2016-10-10 10:41:41 -07:00
|
|
|
final ComponentName cn = getUninstallTarget(launcher, info);
|
2016-03-11 01:10:19 -08:00
|
|
|
|
|
|
|
|
final boolean isUninstallable;
|
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.
|
|
|
|
|
Toast.makeText(launcher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
|
|
|
|
|
isUninstallable = false;
|
|
|
|
|
} else {
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_DELETE,
|
|
|
|
|
Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
|
|
|
|
|
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|
|
|
|
|
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
|
2016-12-15 15:53:17 -08:00
|
|
|
intent.putExtra(Intent.EXTRA_USER, info.user);
|
2016-03-11 01:10:19 -08:00
|
|
|
launcher.startActivity(intent);
|
|
|
|
|
isUninstallable = true;
|
|
|
|
|
}
|
|
|
|
|
if (callback != null) {
|
2016-10-10 10:41:41 -07:00
|
|
|
sendUninstallResult(launcher, isUninstallable, cn, info.user, callback);
|
2016-03-11 01:10:19 -08:00
|
|
|
}
|
|
|
|
|
return isUninstallable;
|
|
|
|
|
}
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2016-03-11 01:10:19 -08:00
|
|
|
/**
|
|
|
|
|
* Notifies the {@param callback} whether the uninstall was successful or not.
|
|
|
|
|
*
|
|
|
|
|
* Since there is no direct callback for an uninstall request, we check the package existence
|
|
|
|
|
* when the launch resumes next time. This assumes that the uninstall activity will finish only
|
|
|
|
|
* after the task is completed
|
|
|
|
|
*/
|
|
|
|
|
protected static void sendUninstallResult(
|
|
|
|
|
final Launcher launcher, boolean activityStarted,
|
2016-12-15 15:53:17 -08:00
|
|
|
final ComponentName cn, final UserHandle user,
|
2016-03-11 01:10:19 -08:00
|
|
|
final DropTargetResultCallback callback) {
|
|
|
|
|
if (activityStarted) {
|
2015-04-10 13:45:42 -07:00
|
|
|
final Runnable checkIfUninstallWasSuccess = new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2017-03-01 17:27:16 -08:00
|
|
|
// We use MATCH_UNINSTALLED_PACKAGES as the app can be on SD card as well.
|
|
|
|
|
boolean uninstallSuccessful = LauncherAppsCompat.getInstance(launcher)
|
|
|
|
|
.getApplicationInfo(cn.getPackageName(),
|
|
|
|
|
PackageManager.MATCH_UNINSTALLED_PACKAGES, user) == null;
|
2016-03-11 01:10:19 -08:00
|
|
|
callback.onDragObjectRemoved(uninstallSuccessful);
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
};
|
2016-03-11 01:10:19 -08:00
|
|
|
launcher.addOnResumeCallback(checkIfUninstallWasSuccess);
|
2015-04-10 13:45:42 -07:00
|
|
|
} else {
|
2016-03-11 01:10:19 -08:00
|
|
|
callback.onDragObjectRemoved(false);
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-11 01:10:19 -08:00
|
|
|
public interface DropTargetResultCallback {
|
|
|
|
|
/**
|
|
|
|
|
* A drag operation was complete.
|
|
|
|
|
* @param isRemoved true if the drag object should be removed, false otherwise.
|
|
|
|
|
*/
|
|
|
|
|
void onDragObjectRemoved(boolean isRemoved);
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Interface defining an object that can provide uninstallable drag objects.
|
|
|
|
|
*/
|
2016-03-11 01:10:19 -08:00
|
|
|
public interface DropTargetSource extends DropTargetResultCallback {
|
2015-04-10 13:45:42 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicates that an uninstall request are made and the actual result may come
|
|
|
|
|
* after some time.
|
|
|
|
|
*/
|
|
|
|
|
void deferCompleteDropAfterUninstallActivity();
|
|
|
|
|
}
|
|
|
|
|
}
|