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" } }