EtchDroid/app/src/main/java/eu/depau/etchdroid/fragments/ImageLocationFragment.kt

212 lines
7.9 KiB
Kotlin
Raw Normal View History

2018-08-15 17:04:45 +00:00
package eu.depau.etchdroid.fragments
2018-08-12 14:33:11 +00:00
import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
2018-08-16 14:21:43 +00:00
import android.os.Environment
2018-08-12 14:33:11 +00:00
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
2018-08-12 14:33:11 +00:00
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.codekidlabs.storagechooser.StorageChooser
2018-08-15 17:04:45 +00:00
import eu.depau.etchdroid.R
import eu.depau.etchdroid.StateKeeper
import eu.depau.etchdroid.activities.WizardActivity
import eu.depau.etchdroid.adapters.PartitionTableRecyclerViewAdapter
2018-08-16 14:24:04 +00:00
import eu.depau.etchdroid.enums.FlashMethod
import eu.depau.etchdroid.enums.ImageLocation
import eu.depau.etchdroid.enums.WizardStep
2018-08-16 22:27:03 +00:00
import eu.depau.etchdroid.img_types.DMGImage
import eu.depau.etchdroid.kotlin_exts.getFileName
import eu.depau.etchdroid.kotlin_exts.snackbar
2018-08-13 23:32:02 +00:00
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_select_location.*
2018-08-16 14:21:43 +00:00
import java.io.File
2018-08-12 14:33:11 +00:00
/**
* A placeholder fragment containing a simple view.
*/
class ImageLocationFragment : WizardFragment() {
2018-08-12 14:33:11 +00:00
val READ_REQUEST_CODE = 42
2018-08-16 14:21:43 +00:00
val MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 29
2018-08-12 14:33:11 +00:00
val TAG = "ImageLocationFragment"
2018-08-16 14:21:43 +00:00
val PICKER_DIALOG_TAG = "eu.depau.etchdroid.filepicker.DIALOG_TAG"
var issuesFound = false
2018-08-12 14:33:11 +00:00
fun isStreamingAvailable(): Boolean {
if (StateKeeper.imageLocation != ImageLocation.REMOTE)
return false
2018-08-16 14:21:43 +00:00
if (StateKeeper.flashMethod != FlashMethod.FLASH_DMG_API && StateKeeper.flashMethod != FlashMethod.FLASH_API)
2018-08-12 14:33:11 +00:00
return false
return true
}
override fun onRadioButtonClicked(view: View) {
StateKeeper.imageLocation = ImageLocation.LOCAL
activity?.fab?.show()
2018-08-13 23:32:02 +00:00
pick_file_btn?.isEnabled = StateKeeper.imageLocation == ImageLocation.LOCAL
2018-08-16 22:27:03 +00:00
loadImageChanges(activity as WizardActivity)
2018-08-12 14:33:11 +00:00
}
override fun onButtonClicked(view: View) {
if (view.id == R.id.pick_file_btn) {
2018-08-16 14:21:43 +00:00
when (StateKeeper.flashMethod) {
FlashMethod.FLASH_API -> {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("*/*");
activity?.startActivityForResult(intent, READ_REQUEST_CODE)
}
FlashMethod.FLASH_DMG_API -> {
if (checkAndRequestStorageReadPerm()) {
val sdcard = Environment.getExternalStorageDirectory().absolutePath
val chooser = StorageChooser.Builder()
.withActivity(activity)
.withFragmentManager(activity!!.fragmentManager)
.withMemoryBar(true)
.allowCustomPath(true)
.setType(StorageChooser.FILE_PICKER)
.customFilter(arrayListOf("dmg"))
.build()
chooser.show()
chooser.setOnSelectListener {
StateKeeper.imageFile = Uri.fromFile(File(it))
loadImageChanges(activity as WizardActivity)
activity?.fab?.show()
}
2018-08-16 14:21:43 +00:00
}
}
FlashMethod.FLASH_UNETBOOTIN -> {
}
FlashMethod.FLASH_WOEUSB -> {
}
null -> {
}
}
2018-08-12 14:33:11 +00:00
}
}
2018-08-16 14:21:43 +00:00
fun checkAndRequestStorageReadPerm(): Boolean {
if ((ContextCompat.checkSelfPermission(activity!!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity!!,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
view!!.snackbar("Storage permission is required to read DMG images")
} else {
ActivityCompat.requestPermissions(activity!!,
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE)
}
} else {
// Permission granted
return true
}
return false
}
2018-08-13 23:32:02 +00:00
override fun nextStep(view: View?) {
if (issuesFound) {
view?.snackbar(getString(R.string.issues_found_expl))
return
}
2018-08-12 14:33:11 +00:00
if (StateKeeper.imageLocation == null) {
2018-08-13 23:32:02 +00:00
view?.snackbar(getString(R.string.select_image_location))
2018-08-12 14:33:11 +00:00
return
}
if (StateKeeper.imageFile == null) {
2018-08-13 23:32:02 +00:00
view?.snackbar(getString(R.string.provide_image_file))
2018-08-12 14:33:11 +00:00
return
}
2018-08-13 23:32:02 +00:00
(activity as WizardActivity).goToNewFragment(UsbDriveFragment())
2018-08-12 14:33:11 +00:00
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
2018-08-16 14:21:43 +00:00
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE -> {
onButtonClicked(pick_file_btn)
2018-08-12 14:33:11 +00:00
return
}
2018-08-30 17:43:27 +00:00
else -> {}
2018-08-12 14:33:11 +00:00
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.fab?.show()
2018-08-12 14:33:11 +00:00
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
StateKeeper.currentFragment = this
StateKeeper.wizardStep = WizardStep.SELECT_LOCATION
return inflater.inflate(R.layout.fragment_select_location, container, false)
}
2018-08-16 22:27:03 +00:00
fun loadImageChanges(context: WizardActivity) {
2018-08-13 23:32:02 +00:00
val button = pick_file_btn
2018-08-16 22:27:03 +00:00
val uri = StateKeeper.imageFile ?: return
2018-08-12 14:33:11 +00:00
2018-08-16 22:27:03 +00:00
val text = uri.getFileName(context)
2018-08-12 14:33:11 +00:00
2018-08-13 23:32:02 +00:00
if (text != null)
button.text = text
else
button.text = getString(R.string.pick_a_file)
2018-08-16 22:27:03 +00:00
if (StateKeeper.flashMethod == FlashMethod.FLASH_DMG_API) {
StateKeeper.imageRepr = DMGImage(uri, context)
val imgRepr = StateKeeper.imageRepr as DMGImage
if (imgRepr.tableType == null && (imgRepr.partitionTable == null || imgRepr.partitionTable?.size == 0)) {
part_table_header.text = getString(R.string.image_is_not_dmg)
issuesFound = true
return
} else {
part_table_header.text = if (imgRepr.tableType != null) "Partition table:" else ""
part_table_header_side.text = imgRepr.tableType?.getString(context) ?: ""
issuesFound = false
val viewAdapter = PartitionTableRecyclerViewAdapter(imgRepr.partitionTable!!)
part_table_recycler.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(activity)
adapter = viewAdapter
}
}
2018-08-16 22:27:03 +00:00
}
2018-08-12 14:33:11 +00:00
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter.
// Pull that URI using resultData.getData().
var uri: Uri? = null
if (data != null) {
uri = data.getData()
Log.d(TAG, "Uri: " + uri!!.toString())
StateKeeper.imageFile = uri
2018-08-16 22:27:03 +00:00
loadImageChanges(activity as WizardActivity)
activity?.fab?.show()
2018-08-12 14:33:11 +00:00
}
}
2018-08-16 14:21:43 +00:00
}
2018-08-12 14:33:11 +00:00
}