add the first part of shortcut handling

This commit is contained in:
tibbi 2022-10-07 23:06:28 +02:00
parent 543371632a
commit 9ed0e55c15
3 changed files with 30 additions and 12 deletions

View file

@ -7,8 +7,10 @@ import android.appwidget.AppWidgetHost
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Bitmap
@ -124,6 +126,7 @@ class MainActivity : SimpleActivity(), FlingListener {
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
when (requestCode) {
UNINSTALL_APP_REQUEST_CODE -> {
ensureBackgroundThread {
@ -132,6 +135,13 @@ class MainActivity : SimpleActivity(), FlingListener {
}
REQUEST_ALLOW_BINDING_WIDGET -> mActionOnCanBindWidget?.invoke(resultCode == Activity.RESULT_OK)
REQUEST_CONFIGURE_WIDGET -> mActionOnWidgetConfiguredWidget?.invoke(resultCode == Activity.RESULT_OK)
REQUEST_CREATE_SHORTCUT -> {
if (resultCode == Activity.RESULT_OK && resultData != null) {
val launchIntent = resultData.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT) as? Intent
val label = resultData.getStringExtra(Intent.EXTRA_SHORTCUT_NAME)
val icon = resultData.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON) as? Bitmap
}
}
}
}
@ -591,6 +601,14 @@ class MainActivity : SimpleActivity(), FlingListener {
)
}
fun handleShorcutCreation(activityInfo: ActivityInfo) {
val componentName = ComponentName(activityInfo.packageName, activityInfo.name)
Intent(Intent.ACTION_CREATE_SHORTCUT).apply {
setComponent(componentName)
startActivityForResult(this, REQUEST_CREATE_SHORTCUT)
}
}
// taken from https://gist.github.com/maxjvh/a6ab15cbba9c82a5065d
private fun calculateAverageColor(bitmap: Bitmap): Int {
var red = 0

View file

@ -13,6 +13,7 @@ const val COLUMN_COUNT = 5
const val UNINSTALL_APP_REQUEST_CODE = 50
const val REQUEST_CONFIGURE_WIDGET = 51
const val REQUEST_ALLOW_BINDING_WIDGET = 52
const val REQUEST_CREATE_SHORTCUT = 53
const val ITEM_TYPE_ICON = 0
const val ITEM_TYPE_WIDGET = 1

View file

@ -161,13 +161,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
}
when (draggedItem!!.type) {
ITEM_TYPE_ICON -> addAppIcon()
ITEM_TYPE_ICON, ITEM_TYPE_SHORTCUT -> addAppIconOrShortcut()
ITEM_TYPE_WIDGET -> addWidget()
ITEM_TYPE_SHORTCUT -> {
// replace this with real shortcut handling
draggedItem = null
redrawGrid()
}
}
}
@ -222,7 +217,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
resizedWidget = null
}
private fun addAppIcon() {
private fun addAppIconOrShortcut() {
val center = gridCenters.minBy {
Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top)
}
@ -283,11 +278,15 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
draggedItem!!.activityInfo
)
ensureBackgroundThread {
val newId = context.homeScreenGridItemsDB.insert(newHomeScreenGridItem)
newHomeScreenGridItem.id = newId
gridItems.add(newHomeScreenGridItem)
redrawGrid()
if (newHomeScreenGridItem.type == ITEM_TYPE_ICON) {
ensureBackgroundThread {
val newId = context.homeScreenGridItemsDB.insert(newHomeScreenGridItem)
newHomeScreenGridItem.id = newId
gridItems.add(newHomeScreenGridItem)
redrawGrid()
}
} else if (newHomeScreenGridItem.type == ITEM_TYPE_SHORTCUT) {
(context as? MainActivity)?.handleShorcutCreation(newHomeScreenGridItem.activityInfo!!)
}
}
} else {