adding some more shortcut support

This commit is contained in:
tibbi 2022-10-08 23:10:35 +02:00
parent c4a9a2b14c
commit 2a4a8fd623
3 changed files with 50 additions and 12 deletions

View File

@ -58,6 +58,7 @@ class MainActivity : SimpleActivity(), FlingListener {
private var mLastTouchCoords = Pair(-1f, -1f) private var mLastTouchCoords = Pair(-1f, -1f)
private var mActionOnCanBindWidget: ((granted: Boolean) -> Unit)? = null private var mActionOnCanBindWidget: ((granted: Boolean) -> Unit)? = null
private var mActionOnWidgetConfiguredWidget: ((granted: Boolean) -> Unit)? = null private var mActionOnWidgetConfiguredWidget: ((granted: Boolean) -> Unit)? = null
private var mActionOnAddShortcut: ((label: String, icon: Bitmap?, intent: String) -> Unit)? = null
private lateinit var mDetector: GestureDetectorCompat private lateinit var mDetector: GestureDetectorCompat
@ -138,8 +139,9 @@ class MainActivity : SimpleActivity(), FlingListener {
REQUEST_CREATE_SHORTCUT -> { REQUEST_CREATE_SHORTCUT -> {
if (resultCode == Activity.RESULT_OK && resultData != null) { if (resultCode == Activity.RESULT_OK && resultData != null) {
val launchIntent = resultData.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT) as? Intent val launchIntent = resultData.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT) as? Intent
val label = resultData.getStringExtra(Intent.EXTRA_SHORTCUT_NAME) val label = resultData.getStringExtra(Intent.EXTRA_SHORTCUT_NAME) ?: ""
val icon = resultData.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON) as? Bitmap val icon = resultData.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON) as? Bitmap
mActionOnAddShortcut?.invoke(label, icon, launchIntent?.toUri(0).toString())
} }
} }
} }
@ -308,7 +310,7 @@ class MainActivity : SimpleActivity(), FlingListener {
mIgnoreMoveEvents = true mIgnoreMoveEvents = true
val clickedGridItem = home_screen_grid.isClickingGridItem(x.toInt(), y.toInt()) val clickedGridItem = home_screen_grid.isClickingGridItem(x.toInt(), y.toInt())
if (clickedGridItem != null) { if (clickedGridItem != null) {
if (clickedGridItem.type == ITEM_TYPE_ICON) { if (clickedGridItem.type == ITEM_TYPE_ICON || clickedGridItem.type == ITEM_TYPE_SHORTCUT) {
main_holder.performHapticFeedback() main_holder.performHapticFeedback()
} }
@ -325,7 +327,11 @@ class MainActivity : SimpleActivity(), FlingListener {
home_screen_grid.hideResizeLines() home_screen_grid.hideResizeLines()
val clickedGridItem = home_screen_grid.isClickingGridItem(x.toInt(), y.toInt()) val clickedGridItem = home_screen_grid.isClickingGridItem(x.toInt(), y.toInt())
if (clickedGridItem != null) { if (clickedGridItem != null) {
launchApp(clickedGridItem.packageName) if (clickedGridItem.type == ITEM_TYPE_ICON) {
launchApp(clickedGridItem.packageName)
} else if (clickedGridItem.type == ITEM_TYPE_SHORTCUT) {
launchShortcutIntent(clickedGridItem)
}
} }
} }
@ -601,10 +607,11 @@ class MainActivity : SimpleActivity(), FlingListener {
) )
} }
fun handleShorcutCreation(activityInfo: ActivityInfo) { fun handleShorcutCreation(activityInfo: ActivityInfo, callback: (label: String, icon: Bitmap?, intent: String) -> Unit) {
mActionOnAddShortcut = callback
val componentName = ComponentName(activityInfo.packageName, activityInfo.name) val componentName = ComponentName(activityInfo.packageName, activityInfo.name)
Intent(Intent.ACTION_CREATE_SHORTCUT).apply { Intent(Intent.ACTION_CREATE_SHORTCUT).apply {
setComponent(componentName) component = componentName
startActivityForResult(this, REQUEST_CREATE_SHORTCUT) startActivityForResult(this, REQUEST_CREATE_SHORTCUT)
} }
} }

View File

