2018-08-15 17:04:45 +00:00
package eu.depau.etchdroid.fragments
2018-08-13 23:32:02 +00:00
import android.content.Intent
2018-08-30 14:42:35 +00:00
import android.os.Build
2018-08-13 23:32:02 +00:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2018-08-15 17:04:45 +00:00
import eu.depau.etchdroid.R
import eu.depau.etchdroid.StateKeeper
2018-08-16 14:24:04 +00:00
import eu.depau.etchdroid.enums.FlashMethod
import eu.depau.etchdroid.enums.WizardStep
2018-08-17 00:50:18 +00:00
import eu.depau.etchdroid.kotlin_exts.*
2018-08-17 19:51:13 +00:00
import eu.depau.etchdroid.services.UsbApiDmgWriteService
import eu.depau.etchdroid.services.UsbApiImgWriteService
2018-08-13 23:32:02 +00:00
import kotlinx.android.synthetic.main.fragment_confirminfo.view.*
2018-08-14 13:41:38 +00:00
import java.io.IOException
2018-08-13 23:32:02 +00:00
/ * *
* A placeholder fragment containing a simple view .
* /
class ConfirmInfoFragment : WizardFragment ( ) {
val TAG = " ConfirmInfoFragment "
var canContinue = false
override fun nextStep ( view : View ? ) {
if ( ! canContinue ) {
view ?. snackbar ( " Cannot write image to USB drive " )
return
}
context ?. toast ( " Check notification for progress " )
2018-08-17 19:51:13 +00:00
val intent : Intent = when ( StateKeeper . flashMethod ) {
FlashMethod . FLASH _API -> Intent ( activity , UsbApiImgWriteService :: class . java )
FlashMethod . FLASH _DMG _API -> Intent ( activity , UsbApiDmgWriteService :: class . java )
else -> null !!
}
2018-08-13 23:32:02 +00:00
intent . setDataAndType ( StateKeeper . imageFile , " application/octet-stream " )
intent . putExtra ( " usbDevice " , StateKeeper . usbDevice )
2018-08-30 14:42:35 +00:00
if ( Build . VERSION . SDK _INT >= Build . VERSION_CODES . O )
activity !! . startForegroundService ( intent )
else
activity !! . startService ( intent )
activity !! . finish ( )
2018-08-13 23:32:02 +00:00
}
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? ,
savedInstanceState : Bundle ? ) : View ? {
StateKeeper . currentFragment = this
StateKeeper . wizardStep = WizardStep . CONFIRM
val view = inflater . inflate ( R . layout . fragment _confirminfo , container , false )
view . confirm _sel _method . text = when ( StateKeeper . flashMethod ) {
FlashMethod . FLASH _API -> getString ( R . string . flash _dd _usb _api )
2018-08-16 14:21:43 +00:00
FlashMethod . FLASH _DMG _API -> getString ( R . string . flash _dmg _api )
2018-08-13 23:32:02 +00:00
FlashMethod . FLASH _UNETBOOTIN -> getString ( R . string . flash _unetbootin )
FlashMethod . FLASH _WOEUSB -> getString ( R . string . flash _woeusb )
else -> null
}
view . confirm _sel _image . text = StateKeeper . imageFile ?. getFileName ( context !! )
if ( view . confirm _sel _image . text == null )
view . confirm _sel _image . text = getString ( R . string . unknown _filename )
val imgSize = StateKeeper . imageFile ?. getFileSize ( context !! )
view . confirm _sel _image _size . text = imgSize ?. toHRSize ( )
view . confirm _sel _usbdev . text = StateKeeper . usbDevice ?. name
2018-08-14 15:22:53 +00:00
for ( trial in 0. . 1 ) {
try {
StateKeeper . usbMassStorageDevice !! . init ( )
val blockDev = StateKeeper . usbMassStorageDevice ?. blockDevice
if ( blockDev != null ) {
val devSize = ( blockDev . size . toLong ( ) * blockDev . blockSize . toLong ( ) )
view . confirm _sel _usbdev _size . text = devSize . toHRSize ( )
if ( imgSize !! > devSize )
view . confirm _extra _info . text = getString ( R . string . image _bigger _than _usb )
else {
2018-08-17 00:50:18 +00:00
var text =
if ( StateKeeper . flashMethod == FlashMethod . FLASH _DMG _API )
getString ( R . string . no _image _size _check _dmg ) + " \n "
else
" "
text += getString ( R . string . tap _next _to _write )
view . confirm _extra _info . text = text
2018-08-14 15:22:53 +00:00
canContinue = true
}
} else {
view . confirm _extra _info . text = getString ( R . string . cant _read _usbdev )
}
} catch ( e : IOException ) {
if ( trial == 0 ) {
StateKeeper . usbMassStorageDevice !! . close ( )
continue
} else {
view . confirm _extra _info . text = " Could not access USB device. Maybe you ran the app previously and it crashed? Remove and reinsert the USB drive, then restart the app. "
break
2018-08-14 13:41:38 +00:00
}
2018-08-13 23:32:02 +00:00
}
}
return view
}
}