From 9b4b5914d690db864a03b2a9e4404675d8cb3166 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sun, 18 Jun 2023 12:23:37 +0200 Subject: [PATCH] fix: allowCreateDocumentByStorageAccessFramework method --- app/src/main/AndroidManifest.xml | 6 +++++ .../activities/FileDatabaseSelectActivity.kt | 3 ++- .../activities/helpers/ExternalFileHelper.kt | 17 ------------ .../com/kunzisoft/keepass/utils/UriHelper.kt | 27 +++++++++++++++++++ 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c67d619d9..c36834a4b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,6 +19,12 @@ + + + + + + { - if (ExternalFileHelper.allowCreateDocumentByStorageAccessFramework(packageManager)) { + if (packageManager.allowCreateDocumentByStorageAccessFramework()) { // There is an activity which can handle this intent. createDatabaseButtonView?.visibility = View.VISIBLE } else{ diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/helpers/ExternalFileHelper.kt b/app/src/main/java/com/kunzisoft/keepass/activities/helpers/ExternalFileHelper.kt index d291a53c4..89947cc79 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/helpers/ExternalFileHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/helpers/ExternalFileHelper.kt @@ -22,7 +22,6 @@ package com.kunzisoft.keepass.activities.helpers import android.annotation.SuppressLint import android.content.Context import android.content.Intent -import android.content.pm.PackageManager import android.net.Uri import android.os.Build import android.util.Log @@ -186,23 +185,7 @@ class ExternalFileHelper { companion object { - private const val TAG = "OpenFileHelper" - - @SuppressLint("InlinedApi") - fun allowCreateDocumentByStorageAccessFramework(packageManager: PackageManager, - typeString: String = "application/octet-stream"): Boolean { - return when { - // To check if a custom file manager can manage the ACTION_CREATE_DOCUMENT - Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT -> { - packageManager.queryIntentActivities(Intent(Intent.ACTION_CREATE_DOCUMENT).apply { - addCategory(Intent.CATEGORY_OPENABLE) - type = typeString - }, PackageManager.MATCH_DEFAULT_ONLY).isNotEmpty() - } - else -> true - } - } } } diff --git a/database/src/main/java/com/kunzisoft/keepass/utils/UriHelper.kt b/database/src/main/java/com/kunzisoft/keepass/utils/UriHelper.kt index 4ebf9521f..87a8b028c 100644 --- a/database/src/main/java/com/kunzisoft/keepass/utils/UriHelper.kt +++ b/database/src/main/java/com/kunzisoft/keepass/utils/UriHelper.kt @@ -22,8 +22,10 @@ package com.kunzisoft.keepass.utils import android.annotation.SuppressLint import android.content.ContentResolver import android.content.Context +import android.content.Intent import android.content.pm.PackageInfo import android.content.pm.PackageManager +import android.content.pm.ResolveInfo import android.net.Uri import android.os.Build import android.util.Log @@ -102,5 +104,30 @@ fun PackageManager.getPackageInfoCompat(packageName: String, flags: Int = 0): Pa @Suppress("DEPRECATION") getPackageInfo(packageName, flags) } +@SuppressLint("InlinedApi") +fun PackageManager.allowCreateDocumentByStorageAccessFramework(): Boolean { + return when { + // To check if a custom file manager can manage the ACTION_CREATE_DOCUMENT + // queries filter is in Manifest + Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT -> { + queryIntentActivitiesCompat( + Intent(Intent.ACTION_CREATE_DOCUMENT).apply { + addCategory(Intent.CATEGORY_OPENABLE) + type = "application/octet-stream" + }, PackageManager.MATCH_DEFAULT_ONLY + ).isNotEmpty() + } + else -> true + } +} + +@SuppressLint("QueryPermissionsNeeded") +private fun PackageManager.queryIntentActivitiesCompat(intent: Intent, flags: Int): List { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(flags.toLong())) + } else { + @Suppress("DEPRECATION") queryIntentActivities(intent, PackageManager.GET_META_DATA) + } +} private const val TAG = "UriHelper"