[Android] Fix getPackageInfo deprecation

Nicolas Pomepuy git at videolan.org
Mon Nov 14 09:54:17 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Nov 10 09:44:41 2022 +0100| [7a2b74f1316fa14874d12028eccfa29888165a9f] | committer: Nicolas Pomepuy

Fix getPackageInfo deprecation

> https://code.videolan.org/videolan/vlc-android/commit/7a2b74f1316fa14874d12028eccfa29888165a9f
---

 .../java/org/videolan/resources/AndroidDevices.kt  |  5 ++---
 .../java/org/videolan/resources/util/Extensions.kt | 15 +++++++++++++-
 .../org/videolan/resources/util/VLCCrashHandler.kt |  2 +-
 .../src/main/java/org/videolan/tools/AppUtils.kt   | 23 ----------------------
 .../src/org/videolan/vlc/util/AccessControl.kt     |  3 ++-
 5 files changed, 19 insertions(+), 29 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/AndroidDevices.kt b/application/resources/src/main/java/org/videolan/resources/AndroidDevices.kt
index 7cf22d7ec..65fc51a3f 100644
--- a/application/resources/src/main/java/org/videolan/resources/AndroidDevices.kt
+++ b/application/resources/src/main/java/org/videolan/resources/AndroidDevices.kt
@@ -21,10 +21,8 @@
 package org.videolan.resources
 
 import android.annotation.TargetApi
-import android.app.UiModeManager
 import android.content.Context
 import android.content.pm.PackageManager
-import android.content.res.Configuration
 import android.net.Uri
 import android.os.Build
 import android.os.Build.VERSION_CODES
@@ -35,6 +33,7 @@ import android.view.MotionEvent
 import androidx.core.content.getSystemService
 import androidx.core.net.toUri
 import org.videolan.libvlc.util.AndroidUtil
+import org.videolan.resources.util.getPackageInfoCompat
 import org.videolan.tools.containsName
 import org.videolan.tools.getFileNameFromPath
 import org.videolan.tools.startsWith
@@ -167,7 +166,7 @@ object AndroidDevices {
 
     private fun hasPlayServices(pm: PackageManager): Boolean {
         try {
-            pm.getPackageInfo("com.google.android.gsf", PackageManager.GET_SERVICES)
+            pm.getPackageInfoCompat("com.google.android.gsf", PackageManager.GET_SERVICES)
             return true
         } catch (ignored: PackageManager.NameNotFoundException) {
         }
diff --git a/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt b/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
index ba18bc49d..c7af8d1e9 100644
--- a/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
+++ b/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
@@ -3,6 +3,8 @@ package org.videolan.resources.util
 import android.app.Service
 import android.content.Context
 import android.content.Intent
+import android.content.pm.PackageInfo
+import android.content.pm.PackageManager
 import android.os.Build.VERSION.SDK_INT
 import android.os.Bundle
 import android.os.Parcelable
@@ -182,4 +184,15 @@ inline fun <reified T : Parcelable> Bundle.parcelableArray(key: String): Array<T
 fun Service.stopForegroundCompat(removeNotification:Boolean = true) = when {
     SDK_INT >= 24 -> stopForeground(if (removeNotification) Service.STOP_FOREGROUND_REMOVE else Service.STOP_FOREGROUND_DETACH)
     else -> @Suppress("DEPRECATION") stopForeground(removeNotification)
-}
\ No newline at end of file
+}
+
+ at Suppress("DEPRECATION")
+fun PackageManager.getPackageInfoCompat(packageName: String, vararg flagArgs: Int): PackageInfo {
+    var flags = 0
+    flagArgs.forEach { flag -> flags = flags or flag }
+    return if (SDK_INT >= 33) {
+        getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong()))
+    } else {
+        getPackageInfo(packageName, flags)
+    }
+}
diff --git a/application/resources/src/main/java/org/videolan/resources/util/VLCCrashHandler.kt b/application/resources/src/main/java/org/videolan/resources/util/VLCCrashHandler.kt
index c90f46f44..c89a78a35 100644
--- a/application/resources/src/main/java/org/videolan/resources/util/VLCCrashHandler.kt
+++ b/application/resources/src/main/java/org/videolan/resources/util/VLCCrashHandler.kt
@@ -101,7 +101,7 @@ class VLCCrashHandler : UncaughtExceptionHandler {
             val bw = BufferedWriter(output)
 
             val version = try {
-                val pInfo: PackageInfo = AppContextProvider.appContext.packageManager.getPackageInfo(AppContextProvider.appContext.packageName, 0)
+                val pInfo: PackageInfo = AppContextProvider.appContext.packageManager.getPackageInfoCompat(AppContextProvider.appContext.packageName, 0)
                 pInfo.versionName
             } catch (e: PackageManager.NameNotFoundException) {
                 e.printStackTrace()
diff --git a/application/tools/src/main/java/org/videolan/tools/AppUtils.kt b/application/tools/src/main/java/org/videolan/tools/AppUtils.kt
index a8098eb47..5a38b7c84 100644
--- a/application/tools/src/main/java/org/videolan/tools/AppUtils.kt
+++ b/application/tools/src/main/java/org/videolan/tools/AppUtils.kt
@@ -31,16 +31,6 @@ import android.os.StatFs
 @Suppress("DEPRECATION")
 object AppUtils {
 
-    fun getVersionName(context: Context): String {
-        return context.packageManager.getPackageInfo(context.packageName, 0).versionName
-    }
-
-    fun getVersionCode(context: Context): Long {
-        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
-            context.packageManager.getPackageInfo(context.packageName, 0).longVersionCode
-        else context.packageManager.getPackageInfo(context.packageName, 0).versionCode.toLong()
-    }
-
     fun totalMemory(): Long {
         val statFs = StatFs(Environment.getRootDirectory().absolutePath)
         return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
@@ -54,17 +44,4 @@ object AppUtils {
             statFs.availableBlocksLong * statFs.blockSizeLong
         else (statFs.availableBlocks * statFs.blockSize).toLong()
     }
-
-    fun busyMemory(): Long {
-        val statFs = StatFs(Environment.getRootDirectory().absolutePath)
-        val total = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
-            statFs.blockCountLong * statFs.blockSizeLong
-        else (statFs.blockCount * statFs.blockSize).toLong()
-
-        val free = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
-            statFs.availableBlocksLong * statFs.blockSizeLong
-        else (statFs.availableBlocks * statFs.blockSize).toLong()
-
-        return total - free
-    }
 }
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/util/AccessControl.kt b/application/vlc-android/src/org/videolan/vlc/util/AccessControl.kt
index 6c1f069f3..d3cfd18a3 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/AccessControl.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/AccessControl.kt
@@ -30,6 +30,7 @@ import kotlinx.coroutines.withContext
 import org.json.JSONArray
 import org.json.JSONObject
 import org.videolan.resources.AppContextProvider
+import org.videolan.resources.util.getPackageInfoCompat
 import org.videolan.vlc.R
 import java.security.MessageDigest
 import java.security.NoSuchAlgorithmException
@@ -133,7 +134,7 @@ object AccessControl {
     @Suppress("deprecation")
     private fun getSignature(ctx: Context, callingPackage: String): String? {
         try {
-            val packageInfo = ctx.packageManager.getPackageInfo(callingPackage, PackageManager.GET_SIGNATURES)
+            val packageInfo = ctx.packageManager.getPackageInfoCompat(callingPackage, PackageManager.GET_SIGNATURES)
             if (packageInfo.signatures != null && packageInfo.signatures.size == 1) {
                 return genSigSha256(packageInfo.signatures[0].toByteArray())
             }



More information about the Android mailing list