mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
Create shortcuts dynamically
This commit is contained in:
parent
0b4ee3a5eb
commit
26e2c38f60
@ -82,9 +82,6 @@
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity>
|
||||
|
||||
<activity-alias
|
||||
@ -104,9 +101,6 @@
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity-alias>
|
||||
|
||||
<activity-alias
|
||||
@ -126,9 +120,6 @@
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity-alias>
|
||||
|
||||
<activity-alias
|
||||
@ -148,9 +139,6 @@
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity-alias>
|
||||
|
||||
<activity-alias
|
||||
@ -170,9 +158,6 @@
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity-alias>
|
||||
|
||||
<activity-alias
|
||||
@ -190,9 +175,6 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity-alias>
|
||||
|
||||
<activity-alias
|
||||
@ -212,9 +194,6 @@
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity-alias>
|
||||
|
||||
<activity-alias
|
||||
@ -234,9 +213,6 @@
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity-alias>
|
||||
|
||||
<activity-alias
|
||||
@ -256,9 +232,6 @@
|
||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts" />
|
||||
</activity-alias>
|
||||
|
||||
<activity
|
||||
|
@ -17,6 +17,7 @@ import com.github.libretube.util.ImageHelper
|
||||
import com.github.libretube.util.NotificationHelper
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.github.libretube.util.ProxyHelper
|
||||
import com.github.libretube.util.ShortcutHelper
|
||||
|
||||
class LibreTubeApp : Application() {
|
||||
override fun onCreate() {
|
||||
@ -69,6 +70,11 @@ class LibreTubeApp : Application() {
|
||||
val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
|
||||
val exceptionHandler = ExceptionHandler(defaultExceptionHandler)
|
||||
Thread.setDefaultUncaughtExceptionHandler(exceptionHandler)
|
||||
|
||||
/**
|
||||
* Dynamically create App Shortcuts
|
||||
*/
|
||||
ShortcutHelper.createShortcuts(this)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,4 +17,5 @@ object IntentData {
|
||||
const val subtitleCode = "subtitleCode"
|
||||
const val downloading = "downloading"
|
||||
const val openAudioPlayer = "openAudioPlayer"
|
||||
const val fragmentToOpen = "fragmentToOpen"
|
||||
}
|
||||
|
10
app/src/main/java/com/github/libretube/obj/AppShortcut.kt
Normal file
10
app/src/main/java/com/github/libretube/obj/AppShortcut.kt
Normal file
@ -0,0 +1,10 @@
|
||||
package com.github.libretube.obj
|
||||
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.StringRes
|
||||
|
||||
data class AppShortcut(
|
||||
val action: String,
|
||||
@StringRes val label: Int,
|
||||
@DrawableRes val drawable: Int
|
||||
)
|
@ -0,0 +1,45 @@
|
||||
package com.github.libretube.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.core.content.pm.ShortcutInfoCompat
|
||||
import androidx.core.content.pm.ShortcutManagerCompat
|
||||
import androidx.core.graphics.drawable.IconCompat
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.constants.IntentData
|
||||
import com.github.libretube.obj.AppShortcut
|
||||
import com.github.libretube.ui.activities.MainActivity
|
||||
|
||||
object ShortcutHelper {
|
||||
private val shortcuts = listOf(
|
||||
AppShortcut("home", R.string.startpage, R.drawable.ic_home),
|
||||
AppShortcut("trends", R.string.trends, R.drawable.ic_trending),
|
||||
AppShortcut("subscriptions", R.string.subscriptions, R.drawable.ic_subscriptions),
|
||||
AppShortcut("library", R.string.library, R.drawable.ic_library)
|
||||
).reversed()
|
||||
|
||||
private fun createShortcut(context: Context, action: String, label: String, icon: IconCompat) {
|
||||
val shortcut = ShortcutInfoCompat.Builder(context, action)
|
||||
.setShortLabel(label)
|
||||
.setLongLabel(label)
|
||||
.setIcon(icon)
|
||||
.setIntent(
|
||||
Intent(context, MainActivity::class.java).apply {
|
||||
this.action = Intent.ACTION_VIEW
|
||||
putExtra(IntentData.fragmentToOpen, action)
|
||||
}
|
||||
)
|
||||
.build()
|
||||
|
||||
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
|
||||
}
|
||||
|
||||
fun createShortcuts(context: Context) {
|
||||
ShortcutManagerCompat.getDynamicShortcuts(context).takeIf { it.isEmpty() } ?: return
|
||||
|
||||
shortcuts.forEach {
|
||||
val icon = IconCompat.createWithResource(context, it.drawable)
|
||||
createShortcut(context, it.action, context.getString(it.label), icon)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
tools:targetApi="25">
|
||||
<shortcut
|
||||
android:enabled="true"
|
||||
android:icon="@drawable/ic_home"
|
||||
android:shortcutId="home"
|
||||
android:shortcutShortLabel="@string/startpage">
|
||||
<intent
|
||||
android:action="android.intent.action.VIEW"
|
||||
android:targetClass="com.github.libretube.ui.activities.MainActivity"
|
||||
android:targetPackage="com.github.libretube" />
|
||||
</shortcut>
|
||||
<shortcut
|
||||
android:enabled="true"
|
||||
android:icon="@drawable/ic_trending"
|
||||
android:shortcutId="trends"
|
||||
android:shortcutShortLabel="@string/trends">
|
||||
<intent
|
||||
android:action="android.intent.action.VIEW"
|
||||
android:targetClass="com.github.libretube.ui.activities.MainActivity"
|
||||
android:targetPackage="com.github.libretube" />
|
||||
</shortcut>
|
||||
<shortcut
|
||||
android:enabled="true"
|
||||
android:icon="@drawable/ic_subscriptions"
|
||||
android:shortcutId="subscriptions"
|
||||
android:shortcutShortLabel="@string/subscriptions">
|
||||
<intent
|
||||
android:action="android.intent.action.VIEW"
|
||||
android:targetClass="com.github.libretube.ui.activities.MainActivity"
|
||||
android:targetPackage="com.github.libretube">
|
||||
<extra
|
||||
android:name="fragmentToOpen"
|
||||
android:value="subscriptions" />
|
||||
</intent>
|
||||
</shortcut>
|
||||
<shortcut
|
||||
android:enabled="true"
|
||||
android:icon="@drawable/ic_library"
|
||||
android:shortcutId="library"
|
||||
android:shortcutShortLabel="@string/library">
|
||||
<intent
|
||||
android:action="android.intent.action.VIEW"
|
||||
android:targetClass="com.github.libretube.ui.activities.MainActivity"
|
||||
android:targetPackage="com.github.libretube">
|
||||
<extra
|
||||
android:name="fragmentToOpen"
|
||||
android:value="library" />
|
||||
</intent>
|
||||
</shortcut>
|
||||
</shortcuts>
|
Loading…
x
Reference in New Issue
Block a user