From 04e243a875b15162603aaaaf45adcf49e17aed62 Mon Sep 17 00:00:00 2001 From: MrSluffy Date: Sat, 10 Feb 2024 18:22:34 +0800 Subject: [PATCH] Add Uninstall Button --- .../src/app/lawnchair/LawnchairLauncher.kt | 2 +- .../lawnchair/ui/popup/LawnchairShortcut.kt | 92 +++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/lawnchair/src/app/lawnchair/LawnchairLauncher.kt b/lawnchair/src/app/lawnchair/LawnchairLauncher.kt index 89d6707060..ac1c1d64f9 100644 --- a/lawnchair/src/app/lawnchair/LawnchairLauncher.kt +++ b/lawnchair/src/app/lawnchair/LawnchairLauncher.kt @@ -290,7 +290,7 @@ class LawnchairLauncher : } override fun getSupportedShortcuts(): Stream> = - Stream.concat(super.getSupportedShortcuts(), Stream.of(LawnchairShortcut.CUSTOMIZE)) + Stream.concat(super.getSupportedShortcuts(), Stream.of(LawnchairShortcut.UNINSTALL, LawnchairShortcut.CUSTOMIZE)) override fun updateTheme() { if (themeProvider.colorScheme != colorScheme) { diff --git a/lawnchair/src/app/lawnchair/ui/popup/LawnchairShortcut.kt b/lawnchair/src/app/lawnchair/ui/popup/LawnchairShortcut.kt index 4bca76ff5a..f0ff8de4c0 100644 --- a/lawnchair/src/app/lawnchair/ui/popup/LawnchairShortcut.kt +++ b/lawnchair/src/app/lawnchair/ui/popup/LawnchairShortcut.kt @@ -1,7 +1,15 @@ package app.lawnchair.ui.popup +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.pm.ApplicationInfo import android.content.pm.LauncherActivityInfo +import android.content.pm.LauncherApps +import android.net.Uri +import android.os.UserHandle import android.view.View +import android.widget.Toast import androidx.compose.foundation.layout.PaddingValues import androidx.compose.ui.unit.dp import app.lawnchair.LawnchairLauncher @@ -9,7 +17,9 @@ import app.lawnchair.override.CustomizeAppDialog import app.lawnchair.preferences2.PreferenceManager2 import app.lawnchair.views.ComposeBottomSheet import com.android.launcher3.AbstractFloatingView +import com.android.launcher3.BaseDraggingActivity import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION +import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_TASK import com.android.launcher3.R import com.android.launcher3.Utilities import com.android.launcher3.icons.BitmapInfo @@ -17,7 +27,9 @@ import com.android.launcher3.model.data.AppInfo as ModelAppInfo import com.android.launcher3.model.data.ItemInfo import com.android.launcher3.popup.SystemShortcut import com.android.launcher3.util.ComponentKey +import com.android.launcher3.util.PackageManagerHelper import com.patrykmichalik.opto.core.firstBlocking +import java.net.URISyntaxException class LawnchairShortcut { @@ -37,6 +49,21 @@ class LawnchairShortcut { val key = ComponentKey(itemInfo.targetComponent, itemInfo.user) return launcher.appsView.appsStore.getApp(key) } + + val UNINSTALL = + SystemShortcut.Factory { activity: BaseDraggingActivity, itemInfo: ItemInfo, view: View -> + if (itemInfo.targetComponent == null) { + return@Factory null + } + if (PackageManagerHelper.isSystemApp( + activity, + itemInfo.targetComponent!!.packageName, + ) + ) { + return@Factory null + } + UnInstall(activity, itemInfo, view) + } } class Customize( @@ -69,4 +96,69 @@ class LawnchairShortcut { } } } + + class UnInstall(private var target: BaseDraggingActivity?, private var itemInfo: ItemInfo?, originalView: View?) : + SystemShortcut( + R.drawable.ic_uninstall_no_shadow, + R.string.uninstall_drop_target_label, + target, + itemInfo, + originalView, + ) { + + /** + * @return the component name that should be uninstalled or null. + */ + private fun getUninstallTarget(item: ItemInfo?, context: Context): ComponentName? { + var intent: Intent? = null + var user: UserHandle? = null + if (item != null && + (item.itemType == ITEM_TYPE_APPLICATION || item.itemType == ITEM_TYPE_TASK) + ) { + intent = item.intent + user = item.user + } + if (intent != null) { + val info: LauncherActivityInfo? = + context.getSystemService(LauncherApps::class.java) + .resolveActivity(intent, user) + if (info != null && (info.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM) == 0) { + return info.componentName + } + } + return null + } + + override fun onClick(view: View) { + val cn = getUninstallTarget(itemInfo, view.context) + if (cn == null) { + // 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( + view.context, + R.string.uninstall_system_app_text, + Toast.LENGTH_SHORT, + ).show() + return + } + try { + val intent = Intent.parseUri( + view.context.getString(R.string.delete_package_intent), + 0, + ) + .setData( + Uri.fromParts( + "package", + itemInfo?.targetComponent?.packageName, + itemInfo?.targetComponent?.className, + ), + ) + .putExtra(Intent.EXTRA_USER, itemInfo?.user) + target?.startActivitySafely(view, intent, itemInfo) + AbstractFloatingView.closeAllOpenViews(target) + } catch (e: URISyntaxException) { + // Do nothing. + } + } + } }