@ -7,6 +7,7 @@ import android.provider.Settings
import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.launcher.activities.SettingsActivity import com.simplemobiletools.launcher.activities.SettingsActivity
import com.simplemobiletools.launcher.helpers.UNINSTALL_APP_REQUEST_CODE import com.simplemobiletools.launcher.helpers.UNINSTALL_APP_REQUEST_CODE
import com.simplemobiletools.launcher.models.HomeScreenGridItem
fun Activity.launchApp(packageName: String) { fun Activity.launchApp(packageName: String) {
// if this is true, launch the app settings // if this is true, launch the app settings
@ -36,3 +37,12 @@ fun Activity.uninstallApp(packageName: String) {
startActivityForResult(this, UNINSTALL_APP_REQUEST_CODE) startActivityForResult(this, UNINSTALL_APP_REQUEST_CODE)
} }
} }
fun Activity.launchShortcutIntent(item: HomeScreenGridItem) {
try {
val intent = Intent.parseUri(item.intent, 0)
startActivity(intent)
} catch (e: Exception) {
showErrorToast(e)
}
}

View File

@ -6,6 +6,7 @@ import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo import android.appwidget.AppWidgetProviderInfo
import android.content.Context import android.content.Context
import android.graphics.* import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle import android.os.Bundle
import android.text.Layout import android.text.Layout
import android.text.StaticLayout import android.text.StaticLayout
@ -88,6 +89,14 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
gridItems.forEach { item -> gridItems.forEach { item ->
if (item.type == ITEM_TYPE_ICON) { if (item.type == ITEM_TYPE_ICON) {
item.drawable = context.getDrawableForPackageName(item.packageName) item.drawable = context.getDrawableForPackageName(item.packageName)
} else if (item.type == ITEM_TYPE_SHORTCUT) {
if (item.icon != null) {
item.drawable = BitmapDrawable(item.icon)
} else {
ensureBackgroundThread {
context.homeScreenGridItemsDB.deleteById(item.id!!)
}
}
} }
item.providerInfo = providers.firstOrNull { it.provider.className == item.className } item.providerInfo = providers.firstOrNull { it.provider.className == item.className }
@ -282,13 +291,18 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
if (newHomeScreenGridItem.type == ITEM_TYPE_ICON) { if (newHomeScreenGridItem.type == ITEM_TYPE_ICON) {
ensureBackgroundThread { ensureBackgroundThread {
val newId = context.homeScreenGridItemsDB.insert(newHomeScreenGridItem) storeAndShowGridItem(newHomeScreenGridItem)
newHomeScreenGridItem.id = newId
gridItems.add(newHomeScreenGridItem)
redrawGrid()
} }
} else if (newHomeScreenGridItem.type == ITEM_TYPE_SHORTCUT) { } else if (newHomeScreenGridItem.type == ITEM_TYPE_SHORTCUT) {
(context as? MainActivity)?.handleShorcutCreation(newHomeScreenGridItem.activityInfo!!) (context as? MainActivity)?.handleShorcutCreation(newHomeScreenGridItem.activityInfo!!) { label, icon, intent ->
ensureBackgroundThread {
newHomeScreenGridItem.title = label
newHomeScreenGridItem.icon = icon
newHomeScreenGridItem.intent = intent
newHomeScreenGridItem.drawable = BitmapDrawable(newHomeScreenGridItem.icon)
storeAndShowGridItem(newHomeScreenGridItem)
}
}
} }
} }
} else { } else {
@ -304,6 +318,13 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
} }
private fun storeAndShowGridItem(item: HomeScreenGridItem) {
val newId = context.homeScreenGridItemsDB.insert(item)
item.id = newId
gridItems.add(item)
redrawGrid()
}
private fun addWidget() { private fun addWidget() {
val center = gridCenters.minBy { val center = gridCenters.minBy {
Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top) Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top)
@ -504,7 +525,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
} }
gridItems.filter { it.drawable != null && it.type == ITEM_TYPE_ICON }.forEach { item -> gridItems.filter { it.drawable != null && it.type == ITEM_TYPE_ICON || it.type == ITEM_TYPE_SHORTCUT }.forEach { item ->
if (item.id != draggedItem?.id) { if (item.id != draggedItem?.id) {
val drawableX = cellXCoords[item.left] + iconMargin + sideMargins.left val drawableX = cellXCoords[item.left] + iconMargin + sideMargins.left
@ -648,7 +669,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
fun isClickingGridItem(x: Int, y: Int): HomeScreenGridItem? { fun isClickingGridItem(x: Int, y: Int): HomeScreenGridItem? {
for (gridItem in gridItems) { for (gridItem in gridItems) {
if (gridItem.type == ITEM_TYPE_ICON) { if (gridItem.type == ITEM_TYPE_ICON || gridItem.type == ITEM_TYPE_SHORTCUT) {
val rect = getClickableRect(gridItem) val rect = getClickableRect(gridItem)
if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) { if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) {
return gridItem return gridItem