From 9ed0e55c15c718ebfe7c4618ff43ea3c43eab6d5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 7 Oct 2022 23:06:28 +0200 Subject: [PATCH] add the first part of shortcut handling --- .../launcher/activities/MainActivity.kt | 18 +++++++++++++++ .../launcher/helpers/Constants.kt | 1 + .../launcher/views/HomeScreenGrid.kt | 23 +++++++++---------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt index b768eac..39bd1f4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -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 diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt index ce2a2f3..a86f3b8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt @@ -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 diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt index 59fca86..92e1b3b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -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 {