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 65c7825..e2c76b9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -58,6 +58,7 @@ class MainActivity : SimpleActivity(), FlingListener { private var mLastTouchCoords = Pair(-1f, -1f) private var mActionOnCanBindWidget: ((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 @@ -138,8 +139,9 @@ class MainActivity : SimpleActivity(), FlingListener { 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 label = resultData.getStringExtra(Intent.EXTRA_SHORTCUT_NAME) ?: "" 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 val clickedGridItem = home_screen_grid.isClickingGridItem(x.toInt(), y.toInt()) if (clickedGridItem != null) { - if (clickedGridItem.type == ITEM_TYPE_ICON) { + if (clickedGridItem.type == ITEM_TYPE_ICON || clickedGridItem.type == ITEM_TYPE_SHORTCUT) { main_holder.performHapticFeedback() } @@ -325,7 +327,11 @@ class MainActivity : SimpleActivity(), FlingListener { home_screen_grid.hideResizeLines() val clickedGridItem = home_screen_grid.isClickingGridItem(x.toInt(), y.toInt()) 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) Intent(Intent.ACTION_CREATE_SHORTCUT).apply { - setComponent(componentName) + component = componentName startActivityForResult(this, REQUEST_CREATE_SHORTCUT) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt index e672802..cf409ac 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt @@ -7,6 +7,7 @@ import android.provider.Settings import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.launcher.activities.SettingsActivity import com.simplemobiletools.launcher.helpers.UNINSTALL_APP_REQUEST_CODE +import com.simplemobiletools.launcher.models.HomeScreenGridItem fun Activity.launchApp(packageName: String) { // if this is true, launch the app settings @@ -36,3 +37,12 @@ fun Activity.uninstallApp(packageName: String) { 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) + } +} 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 3edf00d..71f0d18 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -6,6 +6,7 @@ import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProviderInfo import android.content.Context import android.graphics.* +import android.graphics.drawable.BitmapDrawable import android.os.Bundle import android.text.Layout import android.text.StaticLayout @@ -88,6 +89,14 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel gridItems.forEach { item -> if (item.type == ITEM_TYPE_ICON) { 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 } @@ -282,13 +291,18 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel if (newHomeScreenGridItem.type == ITEM_TYPE_ICON) { ensureBackgroundThread { - val newId = context.homeScreenGridItemsDB.insert(newHomeScreenGridItem) - newHomeScreenGridItem.id = newId - gridItems.add(newHomeScreenGridItem) - redrawGrid() + storeAndShowGridItem(newHomeScreenGridItem) } } 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 { @@ -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() { val center = gridCenters.minBy { 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) { 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? { 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) if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) { return gridItem