allow removing widgets

This commit is contained in:
tibbi 2022-09-30 23:55:55 +02:00
parent f37840094b
commit 10373af40d
6 changed files with 40 additions and 13 deletions

View File

@ -317,19 +317,20 @@ class MainActivity : SimpleActivity(), FlingListener {
private fun handleGridItemPopupMenu(anchorView: View, gridItem: HomeScreenGridItem, isOnAllAppsFragment: Boolean): PopupMenu { private fun handleGridItemPopupMenu(anchorView: View, gridItem: HomeScreenGridItem, isOnAllAppsFragment: Boolean): PopupMenu {
if (gridItem.type == ITEM_TYPE_WIDGET) { if (gridItem.type == ITEM_TYPE_WIDGET) {
anchorView.y += resources.getDimension(R.dimen.home_long_press_anchor_offset_y) anchorView.y -= resources.getDimension(R.dimen.home_long_press_anchor_offset_y)
} }
val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme()) val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme())
return PopupMenu(contextTheme, anchorView, Gravity.TOP or Gravity.END).apply { return PopupMenu(contextTheme, anchorView, Gravity.TOP or Gravity.END).apply {
inflate(R.menu.menu_app_icon) inflate(R.menu.menu_app_icon)
menu.findItem(R.id.app_info).isVisible = gridItem.type == ITEM_TYPE_ICON
menu.findItem(R.id.uninstall).isVisible = gridItem.type == ITEM_TYPE_ICON menu.findItem(R.id.uninstall).isVisible = gridItem.type == ITEM_TYPE_ICON
menu.findItem(R.id.remove).isVisible = !isOnAllAppsFragment menu.findItem(R.id.remove).isVisible = !isOnAllAppsFragment
setOnMenuItemClickListener { item -> setOnMenuItemClickListener { item ->
(all_apps_fragment as AllAppsFragment).ignoreTouches = false (all_apps_fragment as AllAppsFragment).ignoreTouches = false
when (item.itemId) { when (item.itemId) {
R.id.app_info -> launchAppInfo(gridItem.packageName) R.id.app_info -> launchAppInfo(gridItem.packageName)
R.id.remove -> home_screen_grid.removeAppIcon(gridItem.id!!) R.id.remove -> home_screen_grid.removeAppIcon(gridItem)
R.id.uninstall -> uninstallApp(gridItem.packageName) R.id.uninstall -> uninstallApp(gridItem.packageName)
} }
true true
@ -436,7 +437,7 @@ class MainActivity : SimpleActivity(), FlingListener {
try { try {
val defaultDialerPackage = (getSystemService(Context.TELECOM_SERVICE) as TelecomManager).defaultDialerPackage val defaultDialerPackage = (getSystemService(Context.TELECOM_SERVICE) as TelecomManager).defaultDialerPackage
appLaunchers.firstOrNull { it.packageName == defaultDialerPackage }?.apply { appLaunchers.firstOrNull { it.packageName == defaultDialerPackage }?.apply {
val dialerIcon = HomeScreenGridItem(null, 0, ROW_COUNT - 1, 1, ROW_COUNT, 1, 1, defaultDialerPackage, title, ITEM_TYPE_ICON, "", null) val dialerIcon = HomeScreenGridItem(null, 0, ROW_COUNT - 1, 1, ROW_COUNT, 1, 1, defaultDialerPackage, title, ITEM_TYPE_ICON, "", -1, null)
homeScreenGridItems.add(dialerIcon) homeScreenGridItems.add(dialerIcon)
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -446,7 +447,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val defaultSMSMessengerPackage = Telephony.Sms.getDefaultSmsPackage(this) val defaultSMSMessengerPackage = Telephony.Sms.getDefaultSmsPackage(this)
appLaunchers.firstOrNull { it.packageName == defaultSMSMessengerPackage }?.apply { appLaunchers.firstOrNull { it.packageName == defaultSMSMessengerPackage }?.apply {
val SMSMessengerIcon = val SMSMessengerIcon =
HomeScreenGridItem(null, 1, ROW_COUNT - 1, 2, ROW_COUNT, 1, 1, defaultSMSMessengerPackage, title, ITEM_TYPE_ICON, "", null) HomeScreenGridItem(null, 1, ROW_COUNT - 1, 2, ROW_COUNT, 1, 1, defaultSMSMessengerPackage, title, ITEM_TYPE_ICON, "", -1, null)
homeScreenGridItems.add(SMSMessengerIcon) homeScreenGridItems.add(SMSMessengerIcon)
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -457,7 +458,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val resolveInfo = packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY) val resolveInfo = packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY)
val defaultBrowserPackage = resolveInfo!!.activityInfo.packageName val defaultBrowserPackage = resolveInfo!!.activityInfo.packageName
appLaunchers.firstOrNull { it.packageName == defaultBrowserPackage }?.apply { appLaunchers.firstOrNull { it.packageName == defaultBrowserPackage }?.apply {
val browserIcon = HomeScreenGridItem(null, 2, ROW_COUNT - 1, 3, ROW_COUNT, 1, 1, defaultBrowserPackage, title, ITEM_TYPE_ICON, "", null) val browserIcon = HomeScreenGridItem(null, 2, ROW_COUNT - 1, 3, ROW_COUNT, 1, 1, defaultBrowserPackage, title, ITEM_TYPE_ICON, "", -1, null)
homeScreenGridItems.add(browserIcon) homeScreenGridItems.add(browserIcon)
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -468,7 +469,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val storePackage = potentialStores.firstOrNull { isPackageInstalled(it) && appLaunchers.map { it.packageName }.contains(it) } val storePackage = potentialStores.firstOrNull { isPackageInstalled(it) && appLaunchers.map { it.packageName }.contains(it) }
if (storePackage != null) { if (storePackage != null) {
appLaunchers.firstOrNull { it.packageName == storePackage }?.apply { appLaunchers.firstOrNull { it.packageName == storePackage }?.apply {
val storeIcon = HomeScreenGridItem(null, 3, ROW_COUNT - 1, 4, ROW_COUNT, 1, 1, storePackage, title, ITEM_TYPE_ICON, "", null) val storeIcon = HomeScreenGridItem(null, 3, ROW_COUNT - 1, 4, ROW_COUNT, 1, 1, storePackage, title, ITEM_TYPE_ICON, "", -1, null)
homeScreenGridItems.add(storeIcon) homeScreenGridItems.add(storeIcon)
} }
} }
@ -480,7 +481,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val resolveInfo = packageManager.resolveActivity(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY) val resolveInfo = packageManager.resolveActivity(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY)
val defaultCameraPackage = resolveInfo!!.activityInfo.packageName val defaultCameraPackage = resolveInfo!!.activityInfo.packageName
appLaunchers.firstOrNull { it.packageName == defaultCameraPackage }?.apply { appLaunchers.firstOrNull { it.packageName == defaultCameraPackage }?.apply {
val cameraIcon = HomeScreenGridItem(null, 4, ROW_COUNT - 1, 5, ROW_COUNT, 1, 1, defaultCameraPackage, title, ITEM_TYPE_ICON, "", null) val cameraIcon = HomeScreenGridItem(null, 4, ROW_COUNT - 1, 5, ROW_COUNT, 1, 1, defaultCameraPackage, title, ITEM_TYPE_ICON, "", -1, null)
homeScreenGridItems.add(cameraIcon) homeScreenGridItems.add(cameraIcon)
} }
} catch (e: Exception) { } catch (e: Exception) {

View File

@ -136,7 +136,7 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
} }
override fun onAppLauncherLongPressed(x: Float, y: Float, appLauncher: AppLauncher) { override fun onAppLauncherLongPressed(x: Float, y: Float, appLauncher: AppLauncher) {
val gridItem = HomeScreenGridItem(null, -1, -1, -1, -1, 1, 1, appLauncher.packageName, appLauncher.title, ITEM_TYPE_ICON, "", appLauncher.drawable) val gridItem = HomeScreenGridItem(null, -1, -1, -1, -1, 1, 1, appLauncher.packageName, appLauncher.title, ITEM_TYPE_ICON, "", -1, appLauncher.drawable)
activity?.showHomeIconMenu(x, y, gridItem, true) activity?.showHomeIconMenu(x, y, gridItem, true)
ignoreTouches = true ignoreTouches = true
} }

View File

@ -240,6 +240,7 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
"", "",
type, type,
appWidget.shortClassName, appWidget.shortClassName,
-1,
appWidget.widgetPreviewImage appWidget.widgetPreviewImage
) )

View File

@ -18,8 +18,9 @@ data class HomeScreenGridItem(
@ColumnInfo(name = "title") var title: String, @ColumnInfo(name = "title") var title: String,
@ColumnInfo(name = "type") var type: Int, @ColumnInfo(name = "type") var type: Int,
@ColumnInfo(name = "short_class_name") var shortClassName: String, @ColumnInfo(name = "short_class_name") var shortClassName: String,
@ColumnInfo(name = "widget_id") var widgetId: Int,
@Ignore var drawable: Drawable? @Ignore var drawable: Drawable?
) { ) {
constructor() : this(null, -1, -1, -1, -1, 1, 1, "", "", ITEM_TYPE_ICON, "", null) constructor() : this(null, -1, -1, -1, -1, 1, 1, "", "", ITEM_TYPE_ICON, "", -1, null)
} }

View File

@ -49,6 +49,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
private var gridItems = ArrayList<HomeScreenGridItem>() private var gridItems = ArrayList<HomeScreenGridItem>()
private var gridCenters = ArrayList<Pair<Int, Int>>() private var gridCenters = ArrayList<Pair<Int, Int>>()
private var draggedItemCurrentCoords = Pair(-1, -1) private var draggedItemCurrentCoords = Pair(-1, -1)
private var widgetViews = ArrayList<MyAppWidgetHostView>()
val appWidgetHost = MyAppWidgetHost(context, WIDGET_HOST_ID) val appWidgetHost = MyAppWidgetHost(context, WIDGET_HOST_ID)
private val appWidgetManager = AppWidgetManager.getInstance(context) private val appWidgetManager = AppWidgetManager.getInstance(context)
@ -88,10 +89,18 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
} }
fun removeAppIcon(iconId: Long) { fun removeAppIcon(item: HomeScreenGridItem) {
ensureBackgroundThread { ensureBackgroundThread {
context.homeScreenGridItemsDB.deleteById(iconId) context.homeScreenGridItemsDB.deleteById(item.id!!)
gridItems.removeIf { it.id == iconId } if (item.type == ITEM_TYPE_WIDGET) {
appWidgetHost.deleteAppWidgetId(item.widgetId)
post {
removeView(widgetViews.firstOrNull { it.tag == item.widgetId })
}
}
gridItems.removeIf { it.id == item.id }
redrawGrid() redrawGrid()
} }
} }
@ -188,6 +197,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
draggedItem!!.title, draggedItem!!.title,
draggedItem!!.type, draggedItem!!.type,
"", "",
-1,
draggedItem!!.drawable draggedItem!!.drawable
) )
@ -297,15 +307,21 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
private fun placeAppWidget(appWidgetId: Int, appWidgetProviderInfo: AppWidgetProviderInfo, item: HomeScreenGridItem) { private fun placeAppWidget(appWidgetId: Int, appWidgetProviderInfo: AppWidgetProviderInfo, item: HomeScreenGridItem) {
item.widgetId = appWidgetId
// we have to pass the base context here, else there will be errors with the themes // we have to pass the base context here, else there will be errors with the themes
val widgetView = appWidgetHost.createView((context as MainActivity).baseContext, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView val widgetView = appWidgetHost.createView((context as MainActivity).baseContext, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView
widgetView.tag = appWidgetId
widgetView.setAppWidget(appWidgetId, appWidgetProviderInfo) widgetView.setAppWidget(appWidgetId, appWidgetProviderInfo)
widgetView.longPressListener = { x, y ->
(context as? MainActivity)?.showHomeIconMenu(x, y, item, false)
}
widgetView.x = item.left * rowWidth + sideMargins.left.toFloat() widgetView.x = item.left * rowWidth + sideMargins.left.toFloat()
widgetView.y = item.top * rowHeight + sideMargins.top.toFloat() widgetView.y = item.top * rowHeight + sideMargins.top.toFloat()
val widgetWidth = item.widthCells * rowWidth val widgetWidth = item.widthCells * rowWidth
val widgetHeight = item.heightCells * rowHeight val widgetHeight = item.heightCells * rowHeight
addView(widgetView, widgetWidth, widgetHeight) addView(widgetView, widgetWidth, widgetHeight)
widgetViews.add(widgetView)
} }
// convert stuff like 102x192 to grid cells like 0x1 // convert stuff like 102x192 to grid cells like 0x1

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.launcher.views
import android.appwidget.AppWidgetHostView import android.appwidget.AppWidgetHostView
import android.content.Context import android.content.Context
import android.graphics.PointF
import android.os.Handler import android.os.Handler
import android.view.MotionEvent import android.view.MotionEvent
import android.view.ViewConfiguration import android.view.ViewConfiguration
@ -10,6 +11,8 @@ import com.simplemobiletools.commons.extensions.performHapticFeedback
class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) { class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
private var longPressHandler = Handler() private var longPressHandler = Handler()
private var hasLongPressed = false private var hasLongPressed = false
private var actionDownCoords = PointF()
var longPressListener: ((x: Float, y: Float) -> Unit)? = null
override fun onInterceptTouchEvent(event: MotionEvent): Boolean { override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
if (hasLongPressed) { if (hasLongPressed) {
@ -18,7 +21,11 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
} }
when (event.actionMasked) { when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> longPressHandler.postDelayed(longPressRunnable, ViewConfiguration.getLongPressTimeout().toLong()) MotionEvent.ACTION_DOWN -> {
longPressHandler.postDelayed(longPressRunnable, ViewConfiguration.getLongPressTimeout().toLong())
actionDownCoords.x = event.rawX
actionDownCoords.y = event.rawY
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> longPressHandler.removeCallbacksAndMessages(null) MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> longPressHandler.removeCallbacksAndMessages(null)
} }
@ -28,6 +35,7 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
private val longPressRunnable = Runnable { private val longPressRunnable = Runnable {
longPressHandler.removeCallbacksAndMessages(null) longPressHandler.removeCallbacksAndMessages(null)
hasLongPressed = true hasLongPressed = true
longPressListener?.invoke(actionDownCoords.x, actionDownCoords.y)
performHapticFeedback() performHapticFeedback()
} }
} }