launch widget Configure activity when appropriate

This commit is contained in:
tibbi 2022-09-26 23:32:41 +02:00
parent 7b52bf2b30
commit e76b2dcc51
8 changed files with 39 additions and 15 deletions

View File

@ -419,7 +419,7 @@ class MainActivity : SimpleActivity(), FlingListener {
try {
val defaultDialerPackage = (getSystemService(Context.TELECOM_SERVICE) as TelecomManager).defaultDialerPackage
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, "", null)
homeScreenGridItems.add(dialerIcon)
}
} catch (e: Exception) {
@ -429,7 +429,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val defaultSMSMessengerPackage = Telephony.Sms.getDefaultSmsPackage(this)
appLaunchers.firstOrNull { it.packageName == defaultSMSMessengerPackage }?.apply {
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, "", null)
homeScreenGridItems.add(SMSMessengerIcon)
}
} catch (e: Exception) {
@ -440,7 +440,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val resolveInfo = packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY)
val defaultBrowserPackage = resolveInfo!!.activityInfo.packageName
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, "", null)
homeScreenGridItems.add(browserIcon)
}
} catch (e: Exception) {
@ -451,7 +451,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val storePackage = potentialStores.firstOrNull { isPackageInstalled(it) && appLaunchers.map { it.packageName }.contains(it) }
if (storePackage != null) {
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, "", null)
homeScreenGridItems.add(storeIcon)
}
}
@ -463,7 +463,7 @@ class MainActivity : SimpleActivity(), FlingListener {
val resolveInfo = packageManager.resolveActivity(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY)
val defaultCameraPackage = resolveInfo!!.activityInfo.packageName
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, "", null)
homeScreenGridItems.add(cameraIcon)
}
} 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) {
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, "", appLauncher.drawable)
activity?.showHomeIconMenu(x, y, gridItem, true)
ignoreTouches = true
}

View File

@ -94,7 +94,8 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
val widgetPreviewImage = info.loadPreviewImage(context, resources.displayMetrics.densityDpi) ?: appIcon
val widthCells = Math.min(COLUMN_COUNT, getTileCount(info.minWidth))
val heightCells = Math.min(ROW_COUNT, getTileCount(info.minHeight))
val widget = AppWidget(appPackageName, appTitle, appIcon, widgetTitle, widgetPreviewImage, widthCells, heightCells, false)
val widget =
AppWidget(appPackageName, appTitle, appIcon, widgetTitle, widgetPreviewImage, widthCells, heightCells, false, info.provider.shortClassName)
appWidgets.add(widget)
}
@ -109,7 +110,7 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
val appIcon = appMetadata.appIcon
val widgetTitle = info.loadLabel(packageManager).toString()
val widgetPreviewImage = packageManager.getDrawable(componentInfo.packageName, info.iconResource, componentInfo)
val widget = AppWidget(appPackageName, appTitle, appIcon, widgetTitle, widgetPreviewImage, 0, 0, true)
val widget = AppWidget(appPackageName, appTitle, appIcon, widgetTitle, widgetPreviewImage, 0, 0, true, "")
appWidgets.add(widget)
}
@ -231,6 +232,7 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
appWidget.appPackageName,
"",
ITEM_TYPE_WIDGET,
appWidget.shortClassName,
appWidget.widgetPreviewImage
)

View File

@ -11,6 +11,8 @@ const val ROW_COUNT = 6
const val COLUMN_COUNT = 5
const val UNINSTALL_APP_REQUEST_CODE = 50
const val REQUEST_CONFIGURE_WIDGET = 51
const val ITEM_TYPE_ICON = 0
const val ITEM_TYPE_WIDGET = 1

View File

@ -0,0 +1,6 @@
package com.simplemobiletools.launcher.helpers
import android.appwidget.AppWidgetHost
import android.content.Context
class MyAppWidgetHost(context: Context, hostId: Int) : AppWidgetHost(context, hostId)

View File

@ -10,7 +10,8 @@ data class AppWidget(
val widgetPreviewImage: Drawable?,
var widthCells: Int,
val heightCells: Int,
val isShortcut: Boolean
val isShortcut: Boolean,
val shortClassName: String // identifier to know which app widget are we using
) : WidgetsListItem() {
override fun getHashToCompare() = getStringToCompare().hashCode()

View File

@ -17,8 +17,9 @@ data class HomeScreenGridItem(
@ColumnInfo(name = "package_name") var packageName: String,
@ColumnInfo(name = "title") var title: String,
@ColumnInfo(name = "type") var type: Int,
@ColumnInfo(name = "short_class_name") var shortClassName: String,
@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, "", null)
}

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.launcher.views
import android.annotation.SuppressLint
import android.appwidget.AppWidgetManager
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
@ -17,12 +18,10 @@ import com.simplemobiletools.commons.extensions.performHapticFeedback
import com.simplemobiletools.commons.extensions.statusBarHeight
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.activities.MainActivity
import com.simplemobiletools.launcher.extensions.getDrawableForPackageName
import com.simplemobiletools.launcher.extensions.homeScreenGridItemsDB
import com.simplemobiletools.launcher.helpers.COLUMN_COUNT
import com.simplemobiletools.launcher.helpers.ITEM_TYPE_ICON
import com.simplemobiletools.launcher.helpers.ITEM_TYPE_WIDGET
import com.simplemobiletools.launcher.helpers.ROW_COUNT
import com.simplemobiletools.launcher.helpers.*
import com.simplemobiletools.launcher.models.HomeScreenGridItem
class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : View(context, attrs, defStyle) {
@ -179,6 +178,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
draggedItem!!.packageName,
draggedItem!!.title,
draggedItem!!.type,
"",
draggedItem!!.drawable
)
ensureBackgroundThread {
@ -231,7 +231,19 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
}
if (areAllCellsEmpty) {
val infoList = AppWidgetManager.getInstance(context).installedProviders
val appWidgetProviderInfo = infoList.firstOrNull { it.provider.shortClassName == draggedItem?.shortClassName }
if (appWidgetProviderInfo != null) {
val appWidgetHost = MyAppWidgetHost(context, 12345)
val appWidgetId = appWidgetHost.allocateAppWidgetId()
val appWidgetManager = AppWidgetManager.getInstance(context)
val canCreateWidget = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetProviderInfo.provider)
if (canCreateWidget) {
if (appWidgetProviderInfo.configure != null) {
appWidgetHost.startAppWidgetConfigureActivityForResult(context as MainActivity, appWidgetId, 0, REQUEST_CONFIGURE_WIDGET, null)
}
}
}
} else {
performHapticFeedback()
}