Store all shortcuts with IDs without Intent
This handles all shortcuts the same, using the recommended mechanism of using `LauncherApps` to get `PinItemRequest` from result intent. This makes `intent` field obsolete. This closes #67
This commit is contained in:
parent
2b951ad277
commit
befd1d5d09
3 changed files with 21 additions and 32 deletions
|
@ -17,6 +17,7 @@ import android.content.res.Configuration
|
|||
import android.graphics.Bitmap
|
||||
import android.graphics.Color
|
||||
import android.graphics.Rect
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
|
@ -69,7 +70,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 var mActionOnAddShortcut: ((shortcutId: String, label: String, icon: Drawable) -> Unit)? = null
|
||||
|
||||
private lateinit var mDetector: GestureDetectorCompat
|
||||
|
||||
|
@ -279,10 +280,13 @@ class MainActivity : SimpleActivity(), FlingListener {
|
|||
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
|
||||
mActionOnAddShortcut?.invoke(label, icon, launchIntent?.toUri(0).toString())
|
||||
val launcherApps = applicationContext.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
|
||||
val item = launcherApps.getPinItemRequest(resultData)
|
||||
item.accept()
|
||||
val shortcutId = item.shortcutInfo?.id!!
|
||||
val label = item.shortcutInfo?.shortLabel?.toString() ?: item.shortcutInfo?.longLabel?.toString() ?: ""
|
||||
val icon = launcherApps.getShortcutIconDrawable(item.shortcutInfo!!, resources.displayMetrics.densityDpi)
|
||||
mActionOnAddShortcut?.invoke(shortcutId, label, icon)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -490,17 +494,12 @@ class MainActivity : SimpleActivity(), FlingListener {
|
|||
if (clickedGridItem.type == ITEM_TYPE_ICON) {
|
||||
launchApp(clickedGridItem.packageName, clickedGridItem.activityName)
|
||||
} else if (clickedGridItem.type == ITEM_TYPE_SHORTCUT) {
|
||||
if (clickedGridItem.intent.isNotEmpty()) {
|
||||
launchShortcutIntent(clickedGridItem)
|
||||
} else {
|
||||
// launch pinned shortcuts
|
||||
val id = clickedGridItem.shortcutId
|
||||
val packageName = clickedGridItem.packageName
|
||||
val userHandle = android.os.Process.myUserHandle()
|
||||
val shortcutBounds = home_screen_grid.getClickableRect(clickedGridItem)
|
||||
val launcherApps = applicationContext.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
|
||||
launcherApps.startShortcut(packageName, id, shortcutBounds, null, userHandle)
|
||||
}
|
||||
val id = clickedGridItem.shortcutId
|
||||
val packageName = clickedGridItem.packageName
|
||||
val userHandle = android.os.Process.myUserHandle()
|
||||
val shortcutBounds = home_screen_grid.getClickableRect(clickedGridItem)
|
||||
val launcherApps = applicationContext.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
|
||||
launcherApps.startShortcut(packageName, id, shortcutBounds, null, userHandle)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -938,7 +937,7 @@ class MainActivity : SimpleActivity(), FlingListener {
|
|||
)
|
||||
}
|
||||
|
||||
fun handleShorcutCreation(activityInfo: ActivityInfo, callback: (label: String, icon: Bitmap?, intent: String) -> Unit) {
|
||||
fun handleShorcutCreation(activityInfo: ActivityInfo, callback: (shortcutId: String, label: String, icon: Drawable) -> Unit) {
|
||||
mActionOnAddShortcut = callback
|
||||
val componentName = ComponentName(activityInfo.packageName, activityInfo.name)
|
||||
Intent(Intent.ACTION_CREATE_SHORTCUT).apply {
|
||||
|
|
|
@ -8,7 +8,6 @@ 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, activityName: String) {
|
||||
// if this is true, launch the app settings
|
||||
|
@ -48,13 +47,3 @@ fun Activity.uninstallApp(packageName: String) {
|
|||
startActivityForResult(this, UNINSTALL_APP_REQUEST_CODE)
|
||||
}
|
||||
}
|
||||
|
||||
// launch static or dynamic shortcuts that have intents as string
|
||||
fun Activity.launchShortcutIntent(item: HomeScreenGridItem) {
|
||||
try {
|
||||
val intent = Intent.parseUri(item.intent, 0)
|
||||
startActivity(intent)
|
||||
} catch (e: Exception) {
|
||||
showErrorToast(e)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ import android.util.Size
|
|||
import android.util.SizeF
|
||||
import android.view.View
|
||||
import android.widget.RelativeLayout
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
import androidx.core.graphics.drawable.toDrawable
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
|
||||
|
@ -426,12 +427,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||
storeAndShowGridItem(newHomeScreenGridItem)
|
||||
}
|
||||
} else if (newHomeScreenGridItem.type == ITEM_TYPE_SHORTCUT) {
|
||||
(context as? MainActivity)?.handleShorcutCreation(newHomeScreenGridItem.activityInfo!!) { label, icon, intent ->
|
||||
(context as? MainActivity)?.handleShorcutCreation(newHomeScreenGridItem.activityInfo!!) { shortcutId, label, icon ->
|
||||
ensureBackgroundThread {
|
||||
newHomeScreenGridItem.shortcutId = shortcutId
|
||||
newHomeScreenGridItem.title = label
|
||||
newHomeScreenGridItem.icon = icon
|
||||
newHomeScreenGridItem.intent = intent
|
||||
newHomeScreenGridItem.drawable = BitmapDrawable(icon)
|
||||
newHomeScreenGridItem.icon = icon.toBitmap()
|
||||
newHomeScreenGridItem.drawable = icon
|
||||
storeAndShowGridItem(newHomeScreenGridItem)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue