Cleanup exception catching under lawnchair (#3065)

This commit is contained in:
Goooler
2022-10-31 13:49:42 +08:00
committed by GitHub
parent 0a6f68690f
commit bc4b9c9518
23 changed files with 55 additions and 99 deletions
@@ -191,8 +191,7 @@ class LawnchairLauncher : QuickstepLauncher(), LifecycleOwner,
lifecycleScope.launch {
try {
RootHelperManager.INSTANCE.get(this@LawnchairLauncher).getService()
} catch (e: RootNotAvailableException) {
// do nothing
} catch (_: RootNotAvailableException) {
}
}
}
@@ -418,10 +417,8 @@ class LawnchairLauncher : QuickstepLauncher(), LifecycleOwner,
val Context.launcher: LawnchairLauncher
get() = BaseActivity.fromContext(this)
val Context.launcherNullable: LawnchairLauncher? get() {
return try {
launcher
} catch (e: IllegalArgumentException) {
null
}
val Context.launcherNullable: LawnchairLauncher? get() = try {
launcher
} catch (_: IllegalArgumentException) {
null
}
@@ -28,11 +28,7 @@ class LawnchairLayoutFactory(context: Context) : LayoutInflater.Factory2 {
): View? {
val view = constructorMap[name]?.let { it(context, attrs) }
if (view is TextView) {
try {
fontManager.overrideFont(view, attrs)
} catch (_: Exception) {
}
runCatching { fontManager.overrideFont(view, attrs) }
}
return view
}
@@ -71,8 +71,7 @@ fun Context.getAppName(name: String): CharSequence {
try {
return packageManager.getApplicationLabel(
packageManager.getApplicationInfo(name, PackageManager.GET_META_DATA))
} catch (ignored: PackageManager.NameNotFoundException) {
} catch (_: PackageManager.NameNotFoundException) {
}
return name
}
@@ -216,8 +216,7 @@ class SearchResultIcon(context: Context, attrs: AttributeSet?) : BubbleTextView(
val bitmap = li.createIconBitmap(activityIcon, 1f, iconSize)
val bitmapInfo = BitmapInfo.of(bitmap, packageIcon.color)
info.bitmap = li.badgeBitmap(info.bitmap.icon, bitmapInfo)
} catch (ignore: PackageManager.NameNotFoundException) {
} catch (_: PackageManager.NameNotFoundException) {
}
} else if (info.hasFlags(FLAG_BADGE_WITH_PACKAGE) && info.bitmap != packageIcon) {
info.bitmap = li.badgeBitmap(info.bitmap.icon, packageIcon)
@@ -83,9 +83,7 @@ fun CreateBackupScreen(viewModel: CreateBackupViewModel) {
} catch (t: Throwable) {
Log.e("CreateBackupScreen", "failed to create backup", t)
Toast.makeText(context, R.string.backup_create_error, Toast.LENGTH_SHORT).show()
try {
DocumentsContract.deleteDocument(context.contentResolver, uri)
} catch (ignore: Throwable) {}
runCatching { DocumentsContract.deleteDocument(context.contentResolver, uri) }
}
creatingBackup = false
}
@@ -238,13 +238,8 @@ class FontCache private constructor(private val context: Context) {
companion object {
fun createTypeface(file: File): Typeface? {
return try {
Typeface.createFromFile(file)
} catch (e: Exception) {
null
}
}
fun createTypeface(file: File): Typeface? =
runCatching { Typeface.createFromFile(file) }.getOrNull()
fun getFontsDir(context: Context): File {
return File(context.filesDir, "customFonts").apply { mkdirs() }
@@ -58,8 +58,7 @@ class FontManager private constructor(private val context: Context) {
if (fontType != -1) {
setCustomFont(textView, fontType, fontWeight)
}
} catch (e: Exception) {
// ignore
} catch (_: Exception) {
}
}
@@ -45,8 +45,8 @@ open class DirectionalGestureListener(ctx: Context?) : OnTouchListener {
}
else -> false
}
} catch (exception: Exception) {
exception.printStackTrace()
} catch (e: Exception) {
e.printStackTrace()
false
}
}
@@ -60,4 +60,4 @@ open class DirectionalGestureListener(ctx: Context?) : OnTouchListener {
init {
mGestureDetector = GestureDetector(ctx, GestureListener())
}
}
}
@@ -53,7 +53,7 @@ class CustomIconPack(context: Context, packPackageName: String) :
packResources.getDrawableForDensity(id, iconDpi, null),
true
)
} catch (e: Resources.NotFoundException) {
} catch (_: Resources.NotFoundException) {
null
}
}
@@ -26,7 +26,7 @@ class IconPackProvider(private val context: Context) {
return iconPacks.getOrPut(packageName) {
try {
CustomIconPack(context, packageName)
} catch (e: PackageManager.NameNotFoundException) {
} catch (_: PackageManager.NameNotFoundException) {
null
}
}
@@ -366,12 +366,7 @@ open class IconShape(val topLeft: Corner,
"diamond" -> Diamond
"egg" -> Egg
"" -> null
else -> try {
parseCustomShape(value)
} catch (ex: Exception) {
Log.e("IconShape", "Error creating shape $value", ex)
null
}
else -> runCatching { parseCustomShape(value) }.getOrNull()
}
}
@@ -391,8 +386,8 @@ open class IconShape(val topLeft: Corner,
return try {
parseCustomShape(iconShape.toString())
true
} catch (ex: Exception) {
Log.e("IconShape", "Error creating shape $iconShape", ex)
} catch (e: Exception) {
Log.e("IconShape", "Error creating shape $iconShape", e)
false
}
}
@@ -202,7 +202,7 @@ abstract class BasePreferenceManager(private val context: Context) : SharedPrefe
if (!loaded) {
currentValue = try {
sp.getInt(key, defaultValueInternal)
} catch (e: ClassCastException) {
} catch (_: ClassCastException) {
sp.getFloat(key, defaultValueInternal.toFloat()).toInt()
}
loaded = true
@@ -310,14 +310,9 @@ abstract class BasePreferenceManager(private val context: Context) : SharedPrefe
primaryListener: ChangeListener? = null
) : StringBasedPref<FontCache.Font>(key, defaultValue, primaryListener) {
override fun parse(stringValue: String): FontCache.Font {
try {
return FontCache.Font.fromJsonString(context, stringValue)
} catch (e: Exception) {
Log.e("FontPref", "Failed to load font $stringValue", e)
}
return defaultValue
}
override fun parse(stringValue: String): FontCache.Font = runCatching {
FontCache.Font.fromJsonString(context, stringValue)
}.getOrDefault(defaultValue)
override fun stringify(value: FontCache.Font) = value.toJsonString()
}
@@ -421,12 +421,7 @@ class PreferenceManager2(private val context: Context) : PreferenceManager {
key = key,
defaultValue = defaultValue,
parse = { value ->
try {
return@preference Json.decodeFromString(value)
} catch (e: Throwable) {
Log.d("PreferenceManager2", "failed to parse preference $key=$value")
return@preference defaultValue
}
runCatching { Json.decodeFromString<T>(value) }.getOrDefault(defaultValue)
},
save = Json::encodeToString,
)
@@ -92,7 +92,7 @@ open class QsbSearchProvider(
Uri.parse("market://details?id=$packageName")
)
)
} catch (e: ActivityNotFoundException) {
} catch (_: ActivityNotFoundException) {
try {
context.startActivity(
Intent(
@@ -100,7 +100,7 @@ open class QsbSearchProvider(
Uri.parse("https://play.google.com/store/apps/details?id=$packageName")
)
)
} catch (e: Exception) {
} catch (_: Exception) {
Toast.makeText(
context,
context.getString(R.string.error_no_market_or_browser_installed),
@@ -33,7 +33,7 @@ object BcSmartSpaceUtil {
return
}
view.setOnClickListener {
try {
runCatching {
if (action.intent != null) {
view.context.startActivity(action.intent)
} else if (action.pendingIntent != null) {
@@ -42,8 +42,6 @@ object BcSmartSpaceUtil {
action.onClick.run()
}
onClickListener?.onClick(view)
} catch (t: Throwable) {
Log.d("BcSmartspaceUtil", "onClick error", t)
}
}
}
@@ -62,10 +62,6 @@ class BatteryStatusProvider(context: Context) : SmartspaceDataSource(
private fun computeChargeTimeRemaining(): Long {
if (!Utilities.ATLEAST_P) return -1
return try {
batteryManager?.computeChargeTimeRemaining() ?: -1
} catch (t: Throwable) {
-1
}
return runCatching { batteryManager?.computeChargeTimeRemaining() ?: -1 }.getOrDefault(-1)
}
}
@@ -165,9 +165,9 @@ class SmartspaceWidgetReader(context: Context) : SmartspaceDataSource(
temperature.contains("K") -> Temperature.Unit.Kelvin
else -> throw IllegalArgumentException("only supports C, F and K")
}), pendingIntent = intent)
} catch (e: NumberFormatException) {
} catch (_: NumberFormatException) {
null
} catch (e: IllegalArgumentException) {
} catch (_: IllegalArgumentException) {
null
}
} else {
@@ -88,8 +88,7 @@ sealed class ColorOption {
val color = Color.parseColor(stringValue.substring(7))
return CustomColor(color)
}
} catch (e: IllegalArgumentException) {
// ignore
} catch (_: IllegalArgumentException) {
}
return when {
Utilities.ATLEAST_S -> SystemAccent
@@ -1,21 +1,20 @@
package app.lawnchair.ui.preferences.components.colorpreference
import android.graphics.Color.colorToHSV
import android.graphics.Color.HSVToColor
import android.graphics.Color.colorToHSV
import android.graphics.Color.parseColor
fun intColorToHsvColorArray(color: Int) =
fun intColorToHsvColorArray(color: Int): FloatArray =
FloatArray(size = 3).apply { colorToHSV(color, this) }
fun hsvValuesToIntColor(hue: Float, saturation: Float, brightness: Float): Int =
HSVToColor(floatArrayOf(hue, saturation, brightness))
fun intColorToColorString(color: Int) =
fun intColorToColorString(color: Int): String =
String.format("#%06X", 0xFFFFFF and color).removePrefix("#")
fun colorStringToIntColor(colorString: String): Int? =
try {
parseColor("#${colorString.removePrefix("#")}")
} catch (e: IllegalArgumentException) {
null
}
fun colorStringToIntColor(colorString: String): Int? = try {
parseColor("#${colorString.removePrefix("#")}")
} catch (_: IllegalArgumentException) {
null
}
@@ -51,8 +51,7 @@ fun Context.getSystemAccent(darkTheme: Boolean): Int {
}
try {
return Color.parseColor(propertyValue)
} catch (e: IllegalArgumentException) {
// do nothing
} catch (_: IllegalArgumentException) {
}
}
@@ -31,7 +31,7 @@ fun getSystemProperty(property: String, defaultValue: String): String {
if (!TextUtils.isEmpty(value)) {
return value
}
} catch (e: Exception) {
} catch (_: Exception) {
Log.d(TAG, "Unable to read system properties")
}
return defaultValue
@@ -2,10 +2,8 @@ package app.lawnchair.util
import android.graphics.drawable.GradientDrawable
fun GradientDrawable.getCornerRadiiCompat(): FloatArray? {
return try {
cornerRadii
} catch (e: NullPointerException) {
null
}
fun GradientDrawable.getCornerRadiiCompat(): FloatArray? = try {
cornerRadii
} catch (_: NullPointerException) {
null
}
@@ -3,28 +3,27 @@ package app.lawnchair.util
import android.content.pm.PackageManager
import com.android.launcher3.Utilities
fun PackageManager.isPackageInstalled(packageName: String) =
fun PackageManager.isPackageInstalled(packageName: String): Boolean =
try {
getPackageInfo(packageName, 0)
true
} catch (e: PackageManager.NameNotFoundException) {
} catch (_: PackageManager.NameNotFoundException) {
false
}
fun PackageManager.getPackageVersionCode(packageName: String) =
fun PackageManager.getPackageVersionCode(packageName: String): Long =
try {
val info = getPackageInfo(packageName, 0)
when {
Utilities.ATLEAST_P -> info.longVersionCode
else -> info.versionCode.toLong()
}
} catch (e: PackageManager.NameNotFoundException) {
} catch (_: PackageManager.NameNotFoundException) {
-1L
}
fun PackageManager.isPackageInstalledAndEnabled(packageName: String) =
try {
getApplicationInfo(packageName, 0).enabled
} catch (e: PackageManager.NameNotFoundException) {
false
}
fun PackageManager.isPackageInstalledAndEnabled(packageName: String) = try {
getApplicationInfo(packageName, 0).enabled
} catch (_: PackageManager.NameNotFoundException) {
false
}