Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 54b0a228 authored by tibbi's avatar tibbi
Browse files

try preventing selecting the same partition for SD card and OTG devices

parent 4a0c57f6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ buildscript {
        propMinSdkVersion = 16
        propTargetSdkVersion = propCompileSdkVersion
        propVersionCode = 1
        propVersionName = '3.11.54'
        propVersionName = '3.11.58'
        kotlin_version = '1.2.21'
        support_libs = '27.0.2'
    }
+16 −5
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ open class BaseSimpleActivity : AppCompatActivity() {

    companion object {
        var funAfterSAFPermission: (() -> Unit)? = null
        var funAfterOTGPermission: ((success: Boolean) -> Unit)? = null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
@@ -73,6 +74,7 @@ open class BaseSimpleActivity : AppCompatActivity() {
    override fun onDestroy() {
        super.onDestroy()
        funAfterSAFPermission = null
        funAfterOTGPermission = null
    }

    override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
@@ -113,6 +115,10 @@ open class BaseSimpleActivity : AppCompatActivity() {
        super.onActivityResult(requestCode, resultCode, resultData)
        if (requestCode == OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null) {
            if (isProperSDFolder(resultData.data)) {
                if (resultData.dataString == baseConfig.OTGTreeUri) {
                    toast(R.string.sd_card_otg_same)
                    return
                }
                saveTreeUri(resultData)
                funAfterSAFPermission?.invoke()
                funAfterSAFPermission = null
@@ -123,9 +129,14 @@ open class BaseSimpleActivity : AppCompatActivity() {
            }
        } else if (requestCode == OPEN_DOCUMENT_TREE_OTG && resultCode == Activity.RESULT_OK && resultData != null) {
            if (isProperOTGFolder(resultData.data)) {
                if (resultData.dataString == baseConfig.treeUri) {
                    funAfterOTGPermission?.invoke(false)
                    toast(R.string.sd_card_otg_same)
                    return
                }
                baseConfig.OTGTreeUri = resultData.dataString
                funAfterSAFPermission?.invoke()
                funAfterSAFPermission = null
                funAfterOTGPermission?.invoke(true)
                funAfterOTGPermission = null
            } else {
                toast(R.string.wrong_root_selected_otg)
                val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
@@ -297,13 +308,13 @@ open class BaseSimpleActivity : AppCompatActivity() {
        }
    }

    fun handleOTGPermission(callback: () -> Unit) {
    fun handleOTGPermission(callback: (success: Boolean) -> Unit) {
        if (baseConfig.OTGTreeUri.isNotEmpty()) {
            callback()
            callback(true)
            return
        }

        funAfterSAFPermission = callback
        funAfterOTGPermission = callback
        WritePermissionDialog(this, true) {
            Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
                if (resolveActivity(packageManager) == null) {
+32 −5
Original line number Diff line number Diff line
@@ -20,30 +20,45 @@ import kotlinx.android.synthetic.main.dialog_radio_group.view.*
 *
 */
class StoragePickerDialog(val activity: BaseSimpleActivity, currPath: String, val callback: (pickedPath: String) -> Unit) {
    var mDialog: AlertDialog
    private val ID_INTERNAL = 1
    private val ID_SD = 2
    private val ID_OTG = 3
    private val ID_ROOT = 4

    private var mDialog: AlertDialog
    private var radioGroup: RadioGroup
    private var defaultSelectedId = 0

    init {
        val inflater = LayoutInflater.from(activity)
        val resources = activity.resources
        val basePath = currPath.getBasePath(activity)
        val layoutParams = RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        val view = inflater.inflate(R.layout.dialog_radio_group, null)
        val radioGroup = view.dialog_radio_group
        radioGroup = view.dialog_radio_group
        val basePath = currPath.getBasePath(activity)

        val internalButton = inflater.inflate(R.layout.radio_button, null) as RadioButton
        internalButton.apply {
            id = ID_INTERNAL
            text = resources.getString(R.string.internal)
            isChecked = basePath == context.internalStoragePath
            setOnClickListener { internalPicked() }
            if (isChecked) {
                defaultSelectedId = id
            }
        }
        radioGroup.addView(internalButton, layoutParams)

        if (activity.hasExternalSDCard()) {
            val sdButton = inflater.inflate(R.layout.radio_button, null) as RadioButton
            sdButton.apply {
                id = ID_SD
                text = resources.getString(R.string.sd_card)
                isChecked = basePath == context.sdCardPath
                setOnClickListener { sdPicked() }
                if (isChecked) {
                    defaultSelectedId = id
                }
            }
            radioGroup.addView(sdButton, layoutParams)
        }
@@ -51,18 +66,26 @@ class StoragePickerDialog(val activity: BaseSimpleActivity, currPath: String, va
        if (activity.hasOTGConnected()) {
            val otgButton = inflater.inflate(R.layout.radio_button, null) as RadioButton
            otgButton.apply {
                id = ID_OTG
                text = resources.getString(R.string.otg)
                isChecked = basePath == OTG_PATH
                setOnClickListener { otgPicked() }
                if (isChecked) {
                    defaultSelectedId = id
                }
            }
            radioGroup.addView(otgButton, layoutParams)
        }

        val rootButton = inflater.inflate(R.layout.radio_button, null) as RadioButton
        rootButton.apply {
            id = ID_ROOT
            text = resources.getString(R.string.root)
            isChecked = basePath == "/"
            setOnClickListener { rootPicked() }
            if (isChecked) {
                defaultSelectedId = id
            }
        }
        radioGroup.addView(rootButton, layoutParams)

@@ -84,8 +107,12 @@ class StoragePickerDialog(val activity: BaseSimpleActivity, currPath: String, va

    private fun otgPicked() {
        activity.handleOTGPermission {
            mDialog.dismiss()
            if (it) {
                callback(OTG_PATH)
                mDialog.dismiss()
            } else {
                radioGroup.check(defaultSelectedId)
            }
        }
    }

+2 −2
Original line number Diff line number Diff line
@@ -126,7 +126,7 @@ fun Activity.launchViewIntent(url: String) {
    }.start()
}

fun Activity.shareUriIntent(path: String, applicationId: String) {
fun Activity.sharePathIntent(path: String, applicationId: String) {
    Thread {
        val newUri = getFinalUriFromPath(path, applicationId) ?: return@Thread
        Intent().apply {
@@ -150,7 +150,7 @@ fun Activity.shareUriIntent(path: String, applicationId: String) {
fun Activity.sharePathsIntent(paths: ArrayList<String>, applicationId: String) {
    Thread {
        if (paths.size == 1) {
            shareUriIntent(paths.first(), applicationId)
            sharePathIntent(paths.first(), applicationId)
        } else {
            val uriPaths = ArrayList<String>()
            val newUris = paths.map {