From 897c8ce5d2a8673593756b0eacbbf7afd1def848 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Thu, 16 Nov 2023 21:00:10 +0300 Subject: [PATCH] resources: re-enable handling of apk (fixes #2711) (#2712) Co-authored-by: dogi --- app/build.gradle | 4 +-- app/src/main/AndroidManifest.xml | 1 + .../myplanet/base/BaseContainerFragment.kt | 36 +++++++++++++++++-- .../myplanet/base/PermissionActivity.java | 17 +++++++++ .../planet/myplanet/utilities/FileUtils.java | 2 +- 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 821f921c63..7e57d414f5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1149 - versionName "0.11.49" + versionCode 1150 + versionName "0.11.50" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 325094d19c..9c2eb9fee5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -30,6 +30,7 @@ + openIntent(items, TextFileViewerActivity::class.java) "md" -> openIntent(items, MarkdownViewerActivity::class.java) "csv" -> openIntent(items, CSVViewerActivity::class.java) + "apk" -> installApk(items) else -> Toast.makeText( activity, getString(R.string.this_file_type_is_currently_unsupported), Toast.LENGTH_LONG ).show() } } + private fun installApk(items: RealmMyLibrary) { + val directory = File(MainApplication.context.getExternalFilesDir(null).toString() + "/ole" + "/" + items.id) + if (!directory.exists()) { + if (!directory.mkdirs()) { + throw RuntimeException("Failed to create directory: " + directory.absolutePath) + } + } + + val apkFile = File(directory, items.resourceLocalAddress) + if (!apkFile.exists()) { + Utilities.toast(activity,"APK file not found") + return + } + + val uri = FileProvider.getUriForFile( + MainApplication.context, "${MainApplication.context.packageName}.fileprovider", + apkFile + ) + + val intent = Intent(Intent.ACTION_INSTALL_PACKAGE) + intent.data = uri + intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK + + if (intent.resolveActivity(requireActivity().packageManager) != null) { + startActivity(intent) + } else { + Utilities.toast(activity,"No app to handle the installation") + } + } + fun openFileType(items: RealmMyLibrary, videotype: String) { val mimetype = Utilities.getMimeType(items.resourceLocalAddress) Utilities.log("Mime type $mimetype") @@ -244,4 +276,4 @@ abstract class BaseContainerFragment : BaseResourceFragment() { } } } -} \ No newline at end of file +} diff --git a/app/src/main/java/org/ole/planet/myplanet/base/PermissionActivity.java b/app/src/main/java/org/ole/planet/myplanet/base/PermissionActivity.java index 5593d6a5d2..590898ae06 100644 --- a/app/src/main/java/org/ole/planet/myplanet/base/PermissionActivity.java +++ b/app/src/main/java/org/ole/planet/myplanet/base/PermissionActivity.java @@ -1,5 +1,7 @@ package org.ole.planet.myplanet.base; +import static org.ole.planet.myplanet.MainApplication.context; + import android.Manifest; import android.app.AppOpsManager; import android.content.Context; @@ -22,12 +24,21 @@ public abstract class PermissionActivity extends AppCompatActivity { private static final int PERMISSION_REQUEST_CODE_FILE = 111; + private static final int INSTALL_UNKNOWN_SOURCES_REQUEST_CODE = 112; + boolean hasInstallPermission = hasInstallPermission(context); public boolean checkPermission(String strPermission) { int result = ContextCompat.checkSelfPermission(this, strPermission); return result == PackageManager.PERMISSION_GRANTED; } + public static boolean hasInstallPermission(android.content.Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + return context.getPackageManager().canRequestPackageInstalls(); + } + return true; + } + public void checkUsagesPermission() { if (!getUsagesPermission(this)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -88,6 +99,12 @@ public void requestAllPermissions() { permissions.add(Manifest.permission.READ_MEDIA_AUDIO); } + if (!hasInstallPermission) { + Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES); + intent.setData(android.net.Uri.parse("package:" + getPackageName())); + startActivityForResult(intent, INSTALL_UNKNOWN_SOURCES_REQUEST_CODE); + } + if (!permissions.isEmpty()) { String[] permissionsArray = permissions.toArray(new String[0]); ActivityCompat.requestPermissions(this, permissionsArray, PERMISSION_REQUEST_CODE_FILE); diff --git a/app/src/main/java/org/ole/planet/myplanet/utilities/FileUtils.java b/app/src/main/java/org/ole/planet/myplanet/utilities/FileUtils.java index 9267e00eaf..4e95c7050c 100644 --- a/app/src/main/java/org/ole/planet/myplanet/utilities/FileUtils.java +++ b/app/src/main/java/org/ole/planet/myplanet/utilities/FileUtils.java @@ -366,4 +366,4 @@ public static String getAvailableOverTotalMemoryFormattedString() { long total = getTotalMemoryCapacity(); return context.getString(R.string.available_space_colon) + formatSize(available) + "/" + formatSize(total); } -} \ No newline at end of file +}