basic support for pinned shortcuts (see #45)
Some checks are pending
Android CI / build (push) Waiting to run

TODO: Show pinned shortcuts in app list
This commit is contained in:
Josia Pietsch 2025-02-18 04:20:27 +01:00
parent befa3afc5d
commit 29f59023a1
Signed by: jrpie
GPG key ID: E70B571D66986A2D
12 changed files with 463 additions and 2 deletions

View file

@ -95,6 +95,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.activity:activity:1.8.0'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'androidx.core:core-ktx:1.15.0'
implementation 'androidx.constraintlayout:constraintlayout:2.2.0'

View file

@ -19,6 +19,16 @@
android:supportsRtl="true"
android:theme="@style/launcherBaseTheme"
tools:ignore="UnusedAttribute">
<activity
android:name=".ui.PinShortcutActivity"
android:autoRemoveFromRecents="true"
android:excludeFromRecents="true"
android:exported="false">
<intent-filter>
<action android:name="android.content.pm.action.CONFIRM_PIN_SHORTCUT" />
<action android:name="android.content.pm.action.CONFIRM_PIN_APPWIDGET" />
</intent-filter>
</activity>
<activity
android:name=".ui.HomeActivity"
android:clearTaskOnLaunch="true"

View file

@ -137,6 +137,9 @@ class Application : android.app.Application() {
)
}
if (Build.VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
removeUnusedShortcuts(this)
}
loadApps()
}

View file

@ -9,7 +9,9 @@ import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.pm.LauncherApps
import android.content.pm.LauncherApps.ShortcutQuery
import android.content.pm.PackageManager
import android.content.pm.ShortcutInfo
import android.net.Uri
import android.os.Build
import android.os.Bundle
@ -18,8 +20,11 @@ import android.os.UserManager
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import de.jrpie.android.launcher.actions.Action
import de.jrpie.android.launcher.actions.Gesture
import de.jrpie.android.launcher.actions.ShortcutAction
import de.jrpie.android.launcher.actions.shortcuts.PinnedShortcutInfo
import de.jrpie.android.launcher.apps.AppInfo
import de.jrpie.android.launcher.apps.DetailedAppInfo
import de.jrpie.android.launcher.apps.getPrivateSpaceUser
@ -81,6 +86,36 @@ fun getUserFromId(userId: Int?, context: Context): UserHandle {
return profiles.firstOrNull { it.hashCode() == userId } ?: profiles[0]
}
@RequiresApi(Build.VERSION_CODES.N_MR1)
fun removeUnusedShortcuts(context: Context) {
val launcherApps = context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps
fun getShortcuts(profile: UserHandle): List<ShortcutInfo>? {
return launcherApps.getShortcuts(
ShortcutQuery().apply {
setQueryFlags(ShortcutQuery.FLAG_MATCH_PINNED)
},
profile
)
}
val userManager = context.getSystemService(Service.USER_SERVICE) as UserManager
val boundActions: Set<PinnedShortcutInfo> =
Gesture.entries.mapNotNull { Action.forGesture(it) as? ShortcutAction }.map { it.shortcut }
.toSet()
try {
userManager.userProfiles.filter { !userManager.isQuietModeEnabled(it) }.forEach { profile ->
Log.e("Shortcuts", "$profile : ${getShortcuts(profile)?.size} shortcuts")
getShortcuts(profile)?.groupBy { it.`package` }?.forEach { (p, shortcuts) ->
launcherApps.pinShortcuts(p,
shortcuts.filter { boundActions.contains(PinnedShortcutInfo(it)) }
.map { it.id }.toList(),
profile
)
}
}
} catch (_: SecurityException) { }
}
fun openInBrowser(url: String, context: Context) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))

View file

@ -0,0 +1,57 @@
package de.jrpie.android.launcher.actions
import android.app.Service
import android.content.Context
import android.content.pm.LauncherApps
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.os.Build
import de.jrpie.android.launcher.actions.shortcuts.PinnedShortcutInfo
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
@SerialName("action:shortcut")
class ShortcutAction(val shortcut: PinnedShortcutInfo) : Action {
override fun invoke(context: Context, rect: Rect?): Boolean {
val launcherApps = context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
// TODO
return false
}
shortcut.getShortcutInfo(context)?.let {
launcherApps.startShortcut(it, rect, null)
}
// TODO: handle null
return true
}
override fun label(context: Context): String {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
return "?"
}
return shortcut.getShortcutInfo(context)?.longLabel?.toString() ?: "?"
}
override fun getIcon(context: Context): Drawable? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
return null
}
val launcherApps = context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps
return shortcut.getShortcutInfo(context)?.let { launcherApps.getShortcutBadgedIconDrawable(it, 0) }
}
override fun isAvailable(context: Context): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
return false
}
return shortcut.getShortcutInfo(context) != null
}
override fun canReachSettings(): Boolean {
return false
}
}

