Skip to content

Commit

Permalink
Merge pull request #4167 from kiwix/Fixes#4166
Browse files Browse the repository at this point in the history
Fixed: Application crash while adding application shortcuts.
  • Loading branch information
kelson42 authored Jan 19, 2025
2 parents bcc6ac8 + 08e004a commit ef1239f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
37 changes: 27 additions & 10 deletions app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
package org.kiwix.kiwixmobile.main

import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.content.res.Configuration
import android.graphics.drawable.Icon
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
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.bundleOf
import androidx.core.view.isVisible
Expand All @@ -45,8 +45,8 @@ import eu.mhutti1.utils.storage.StorageDeviceUtils
import kotlinx.coroutines.launch
import org.kiwix.kiwixmobile.BuildConfig
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.id
import org.kiwix.kiwixmobile.core.R.mipmap
import org.kiwix.kiwixmobile.core.R.string
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
Expand All @@ -56,6 +56,7 @@ import org.kiwix.kiwixmobile.core.extensions.applyEdgeToEdgeInsets
import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.main.ACTION_NEW_TAB
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.utils.LanguageUtils.Companion.handleLocaleChange
import org.kiwix.kiwixmobile.databinding.ActivityKiwixMainBinding
Expand All @@ -66,6 +67,7 @@ import javax.inject.Inject
const val NAVIGATE_TO_ZIM_HOST_FRAGMENT = "navigate_to_zim_host_fragment"
const val ACTION_GET_CONTENT = "GET_CONTENT"
const val OPENING_ZIM_FILE_DELAY = 300L
const val GET_CONTENT_SHORTCUT_ID = "get_content_shortcut"

class KiwixMainActivity : CoreMainActivity() {
private var actionMode: ActionMode? = null
Expand Down Expand Up @@ -324,13 +326,27 @@ class KiwixMainActivity : CoreMainActivity() {
override fun getIconResId() = mipmap.ic_launcher

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"
val newTabShortcut = ShortcutInfo.Builder(this, "new_tab")
val newTabShortcut = ShortcutInfoCompat.Builder(this, NEW_TAB_SHORTCUT_ID)
.setShortLabel(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))
.setIntent(
Intent(this, KiwixMainActivity::class.java).apply {
Expand All @@ -340,17 +356,18 @@ class KiwixMainActivity : CoreMainActivity() {
.build()

// 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))
.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))
.setIntent(
Intent(this, KiwixMainActivity::class.java).apply {
action = ACTION_GET_CONTENT
}
)
.build()
shortcutManager.dynamicShortcuts = listOf(newTabShortcut, getContentShortcut)

return listOf(newTabShortcut, getContentShortcut)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const val FIND_IN_PAGE_SEARCH_STRING = "findInPageSearchString"
const val ZIM_FILE_URI_KEY = "zimFileUri"
const val KIWIX_INTERNAL_ERROR = 10
const val ACTION_NEW_TAB = "NEW_TAB"
const val NEW_TAB_SHORTCUT_ID = "new_tab_shortcut"

abstract class CoreMainActivity : BaseActivity(), WebViewProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,27 @@
package org.kiwix.kiwixmobile.custom.main

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.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
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.drawerlayout.widget.DrawerLayout
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
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.main.ACTION_NEW_TAB
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.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.databinding.ActivityCustomMainBinding

Expand Down Expand Up @@ -192,19 +193,30 @@ class CustomMainActivity : CoreMainActivity() {
override fun getIconResId() = R.mipmap.ic_launcher

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"
val newTabShortcut = ShortcutInfo.Builder(this, "new_tab")
val newTabShortcut = ShortcutInfoCompat.Builder(this, NEW_TAB_SHORTCUT_ID)
.setShortLabel(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))
.setIntent(
Intent(this, CustomMainActivity::class.java).apply {
action = ACTION_NEW_TAB
}
)
.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))
}
}
}
}

0 comments on commit ef1239f

Please sign in to comment.