Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/java/com/github/shingyx/boomswitch/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.content.pm.ShortcutManagerCompat
import com.github.shingyx.boomswitch.BuildConfig
import com.github.shingyx.boomswitch.R
import com.github.shingyx.boomswitch.data.AppColorTheme
Expand Down Expand Up @@ -108,11 +109,15 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
if (!ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
menu.findItem(R.id.create_shortcut)?.isVisible = false
}
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.create_shortcut -> createShortcut()
R.id.choose_theme -> chooseTheme()
R.id.open_source_licenses -> showOpenSourceLicenses()
R.id.help -> showHelp()
Expand Down Expand Up @@ -185,6 +190,11 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
adapter.updateItems(devicesInfo)
}

private fun createShortcut() {
val shortcutInfo = ShortcutActivity.createShortcutInfo(this, Preferences.bluetoothDeviceInfo)
ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null)
}

private fun chooseTheme() {
val themes = AppColorTheme.entries
val themeNames = themes.map { getString(it.descriptionResId) }.toTypedArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.graphics.drawable.IconCompat
import com.github.shingyx.boomswitch.R
import com.github.shingyx.boomswitch.data.BluetoothDeviceInfo
import com.github.shingyx.boomswitch.data.BoomClient
Expand Down Expand Up @@ -66,5 +68,22 @@ class ShortcutActivity : AppCompatActivity(), CoroutineScope by MainScope() {
putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes)
}
}

fun createShortcutInfo(
context: Context,
bluetoothDeviceInfo: BluetoothDeviceInfo?,
): ShortcutInfoCompat {
val shortcutIntent = Intent(ACTION_SWITCH, null, context, ShortcutActivity::class.java)
bluetoothDeviceInfo?.addToIntent(shortcutIntent)
val shortcutId = bluetoothDeviceInfo?.address ?: "last-used"
val shortcutName = bluetoothDeviceInfo?.name ?: context.getString(R.string.app_name)
val icon = IconCompat.createWithResource(context, R.mipmap.ic_launcher)

return ShortcutInfoCompat.Builder(context, shortcutId)
.setIntent(shortcutIntent)
.setShortLabel(shortcutName)
.setIcon(icon)
.build()
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/create_shortcut"
android:title="@string/create_shortcut"
app:showAsAction="never" />
<item
android:id="@+id/choose_theme"
android:title="@string/choose_theme"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<string name="error_timed_out">Timed out switching %1$s\'s power.</string>
<string name="error_switching_already_in_progress">Switching %1$s already in progress</string>

<string name="create_shortcut">Create shortcut</string>
<string name="choose_theme">Choose theme</string>
<string name="light">Light</string>
<string name="dark">Dark</string>
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
agp = "8.12.1"
agp = "8.13.0"
appcompat = "1.7.1"
constraintlayout = "2.2.1"
kotlin = "2.2.10"
Expand Down