View file

@ -0,0 +1,60 @@
package de.jrpie.android.launcher.actions.shortcuts
import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.pm.LauncherApps
import android.content.pm.LauncherApps.ShortcutQuery
import android.content.pm.ShortcutInfo
import android.os.Build
import androidx.annotation.RequiresApi
import de.jrpie.android.launcher.getUserFromId
import kotlinx.serialization.Serializable
@RequiresApi(Build.VERSION_CODES.N_MR1)
@Serializable
class PinnedShortcutInfo(
val id: String,
val packageName: String,
val activityName: String,
val user: Int
) {
constructor(info: ShortcutInfo) : this(info.id, info.`package`, info.activity?.className ?: "", info.userHandle.hashCode())
fun getShortcutInfo(context: Context): ShortcutInfo? {
val launcherApps = context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps
return launcherApps.getShortcuts(
ShortcutQuery().apply {
setQueryFlags(ShortcutQuery.FLAG_MATCH_PINNED)
setPackage(packageName)
setActivity(ComponentName(packageName, activityName))
setShortcutIds(listOf(id))
},
getUserFromId(user, context)
)?.firstOrNull()
}
override fun equals(other: Any?): Boolean {
return (other as? PinnedShortcutInfo)?.let {
packageName == this.packageName &&
activityName == this.activityName &&
id == this.id &&
user == this.user
} ?: false
}
override fun hashCode(): Int {
var result = id.hashCode()
result = 31 * result + packageName.hashCode()
result = 31 * result + activityName.hashCode()
result = 31 * result + user
return result
}
override fun toString(): String {
return "PinnedShortcutInfo { package=$packageName, activity=$activityName, user=$user, id=$id}"
}
}

View file

@ -0,0 +1,116 @@
package de.jrpie.android.launcher.ui
import android.app.AlertDialog
import android.app.Service
import android.content.pm.LauncherApps
import android.content.pm.LauncherApps.PinItemRequest
import android.content.res.Resources
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.actions.Action
import de.jrpie.android.launcher.actions.Gesture
import de.jrpie.android.launcher.actions.ShortcutAction
import de.jrpie.android.launcher.actions.shortcuts.PinnedShortcutInfo
import de.jrpie.android.launcher.databinding.ActivityPinShortcutBinding
import de.jrpie.android.launcher.preferences.LauncherPreferences
class PinShortcutActivity : AppCompatActivity(), UIObject {
private lateinit var binding: ActivityPinShortcutBinding
override fun onCreate(savedInstanceState: Bundle?) {
super<AppCompatActivity>.onCreate(savedInstanceState)
enableEdgeToEdge()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
finish()
return
}
binding = ActivityPinShortcutBinding.inflate(layoutInflater)
setContentView(binding.root)
val launcherApps = getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps
val request = launcherApps.getPinItemRequest(intent)
if (request == null || request.requestType != PinItemRequest.REQUEST_TYPE_SHORTCUT) {
finish()
return
}
request.accept()
binding.pinShortcutLabel.text = request.shortcutInfo!!.shortLabel ?: "?"
binding.pinShortcutIcon.setImageDrawable(launcherApps.getShortcutBadgedIconDrawable(request.shortcutInfo, 0))
binding.pinShortcutButtonBind.setOnClickListener {
AlertDialog.Builder(this, R.style.AlertDialogCustom)
.setTitle(getString(R.string.pin_shortcut_button_bind))
.setView(R.layout.dialog_select_gesture)
.setNegativeButton(android.R.string.cancel, null)
.create().also { it.show() }.let { dialog ->
val viewManager = LinearLayoutManager(dialog.context)
val viewAdapter = GestureRecyclerAdapter { gesture ->
val editor = LauncherPreferences.getSharedPreferences().edit()
ShortcutAction(PinnedShortcutInfo(request.shortcutInfo!!)).bindToGesture(editor, gesture.id)
editor.apply()
dialog.dismiss()
}
dialog.findViewById<RecyclerView>(R.id.dialog_select_gesture_recycler).apply {
setHasFixedSize(true)
layoutManager = viewManager
adapter = viewAdapter
}
}
}
}
override fun onStart() {
super<AppCompatActivity>.onStart()
super<UIObject>.onStart()
}
override fun getTheme(): Resources.Theme {
return modifyTheme(super.getTheme())
}
inner class GestureRecyclerAdapter(val onClick: (Gesture) -> Unit): RecyclerView.Adapter<GestureRecyclerAdapter.ViewHolder>() {
val gestures = Gesture.entries.filter { it.isEnabled() }.toList()
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val label = itemView.findViewById<TextView>(R.id.dialog_select_gesture_row_name)
val description = itemView.findViewById<TextView>(R.id.dialog_select_gesture_row_description)
val icon = itemView.findViewById<ImageView>(R.id.dialog_select_gesture_row_icon)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view: View = inflater.inflate(R.layout.dialog_select_gesture_row, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val gesture = gestures[position]
holder.label.text = gesture.getLabel(applicationContext)
holder.description.text = gesture.getDescription(applicationContext)
holder.icon.setImageDrawable(
Action.forGesture(gesture)?.getIcon(applicationContext)
)
holder.itemView.setOnClickListener {
onClick(gesture)
}
}
override fun getItemCount(): Int {
return gestures.size
}
}
}

