Skip to content

Commit

Permalink
Handle exceptions when getting app icon in SplitTunnelingScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Jan 23, 2024
1 parent e0307dd commit ca4d52d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.graphics.drawable.toBitmapOrNull
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import net.mullvad.mullvadvpn.R
Expand All @@ -42,6 +41,7 @@ import net.mullvad.mullvadvpn.compose.state.SplitTunnelingUiState
import net.mullvad.mullvadvpn.compose.transitions.SlideInFromRightTransition
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.util.getApplicationIconBitmapOrNull
import net.mullvad.mullvadvpn.viewmodel.SplitTunnelingViewModel
import org.koin.androidx.compose.koinViewModel

Expand Down Expand Up @@ -93,7 +93,7 @@ fun SplitTunneling(navigator: DestinationsNavigator) {
onIncludeAppClick = viewModel::onIncludeAppClick,
onBackClick = navigator::navigateUp,
onResolveIcon = { packageName ->
packageManager.getApplicationIcon(packageName).toBitmapOrNull()
packageManager.getApplicationIconBitmapOrNull(packageName)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.mullvad.mullvadvpn.util

import android.content.pm.PackageManager
import android.graphics.Bitmap
import androidx.core.graphics.drawable.toBitmapOrNull

fun PackageManager.getApplicationIconBitmapOrNull(packageName: String): Bitmap? =
try {
getApplicationIcon(packageName).toBitmapOrNull()
} catch (e: Exception) {
// Name not found is thrown if the application is not installed
// IllegalArgumentException is thrown if the application has an invalid icon
when (e) {
is PackageManager.NameNotFoundException,
is IllegalArgumentException -> null
else -> throw e
}
}

0 comments on commit ca4d52d

Please sign in to comment.