Skip to content

Commit

Permalink
Support reading addon/extra directory
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed Mar 30, 2020
1 parent 6fe8131 commit 108752f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class CelestiaFragment : Fragment(), GLSurfaceView.Renderer {
// MARK: Celestia
private var pathToLoad: String? = null
private var cfgToLoad: String? = null
private var addonToLoad: String? = null
private val core by lazy { CelestiaAppCore.shared() }

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -37,6 +38,7 @@ class CelestiaFragment : Fragment(), GLSurfaceView.Renderer {
arguments?.let {
pathToLoad = it.getString(ARG_DATA_DIR)
cfgToLoad = it.getString(ARG_CFG_FILE)
addonToLoad = it.getString(ARG_ADDON_DIR)
}

retainInstance = true
Expand Down Expand Up @@ -87,12 +89,14 @@ class CelestiaFragment : Fragment(), GLSurfaceView.Renderer {
glViewContainer?.addView(glView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
}

private fun loadCelestia(path: String, cfg: String) {
private fun loadCelestia(path: String, cfg: String, addon: String?) {
CelestiaAppCore.chdir(path)

CelestiaAppCore.setLocaleDirectoryPath("$path/locale", Locale.getDefault().toString())

if (!core.startSimulation(cfg, null, AppStatusReporter.shared())) {
val extraDirs = if (addon != null) arrayOf(addon) else null

if (!core.startSimulation(cfg, extraDirs, AppStatusReporter.shared())) {
AppStatusReporter.shared().celestiaLoadResult(false)
return
}
Expand Down Expand Up @@ -124,7 +128,7 @@ class CelestiaFragment : Fragment(), GLSurfaceView.Renderer {
glView?.queueEvent {
// Celestia initialization have to be called with an OpenGL context
if (pathToLoad != null && cfgToLoad != null) {
loadCelestia(pathToLoad!!, cfgToLoad!!)
loadCelestia(pathToLoad!!, cfgToLoad!!, addonToLoad)
}
}
}
Expand All @@ -145,14 +149,18 @@ class CelestiaFragment : Fragment(), GLSurfaceView.Renderer {
companion object {
private const val ARG_DATA_DIR = "data"
private const val ARG_CFG_FILE = "cfg"
private const val ARG_ADDON_DIR = "addon"

private const val TAG = "CelestiaFragment"

fun newInstance(data: String, cfg: String) =
fun newInstance(data: String, cfg: String, addon: String?) =
CelestiaFragment().apply {
arguments = Bundle().apply {
putString(ARG_DATA_DIR, data)
putString(ARG_CFG_FILE, cfg)
if (addon != null) {
putString(ARG_ADDON_DIR, addon)
}
}
}
}
Expand Down
55 changes: 50 additions & 5 deletions app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package space.celestia.mobilecelestia
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
Expand All @@ -12,6 +14,7 @@ import android.view.WindowManager
import android.widget.DatePicker
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.app.ShareCompat
import androidx.fragment.app.Fragment
import com.google.gson.Gson
Expand Down Expand Up @@ -68,12 +71,10 @@ class MainActivity : AppCompatActivity(),
AboutFragment.Listener,
AppStatusReporter.Listener {

private val celestiaFolderName = "CelestiaResources"
private val celestiaCfgName = "celestia.cfg"

private val preferenceManager by lazy { PreferenceManager(this, "celestia") }
private val settingManager by lazy { PreferenceManager(this, "celestia_setting") }
private val celestiaParentPath by lazy { this.filesDir.absolutePath }
private var addonPath: String? = null

private val core by lazy { CelestiaAppCore.shared() }
private var currentSelection: CelestiaSelection? = null
Expand Down Expand Up @@ -230,7 +231,7 @@ class MainActivity : AppCompatActivity(),

GlobalScope.launch(Dispatchers.IO) {
try {
AssetUtils.copyFileOrDir(this@MainActivity, celestiaFolderName, celestiaParentPath)
AssetUtils.copyFileOrDir(this@MainActivity, CELESTIA_DATA_FOLDER_NAME, celestiaParentPath)
preferenceManager[PreferenceManager.PredefinedKey.DataVersion] = CURRENT_DATA_VERSION
withContext(Dispatchers.Main) {
copyAssetSuccess()
Expand Down Expand Up @@ -357,6 +358,43 @@ class MainActivity : AppCompatActivity(),
}

private fun copyAssetSuccess() {
// 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) {
requestPermissionCompleted()
return
}
// Requesting permission to create add-on folder
val permission = arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
ActivityCompat.requestPermissions(this, permission, WRITE_EXTERNAL_STROAGE_REQUEST)
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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"
}
}

0 comments on commit 108752f

Please sign in to comment.