View file

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ui.PinShortcutActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/pin_shortcut_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:gravity="center"
app:elevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/list_heading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="@dimen/appbar_padding"
android:text="@string/pin_shortcut_title"
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:textSize="30sp" />
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
app:layout_constraintTop_toBottomOf="@id/pin_shortcut_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/pin_shortcut_icon"
android:layout_width="40sp"
android:layout_height="40sp" />
<TextView
android:layout_marginLeft="10dp"
android:id="@+id/pin_shortcut_label"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
tools:text="Shortcut name" />
</LinearLayout>
<!--
<Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<CheckBox
android:id="@+id/pin_shortcut_switch_visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColor"
android:text="@string/pin_shortcut_switch_visible" />
-->
<Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<Button
android:id="@+id/pin_shortcut_button_bind"
android:text="@string/pin_shortcut_button_bind"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dialog_select_gesture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/dialog_select_gesture_recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:fadeScrollbars="false"
app:fastScrollEnabled="true"
app:fastScrollHorizontalThumbDrawable="@drawable/fast_scroll_thumb_drawable"
app:fastScrollHorizontalTrackDrawable="@drawable/fast_scroll_track_drawable"
app:fastScrollVerticalThumbDrawable="@drawable/fast_scroll_thumb_drawable"
app:fastScrollVerticalTrackDrawable="@drawable/fast_scroll_track_drawable" >
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

View file

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dialog_select_gesture_row_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:gravity="start"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/dialog_select_gesture_row_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/dialog_select_gesture_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:textSize="15sp"
tools:text="Action label" />
<TextView
android:id="@+id/dialog_select_gesture_row_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:textSize="11sp"
android:visibility="visible"
tools:text="A verbose description of how to perform the action" />
</LinearLayout>
<ImageView
android:id="@+id/dialog_select_gesture_row_icon"
android:layout_width="@dimen/app_icon_side"
android:layout_height="@dimen/app_icon_side"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -250,6 +250,11 @@
<string name="list_other_lock_screen">Lock Screen</string>
<string name="list_other_torch">Toggle Torch</string>
<!-- Pin shortcuts -->
<string name="pin_shortcut_title">Add Shortcut</string>
<string name="pin_shortcut_button_bind">Bind to gesture</string>
<string name="pin_shortcut_switch_visible">Show in app list</string>
<!--
-
- Tutorial

View file

@ -2,7 +2,7 @@
buildscript {
ext.kotlin_version = '2.0.0'
ext.android_plugin_version = '8.8.0'
ext.android_plugin_version = '8.8.1'
repositories {
google()
mavenCentral()
@ -10,7 +10,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:8.8.0'
classpath 'com.android.tools.build:gradle:8.8.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.android.tools.build:gradle:$android_plugin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"