add handler for ActivityNotFoundException

This commit is contained in:
Josia Pietsch 2025-01-28 02:15:04 +01:00
parent 9f3142cb69
commit f280d36667
Signed by: jrpie
GPG key ID: E70B571D66986A2D
4 changed files with 28 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package de.jrpie.android.launcher
import android.app.Activity import android.app.Activity
import android.app.Service import android.app.Service
import android.app.role.RoleManager import android.app.role.RoleManager
import android.content.ActivityNotFoundException
import android.content.ClipData import android.content.ClipData
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context import android.content.Context
@ -16,6 +17,7 @@ import android.os.UserHandle
import android.os.UserManager import android.os.UserManager
import android.provider.Settings import android.provider.Settings
import android.util.Log import android.util.Log
import android.widget.Toast
import de.jrpie.android.launcher.actions.Action import de.jrpie.android.launcher.actions.Action
import de.jrpie.android.launcher.actions.Gesture import de.jrpie.android.launcher.actions.Gesture
import de.jrpie.android.launcher.apps.AppInfo import de.jrpie.android.launcher.apps.AppInfo
@ -83,14 +85,18 @@ fun getPrivateSpaceUser(context: Context): UserHandle? {
val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
return userManager.userProfiles.firstOrNull { u -> return userManager.userProfiles.firstOrNull { u ->
launcherApps.getLauncherUserInfo(u)?.userType == UserManager.USER_TYPE_PROFILE_PRIVATE launcherApps.getLauncherUserInfo(u)?.userType == UserManager.USER_TYPE_PROFILE_PRIVATE
} }
} }
fun openInBrowser(url: String, context: Context) { fun openInBrowser(url: String, context: Context) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
intent.putExtras(Bundle().apply { putBoolean("new_window", true) }) intent.putExtras(Bundle().apply { putBoolean("new_window", true) })
context.startActivity(intent) try {
context.startActivity(intent)
} catch (_: ActivityNotFoundException) {
Toast.makeText(context, R.string.toast_activity_not_found_browser, Toast.LENGTH_LONG).show()
}
} }
fun openTutorial(context: Context) { fun openTutorial(context: Context) {

View file

@ -2,6 +2,7 @@ package de.jrpie.android.launcher.actions
import android.app.AlertDialog import android.app.AlertDialog
import android.app.Service import android.app.Service
import android.content.ActivityNotFoundException
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.LauncherApps import android.content.pm.LauncherApps
@ -34,7 +35,11 @@ class AppAction(val app: AppInfo) : Action {
context.packageManager.getLaunchIntentForPackage(packageName)?.let { context.packageManager.getLaunchIntentForPackage(packageName)?.let {
it.addCategory(Intent.CATEGORY_LAUNCHER) it.addCategory(Intent.CATEGORY_LAUNCHER)
context.startActivity(it) try {
context.startActivity(it)
} catch (_: ActivityNotFoundException) {
return false
}
return true return true
} }

View file

@ -1,11 +1,13 @@
package de.jrpie.android.launcher.ui.list.apps package de.jrpie.android.launcher.ui.list.apps
import android.content.ActivityNotFoundException
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import de.jrpie.android.launcher.R import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.apps.AppFilter import de.jrpie.android.launcher.apps.AppFilter
@ -91,7 +93,16 @@ class ListFragmentApps : Fragment(), UIObject {
if (LauncherPreferences.functionality().searchWeb()) { if (LauncherPreferences.functionality().searchWeb()) {
val i = Intent(Intent.ACTION_WEB_SEARCH).putExtra("query", query) val i = Intent(Intent.ACTION_WEB_SEARCH).putExtra("query", query)
activity?.startActivity(i) try {
activity?.startActivity(i)
} catch (_: ActivityNotFoundException) {
Toast.makeText(
requireContext(),
R.string.toast_activity_not_found_search_web,
Toast.LENGTH_LONG
).show()
}
} else { } else {
appsRecyclerAdapter.selectItem(0) appsRecyclerAdapter.selectItem(0)
} }

View file

@ -328,5 +328,7 @@
<string name="dialog_cancel">Cancel</string> <string name="dialog_cancel">Cancel</string>
<string name="settings_meta_licenses">Open Source Licenses</string> <string name="settings_meta_licenses">Open Source Licenses</string>
<string name="legal_info_title">Open Source Licenses</string> <string name="legal_info_title">Open Source Licenses</string>
<string name="toast_activity_not_found_search_web">No app found to handle search.</string>
<string name="toast_activity_not_found_browser">Can\'t open URL: no browser found.</string>
</resources> </resources>