Fix for #1110 crash on open uninstalled app details

This commit is contained in:
Marvin W 2020-07-31 10:30:29 +02:00
parent 32ab82924e
commit 914a8307e9
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
6 changed files with 39 additions and 15 deletions

View File

@ -0,0 +1,8 @@
/*
* SPDX-FileCopyrightText: 2020, microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/
package org.microg.gms.ui
const val TAG = "GmsUi"

View File

@ -56,11 +56,7 @@ class PushNotificationAllAppsFragment : PreferenceFragmentCompat() {
val context = requireContext()
val apps = withContext(Dispatchers.IO) {
val res = database.appList.map { app ->
try {
app to context.packageManager.getApplicationInfo(app.packageName, 0)
} catch (ignored: Exception) {
app to null
}
app to context.packageManager.getApplicationInfoIfExists(app.packageName)
}.map { (app, applicationInfo) ->
val pref = AppIconPreference(context)
pref.title = applicationInfo?.loadLabel(context.packageManager) ?: app.packageName

View File

@ -15,7 +15,6 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.content.res.AppCompatResources
import androidx.fragment.app.Fragment
import androidx.fragment.app.findFragment
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.R
import com.google.android.gms.databinding.PushNotificationAppFragmentBinding
@ -34,7 +33,11 @@ class PushNotificationAppFragment : Fragment(R.layout.push_notification_fragment
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
val uri: Uri = Uri.fromParts("package", packageName, null)
intent.data = uri
context!!.startActivity(intent)
try {
context!!.startActivity(intent)
} catch (e: Exception) {
Log.w(TAG, "Failed to launch app", e)
}
}
}
childFragmentManager.findFragmentById(R.id.sub_preferences)?.arguments = arguments
@ -45,7 +48,7 @@ class PushNotificationAppFragment : Fragment(R.layout.push_notification_fragment
super.onResume()
lifecycleScope.launchWhenResumed {
val pm = requireContext().packageManager
val applicationInfo = packageName?.let { pm.getApplicationInfo(it, 0) }
val applicationInfo = pm.getApplicationInfoIfExists(packageName)
binding.appName = applicationInfo?.loadLabel(pm)?.toString() ?: packageName
binding.appIcon = applicationInfo?.loadIcon(pm)
?: AppCompatResources.getDrawable(requireContext(), android.R.mipmap.sym_def_app_icon)

View File

@ -71,7 +71,7 @@ class PushNotificationAppPreferencesFragment : PreferenceFragmentCompat() {
private fun showUnregisterConfirm(unregisterConfirmDesc: Int) {
val pm = requireContext().packageManager
val applicationInfo = packageName?.let { pm.getApplicationInfo(it, 0) }
val applicationInfo = pm.getApplicationInfoIfExists(packageName)
AlertDialog.Builder(requireContext())
.setTitle(getString(R.string.gcm_unregister_confirm_title, applicationInfo?.loadLabel(pm)
?: packageName))

View File

@ -80,12 +80,10 @@ class PushNotificationPreferencesFragment : PreferenceFragmentCompat() {
val (apps, showAll) = withContext(Dispatchers.IO) {
val apps = database.appList.sortedByDescending { it.lastMessageTimestamp }
val res = apps.map { app ->
try {
app to context.packageManager.getApplicationInfo(app.packageName, 0)
} catch (ignored: Exception) {
null
}
}.filterNotNull().take(3).mapIndexed { idx, (app, applicationInfo) ->
app to context.packageManager.getApplicationInfoIfExists(app.packageName)
}.mapNotNull { (app, info) ->
if (info == null) null else app to info
}.take(3).mapIndexed { idx, (app, applicationInfo) ->
val pref = AppIconPreference(context)
pref.order = idx
pref.title = applicationInfo.loadLabel(context.packageManager)

View File

@ -0,0 +1,19 @@
/*
* SPDX-FileCopyrightText: 2020, microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/
package org.microg.gms.ui
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.util.Log
fun PackageManager.getApplicationInfoIfExists(packageName: String?, flags: Int = 0): ApplicationInfo? = packageName?.let {
try {
getApplicationInfo(it, flags)
} catch (e: Exception) {
Log.w(TAG, "Package does not exist", e)
null
}
}