mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 23:07:26 -04:00
Merge pull request #4167 from kiwix/Fixes#4166
Fixed: Application crash while adding application shortcuts.
This commit is contained in:
commit
ef1239fa4a
@ -19,16 +19,16 @@
|
|||||||
package org.kiwix.kiwixmobile.main
|
package org.kiwix.kiwixmobile.main
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.ShortcutInfo
|
|
||||||
import android.content.pm.ShortcutManager
|
|
||||||
import android.content.res.Configuration
|
import android.content.res.Configuration
|
||||||
import android.graphics.drawable.Icon
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.view.ActionMode
|
import androidx.appcompat.view.ActionMode
|
||||||
|
import androidx.core.content.pm.ShortcutInfoCompat
|
||||||
|
import androidx.core.content.pm.ShortcutManagerCompat
|
||||||
|
import androidx.core.graphics.drawable.IconCompat
|
||||||
import androidx.core.os.ConfigurationCompat
|
import androidx.core.os.ConfigurationCompat
|
||||||
import androidx.core.os.bundleOf
|
import androidx.core.os.bundleOf
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
@ -45,8 +45,8 @@ import eu.mhutti1.utils.storage.StorageDeviceUtils
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.kiwix.kiwixmobile.BuildConfig
|
import org.kiwix.kiwixmobile.BuildConfig
|
||||||
import org.kiwix.kiwixmobile.R
|
import org.kiwix.kiwixmobile.R
|
||||||
import org.kiwix.kiwixmobile.core.R.id
|
|
||||||
import org.kiwix.kiwixmobile.core.R.drawable
|
import org.kiwix.kiwixmobile.core.R.drawable
|
||||||
|
import org.kiwix.kiwixmobile.core.R.id
|
||||||
import org.kiwix.kiwixmobile.core.R.mipmap
|
import org.kiwix.kiwixmobile.core.R.mipmap
|
||||||
import org.kiwix.kiwixmobile.core.R.string
|
import org.kiwix.kiwixmobile.core.R.string
|
||||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
|
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
|
||||||
@ -56,6 +56,7 @@ import org.kiwix.kiwixmobile.core.extensions.applyEdgeToEdgeInsets
|
|||||||
import org.kiwix.kiwixmobile.core.extensions.toast
|
import org.kiwix.kiwixmobile.core.extensions.toast
|
||||||
import org.kiwix.kiwixmobile.core.main.ACTION_NEW_TAB
|
import org.kiwix.kiwixmobile.core.main.ACTION_NEW_TAB
|
||||||
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
||||||
|
import org.kiwix.kiwixmobile.core.main.NEW_TAB_SHORTCUT_ID
|
||||||
import org.kiwix.kiwixmobile.core.main.ZIM_FILE_URI_KEY
|
import org.kiwix.kiwixmobile.core.main.ZIM_FILE_URI_KEY
|
||||||
import org.kiwix.kiwixmobile.core.utils.LanguageUtils.Companion.handleLocaleChange
|
import org.kiwix.kiwixmobile.core.utils.LanguageUtils.Companion.handleLocaleChange
|
||||||
import org.kiwix.kiwixmobile.databinding.ActivityKiwixMainBinding
|
import org.kiwix.kiwixmobile.databinding.ActivityKiwixMainBinding
|
||||||
@ -66,6 +67,7 @@ import javax.inject.Inject
|
|||||||
const val NAVIGATE_TO_ZIM_HOST_FRAGMENT = "navigate_to_zim_host_fragment"
|
const val NAVIGATE_TO_ZIM_HOST_FRAGMENT = "navigate_to_zim_host_fragment"
|
||||||
const val ACTION_GET_CONTENT = "GET_CONTENT"
|
const val ACTION_GET_CONTENT = "GET_CONTENT"
|
||||||
const val OPENING_ZIM_FILE_DELAY = 300L
|
const val OPENING_ZIM_FILE_DELAY = 300L
|
||||||
|
const val GET_CONTENT_SHORTCUT_ID = "get_content_shortcut"
|
||||||
|
|
||||||
class KiwixMainActivity : CoreMainActivity() {
|
class KiwixMainActivity : CoreMainActivity() {
|
||||||
private var actionMode: ActionMode? = null
|
private var actionMode: ActionMode? = null
|
||||||
@ -324,13 +326,27 @@ class KiwixMainActivity : CoreMainActivity() {
|
|||||||
override fun getIconResId() = mipmap.ic_launcher
|
override fun getIconResId() = mipmap.ic_launcher
|
||||||
|
|
||||||
override fun createApplicationShortcuts() {
|
override fun createApplicationShortcuts() {
|
||||||
val shortcutManager = getSystemService(ShortcutManager::class.java)
|
// Remove previously added dynamic shortcuts for old ids if any found.
|
||||||
|
removeOutdatedIdShortcuts()
|
||||||
|
ShortcutManagerCompat.addDynamicShortcuts(this, dynamicShortcutList())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Outdated shortcut ids(new_tab, get_content)
|
||||||
|
// Remove if the application has the outdated shortcuts.
|
||||||
|
private fun removeOutdatedIdShortcuts() {
|
||||||
|
ShortcutManagerCompat.getDynamicShortcuts(this).forEach {
|
||||||
|
if (it.id == "new_tab" || it.id == "get_content") {
|
||||||
|
ShortcutManagerCompat.removeDynamicShortcuts(this, arrayListOf(it.id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun dynamicShortcutList(): List<ShortcutInfoCompat> {
|
||||||
// Create a shortcut for opening the "New tab"
|
// Create a shortcut for opening the "New tab"
|
||||||
val newTabShortcut = ShortcutInfo.Builder(this, "new_tab")
|
val newTabShortcut = ShortcutInfoCompat.Builder(this, NEW_TAB_SHORTCUT_ID)
|
||||||
.setShortLabel(getString(string.new_tab_shortcut_label))
|
.setShortLabel(getString(string.new_tab_shortcut_label))
|
||||||
.setLongLabel(getString(string.new_tab_shortcut_label))
|
.setLongLabel(getString(string.new_tab_shortcut_label))
|
||||||
.setIcon(Icon.createWithResource(this, drawable.ic_shortcut_new_tab))
|
.setIcon(IconCompat.createWithResource(this, drawable.ic_shortcut_new_tab))
|
||||||
.setDisabledMessage(getString(string.shortcut_disabled_message))
|
.setDisabledMessage(getString(string.shortcut_disabled_message))
|
||||||
.setIntent(
|
.setIntent(
|
||||||
Intent(this, KiwixMainActivity::class.java).apply {
|
Intent(this, KiwixMainActivity::class.java).apply {
|
||||||
@ -340,10 +356,10 @@ class KiwixMainActivity : CoreMainActivity() {
|
|||||||
.build()
|
.build()
|
||||||
|
|
||||||
// create a shortCut for opening the online fragment.
|
// create a shortCut for opening the online fragment.
|
||||||
val getContentShortcut = ShortcutInfo.Builder(this, "get_content")
|
val getContentShortcut = ShortcutInfoCompat.Builder(this, GET_CONTENT_SHORTCUT_ID)
|
||||||
.setShortLabel(getString(string.get_content_shortcut_label))
|
.setShortLabel(getString(string.get_content_shortcut_label))
|
||||||
.setLongLabel(getString(string.get_content_shortcut_label))
|
.setLongLabel(getString(string.get_content_shortcut_label))
|
||||||
.setIcon(Icon.createWithResource(this, drawable.ic_shortcut_get_content))
|
.setIcon(IconCompat.createWithResource(this, drawable.ic_shortcut_get_content))
|
||||||
.setDisabledMessage(getString(string.shortcut_disabled_message))
|
.setDisabledMessage(getString(string.shortcut_disabled_message))
|
||||||
.setIntent(
|
.setIntent(
|
||||||
Intent(this, KiwixMainActivity::class.java).apply {
|
Intent(this, KiwixMainActivity::class.java).apply {
|
||||||
@ -351,6 +367,7 @@ class KiwixMainActivity : CoreMainActivity() {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
shortcutManager.dynamicShortcuts = listOf(newTabShortcut, getContentShortcut)
|
|
||||||
|
return listOf(newTabShortcut, getContentShortcut)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,6 +77,7 @@ const val FIND_IN_PAGE_SEARCH_STRING = "findInPageSearchString"
|
|||||||
const val ZIM_FILE_URI_KEY = "zimFileUri"
|
const val ZIM_FILE_URI_KEY = "zimFileUri"
|
||||||
const val KIWIX_INTERNAL_ERROR = 10
|
const val KIWIX_INTERNAL_ERROR = 10
|
||||||
const val ACTION_NEW_TAB = "NEW_TAB"
|
const val ACTION_NEW_TAB = "NEW_TAB"
|
||||||
|
const val NEW_TAB_SHORTCUT_ID = "new_tab_shortcut"
|
||||||
|
|
||||||
abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
|
abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
|
||||||
|
|
||||||
|
@ -19,26 +19,27 @@
|
|||||||
package org.kiwix.kiwixmobile.custom.main
|
package org.kiwix.kiwixmobile.custom.main
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.ShortcutInfo
|
|
||||||
import android.content.pm.ShortcutManager
|
|
||||||
import android.graphics.drawable.Icon
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.widget.Toolbar
|
import androidx.appcompat.widget.Toolbar
|
||||||
|
import androidx.core.content.pm.ShortcutInfoCompat
|
||||||
|
import androidx.core.content.pm.ShortcutManagerCompat
|
||||||
|
import androidx.core.graphics.drawable.IconCompat
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import androidx.drawerlayout.widget.DrawerLayout
|
import androidx.drawerlayout.widget.DrawerLayout
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.fragment.NavHostFragment
|
import androidx.navigation.fragment.NavHostFragment
|
||||||
import com.google.android.material.navigation.NavigationView
|
import com.google.android.material.navigation.NavigationView
|
||||||
|
import org.kiwix.kiwixmobile.core.R.drawable
|
||||||
|
import org.kiwix.kiwixmobile.core.R.string
|
||||||
|
import org.kiwix.kiwixmobile.core.extensions.applyEdgeToEdgeInsets
|
||||||
import org.kiwix.kiwixmobile.core.extensions.browserIntent
|
import org.kiwix.kiwixmobile.core.extensions.browserIntent
|
||||||
import org.kiwix.kiwixmobile.core.main.ACTION_NEW_TAB
|
import org.kiwix.kiwixmobile.core.main.ACTION_NEW_TAB
|
||||||
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
||||||
|
import org.kiwix.kiwixmobile.core.main.NEW_TAB_SHORTCUT_ID
|
||||||
import org.kiwix.kiwixmobile.custom.BuildConfig
|
import org.kiwix.kiwixmobile.custom.BuildConfig
|
||||||
import org.kiwix.kiwixmobile.custom.R
|
import org.kiwix.kiwixmobile.custom.R
|
||||||
import org.kiwix.kiwixmobile.core.R.string
|
|
||||||
import org.kiwix.kiwixmobile.core.R.drawable
|
|
||||||
import org.kiwix.kiwixmobile.core.extensions.applyEdgeToEdgeInsets
|
|
||||||
import org.kiwix.kiwixmobile.custom.customActivityComponent
|
import org.kiwix.kiwixmobile.custom.customActivityComponent
|
||||||
import org.kiwix.kiwixmobile.custom.databinding.ActivityCustomMainBinding
|
import org.kiwix.kiwixmobile.custom.databinding.ActivityCustomMainBinding
|
||||||
|
|
||||||
@ -192,12 +193,13 @@ class CustomMainActivity : CoreMainActivity() {
|
|||||||
override fun getIconResId() = R.mipmap.ic_launcher
|
override fun getIconResId() = R.mipmap.ic_launcher
|
||||||
|
|
||||||
override fun createApplicationShortcuts() {
|
override fun createApplicationShortcuts() {
|
||||||
val shortcutManager = getSystemService(ShortcutManager::class.java)
|
// Remove previously added dynamic shortcuts for old ids if any found.
|
||||||
|
removeOutdatedIdShortcuts()
|
||||||
// Create a shortcut for opening the "New tab"
|
// Create a shortcut for opening the "New tab"
|
||||||
val newTabShortcut = ShortcutInfo.Builder(this, "new_tab")
|
val newTabShortcut = ShortcutInfoCompat.Builder(this, NEW_TAB_SHORTCUT_ID)
|
||||||
.setShortLabel(getString(string.new_tab_shortcut_label))
|
.setShortLabel(getString(string.new_tab_shortcut_label))
|
||||||
.setLongLabel(getString(string.new_tab_shortcut_label))
|
.setLongLabel(getString(string.new_tab_shortcut_label))
|
||||||
.setIcon(Icon.createWithResource(this, drawable.ic_shortcut_new_tab))
|
.setIcon(IconCompat.createWithResource(this, drawable.ic_shortcut_new_tab))
|
||||||
.setDisabledMessage(getString(string.shortcut_disabled_message))
|
.setDisabledMessage(getString(string.shortcut_disabled_message))
|
||||||
.setIntent(
|
.setIntent(
|
||||||
Intent(this, CustomMainActivity::class.java).apply {
|
Intent(this, CustomMainActivity::class.java).apply {
|
||||||
@ -205,6 +207,16 @@ class CustomMainActivity : CoreMainActivity() {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
shortcutManager.dynamicShortcuts = listOf(newTabShortcut)
|
ShortcutManagerCompat.addDynamicShortcuts(this, listOf(newTabShortcut))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Outdated shortcut id(new_tab)
|
||||||
|
// Remove if the application has the outdated shortcut.
|
||||||
|
private fun removeOutdatedIdShortcuts() {
|
||||||
|
ShortcutManagerCompat.getDynamicShortcuts(this).forEach {
|
||||||
|
if (it.id == "new_tab") {
|
||||||
|
ShortcutManagerCompat.removeDynamicShortcuts(this, arrayListOf(it.id))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user