From 108752ff8b2d78e13950f28ae68af344f2e233d4 Mon Sep 17 00:00:00 2001 From: Li Linfeng Date: Mon, 30 Mar 2020 10:39:30 +0800 Subject: [PATCH] Support reading addon/extra directory --- app/src/main/AndroidManifest.xml | 1 + .../mobilecelestia/CelestiaFragment.kt | 16 ++++-- .../celestia/mobilecelestia/MainActivity.kt | 55 +++++++++++++++++-- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6d94ab59..010bcad5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + , + grantResults: IntArray + ) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + if (requestCode != WRITE_EXTERNAL_STROAGE_REQUEST) { return } + // No matter what the result for permission request, proceed + requestPermissionCompleted() + } + + private fun requestPermissionCompleted() { + // Check permission to create folder + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + createAddonFolderCompleted() + return + } + + // Permission granted, create add-on directory if not exits + val folder = getExternalFilesDir(CELESTIA_EXTRA_FOLDER_NAME) + if (folder != null && (folder.exists() || folder.mkdir())) { + addonPath = folder.absolutePath + } + createAddonFolderCompleted() + } + + private fun createAddonFolderCompleted() { // Load library AppStatusReporter.shared().updateStatus("Loading library...") GlobalScope.launch(Dispatchers.IO) { @@ -369,7 +407,7 @@ class MainActivity : AppCompatActivity(), private fun loadLibrarySuccess() { // Add gl fragment - val celestiaFragment = CelestiaFragment.newInstance("$celestiaParentPath/$celestiaFolderName", "$celestiaParentPath/$celestiaFolderName/$celestiaCfgName") + val celestiaFragment = CelestiaFragment.newInstance("$celestiaParentPath/$CELESTIA_DATA_FOLDER_NAME", "$celestiaParentPath/$CELESTIA_DATA_FOLDER_NAME/$CELESTIA_CFG_NAME", addonPath) supportFragmentManager .beginTransaction() .add(R.id.celestia_fragment_container, celestiaFragment) @@ -772,6 +810,13 @@ class MainActivity : AppCompatActivity(), companion object { private const val CURRENT_DATA_VERSION = "1" + private const val WRITE_EXTERNAL_STROAGE_REQUEST = 1 + + private const val CELESTIA_DATA_FOLDER_NAME = "CelestiaResources" + private const val CELESTIA_CFG_NAME = "celestia.cfg" + private const val CELESTIA_EXTRA_FOLDER_NAME = "CelestiaResources/extras" + + private const val TAG = "MainActivity" } }