From 91227e445a9411059aac5062fc863b1fcb5dcb60 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 22 Mar 2023 21:17:18 +0530 Subject: ui: address PackageManager API deprecations Signed-off-by: Harsh Shandilya --- .../wireguard/android/fragment/AppListDialogFragment.kt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'ui') diff --git a/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt index e137a8b4..627a9728 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt @@ -6,6 +6,10 @@ package com.wireguard.android.fragment import android.Manifest import android.app.Dialog +import android.content.pm.PackageInfo +import android.content.pm.PackageManager +import android.content.pm.PackageManager.PackageInfoFlags +import android.os.Build import android.os.Bundle import android.widget.Button import android.widget.Toast @@ -41,7 +45,7 @@ class AppListDialogFragment : DialogFragment() { try { val applicationData: MutableList = ArrayList() withContext(Dispatchers.IO) { - val packageInfos = pm.getPackagesHoldingPermissions(arrayOf(Manifest.permission.INTERNET), 0) + val packageInfos = getPackagesHoldingPermissions(pm, arrayOf(Manifest.permission.INTERNET)) packageInfos.forEach { val packageName = it.packageName val appInfo = it.applicationInfo @@ -78,6 +82,15 @@ class AppListDialogFragment : DialogFragment() { initiallyExcluded = arguments?.getBoolean(KEY_IS_EXCLUDED) ?: true } + private fun getPackagesHoldingPermissions(pm: PackageManager, permissions: Array): List { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + pm.getPackagesHoldingPermissions(permissions, PackageInfoFlags.of(0L)) + } else { + @Suppress("DEPRECATION") + pm.getPackagesHoldingPermissions(permissions, 0) + } + } + private fun setButtonText() { val numSelected = appData.count { it.isSelected } button?.text = if (numSelected == 0) -- cgit v1.2.3