Skip to content

Commit

Permalink
resources: re-enable handling of apk (fixes #2711) (#2712)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Nov 16, 2023
1 parent d559a99 commit 897c8ce
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

<application
android:name=".MainApplication"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.AppCompatRatingBar
import androidx.core.content.FileProvider
import com.google.gson.JsonObject
import io.realm.RealmResults
import org.ole.planet.myplanet.MainApplication
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.callback.OnHomeItemClickListener
import org.ole.planet.myplanet.callback.OnRatingChangeListener
Expand All @@ -32,7 +34,6 @@ import org.ole.planet.myplanet.ui.viewer.*
import org.ole.planet.myplanet.utilities.FileUtils
import org.ole.planet.myplanet.utilities.Utilities
import java.io.File
import java.util.*

abstract class BaseContainerFragment : BaseResourceFragment() {
var timesRated: TextView? = null
Expand Down Expand Up @@ -141,12 +142,43 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
"txt" -> 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")
Expand Down Expand Up @@ -244,4 +276,4 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,4 @@ public static String getAvailableOverTotalMemoryFormattedString() {
long total = getTotalMemoryCapacity();
return context.getString(R.string.available_space_colon) + formatSize(available) + "/" + formatSize(total);
}
}
}

0 comments on commit 897c8ce

Please sign in to comment.