Add Uninstall Button

This commit is contained in:
MrSluffy committed 2024-02-10 18:25:36 +08:00
1 parent 594dc1c3d2
commit 04e243a875
2 files changed
+93 -1

No files matched your search

@@ -290,7 +290,7 @@ class LawnchairLauncher :
}
override fun getSupportedShortcuts(): Stream<SystemShortcut.Factory<*>> =
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) {
@@ -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<BaseDraggingActivity>(
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.
}
}
}
}