fix: PackageInfo compat

This commit is contained in:
J-Jamet 2023-06-18 11:00:22 +02:00
parent c3e4504a1a
commit 59794557b3
17 changed files with 95 additions and 92 deletions

View file

@ -31,6 +31,7 @@ import com.kunzisoft.keepass.BuildConfig
import com.kunzisoft.keepass.R import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.stylish.StylishActivity import com.kunzisoft.keepass.activities.stylish.StylishActivity
import com.kunzisoft.keepass.utils.UriUtil.isContributingUser import com.kunzisoft.keepass.utils.UriUtil.isContributingUser
import com.kunzisoft.keepass.utils.getPackageInfoCompat
import org.joda.time.DateTime import org.joda.time.DateTime
class AboutActivity : StylishActivity() { class AboutActivity : StylishActivity() {
@ -55,7 +56,7 @@ class AboutActivity : StylishActivity() {
var version: String var version: String
var build: String var build: String
try { try {
version = packageManager.getPackageInfo(packageName, 0).versionName version = packageManager.getPackageInfoCompat(packageName).versionName
build = BuildConfig.BUILD_VERSION build = BuildConfig.BUILD_VERSION
} catch (e: NameNotFoundException) { } catch (e: NameNotFoundException) {
Log.w(javaClass.simpleName, "Unable to get the app or the build version", e) Log.w(javaClass.simpleName, "Unable to get the app or the build version", e)

View file

@ -69,7 +69,7 @@ import com.kunzisoft.keepass.utils.DexUtil
import com.kunzisoft.keepass.utils.MagikeyboardUtil import com.kunzisoft.keepass.utils.MagikeyboardUtil
import com.kunzisoft.keepass.utils.MenuUtil import com.kunzisoft.keepass.utils.MenuUtil
import com.kunzisoft.keepass.utils.getParcelableCompat import com.kunzisoft.keepass.utils.getParcelableCompat
import com.kunzisoft.keepass.utils.UriHelper.parseUri import com.kunzisoft.keepass.utils.parseUri
import com.kunzisoft.keepass.utils.UriUtil.isContributingUser import com.kunzisoft.keepass.utils.UriUtil.isContributingUser
import com.kunzisoft.keepass.utils.UriUtil.openUrl import com.kunzisoft.keepass.utils.UriUtil.openUrl
import com.kunzisoft.keepass.view.asError import com.kunzisoft.keepass.view.asError

View file

@ -9,7 +9,7 @@ import com.kunzisoft.keepass.database.MainCredential
import com.kunzisoft.keepass.database.DatabaseTaskProvider import com.kunzisoft.keepass.database.DatabaseTaskProvider
import com.kunzisoft.keepass.model.CipherEncryptDatabase import com.kunzisoft.keepass.model.CipherEncryptDatabase
import com.kunzisoft.keepass.tasks.ActionRunnable import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.utils.UriHelper.getBinaryDir import com.kunzisoft.keepass.utils.getBinaryDir
import com.kunzisoft.keepass.viewmodels.DatabaseViewModel import com.kunzisoft.keepass.viewmodels.DatabaseViewModel
abstract class DatabaseActivity: StylishActivity(), DatabaseRetrieval { abstract class DatabaseActivity: StylishActivity(), DatabaseRetrieval {

View file

@ -27,8 +27,8 @@ import com.kunzisoft.keepass.model.DatabaseFile
import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.utils.IOActionTask import com.kunzisoft.keepass.utils.IOActionTask
import com.kunzisoft.keepass.utils.SingletonHolderParameter import com.kunzisoft.keepass.utils.SingletonHolderParameter
import com.kunzisoft.keepass.utils.UriHelper.decodeUri import com.kunzisoft.keepass.utils.decodeUri
import com.kunzisoft.keepass.utils.UriHelper.parseUri import com.kunzisoft.keepass.utils.parseUri
import com.kunzisoft.keepass.viewmodels.FileDatabaseInfo import com.kunzisoft.keepass.viewmodels.FileDatabaseInfo
class FileDatabaseHistoryAction(private val applicationContext: Context) { class FileDatabaseHistoryAction(private val applicationContext: Context) {

View file

@ -25,7 +25,7 @@ import android.os.Parcelable
import com.kunzisoft.keepass.database.element.MasterCredential import com.kunzisoft.keepass.database.element.MasterCredential
import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.utils.readParcelableCompat import com.kunzisoft.keepass.utils.readParcelableCompat
import com.kunzisoft.keepass.utils.UriHelper.getUriInputStream import com.kunzisoft.keepass.utils.getUriInputStream
import com.kunzisoft.keepass.utils.readEnum import com.kunzisoft.keepass.utils.readEnum
import com.kunzisoft.keepass.utils.writeEnum import com.kunzisoft.keepass.utils.writeEnum

View file

@ -24,7 +24,7 @@ import android.net.Uri
import com.kunzisoft.keepass.database.ContextualDatabase import com.kunzisoft.keepass.database.ContextualDatabase
import com.kunzisoft.keepass.database.MainCredential import com.kunzisoft.keepass.database.MainCredential
import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.utils.UriHelper.getBinaryDir import com.kunzisoft.keepass.utils.getBinaryDir
class CreateDatabaseRunnable( class CreateDatabaseRunnable(
context: Context, context: Context,

View file

@ -29,8 +29,8 @@ import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException
import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.tasks.ActionRunnable import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
import com.kunzisoft.keepass.utils.UriHelper.getBinaryDir import com.kunzisoft.keepass.utils.getBinaryDir
import com.kunzisoft.keepass.utils.UriHelper.getUriInputStream import com.kunzisoft.keepass.utils.getUriInputStream
class LoadDatabaseRunnable( class LoadDatabaseRunnable(
private val context: Context, private val context: Context,

View file

@ -28,7 +28,7 @@ import com.kunzisoft.keepass.database.exception.DatabaseException
import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException
import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
import com.kunzisoft.keepass.utils.UriHelper.getUriInputStream import com.kunzisoft.keepass.utils.getUriInputStream
class MergeDatabaseRunnable( class MergeDatabaseRunnable(
context: Context, context: Context,

View file

@ -26,8 +26,8 @@ import com.kunzisoft.keepass.database.exception.DatabaseException
import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException
import com.kunzisoft.keepass.tasks.ActionRunnable import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
import com.kunzisoft.keepass.utils.UriHelper.getBinaryDir import com.kunzisoft.keepass.utils.getBinaryDir
import com.kunzisoft.keepass.utils.UriHelper.getUriInputStream import com.kunzisoft.keepass.utils.getUriInputStream
class ReloadDatabaseRunnable( class ReloadDatabaseRunnable(
private val context: Context, private val context: Context,

View file

@ -26,7 +26,7 @@ import com.kunzisoft.keepass.database.MainCredential
import com.kunzisoft.keepass.database.exception.DatabaseException import com.kunzisoft.keepass.database.exception.DatabaseException
import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.tasks.ActionRunnable import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.utils.UriHelper.getUriOutputStream import com.kunzisoft.keepass.utils.getUriOutputStream
import java.io.File import java.io.File
open class SaveDatabaseRunnable( open class SaveDatabaseRunnable(

View file

@ -8,8 +8,8 @@ import android.util.Log
import com.kunzisoft.keepass.database.ContextualDatabase import com.kunzisoft.keepass.database.ContextualDatabase
import com.kunzisoft.keepass.database.element.binary.BinaryCache import com.kunzisoft.keepass.database.element.binary.BinaryCache
import com.kunzisoft.keepass.database.element.binary.BinaryData import com.kunzisoft.keepass.database.element.binary.BinaryData
import com.kunzisoft.keepass.utils.UriHelper.getUriInputStream import com.kunzisoft.keepass.utils.getUriInputStream
import com.kunzisoft.keepass.utils.UriHelper.getUriOutputStream import com.kunzisoft.keepass.utils.getUriOutputStream
import com.kunzisoft.keepass.utils.readAllBytes import com.kunzisoft.keepass.utils.readAllBytes
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream

View file

@ -36,7 +36,6 @@ import com.kunzisoft.keepass.services.ClipboardEntryNotificationService
import com.kunzisoft.keepass.services.KeyboardEntryNotificationService import com.kunzisoft.keepass.services.KeyboardEntryNotificationService
import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.timeout.TimeoutHelper import com.kunzisoft.keepass.timeout.TimeoutHelper
import com.kunzisoft.keepass.utils.UriHelper.getBinaryDir
import com.kunzisoft.keepass.utils.UriUtil.releaseAllUnnecessaryPermissionUris import com.kunzisoft.keepass.utils.UriUtil.releaseAllUnnecessaryPermissionUris
const val DATABASE_START_TASK_ACTION = "com.kunzisoft.keepass.DATABASE_START_TASK_ACTION" const val DATABASE_START_TASK_ACTION = "com.kunzisoft.keepass.DATABASE_START_TASK_ACTION"

View file

@ -32,8 +32,6 @@ import com.kunzisoft.keepass.BuildConfig
import com.kunzisoft.keepass.R import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
import com.kunzisoft.keepass.education.Education import com.kunzisoft.keepass.education.Education
import com.kunzisoft.keepass.utils.UriHelper.withContentScheme
import com.kunzisoft.keepass.utils.UriHelper.withFileScheme
import java.io.File import java.io.File
@ -216,7 +214,7 @@ object UriUtil {
fun Context.isExternalAppInstalled(packageName: String, showError: Boolean = true): Boolean { fun Context.isExternalAppInstalled(packageName: String, showError: Boolean = true): Boolean {
try { try {
this.applicationContext.packageManager.getPackageInfo( this.applicationContext.packageManager.getPackageInfoCompat(
packageName, packageName,
PackageManager.GET_ACTIVITIES PackageManager.GET_ACTIVITIES
) )

View file

@ -9,7 +9,7 @@ import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
import com.kunzisoft.keepass.model.DatabaseFile import com.kunzisoft.keepass.model.DatabaseFile
import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.utils.IOActionTask import com.kunzisoft.keepass.utils.IOActionTask
import com.kunzisoft.keepass.utils.UriHelper.parseUri import com.kunzisoft.keepass.utils.parseUri
class DatabaseFileViewModel(application: Application) : AndroidViewModel(application) { class DatabaseFileViewModel(application: Application) : AndroidViewModel(application) {

View file

@ -10,7 +10,7 @@ import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.model.DatabaseFile import com.kunzisoft.keepass.model.DatabaseFile
import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.utils.IOActionTask import com.kunzisoft.keepass.utils.IOActionTask
import com.kunzisoft.keepass.utils.UriHelper.parseUri import com.kunzisoft.keepass.utils.parseUri
import com.kunzisoft.keepass.utils.UriUtil.releaseUriPermission import com.kunzisoft.keepass.utils.UriUtil.releaseUriPermission
class DatabaseFilesViewModel(application: Application) : AndroidViewModel(application) { class DatabaseFilesViewModel(application: Application) : AndroidViewModel(application) {

View file

@ -23,7 +23,7 @@ import android.content.Context
import android.net.Uri import android.net.Uri
import android.text.format.Formatter import android.text.format.Formatter
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import com.kunzisoft.keepass.utils.UriHelper.parseUri import com.kunzisoft.keepass.utils.parseUri
import com.kunzisoft.keepass.utils.UriUtil.getDocumentFile import com.kunzisoft.keepass.utils.UriUtil.getDocumentFile
import com.kunzisoft.keepass.utils.UriUtil.takeUriPermission import com.kunzisoft.keepass.utils.UriUtil.takeUriPermission
import java.io.Serializable import java.io.Serializable

View file

@ -22,80 +22,85 @@ package com.kunzisoft.keepass.utils
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.ContentResolver import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import java.io.* import java.io.*
import java.util.* import java.util.*
fun String.parseUri(): Uri? {
object UriHelper { return if (this.isNotEmpty()) Uri.parse(this) else null
fun String.parseUri(): Uri? {
return if (this.isNotEmpty()) Uri.parse(this) else null
}
fun String.decodeUri(): String {
return Uri.decode(this) ?: ""
}
fun Context.getBinaryDir(): File {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.applicationContext.noBackupFilesDir
} else {
this.applicationContext.filesDir
}
}
@Throws(FileNotFoundException::class)
fun ContentResolver.getUriInputStream(fileUri: Uri?): InputStream? {
if (fileUri == null)
return null
return when {
fileUri.withFileScheme() -> fileUri.path?.let { FileInputStream(it) }
fileUri.withContentScheme() -> this.openInputStream(fileUri)
else -> null
}
}
@SuppressLint("Recycle")
@Throws(FileNotFoundException::class)
fun ContentResolver.getUriOutputStream(fileUri: Uri?): OutputStream? {
if (fileUri == null)
return null
return when {
fileUri.withFileScheme() -> fileUri.path?.let { FileOutputStream(it) }
fileUri.withContentScheme() -> {
try {
this.openOutputStream(fileUri, "wt")
} catch (e: FileNotFoundException) {
Log.e(TAG, "Unable to open stream in `wt` mode, retry in `rwt` mode.", e)
// https://issuetracker.google.com/issues/180526528
// Try with rwt to fix content provider issue
val outStream = this.openOutputStream(fileUri, "rwt")
Log.w(TAG, "`rwt` mode used.")
outStream
}
}
else -> null
}
}
fun Uri.withFileScheme(): Boolean {
val scheme = this.scheme
if (scheme.isNullOrEmpty() || scheme.lowercase(Locale.ENGLISH) == "file") {
return true
}
return false
}
fun Uri.withContentScheme(): Boolean {
val scheme = this.scheme
if (scheme != null && scheme.lowercase(Locale.ENGLISH) == "content") {
return true
}
return false
}
private const val TAG = "UriHelper"
} }
fun String.decodeUri(): String {
return Uri.decode(this) ?: ""
}
fun Context.getBinaryDir(): File {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.applicationContext.noBackupFilesDir
} else {
this.applicationContext.filesDir
}
}
@Throws(FileNotFoundException::class)
fun ContentResolver.getUriInputStream(fileUri: Uri?): InputStream? {
if (fileUri == null)
return null
return when {
fileUri.withFileScheme() -> fileUri.path?.let { FileInputStream(it) }
fileUri.withContentScheme() -> this.openInputStream(fileUri)
else -> null
}
}
@SuppressLint("Recycle")
@Throws(FileNotFoundException::class)
fun ContentResolver.getUriOutputStream(fileUri: Uri?): OutputStream? {
if (fileUri == null)
return null
return when {
fileUri.withFileScheme() -> fileUri.path?.let { FileOutputStream(it) }
fileUri.withContentScheme() -> {
try {
this.openOutputStream(fileUri, "wt")
} catch (e: FileNotFoundException) {
Log.e(TAG, "Unable to open stream in `wt` mode, retry in `rwt` mode.", e)
// https://issuetracker.google.com/issues/180526528
// Try with rwt to fix content provider issue
val outStream = this.openOutputStream(fileUri, "rwt")
Log.w(TAG, "`rwt` mode used.")
outStream
}
}
else -> null
}
}
fun Uri.withFileScheme(): Boolean {
val scheme = this.scheme
if (scheme.isNullOrEmpty() || scheme.lowercase(Locale.ENGLISH) == "file") {
return true
}
return false
}
fun Uri.withContentScheme(): Boolean {
val scheme = this.scheme
if (scheme != null && scheme.lowercase(Locale.ENGLISH) == "content") {
return true
}
return false
}
fun PackageManager.getPackageInfoCompat(packageName: String, flags: Int = 0): PackageInfo =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong()))
} else {
@Suppress("DEPRECATION") getPackageInfo(packageName, flags)
}
private const val TAG = "UriHelper"