Merge branch 'stable'
This commit is contained in:
142 files changed
+2901
-452
No files matched your search
+5
-1
@@ -35,7 +35,8 @@ hs_err_pid*
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
.idea
|
||||
.idea/
|
||||
.idea.*/
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
@@ -121,3 +122,6 @@ fastlane/screenshots
|
||||
fastlane/test_output
|
||||
fastlane/readme.md
|
||||
|
||||
|
||||
# Project exclude paths
|
||||
/dmg2img/.cxx/
|
||||
@@ -1,6 +1,3 @@
|
||||
[submodule "libaums"]
|
||||
path = libaums
|
||||
url = https://github.com/Depau/libaums.git
|
||||
[submodule "dmg2img"]
|
||||
path = dmg2img/src/c/dmg2img
|
||||
url = https://github.com/Depau/dmg2img-cmake.git
|
||||
|
||||
+5
-2
@@ -2,7 +2,7 @@ language: android
|
||||
|
||||
android:
|
||||
components:
|
||||
- build-tools-28.0.3
|
||||
- build-tools-29.0.2
|
||||
|
||||
install:
|
||||
- yes | sdkmanager "tools" > /dev/null
|
||||
@@ -10,8 +10,11 @@ install:
|
||||
- yes | sdkmanager "platforms;android-28" > /dev/null
|
||||
- yes | sdkmanager "ndk-bundle" > /dev/null
|
||||
- yes | sdkmanager "ndk;20.0.5594570" > /dev/null
|
||||
- yes | sdkmanager "build-tools;28.0.3" > /dev/null
|
||||
- yes | sdkmanager "build-tools;29.0.2" > /dev/null
|
||||
- yes | sdkmanager "cmake;3.10.2.4988404" > /dev/null
|
||||
- yes | sdkmanager "lldb;3.1" > /dev/null
|
||||
- yes | sdkmanager "extras;android;m2repository" > /dev/null
|
||||
- yes | sdkmanager "extras;google;m2repository" > /dev/null
|
||||
|
||||
script:
|
||||
- ./gradlew build test
|
||||
+37
-16
@@ -5,50 +5,71 @@ apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
|
||||
android {
|
||||
compileSdkVersion 28
|
||||
compileSdkVersion 29
|
||||
buildToolsVersion '28.0.3'
|
||||
defaultConfig {
|
||||
applicationId "eu.depau.etchdroid"
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 28
|
||||
versionCode 9
|
||||
versionName "1.3.32"
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 29
|
||||
versionCode 11
|
||||
versionName "1.4"
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
vectorDrawables.useSupportLibrary = true
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
minifyEnabled true
|
||||
shrinkResources false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
debug {
|
||||
minifyEnabled true
|
||||
shrinkResources false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
packagingOptions {
|
||||
exclude 'META-INF/libaums_release.kotlin_module'
|
||||
}
|
||||
compileOptions {
|
||||
encoding = 'UTF-8'
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
lintOptions {
|
||||
abortOnError false
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation 'androidx.appcompat:appcompat:1.1.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta3'
|
||||
implementation 'com.google.android.material:material:1.2.0-alpha02'
|
||||
implementation 'androidx.recyclerview:recyclerview:1.1.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.2.0-alpha01'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
|
||||
implementation 'com.google.android.material:material:1.2.0-alpha03'
|
||||
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha01'
|
||||
implementation 'androidx.gridlayout:gridlayout:1.0.0'
|
||||
implementation 'androidx.core:core:1.2.0-rc01'
|
||||
implementation 'androidx.fragment:fragment:1.2.0-rc02'
|
||||
implementation 'androidx.fragment:fragment:1.2.0-rc05'
|
||||
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-alpha03'
|
||||
api 'com.google.guava:guava:27.0.1-android'
|
||||
|
||||
implementation 'com.github.Depau:kotlet-android:v0.2.5'
|
||||
implementation 'com.google.code.gson:gson:2.8.5'
|
||||
implementation 'com.google.guava:guava:27.0.1-android'
|
||||
api 'com.github.codekidX:storage-chooser:2.0.4.2'
|
||||
// implementation 'com.github.mjdev:libaums:0.5.5'
|
||||
implementation project(':libaums')
|
||||
|
||||
api 'com.github.EtchDroid:libaums_wrapper:v1.1'
|
||||
api 'me.jahnen:libaums-libusbcommunication:0.1'
|
||||
|
||||
implementation project(':dmg2img')
|
||||
|
||||
implementation 'me.jfenn:Attribouter:0.1.5'
|
||||
testImplementation 'junit:junit:4.13-beta-1'
|
||||
implementation 'androidx.room:room-runtime:2.2.3'
|
||||
annotationProcessor 'androidx.room:room-compiler:2.2.3'
|
||||
|
||||
androidTestImplementation 'androidx.test:runner:1.2.0'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
|
||||
testImplementation 'junit:junit:4.13-beta-3'
|
||||
testImplementation 'androidx.test:core:1.2.0'
|
||||
testImplementation 'org.mockito:mockito-core:2.28.2'
|
||||
}
|
||||
Vendored
+3
@@ -19,3 +19,6 @@
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
|
||||
-dontobfuscate
|
||||
-keep class java.lang.ClassValue
|
||||
@@ -21,7 +21,7 @@
|
||||
android:theme="@style/AppTheme"
|
||||
tools:ignore="GoogleAppIndexingWarning">
|
||||
<activity
|
||||
android:name=".activities.StartActivity"
|
||||
android:name="eu.depau.etchdroid.ui.activities.StartActivity"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/MaterialAppTheme">
|
||||
<intent-filter>
|
||||
@@ -31,14 +31,14 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".activities.UsbDrivePickerActivity"
|
||||
android:name="eu.depau.etchdroid.ui.activities.UsbDrivePickerActivity"
|
||||
android:label="@string/title_activity_usb_drive_picker"
|
||||
android:parentActivityName=".activities.StartActivity"
|
||||
android:parentActivityName="eu.depau.etchdroid.ui.activities.StartActivity"
|
||||
android:theme="@style/MaterialAppTheme">
|
||||
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="eu.depau.etchdroid.activities.StartActivity"/>
|
||||
android:value="eu.depau.etchdroid.ui.activities.StartActivity"/>
|
||||
|
||||
<!-- Open ISO files by mimetype -->
|
||||
<intent-filter android:label="@string/app_name">
|
||||
@@ -126,17 +126,17 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".activities.ConfirmationActivity"
|
||||
android:name="eu.depau.etchdroid.ui.activities.ConfirmationActivity"
|
||||
android:label="@string/title_activity_confirmation"
|
||||
android:parentActivityName=".activities.UsbDrivePickerActivity"
|
||||
android:parentActivityName="eu.depau.etchdroid.ui.activities.UsbDrivePickerActivity"
|
||||
android:theme="@style/MaterialAppTheme">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="eu.depau.etchdroid.activities.UsbDrivePickerActivity"/>
|
||||
android:value="eu.depau.etchdroid.ui.activities.UsbDrivePickerActivity"/>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".activities.ErrorActivity"
|
||||
android:name="eu.depau.etchdroid.ui.activities.ErrorActivity"
|
||||
android:excludeFromRecents="true"
|
||||
android:label="@string/write_failed"
|
||||
android:launchMode="singleTask"
|
||||
@@ -151,6 +151,12 @@
|
||||
android:name=".services.UsbApiDmgWriteService"
|
||||
android:exported="false"/>
|
||||
|
||||
</application>
|
||||
<!--suppress AndroidDomInspection -->
|
||||
<provider
|
||||
android:name="com.github.mjdev.libaums.storageprovider.UsbDocumentProvider"
|
||||
android:exported="false"
|
||||
tools:node="remove">
|
||||
</provider>
|
||||
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,9 @@
|
||||
package eu.depau.etchdroid
|
||||
|
||||
object AppBuildConfig {
|
||||
@JvmField
|
||||
var TEST_BUILD = false
|
||||
|
||||
@JvmField
|
||||
var USE_LOCAL_BROADCASTS = true
|
||||
}
|
||||
@@ -2,15 +2,14 @@ package eu.depau.etchdroid
|
||||
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.net.Uri
|
||||
import com.github.mjdev.libaums.UsbMassStorageDevice
|
||||
import eu.depau.etchdroid.enums.FlashMethod
|
||||
import eu.depau.etchdroid.img_types.Image
|
||||
import eu.depau.etchdroid.libaums_wrapper.EtchDroidUsbMassStorageDevice
|
||||
import eu.depau.etchdroid.utils.enums.FlashMethod
|
||||
import eu.depau.etchdroid.utils.imagetypes.Image
|
||||
|
||||
object StateKeeper {
|
||||
var flashMethod: FlashMethod? = null
|
||||
var imageFile: Uri? = null
|
||||
var imageRepr: Image? = null
|
||||
|
||||
var usbDevice: UsbDevice? = null
|
||||
var usbMassStorageDevice: UsbMassStorageDevice? = null
|
||||
var libusbRegistered: Boolean = false
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package eu.depau.etchdroid.activities
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.adapters.LicenseRecyclerViewAdapter
|
||||
import eu.depau.etchdroid.utils.ClickListener
|
||||
import eu.depau.etchdroid.utils.License
|
||||
import eu.depau.etchdroid.utils.RecyclerViewTouchListener
|
||||
import kotlinx.android.synthetic.main.activity_licenses.*
|
||||
|
||||
|
||||
class LicensesActivity : AppCompatActivity() {
|
||||
private lateinit var recyclerView: RecyclerView
|
||||
private lateinit var viewManager: RecyclerView.LayoutManager
|
||||
private lateinit var viewAdapter: LicenseRecyclerViewAdapter
|
||||
|
||||
lateinit var licenses: Array<License>
|
||||
|
||||
private fun updateLicenses() {
|
||||
if (!::licenses.isInitialized) {
|
||||
licenses = arrayOf(
|
||||
License(getString(R.string.this_app), Uri.parse("https://github.com/Depau/EtchDroid"), getString(R.string.license_gpl3)),
|
||||
License("Storage Chooser 2.0", Uri.parse("https://github.com/codekidX/storage-chooser"), getString(R.string.license_mpl_2_0), getString(R.string.storagechooser_license_description)),
|
||||
License("libaums (fork)", Uri.parse("https://github.com/Depau/libaums"), getString(R.string.license_apache2_0), getString(R.string.libaums_license_desc)),
|
||||
License("dmg2img (fork)", Uri.parse("https://github.com/Depau/dmg2img-cmake"), getString(R.string.license_gpl2), getString(R.string.dmg2img_license_desc)),
|
||||
License("bzip2", Uri.parse("https://github.com/LuaDist/bzip2/"), getString(R.string.license_bzip2)),
|
||||
License("LibreSSL", Uri.parse("https://github.com/libressl-portable/portable"), getString(R.string.license_custom))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_licenses)
|
||||
updateLicenses()
|
||||
|
||||
// Enable back button in action bar
|
||||
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
|
||||
|
||||
viewManager = LinearLayoutManager(this)
|
||||
recyclerView = licenses_recycler_view
|
||||
viewAdapter = LicenseRecyclerViewAdapter(licenses)
|
||||
recyclerView.adapter = viewAdapter
|
||||
recyclerView.layoutManager = viewManager
|
||||
|
||||
recyclerView.addOnItemTouchListener(RecyclerViewTouchListener(this, recyclerView, object : ClickListener {
|
||||
override fun onClick(view: View, position: Int) {
|
||||
val intent = Intent(Intent.ACTION_VIEW, viewAdapter.get(position).url)
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
override fun onLongClick(view: View, position: Int) {}
|
||||
}))
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
|
||||
when (item?.itemId) {
|
||||
android.R.id.home -> {
|
||||
finish()
|
||||
return true
|
||||
}
|
||||
}
|
||||
return super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package eu.depau.etchdroid.adapters
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.RelativeLayout
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.utils.License
|
||||
import kotlinx.android.synthetic.main.license_row.view.*
|
||||
|
||||
|
||||
class LicenseRecyclerViewAdapter(private val dataset: Array<License>) : RecyclerView.Adapter<LicenseRecyclerViewAdapter.ViewHolder>() {
|
||||
|
||||
class ViewHolder(val relLayout: RelativeLayout) : RecyclerView.ViewHolder(relLayout)
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
|
||||
ViewHolder {
|
||||
|
||||
val relLayout = LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.license_row, parent, false) as RelativeLayout
|
||||
return ViewHolder(relLayout)
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val license = dataset[position]
|
||||
|
||||
holder.relLayout.software_name.text = license.name
|
||||
holder.relLayout.software_desc.text = license.description
|
||||
holder.relLayout.software_license.text = license.license
|
||||
holder.relLayout.software_url.text = license.url.toString()
|
||||
|
||||
holder.relLayout.software_desc.visibility = if (license.description == null) View.GONE else View.VISIBLE
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = dataset.size
|
||||
|
||||
fun get(position: Int): License {
|
||||
return dataset[position]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package eu.depau.etchdroid.broadcasts
|
||||
|
||||
import android.content.Intent
|
||||
import eu.depau.etchdroid.broadcasts.dto.JobProgressUpdateBroadcastDTO
|
||||
|
||||
object JobProgressUpdateBroadcast {
|
||||
const val ACTION = "eu.depau.etchdroid.broadcast.JOB_PROGRESS_UPDATE"
|
||||
|
||||
fun getIntent(dto: JobProgressUpdateBroadcastDTO) = Intent().apply {
|
||||
action = ACTION
|
||||
dto.writeToIntent(this)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package eu.depau.etchdroid.broadcasts.dto
|
||||
|
||||
import android.content.Intent
|
||||
import eu.depau.etchdroid.utils.worker.enums.RateUnit
|
||||
import java.io.Serializable
|
||||
|
||||
|
||||
data class JobProgressUpdateBroadcastDTO(
|
||||
val jobId: Long,
|
||||
val indefinite: Boolean = false,
|
||||
val showProgressBar: Boolean = true,
|
||||
val completed: Boolean = false,
|
||||
val step: Int? = null,
|
||||
val percentage: Double? = null,
|
||||
val rate: Double? = null,
|
||||
val rateUnit: RateUnit? = null,
|
||||
val currentMessage: String? = null,
|
||||
val error: Throwable? = null
|
||||
) : Serializable {
|
||||
fun writeToIntent(intent: Intent) {
|
||||
intent.putExtra(EXTRA, this)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val EXTRA = "eu.depau.etchdroid.broadcast.extras.UPDATE_DTO"
|
||||
|
||||
fun readFromIntent(intent: Intent) =
|
||||
intent.getSerializableExtra(EXTRA)!! as JobProgressUpdateBroadcastDTO
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package eu.depau.etchdroid.db
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import eu.depau.etchdroid.db.converter.JobProcedureConverter
|
||||
import eu.depau.etchdroid.db.entity.Job
|
||||
import eu.depau.etchdroid.db.repository.JobRepository
|
||||
|
||||
@Database(entities = [Job::class], version = 1)
|
||||
@TypeConverters(JobProcedureConverter::class)
|
||||
abstract class EtchDroidDatabase : RoomDatabase() {
|
||||
abstract fun jobRepository(): JobRepository
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var INSTANCE: EtchDroidDatabase? = null
|
||||
|
||||
fun getDatabase(context: Context): EtchDroidDatabase {
|
||||
val tempInstance = INSTANCE
|
||||
if (tempInstance != null) {
|
||||
return tempInstance
|
||||
}
|
||||
synchronized(this) {
|
||||
val instance = Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
EtchDroidDatabase::class.java,
|
||||
"etchdroid_db"
|
||||
).build()
|
||||
INSTANCE = instance
|
||||
return instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package eu.depau.etchdroid.db.converter
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import com.google.gson.Gson
|
||||
import eu.depau.etchdroid.utils.job.IJobProcedure
|
||||
|
||||
class JobProcedureConverter {
|
||||
@TypeConverter
|
||||
fun fromJobProcedure(value: IJobProcedure): String {
|
||||
val gson = Gson()
|
||||
return gson.toJson(value)
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun fromString(value: String): IJobProcedure {
|
||||
val gson = Gson()
|
||||
return gson.fromJson(value, IJobProcedure::class.java)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package eu.depau.etchdroid.db.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import eu.depau.etchdroid.utils.job.IJobProcedure
|
||||
|
||||
@Entity
|
||||
data class Job(
|
||||
@PrimaryKey(autoGenerate = true) val jobId: Long = 0,
|
||||
var jobProcedure: IJobProcedure,
|
||||
var checkpointActionIndex: Int = 0,
|
||||
var checkpointData: String? = null,
|
||||
var completed: Boolean = false
|
||||
)
|
||||
@@ -0,0 +1,25 @@
|
||||
package eu.depau.etchdroid.db.repository
|
||||
|
||||
import androidx.room.*
|
||||
import eu.depau.etchdroid.db.entity.Job
|
||||
|
||||
@Dao
|
||||
interface JobRepository {
|
||||
@Query("SELECT * FROM job")
|
||||
fun getAll(): List<Job>
|
||||
|
||||
@Query("SELECT * FROM job WHERE jobId == :jobId LIMIT 1")
|
||||
fun getById(jobId: Long): Job
|
||||
|
||||
@Insert
|
||||
fun insert(job: Job): Long
|
||||
|
||||
@Insert
|
||||
fun insertAll(vararg jobs: Job)
|
||||
|
||||
@Update
|
||||
fun update(job: Job)
|
||||
|
||||
@Delete
|
||||
fun delete(job: Job)
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
package eu.depau.etchdroid.enums
|
||||
|
||||
enum class PartitionType
|
||||
@@ -1,112 +0,0 @@
|
||||
package eu.depau.etchdroid.img_types
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import eu.depau.etchdroid.enums.FilesystemType
|
||||
import eu.depau.etchdroid.enums.PartitionTableType
|
||||
import eu.depau.etchdroid.kotlin_exts.getBinary
|
||||
import eu.depau.etchdroid.kotlin_exts.readString
|
||||
import eu.depau.etchdroid.utils.Partition
|
||||
import eu.depau.etchdroid.utils.PartitionBuilder
|
||||
import java.io.File
|
||||
|
||||
val SECTOR_SIZE = 512
|
||||
private val partRegex = Regex("partition (\\d+): begin=(\\d+), size=(\\d+), decoded=(\\d+), firstsector=(\\d+), sectorcount=(\\d+), blocksruncount=(\\d+)\\s+(.*) \\((.+) : \\d+\\)", RegexOption.MULTILINE)
|
||||
|
||||
private fun readPartitionTable(dmg2img: File, libDir: String, file: File): Triple<PartitionTableType?, List<Partition>?, Long?> {
|
||||
val pt = ArrayList<Partition>()
|
||||
var ptType: PartitionTableType? = null
|
||||
var imgSize = 0L
|
||||
|
||||
val pb = ProcessBuilder(dmg2img.path, "-v", "-l", file.path)
|
||||
pb.environment()["LD_LIBRARY_PATH"] = libDir
|
||||
pb.redirectErrorStream(true)
|
||||
|
||||
val p = pb.start()
|
||||
val out = p.inputStream.readString()
|
||||
System.err.println(out)
|
||||
p.waitFor()
|
||||
val matches = partRegex.findAll(out)
|
||||
|
||||
matchloop@ for (m in matches) {
|
||||
val (
|
||||
number, begin, size,
|
||||
decoded, firstsector,
|
||||
sectorcount, blocksruncount,
|
||||
label, type
|
||||
) = m.destructured
|
||||
|
||||
val pb = PartitionBuilder()
|
||||
|
||||
pb.number = number.toInt()
|
||||
pb.size = SECTOR_SIZE * sectorcount.toLong()
|
||||
imgSize += pb.size!!
|
||||
|
||||
if (label.isNotEmpty())
|
||||
pb.fsLabel = label
|
||||
|
||||
pb.partLabel = type
|
||||
|
||||
when (type) {
|
||||
"Apple_partition_map" -> {
|
||||
ptType = PartitionTableType.MAC
|
||||
}
|
||||
"MBR" -> {
|
||||
ptType = PartitionTableType.MSDOS
|
||||
continue@matchloop
|
||||
}
|
||||
}
|
||||
|
||||
pb.fsType = when {
|
||||
type == "Apple_Empty" || type == "Apple_Scratch" || type == "Apple_Free" -> FilesystemType.FREE
|
||||
type == "Apple_HFS" -> FilesystemType.HFSPLUS
|
||||
type == "Apple_HFSX" -> FilesystemType.HFSPLUS
|
||||
type == "Windows_FAT_32" -> FilesystemType.FAT32
|
||||
type.startsWith("Apple_") || type == "DDM" -> FilesystemType.APT_DATA
|
||||
else -> FilesystemType.UNKNOWN
|
||||
}
|
||||
|
||||
pt.add(pb.build())
|
||||
}
|
||||
|
||||
return Triple(ptType, pt, imgSize)
|
||||
}
|
||||
|
||||
class DMGImage(private val uri: Uri, private val context: Context) : Image {
|
||||
private val dmg2img: File = context.getBinary("dmg2img")
|
||||
private val libDir: String = context.applicationInfo.nativeLibraryDir
|
||||
private var loaded: Boolean = false
|
||||
private var partTable: List<Partition>? = null
|
||||
private var partTableType: PartitionTableType? = null
|
||||
private var imgSize: Long? = null
|
||||
|
||||
private fun readInfo() {
|
||||
if (loaded)
|
||||
return
|
||||
val triple = readPartitionTable(dmg2img, libDir, File(uri.path))
|
||||
loaded = true
|
||||
partTableType = triple.first
|
||||
partTable = triple.second
|
||||
imgSize = triple.third
|
||||
}
|
||||
|
||||
override val partitionTable: List<Partition>?
|
||||
get() {
|
||||
readInfo()
|
||||
return partTable
|
||||
}
|
||||
|
||||
|
||||
override val tableType: PartitionTableType?
|
||||
get() {
|
||||
readInfo()
|
||||
return partTableType
|
||||
}
|
||||
|
||||
override val size: Long?
|
||||
get() {
|
||||
readInfo()
|
||||
return imgSize
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package eu.depau.etchdroid.img_types
|
||||
|
||||
import eu.depau.etchdroid.enums.PartitionTableType
|
||||
import eu.depau.etchdroid.utils.Partition
|
||||
|
||||
interface Image {
|
||||
val partitionTable: List<Partition>?
|
||||
val tableType: PartitionTableType?
|
||||
val size: Long?
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package eu.depau.etchdroid.job
|
||||
|
||||
import android.os.Parcel
|
||||
import eu.depau.etchdroid.utils.StringResBuilder
|
||||
import eu.depau.etchdroid.utils.job.IJobAction
|
||||
import eu.depau.kotlet.android.parcelable.*
|
||||
import java.io.Serializable
|
||||
|
||||
abstract class AbstractJobAction(
|
||||
override val name: StringResBuilder,
|
||||
override val progressWeight: Double,
|
||||
override var checkpoint: Serializable? = null,
|
||||
override val showInGUI: Boolean = true,
|
||||
override val runAlways: Boolean = true
|
||||
) : IJobAction, KotletParcelable {
|
||||
|
||||
constructor(parcel: Parcel) : this(
|
||||
name = parcel.readTypedObjectCompat(StringResBuilder.CREATOR)!!,
|
||||
progressWeight = parcel.readDouble(),
|
||||
checkpoint = parcel.readNullable { parcel.readSerializable() },
|
||||
showInGUI = parcel.readBool(),
|
||||
runAlways = parcel.readBool()
|
||||
)
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.apply {
|
||||
writeTypedObjectCompat(name, flags)
|
||||
writeDouble(progressWeight)
|
||||
writeNullable(checkpoint) { writeSerializable(it) }
|
||||
writeBool(showInGUI)
|
||||
writeBool(runAlways)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package eu.depau.etchdroid.job
|
||||
|
||||
import android.content.Context
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.net.Uri
|
||||
import android.os.Parcel
|
||||
import eu.depau.etchdroid.job.enums.JobType
|
||||
import eu.depau.etchdroid.job.enums.JobType.*
|
||||
import eu.depau.etchdroid.job.enums.TargetType
|
||||
import eu.depau.etchdroid.job.enums.TargetType.*
|
||||
import eu.depau.etchdroid.job.procedures.buildInOutStreamCopyProcedure
|
||||
import eu.depau.etchdroid.utils.job.IJobProcedure
|
||||
import eu.depau.etchdroid.utils.job.IJobProcedureBuilder
|
||||
import eu.depau.kotlet.android.parcelable.*
|
||||
|
||||
class JobProcedureBuilder() : IJobProcedureBuilder, KotletParcelable {
|
||||
lateinit var jobType: JobType
|
||||
|
||||
internal var inputType: TargetType? = null
|
||||
internal var input: Any? = null
|
||||
|
||||
internal var outputType: TargetType? = null
|
||||
internal var output: Any? = null
|
||||
|
||||
|
||||
override fun build(context: Context): IJobProcedure {
|
||||
assert(::jobType.isInitialized) { "Job type not set, aborting" }
|
||||
|
||||
return when (jobType) {
|
||||
INOUT_STREAM_COPY -> buildInOutStreamCopyProcedure(context)
|
||||
DMG_EXTRACT_TO_OUTPUTSTREAM -> TODO()
|
||||
WINDOWS_INSTALLER -> TODO()
|
||||
MACOS_INSTALLER -> TODO()
|
||||
UNETBOOTIN -> TODO()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun setInput(uri: Uri) {
|
||||
inputType = ANDROID_URI
|
||||
input = uri
|
||||
}
|
||||
|
||||
fun setInput(usbDev: UsbDevice) {
|
||||
inputType = AUMS_BLOCKDEV
|
||||
input = usbDev
|
||||
}
|
||||
|
||||
fun setInput(path: String) {
|
||||
inputType = FS_FILE
|
||||
input = path
|
||||
}
|
||||
|
||||
fun setOutput(uri: Uri) {
|
||||
outputType = ANDROID_URI
|
||||
output = uri
|
||||
}
|
||||
|
||||
fun setOutput(usbDev: UsbDevice) {
|
||||
outputType = AUMS_BLOCKDEV
|
||||
output = usbDev
|
||||
}
|
||||
|
||||
fun setOutput(path: String) {
|
||||
outputType = FS_FILE
|
||||
output = path
|
||||
}
|
||||
|
||||
constructor(parcel: Parcel) : this() {
|
||||
parcel.apply {
|
||||
jobType = readEnum<JobType>()!!
|
||||
inputType = readNullable { readEnum<TargetType>() }
|
||||
outputType = readNullable { readEnum<TargetType>() }
|
||||
|
||||
input = inputType?.let {
|
||||
when (it) {
|
||||
ANDROID_URI -> readTypedObjectCompat(Uri.CREATOR)
|
||||
AUMS_BLOCKDEV -> readTypedObjectCompat(UsbDevice.CREATOR)
|
||||
FS_FILE -> readString()
|
||||
}!!
|
||||
}
|
||||
|
||||
output = outputType?.let {
|
||||
when (it) {
|
||||
ANDROID_URI -> readTypedObjectCompat(Uri.CREATOR)
|
||||
AUMS_BLOCKDEV -> readTypedObjectCompat(UsbDevice.CREATOR)
|
||||
FS_FILE -> readString()
|
||||
}!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.apply {
|
||||
writeEnum(jobType)
|
||||
writeNullable(inputType) { writeEnum(it) }
|
||||
writeNullable(outputType) { writeEnum(it) }
|
||||
|
||||
inputType?.let {
|
||||
when (it) {
|
||||
ANDROID_URI -> writeTypedObjectCompat(input as Uri, flags)
|
||||
AUMS_BLOCKDEV -> writeTypedObjectCompat(input as UsbDevice, flags)
|
||||
FS_FILE -> writeString(input as String)
|
||||
}
|
||||
}
|
||||
|
||||
outputType?.let {
|
||||
when (it) {
|
||||
ANDROID_URI -> writeTypedObjectCompat(output as Uri, flags)
|
||||
AUMS_BLOCKDEV -> writeTypedObjectCompat(output as UsbDevice, flags)
|
||||
FS_FILE -> writeString(output as String)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val CREATOR = parcelableCreator { JobProcedureBuilder::class.java }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package eu.depau.etchdroid.job.actions
|
||||
|
||||
import android.os.Parcel
|
||||
import eu.depau.etchdroid.job.AbstractJobAction
|
||||
import eu.depau.etchdroid.utils.StringResBuilder
|
||||
import eu.depau.etchdroid.utils.job.enums.SharedDataType
|
||||
import eu.depau.etchdroid.utils.worker.IAsyncWorker
|
||||
import eu.depau.etchdroid.workers.Input2OutputStreamCopyAsyncWorker
|
||||
import eu.depau.kotlet.android.parcelable.parcelableCreator
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.io.Serializable
|
||||
|
||||
class Input2OutputStreamCopyAction : AbstractJobAction {
|
||||
constructor(
|
||||
name: StringResBuilder,
|
||||
progressWeight: Double,
|
||||
checkpoint: Serializable?,
|
||||
showInGUI: Boolean = true,
|
||||
runAlways: Boolean = false
|
||||
) : super(name, progressWeight, checkpoint, showInGUI, runAlways)
|
||||
|
||||
constructor(parcel: Parcel) : super(parcel)
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val CREATOR = parcelableCreator { Input2OutputStreamCopyAction::class.java }
|
||||
}
|
||||
|
||||
override fun getWorker(sharedData: MutableMap<SharedDataType, Any?>): IAsyncWorker {
|
||||
val inputStream = sharedData[SharedDataType.INPUT_STREAM] as InputStream
|
||||
val outputStream = sharedData[SharedDataType.OUTPUT_STREAM] as OutputStream
|
||||
val size = sharedData[SharedDataType.SIZE] as Long
|
||||
|
||||
// TODO use checkpoint data
|
||||
|
||||
return Input2OutputStreamCopyAsyncWorker(
|
||||
source = inputStream,
|
||||
dest = outputStream,
|
||||
seek = 0L /*stub*/,
|
||||
size = size
|
||||
)
|
||||
}
|
||||
|
||||
override fun checkPreconditions(): List<StringResBuilder> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package eu.depau.etchdroid.job.actions
|
||||
|
||||
import android.os.Parcel
|
||||
import eu.depau.etchdroid.job.AbstractJobAction
|
||||
import eu.depau.etchdroid.utils.StringResBuilder
|
||||
import eu.depau.etchdroid.utils.job.enums.SharedDataType
|
||||
import eu.depau.etchdroid.utils.worker.IAsyncWorker
|
||||
import eu.depau.etchdroid.workers.StreamCloseWorker
|
||||
import eu.depau.kotlet.android.parcelable.parcelableCreator
|
||||
import eu.depau.kotlet.android.parcelable.readEnum
|
||||
import eu.depau.kotlet.android.parcelable.writeEnum
|
||||
import java.io.Serializable
|
||||
|
||||
class StreamCloseAction : AbstractJobAction {
|
||||
private val destKey: SharedDataType
|
||||
|
||||
override fun getWorker(sharedData: MutableMap<SharedDataType, Any?>): IAsyncWorker =
|
||||
StreamCloseWorker(sharedData, destKey)
|
||||
|
||||
override fun checkPreconditions(): List<StringResBuilder> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
constructor(
|
||||
name: StringResBuilder,
|
||||
progressWeight: Double,
|
||||
checkpoint: Serializable?,
|
||||
destKey: SharedDataType,
|
||||
showInGUI: Boolean = true,
|
||||
runAlways: Boolean = false
|
||||
) : super(name, progressWeight, checkpoint, showInGUI, runAlways) {
|
||||
this.destKey = destKey
|
||||
}
|
||||
|
||||
constructor(parcel: Parcel) : super(parcel) {
|
||||
this.destKey = parcel.readEnum<SharedDataType>()!!
|
||||
}
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
super.writeToParcel(parcel, flags)
|
||||
parcel.writeEnum(destKey)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val CREATOR = parcelableCreator { StreamCloseAction::class.java }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package eu.depau.etchdroid.job.actions
|
||||
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.net.Uri
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import eu.depau.etchdroid.job.AbstractJobAction
|
||||
import eu.depau.etchdroid.job.enums.TargetType
|
||||
import eu.depau.etchdroid.job.enums.TargetType.*
|
||||
import eu.depau.etchdroid.utils.StringResBuilder
|
||||
import eu.depau.etchdroid.utils.job.enums.SharedDataType
|
||||
import eu.depau.etchdroid.utils.worker.IAsyncWorker
|
||||
import eu.depau.etchdroid.workers.StreamOpenWorker
|
||||
import eu.depau.etchdroid.workers.enums.StreamDirection
|
||||
import eu.depau.kotlet.android.parcelable.*
|
||||
import java.io.Serializable
|
||||
|
||||
class StreamOpenAction : AbstractJobAction {
|
||||
private val targetDescriptor: Any
|
||||
private val targetType: TargetType
|
||||
private val destKey: SharedDataType
|
||||
private val streamDirection: StreamDirection
|
||||
|
||||
constructor(
|
||||
name: StringResBuilder,
|
||||
progressWeight: Double,
|
||||
checkpoint: Serializable?,
|
||||
destKey: SharedDataType,
|
||||
streamDirection: StreamDirection,
|
||||
targetDescriptor: Any,
|
||||
targetType: TargetType,
|
||||
showInGUI: Boolean = false,
|
||||
runAlways: Boolean = true
|
||||
) : super(name, progressWeight, checkpoint, showInGUI, runAlways) {
|
||||
this.destKey = destKey
|
||||
this.streamDirection = streamDirection
|
||||
this.targetDescriptor = targetDescriptor
|
||||
this.targetType = targetType
|
||||
}
|
||||
|
||||
override fun getWorker(sharedData: MutableMap<SharedDataType, Any?>): IAsyncWorker {
|
||||
return when (targetType) {
|
||||
ANDROID_URI -> StreamOpenWorker(sharedData, destKey, streamDirection, targetType, targetDescriptor as Uri)
|
||||
AUMS_BLOCKDEV -> StreamOpenWorker(sharedData, destKey, streamDirection, targetType, targetDescriptor as UsbDevice)
|
||||
FS_FILE -> StreamOpenWorker(sharedData, destKey, streamDirection, targetType, targetDescriptor as String)
|
||||
}
|
||||
}
|
||||
|
||||
override fun checkPreconditions(): List<StringResBuilder> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
|
||||
// Parcelable implementation
|
||||
|
||||
constructor(parcel: Parcel) : super(parcel) {
|
||||
parcel.apply {
|
||||
destKey = readEnum<SharedDataType>()!!
|
||||
streamDirection = readEnum<StreamDirection>()!!
|
||||
targetType = readEnum<TargetType>()!!
|
||||
targetDescriptor = when (targetType) {
|
||||
ANDROID_URI -> readTypedObjectCompat(Uri.CREATOR)
|
||||
AUMS_BLOCKDEV -> readTypedObjectCompat(UsbDevice.CREATOR)
|
||||
FS_FILE -> readString()
|
||||
}!!
|
||||
}
|
||||
}
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
super.writeToParcel(parcel, flags)
|
||||
parcel.apply {
|
||||
writeEnum(destKey)
|
||||
writeEnum(streamDirection)
|
||||
writeEnum(targetType)
|
||||
when (targetType) {
|
||||
ANDROID_URI, AUMS_BLOCKDEV -> writeTypedObjectCompat(targetDescriptor as Parcelable, flags)
|
||||
FS_FILE -> writeString(targetDescriptor as String)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val CREATOR = parcelableCreator { Input2OutputStreamCopyAction::class.java }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package eu.depau.etchdroid.job.enums
|
||||
|
||||
enum class JobType {
|
||||
INOUT_STREAM_COPY,
|
||||
DMG_EXTRACT_TO_OUTPUTSTREAM,
|
||||
WINDOWS_INSTALLER, // TODO: implement
|
||||
MACOS_INSTALLER, // TODO: implement
|
||||
UNETBOOTIN // TODO: implement
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package eu.depau.etchdroid.job.enums
|
||||
|
||||
enum class TargetType {
|
||||
ANDROID_URI,
|
||||
AUMS_BLOCKDEV,
|
||||
FS_FILE
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package eu.depau.etchdroid.job.procedures
|
||||
|
||||
import android.content.Context
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.job.JobProcedureBuilder
|
||||
import eu.depau.etchdroid.job.actions.Input2OutputStreamCopyAction
|
||||
import eu.depau.etchdroid.job.actions.StreamCloseAction
|
||||
import eu.depau.etchdroid.job.actions.StreamOpenAction
|
||||
import eu.depau.etchdroid.utils.StringResBuilder
|
||||
import eu.depau.etchdroid.utils.job.IJobProcedure
|
||||
import eu.depau.etchdroid.utils.job.enums.SharedDataType
|
||||
import eu.depau.etchdroid.utils.job.impl.JobProcedure
|
||||
import eu.depau.etchdroid.workers.enums.StreamDirection
|
||||
|
||||
/**
|
||||
* Procedure for a simple dd-like copy
|
||||
*/
|
||||
|
||||
fun JobProcedureBuilder.buildInOutStreamCopyProcedure(context: Context): IJobProcedure {
|
||||
val inputName = autoName(inputType!!, input!!, context)
|
||||
val outputName = autoName(outputType!!, output!!, context)
|
||||
val procedureName = StringResBuilder(R.string.write_x_to_y, inputName, outputName)
|
||||
val procedure = JobProcedure(procedureName)
|
||||
|
||||
return procedure.apply {
|
||||
// Open streams
|
||||
add(
|
||||
StreamOpenAction(
|
||||
name = StringResBuilder(R.string.empty_string, "Open InputStream for '$inputName'"),
|
||||
progressWeight = 0.0,
|
||||
checkpoint = null,
|
||||
destKey = SharedDataType.INPUT_STREAM,
|
||||
streamDirection = StreamDirection.INPUT,
|
||||
targetDescriptor = input!!,
|
||||
targetType = inputType!!,
|
||||
showInGUI = false,
|
||||
runAlways = true
|
||||
)
|
||||
)
|
||||
|
||||
add(
|
||||
StreamOpenAction(
|
||||
name = StringResBuilder(R.string.empty_string, "Open OutputStream for '$outputName'"),
|
||||
progressWeight = 0.0,
|
||||
checkpoint = null,
|
||||
destKey = SharedDataType.OUTPUT_STREAM,
|
||||
streamDirection = StreamDirection.OUTPUT,
|
||||
targetDescriptor = output!!,
|
||||
targetType = outputType!!,
|
||||
showInGUI = false,
|
||||
runAlways = true
|
||||
)
|
||||
)
|
||||
|
||||
// Pipe data from input to output
|
||||
add(
|
||||
Input2OutputStreamCopyAction(
|
||||
name = StringResBuilder(R.string.i2o_action_name, inputName, outputName),
|
||||
progressWeight = 100.0,
|
||||
checkpoint = null /* TODO: implement */,
|
||||
showInGUI = true,
|
||||
runAlways = false
|
||||
)
|
||||
)
|
||||
|
||||
// Close streams
|
||||
add(
|
||||
StreamCloseAction(
|
||||
name = StringResBuilder(R.string.empty_string, "Close InputStream for '$inputName'"),
|
||||
progressWeight = 0.0,
|
||||
checkpoint = null,
|
||||
destKey = SharedDataType.INPUT_STREAM,
|
||||
showInGUI = false,
|
||||
runAlways = true
|
||||
)
|
||||
)
|
||||
add(
|
||||
StreamCloseAction(
|
||||
name = StringResBuilder(R.string.empty_string, "Close OutputStream for '$outputName'"),
|
||||
progressWeight = 0.0,
|
||||
checkpoint = null,
|
||||
destKey = SharedDataType.OUTPUT_STREAM,
|
||||
showInGUI = false,
|
||||
runAlways = true
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package eu.depau.etchdroid.job.procedures
|
||||
|
||||
import android.content.Context
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.net.Uri
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.job.enums.TargetType
|
||||
import eu.depau.etchdroid.job.enums.TargetType.*
|
||||
import eu.depau.etchdroid.utils.ktexts.name
|
||||
import eu.depau.kotlet.android.extensions.uri.getFilePath
|
||||
|
||||
/**
|
||||
* Utilities to give human-readable names to random stuff
|
||||
*/
|
||||
|
||||
fun autoName(targetType: TargetType, targetDescriptor: Any, context: Context): String {
|
||||
return when (targetType) {
|
||||
ANDROID_URI -> (targetDescriptor as Uri).getFileName(context)
|
||||
AUMS_BLOCKDEV -> (targetDescriptor as UsbDevice).name
|
||||
FS_FILE -> getPathFileName(targetDescriptor as String, context)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun getPathFileName(path: String, context: Context): String {
|
||||
val cut = path.lastIndexOf('/')
|
||||
if (cut != -1) {
|
||||
return path.substring(cut + 1)
|
||||
}
|
||||
|
||||
return context.getString(R.string.file_unknown)
|
||||
}
|
||||
|
||||
|
||||
fun Uri.getFileName(context: Context): String {
|
||||
return try {
|
||||
this.getFilePath(context)!!
|
||||
} catch (e: Exception) {
|
||||
getPathFileName(this.encodedPath ?: this.path ?: this.toString(), context)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package eu.depau.etchdroid.notification
|
||||
|
||||
import androidx.core.app.NotificationCompat
|
||||
|
||||
interface IServiceNotificationBuilder {
|
||||
fun getBuilder(): NotificationCompat.Builder
|
||||
fun getBuilderDone(): NotificationCompat.Builder
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package eu.depau.etchdroid.notification.impl
|
||||
|
||||
import android.app.NotificationManager
|
||||
import android.content.Context
|
||||
import android.content.Context.NOTIFICATION_SERVICE
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.notification.IServiceNotificationBuilder
|
||||
import eu.depau.kotlet.android.extensions.notification.NotificationImportanceCompat
|
||||
import eu.depau.kotlet.android.extensions.notification.registerNotificationChannel
|
||||
|
||||
class JobServiceNotificationBuilder(val context: Context) : IServiceNotificationBuilder {
|
||||
private val notificationManager = context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
private val notificationChannelProgress = notificationManager
|
||||
.registerNotificationChannel(
|
||||
"eu.depau.etchdroid.notifications.JOB_PROGRESS",
|
||||
context.getString(R.string.notif_channel_job_progress_name),
|
||||
context.getString(R.string.notif_channel_job_progress_desc),
|
||||
NotificationImportanceCompat.IMPORTANCE_LOW
|
||||
)
|
||||
|
||||
private val notificationChannelResult = notificationManager
|
||||
.registerNotificationChannel(
|
||||
"eu.depau.etchdroid.notifications.JOB_RESULT",
|
||||
context.getString(R.string.notif_channel_job_result_name),
|
||||
context.getString(R.string.notif_channel_job_result_desc),
|
||||
NotificationImportanceCompat.IMPORTANCE_DEFAULT
|
||||
)
|
||||
|
||||
private fun getNotificationBuilder(channel: String): NotificationCompat.Builder {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
NotificationCompat.Builder(context, channel)
|
||||
} else {
|
||||
NotificationCompat.Builder(context)
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
// Delete old notification channels
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
notificationManager.apply {
|
||||
deleteNotificationChannel("eu.depau.etchdroid.notifications.USB_WRITE_PROGRESS")
|
||||
deleteNotificationChannel("eu.depau.etchdroid.notifications.USB_WRITE_RESULT")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBuilder(): NotificationCompat.Builder {
|
||||
val builder = getNotificationBuilder(notificationChannelProgress)
|
||||
return builder.apply {
|
||||
setSmallIcon(R.drawable.ic_usb_white_24dp)
|
||||
setContentTitle(context.getString(R.string.notif_starting_job))
|
||||
setProgress(100, 0, true)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBuilderDone(): NotificationCompat.Builder {
|
||||
val builder = getNotificationBuilder(notificationChannelResult)
|
||||
return builder.apply {
|
||||
setSmallIcon(R.drawable.ic_usb_white_24dp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,10 @@ import android.net.Uri
|
||||
import com.google.common.util.concurrent.SimpleTimeLimiter
|
||||
import com.google.common.util.concurrent.TimeLimiter
|
||||
import com.google.common.util.concurrent.UncheckedTimeoutException
|
||||
import eu.depau.etchdroid.kotlin_exts.getBinary
|
||||
import eu.depau.etchdroid.kotlin_exts.getFileName
|
||||
import eu.depau.etchdroid.kotlin_exts.getFileSize
|
||||
import eu.depau.etchdroid.kotlin_exts.name
|
||||
import eu.depau.etchdroid.utils.ktexts.getBinary
|
||||
import eu.depau.etchdroid.utils.ktexts.getFileName
|
||||
import eu.depau.etchdroid.utils.ktexts.getFileSize
|
||||
import eu.depau.etchdroid.utils.ktexts.name
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStream
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@@ -2,9 +2,9 @@ package eu.depau.etchdroid.services
|
||||
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.net.Uri
|
||||
import eu.depau.etchdroid.kotlin_exts.getFileName
|
||||
import eu.depau.etchdroid.kotlin_exts.getFileSize
|
||||
import eu.depau.etchdroid.kotlin_exts.name
|
||||
import eu.depau.etchdroid.utils.ktexts.getFileName
|
||||
import eu.depau.etchdroid.utils.ktexts.getFileSize
|
||||
import eu.depau.etchdroid.utils.ktexts.name
|
||||
import java.io.InputStream
|
||||
|
||||
class UsbApiImgWriteService : UsbApiWriteService("UsbApiImgWriteService") {
|
||||
|
||||
@@ -4,10 +4,10 @@ import android.content.Intent
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import com.github.mjdev.libaums.UsbMassStorageDevice
|
||||
import eu.depau.etchdroid.exceptions.UsbWriteException
|
||||
import eu.depau.etchdroid.kotlin_exts.getFileName
|
||||
import eu.depau.etchdroid.kotlin_exts.name
|
||||
import eu.depau.etchdroid.libaums_wrapper.EtchDroidUsbMassStorageDevice
|
||||
import eu.depau.etchdroid.utils.exception.UsbWriteException
|
||||
import eu.depau.etchdroid.utils.ktexts.getFileName
|
||||
import eu.depau.etchdroid.utils.ktexts.name
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.InputStream
|
||||
import java.nio.ByteBuffer
|
||||
@@ -24,8 +24,8 @@ abstract class UsbApiWriteService(name: String) : UsbWriteService(name) {
|
||||
abstract fun getSendProgress(usbDevice: UsbDevice, uri: Uri): (Long) -> Unit
|
||||
abstract fun getInputStream(uri: Uri): InputStream
|
||||
|
||||
private fun getUsbMSDevice(usbDevice: UsbDevice): UsbMassStorageDevice? {
|
||||
val msDevs = UsbMassStorageDevice.getMassStorageDevices(this)
|
||||
private fun getUsbMSDevice(usbDevice: UsbDevice): EtchDroidUsbMassStorageDevice? {
|
||||
val msDevs = EtchDroidUsbMassStorageDevice.getMassStorageDevices(this)
|
||||
|
||||
for (dev in msDevs) {
|
||||
if (dev.usbDevice == usbDevice)
|
||||
@@ -35,8 +35,8 @@ abstract class UsbApiWriteService(name: String) : UsbWriteService(name) {
|
||||
return null
|
||||
}
|
||||
|
||||
fun writeInputStream(inputStream: InputStream, msDev: UsbMassStorageDevice, sendProgress: (Long) -> Unit): Long {
|
||||
val blockDev = msDev.blockDevice
|
||||
fun writeInputStream(inputStream: InputStream, msDev: EtchDroidUsbMassStorageDevice, sendProgress: (Long) -> Unit): Long {
|
||||
val blockDev = msDev.blockDevices[0]!!
|
||||
val bsFactor = DD_BLOCKSIZE / blockDev.blockSize
|
||||
val buffIS = BufferedInputStream(inputStream)
|
||||
val byteBuffer = ByteBuffer.allocate(blockDev.blockSize * bsFactor)
|
||||
@@ -50,7 +50,7 @@ abstract class UsbApiWriteService(name: String) : UsbWriteService(name) {
|
||||
|
||||
while (true) {
|
||||
wakeLock(true)
|
||||
lastReadBytes = buffIS.read(byteBuffer.array()!!, remaining, byteBuffer.array().size - remaining)
|
||||
lastReadBytes = buffIS.read(byteBuffer.array(), remaining, byteBuffer.array().size - remaining)
|
||||
if (lastReadBytes < 0 && readBytes > 0) {
|
||||
// EOF, pad with some extra bits until next block
|
||||
if (readBytes % blockDev.blockSize > 0)
|
||||
@@ -109,8 +109,7 @@ abstract class UsbApiWriteService(name: String) : UsbWriteService(name) {
|
||||
resultNotification(usbDevice.name, uri.getFileName(this)!!, null, writtenBytes, startTime)
|
||||
} catch (e: Exception) {
|
||||
resultNotification(usbDevice.name, uri.getFileName(this)!!, e, writtenBytes, startTime)
|
||||
Log.e(TAG, "Could't write image to ${usbDevice.name}")
|
||||
throw e
|
||||
Log.e(TAG, "Could't write image to ${usbDevice.name}", e)
|
||||
} finally {
|
||||
wakeLock(false)
|
||||
msDev.close()
|
||||
|
||||
@@ -7,9 +7,9 @@ import android.os.Build
|
||||
import android.os.PowerManager
|
||||
import androidx.core.app.NotificationCompat
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.activities.ErrorActivity
|
||||
import eu.depau.etchdroid.kotlin_exts.toHRSize
|
||||
import eu.depau.etchdroid.kotlin_exts.toHRTime
|
||||
import eu.depau.etchdroid.ui.activities.ErrorActivity
|
||||
import eu.depau.etchdroid.utils.ktexts.toHRSize
|
||||
import eu.depau.etchdroid.utils.ktexts.toHRTime
|
||||
import java.util.*
|
||||
import kotlin.math.max
|
||||
|
||||
@@ -101,7 +101,7 @@ abstract class UsbWriteService(name: String) : IntentService(name) {
|
||||
intent.putExtra("error", exception.message)
|
||||
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
b.setContentTitle(getString(R.string.write_failed_tap_for_info))
|
||||
b.setContentTitle(getString(R.string.failed_tap_for_info))
|
||||
.setContentText(getString(R.string.error_notif_content_text, usbDevice))
|
||||
.setContentIntent(pendingIntent)
|
||||
.setAutoCancel(true)
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package eu.depau.etchdroid.services.job
|
||||
|
||||
import android.app.IntentService
|
||||
import android.content.Intent
|
||||
import eu.depau.etchdroid.services.job.dto.JobServiceIntentDTO
|
||||
|
||||
/**
|
||||
* Job service. Creates an instance of and runs another class to prevent Android
|
||||
* from reusing data in another thread
|
||||
*/
|
||||
open class JobService(name: String) : IntentService(name) {
|
||||
override fun onHandleIntent(intent: Intent?) {
|
||||
intent ?: return
|
||||
|
||||
if (intent.action != ACTION)
|
||||
return
|
||||
|
||||
val jobDTO = intent.getParcelableExtra<JobServiceIntentDTO>(JobServiceIntentDTO.EXTRA)
|
||||
JobServiceIntentHandler(this, jobDTO).handle()
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val ACTION = "eu.depau.etchdroid.services.JOB_SERVICE_START"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package eu.depau.etchdroid.services.job
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.Service
|
||||
import android.content.IntentFilter
|
||||
import android.util.Log
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import eu.depau.etchdroid.AppBuildConfig
|
||||
import eu.depau.etchdroid.broadcasts.JobProgressUpdateBroadcast
|
||||
import eu.depau.etchdroid.db.EtchDroidDatabase
|
||||
import eu.depau.etchdroid.db.entity.Job
|
||||
import eu.depau.etchdroid.notification.impl.JobServiceNotificationBuilder
|
||||
import eu.depau.etchdroid.services.job.dto.JobServiceIntentDTO
|
||||
import eu.depau.etchdroid.services.job.exception.JobActionFailedException
|
||||
import eu.depau.etchdroid.utils.job.IJobAction
|
||||
import eu.depau.etchdroid.utils.job.IJobProcedure
|
||||
import eu.depau.etchdroid.utils.job.enums.SharedDataType
|
||||
import eu.depau.etchdroid.utils.job.impl.AbstractJobProgressSender
|
||||
import eu.depau.etchdroid.utils.worker.IAsyncWorker
|
||||
|
||||
class JobServiceIntentHandler(
|
||||
val context: JobService,
|
||||
private val jobDTO: JobServiceIntentDTO
|
||||
) : AbstractJobProgressSender() {
|
||||
private val jobId = jobDTO.jobId
|
||||
private val svcNotificationBuilder = JobServiceNotificationBuilder(context)
|
||||
private lateinit var job: Job
|
||||
private val broadcastForwarder = ProgressBroadcastForwarder(jobId, context)
|
||||
private val sharedWorkerData = mutableMapOf<SharedDataType, Any?>()
|
||||
|
||||
// Only build notification if we're not in test mode
|
||||
private val notification = if (!AppBuildConfig.TEST_BUILD)
|
||||
svcNotificationBuilder.getBuilder().build()
|
||||
else
|
||||
Notification()
|
||||
|
||||
init {
|
||||
assert(jobId >= 0) { "Invalid jobId (jobId < 0)" }
|
||||
}
|
||||
|
||||
fun handle() = context.runForeground(jobId.toInt(), notification, true) {
|
||||
val db = EtchDroidDatabase.getDatabase(context)
|
||||
|
||||
job = db.jobRepository().getById(jobId).also {
|
||||
assert(!it.completed) { "Job $jobId already completed" }
|
||||
}
|
||||
|
||||
// Add service context to shared worker data
|
||||
sharedWorkerData[SharedDataType.CONTEXT] = context
|
||||
|
||||
try {
|
||||
withNotificationBroadcastReceiver {
|
||||
job.attachAndRun { procedure ->
|
||||
procedure.forEachAction(sharedWorkerData) { _, _, worker ->
|
||||
while (worker.runStep()) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
job.completed = true
|
||||
db.jobRepository().update(job)
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
Log.e(TAG, "JobProcedure failed", e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private inline fun <T> Job.attachAndRun(body: (procedure: IJobProcedure) -> T): T {
|
||||
attachJobProgressListener(broadcastForwarder)
|
||||
try {
|
||||
notifyJobProcedureStart(this.checkpointActionIndex ?: 0)
|
||||
val result = body(this.jobProcedure)
|
||||
notifyJobProcedureDone()
|
||||
return result
|
||||
} catch (t: Throwable) {
|
||||
notifyJobProcedureError(t)
|
||||
throw t
|
||||
} finally {
|
||||
detachJobProgressListener(broadcastForwarder)
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun IJobProcedure.forEachAction(
|
||||
sharedWorkerData: MutableMap<SharedDataType, Any?>,
|
||||
body: (index: Int, action: IJobAction, worker: IAsyncWorker) -> Unit
|
||||
) {
|
||||
val startIndex = job.checkpointActionIndex
|
||||
|
||||
var errorIndex: Int? = null
|
||||
var errorAction: IJobAction? = null
|
||||
var error: Throwable? = null
|
||||
|
||||
this
|
||||
.filterIndexed { index, action ->
|
||||
index >= startIndex || action.runAlways
|
||||
}
|
||||
.forEachIndexed { index, action ->
|
||||
// Only run "runAlways" actions if there's an error
|
||||
if (error != null && !action.runAlways) {
|
||||
return@forEachIndexed
|
||||
}
|
||||
|
||||
val worker = action.getWorker(sharedWorkerData)
|
||||
worker.attachWorkerProgressListener(broadcastForwarder)
|
||||
|
||||
try {
|
||||
broadcastForwarder.onActionStart(index)
|
||||
body(index, action, worker)
|
||||
broadcastForwarder.onActionDone()
|
||||
} catch (e: Throwable) {
|
||||
errorIndex = index
|
||||
errorAction = action
|
||||
error = e
|
||||
} finally {
|
||||
worker.detachWorkerProgressListener(broadcastForwarder)
|
||||
}
|
||||
}
|
||||
|
||||
if (error != null) {
|
||||
throw JobActionFailedException(errorAction!!, errorIndex!!, error!!)
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> Service.runForeground(id: Int, notification: Notification, removeNotification: Boolean, body: () -> T): T {
|
||||
this.startForeground(id, notification)
|
||||
try {
|
||||
return body()
|
||||
} finally {
|
||||
stopForeground(removeNotification)
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun withNotificationBroadcastReceiver(body: () -> Unit) {
|
||||
if (AppBuildConfig.TEST_BUILD) {
|
||||
return body()
|
||||
}
|
||||
|
||||
val intentFilter = IntentFilter(JobProgressUpdateBroadcast.ACTION)
|
||||
val broadcastReceiver = JobServiceNotificationBroadcastReceiver(jobId.toInt(), job, svcNotificationBuilder)
|
||||
|
||||
if (AppBuildConfig.USE_LOCAL_BROADCASTS) {
|
||||
LocalBroadcastManager
|
||||
.getInstance(context)
|
||||
.registerReceiver(broadcastReceiver, intentFilter)
|
||||
} else {
|
||||
context.registerReceiver(broadcastReceiver, intentFilter)
|
||||
}
|
||||
|
||||
try {
|
||||
body()
|
||||
} finally {
|
||||
if (AppBuildConfig.USE_LOCAL_BROADCASTS) {
|
||||
LocalBroadcastManager.getInstance(context).unregisterReceiver(broadcastReceiver)
|
||||
} else {
|
||||
context.unregisterReceiver(broadcastReceiver)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TAG = ".s.j.JobInHandl"
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package eu.depau.etchdroid.services.job
|
||||
|
||||
import android.app.IntentService
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.broadcasts.JobProgressUpdateBroadcast
|
||||
import eu.depau.etchdroid.broadcasts.dto.JobProgressUpdateBroadcastDTO
|
||||
import eu.depau.etchdroid.db.entity.Job
|
||||
import eu.depau.etchdroid.notification.IServiceNotificationBuilder
|
||||
import eu.depau.etchdroid.ui.activities.ErrorActivity
|
||||
import java.io.Serializable
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
|
||||
class JobServiceNotificationBroadcastReceiver(
|
||||
private val foregroundId: Int,
|
||||
private val job: Job,
|
||||
private val notificationBuilder: IServiceNotificationBuilder
|
||||
) : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.action != JobProgressUpdateBroadcast.ACTION)
|
||||
return
|
||||
|
||||
val dto = intent.getSerializableExtra(JobProgressUpdateBroadcastDTO.EXTRA)
|
||||
as JobProgressUpdateBroadcastDTO
|
||||
|
||||
val notification = if (dto.completed) {
|
||||
notificationBuilder.getBuilderDone().apply {
|
||||
if (dto.error == null) {
|
||||
val activityIntent = Intent(context, ErrorActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
putExtra("error", dto.error as Serializable)
|
||||
}
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
setSubText(job.jobProcedure.name.build(context))
|
||||
setContentTitle(context.getString(R.string.failed_tap_for_info))
|
||||
setContentIntent(pendingIntent)
|
||||
setAutoCancel(true)
|
||||
} else {
|
||||
// TODO: add success activity intent
|
||||
setContentTitle(context.getString(R.string.done))
|
||||
setContentText(job.jobProcedure.name.build(context))
|
||||
}
|
||||
}.build()
|
||||
} else {
|
||||
notificationBuilder.getBuilder().apply {
|
||||
val subTextContent = mutableListOf<String>()
|
||||
setContentTitle(job.jobProcedure.name.build(context))
|
||||
if (dto.showProgressBar) {
|
||||
if (dto.indefinite || dto.percentage == null) {
|
||||
setProgress(100, 0, true)
|
||||
} else {
|
||||
setProgress(100, dto.percentage.roundToInt(), false)
|
||||
subTextContent.add("${dto.percentage.roundToInt()}%")
|
||||
}
|
||||
}
|
||||
if (dto.step != null) {
|
||||
subTextContent.add(context.getString(R.string.step_x_of_y, dto.step + 1, job.jobProcedure.size + 1))
|
||||
}
|
||||
if (dto.currentMessage != null) {
|
||||
setContentText(dto.currentMessage)
|
||||
}
|
||||
setSubText(subTextContent.joinToString(" • "))
|
||||
|
||||
// TODO: add rate
|
||||
}.build()
|
||||
}
|
||||
|
||||
|
||||
val notificationManager = context
|
||||
.getSystemService(IntentService.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
notificationManager.notify(foregroundId, notification)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package eu.depau.etchdroid.services.job
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import eu.depau.etchdroid.AppBuildConfig
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.broadcasts.JobProgressUpdateBroadcast
|
||||
import eu.depau.etchdroid.broadcasts.dto.JobProgressUpdateBroadcastDTO
|
||||
import eu.depau.etchdroid.db.EtchDroidDatabase
|
||||
import eu.depau.etchdroid.utils.job.IJobProgressListener
|
||||
import eu.depau.etchdroid.utils.worker.IWorkerProgressListener
|
||||
import eu.depau.etchdroid.utils.worker.dto.ProgressUpdateDTO
|
||||
|
||||
/**
|
||||
* This class is subscribed to IAsyncWorker+IWorkerProgressSender events by the worker service.
|
||||
* It is aware of the current JobProcedure.
|
||||
*
|
||||
* It receives the job progress and forwards it to the rest of the app.
|
||||
*/
|
||||
class ProgressBroadcastForwarder(
|
||||
private val jobId: Long, private val context: Context
|
||||
) : IJobProgressListener, IWorkerProgressListener {
|
||||
|
||||
private val db = EtchDroidDatabase.getDatabase(context)
|
||||
private val job = db.jobRepository().getById(jobId)
|
||||
private val procedure = job.jobProcedure
|
||||
|
||||
private val totalProgressWeights = job.jobProcedure.map { it.progressWeight }.sum()
|
||||
|
||||
private var currentActionIdx: Int? = null
|
||||
private var prevActionsProgressWeight: Double? = null
|
||||
|
||||
|
||||
private fun assertProcedureStarted() {
|
||||
if (currentActionIdx == null || prevActionsProgressWeight == null) {
|
||||
throw IllegalStateException("Call to a .on*() method without a call to .onProcedureStart()")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun sendBroadcast(dto: JobProgressUpdateBroadcastDTO) {
|
||||
val intent = JobProgressUpdateBroadcast.getIntent(dto)
|
||||
|
||||
when {
|
||||
AppBuildConfig.USE_LOCAL_BROADCASTS ->
|
||||
LocalBroadcastManager
|
||||
.getInstance(context.applicationContext)
|
||||
.sendBroadcast(intent)
|
||||
else ->
|
||||
context
|
||||
.sendBroadcast(intent.apply { `package` = context.packageName })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by the worker service to notify the forwarder that the currently referenced procedure
|
||||
* is starting.
|
||||
*/
|
||||
override fun onProcedureStart(startActionIndex: Int) {
|
||||
currentActionIdx = startActionIndex
|
||||
prevActionsProgressWeight = job.jobProcedure
|
||||
.subList(0, currentActionIdx!!)
|
||||
.map { it.progressWeight }
|
||||
.sum()
|
||||
|
||||
val isCheckpoint = currentActionIdx!! > 0
|
||||
val messageResId = if (isCheckpoint) R.string.resuming_job else R.string.starting_job
|
||||
|
||||
Log.d(TAG, "-> onProcedureStart job $jobId, startAt $startActionIndex, " +
|
||||
"currentAction $currentActionIdx, prevProgress: $prevActionsProgressWeight")
|
||||
//context.getString(R.string.starting_job)
|
||||
val dto = JobProgressUpdateBroadcastDTO(
|
||||
jobId = jobId,
|
||||
indefinite = true,
|
||||
step = currentActionIdx!!,
|
||||
currentMessage = context.getString(messageResId)
|
||||
)
|
||||
sendBroadcast(dto)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by the worker service to notify the forwarder that the currently referenced procedure
|
||||
* has finished successfully.
|
||||
*/
|
||||
override fun onProcedureDone() {
|
||||
assertProcedureStarted()
|
||||
|
||||
Log.d(TAG, "-> onProcedureDone job $jobId action $currentActionIdx")
|
||||
|
||||
// Ensure no more updates are sent after the procedure is done
|
||||
currentActionIdx = null
|
||||
|
||||
val dto = JobProgressUpdateBroadcastDTO(
|
||||
jobId = jobId,
|
||||
showProgressBar = false,
|
||||
percentage = 100.0,
|
||||
completed = true,
|
||||
currentMessage = context.getString(R.string.done)
|
||||
)
|
||||
sendBroadcast(dto)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by the worker service to notify the forwarder that the currently referenced procedure
|
||||
* has failed with the provided exception.
|
||||
*/
|
||||
override fun onProcedureError(error: Throwable) {
|
||||
assertProcedureStarted()
|
||||
|
||||
Log.d(TAG, "-> onProcedureError job $jobId action $currentActionIdx")
|
||||
|
||||
// Ensure no more updates are sent after the procedure failed
|
||||
currentActionIdx = null
|
||||
|
||||
val dto = JobProgressUpdateBroadcastDTO(
|
||||
jobId = jobId,
|
||||
showProgressBar = false,
|
||||
completed = true,
|
||||
currentMessage = context.getString(R.string.job_failed),
|
||||
error = error
|
||||
)
|
||||
sendBroadcast(dto)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by the current worker to update the progress listener on its status relative to
|
||||
* the current JobAction that describes its task.
|
||||
*
|
||||
* @param dto a ProgressUpdateDTO describing the current status of the worker
|
||||
*/
|
||||
override fun onWorkerProgress(dto: ProgressUpdateDTO) {
|
||||
assertProcedureStarted()
|
||||
|
||||
val currentActionWeight = procedure[currentActionIdx!!].progressWeight
|
||||
val indefinite = dto.progress == null
|
||||
val progress: Double? =
|
||||
if (!indefinite)
|
||||
prevActionsProgressWeight!! + dto.progress!!.toDouble() * currentActionWeight
|
||||
else
|
||||
null
|
||||
|
||||
Log.d(TAG, " -> onWorkerProgress job $jobId action $currentActionIdx dto $dto")
|
||||
|
||||
val broadcastDTO = JobProgressUpdateBroadcastDTO(
|
||||
jobId = jobId,
|
||||
showProgressBar = true,
|
||||
indefinite = indefinite,
|
||||
percentage = if (!indefinite) progress!!.toDouble() / totalProgressWeights * 100 else null,
|
||||
step = currentActionIdx!!
|
||||
)
|
||||
sendBroadcast(broadcastDTO)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by the worker service to update the forwarder on the current action index.
|
||||
*
|
||||
* @param actionIndex the current action index within the JobProcedure
|
||||
*/
|
||||
override fun onActionStart(actionIndex: Int) {
|
||||
assertProcedureStarted()
|
||||
currentActionIdx = actionIndex
|
||||
|
||||
Log.d(TAG, " -> onActionStart job $jobId action $currentActionIdx")
|
||||
|
||||
val dto = JobProgressUpdateBroadcastDTO(
|
||||
jobId = jobId,
|
||||
showProgressBar = true,
|
||||
indefinite = false,
|
||||
percentage = prevActionsProgressWeight!!.toDouble() / totalProgressWeights * 100,
|
||||
step = currentActionIdx!!
|
||||
)
|
||||
sendBroadcast(dto)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by the worker service to notify the forwarder that the current action has finished
|
||||
* running. Whether it has finished successfully or not is unspecified.
|
||||
*
|
||||
* If it failed, a call to onProcedureError will follow with an Exception reference.
|
||||
*/
|
||||
override fun onActionDone() {
|
||||
assertProcedureStarted()
|
||||
prevActionsProgressWeight = prevActionsProgressWeight!!.plus(job.jobProcedure[currentActionIdx!!].progressWeight)
|
||||
|
||||
Log.d(TAG, " -> onActionDone job $jobId action $currentActionIdx")
|
||||
|
||||
// We don't really need to broadcast this event
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TAG = ".s.j.PrgBcastFw"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package eu.depau.etchdroid.services.job.dto
|
||||
|
||||
import android.os.Parcel
|
||||
import eu.depau.kotlet.android.parcelable.KotletParcelable
|
||||
import eu.depau.kotlet.android.parcelable.parcelableCreator
|
||||
import java.io.Serializable
|
||||
|
||||
data class JobServiceIntentDTO(
|
||||
val jobId: Long
|
||||
) : KotletParcelable, Serializable {
|
||||
constructor(parcel: Parcel) : this(
|
||||
parcel.readLong()
|
||||
)
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.apply {
|
||||
writeLong(jobId)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val CREATOR = parcelableCreator(::JobServiceIntentDTO)
|
||||
|
||||
@JvmField
|
||||
val EXTRA = "eu.depau.etchdroid.broadcast.extras.JOB_SERVICE_DTO"
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package eu.depau.etchdroid.services.job.exception
|
||||
|
||||
import eu.depau.etchdroid.utils.job.IJobAction
|
||||
|
||||
class JobActionFailedException(action: IJobAction, index: Int, cause: Throwable) :
|
||||
Exception(
|
||||
"JobAction $action (step $index) failed",
|
||||
cause
|
||||
)
|
||||
+23
-10
@@ -1,7 +1,10 @@
|
||||
package eu.depau.etchdroid.activities
|
||||
package eu.depau.etchdroid.ui.activities
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.content.res.Configuration
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
@@ -10,16 +13,14 @@ import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.kotlin_exts.toast
|
||||
import eu.depau.etchdroid.utils.DoNotShowAgainDialogFragment
|
||||
import eu.depau.etchdroid.utils.NightModeHelper
|
||||
import eu.depau.etchdroid.ui.misc.DoNotShowAgainDialogFragment
|
||||
import eu.depau.etchdroid.ui.misc.NightModeHelper
|
||||
import eu.depau.etchdroid.utils.ktexts.toast
|
||||
import me.jfenn.attribouter.Attribouter
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
|
||||
|
||||
abstract class ActivityBase : AppCompatActivity() {
|
||||
protected lateinit var nightModeHelper: NightModeHelper
|
||||
protected var nightModeHelper: NightModeHelper? = null
|
||||
val DISMISSED_DIALOGS_PREFS = "dismissed_dialogs"
|
||||
val READ_REQUEST_CODE = 42
|
||||
val READ_EXTERNAL_STORAGE_PERMISSION = 29
|
||||
@@ -38,8 +39,11 @@ abstract class ActivityBase : AppCompatActivity() {
|
||||
editor.apply()
|
||||
}
|
||||
|
||||
internal val isNightMode: Boolean
|
||||
get() = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_YES != 0
|
||||
|
||||
fun showAndroidPieAlertDialog(callback: () -> Unit) {
|
||||
val dialogFragment = DoNotShowAgainDialogFragment(nightModeHelper.nightMode)
|
||||
val dialogFragment = DoNotShowAgainDialogFragment(isNightMode)
|
||||
dialogFragment.title = getString(R.string.android_pie_bug)
|
||||
dialogFragment.message = getString(R.string.android_pie_bug_dialog_text)
|
||||
dialogFragment.positiveButton = getString(R.string.i_understand)
|
||||
@@ -72,12 +76,21 @@ abstract class ActivityBase : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
nightModeHelper = NightModeHelper(this, R.style.AppTheme)
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
|
||||
nightModeHelper = NightModeHelper(this, R.style.AppTheme)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
menuInflater.inflate(R.menu.menu_main, menu)
|
||||
|
||||
// Hide night mode menu on Android 10 as it causes weird issues
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
menu.findItem(R.id.action_nightmode).isVisible = false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -102,7 +115,7 @@ abstract class ActivityBase : AppCompatActivity() {
|
||||
return true
|
||||
}
|
||||
R.id.action_nightmode -> {
|
||||
nightModeHelper.toggle()
|
||||
nightModeHelper?.toggle()
|
||||
return true
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
+19
-10
@@ -1,24 +1,31 @@
|
||||
package eu.depau.etchdroid.activities
|
||||
package eu.depau.etchdroid.ui.activities
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.StateKeeper
|
||||
import eu.depau.etchdroid.adapters.PartitionTableRecyclerViewAdapter
|
||||
import eu.depau.etchdroid.enums.FlashMethod
|
||||
import eu.depau.etchdroid.img_types.DMGImage
|
||||
import eu.depau.etchdroid.kotlin_exts.*
|
||||
import eu.depau.etchdroid.libaums_wrapper.EtchDroidUsbMassStorageDevice.Companion.getMassStorageDevices
|
||||
import eu.depau.etchdroid.libaums_wrapper.kotlinexts.size
|
||||
import eu.depau.etchdroid.services.UsbApiDmgWriteService
|
||||
import eu.depau.etchdroid.services.UsbApiImgWriteService
|
||||
import eu.depau.etchdroid.utils.DoNotShowAgainDialogFragment
|
||||
import eu.depau.etchdroid.ui.adapters.PartitionTableRecyclerViewAdapter
|
||||
import eu.depau.etchdroid.ui.misc.DoNotShowAgainDialogFragment
|
||||
import eu.depau.etchdroid.utils.enums.FlashMethod
|
||||
import eu.depau.etchdroid.utils.imagetypes.DMGImage
|
||||
import eu.depau.etchdroid.utils.ktexts.*
|
||||
import kotlinx.android.synthetic.main.activity_confirmation.*
|
||||
import java.io.IOException
|
||||
|
||||
class ConfirmationActivity : ActivityBase() {
|
||||
companion object {
|
||||
const val TAG = ".ui.a.ConfActvt"
|
||||
}
|
||||
|
||||
var canContinue: Boolean = false
|
||||
var issuesFound: String? = null
|
||||
|
||||
@@ -36,7 +43,7 @@ class ConfirmationActivity : ActivityBase() {
|
||||
|
||||
|
||||
fun showDataLossAlertDialog() {
|
||||
val dialogFragment = DoNotShowAgainDialogFragment(nightModeHelper.nightMode)
|
||||
val dialogFragment = DoNotShowAgainDialogFragment(isNightMode)
|
||||
dialogFragment.title = getString(R.string.warning)
|
||||
dialogFragment.message = getString(R.string.dataloss_confirmation_dialog_message)
|
||||
dialogFragment.positiveButton = getString(R.string.confirm_flash_image)
|
||||
@@ -93,9 +100,10 @@ class ConfirmationActivity : ActivityBase() {
|
||||
confirm_sel_usbdev.text = StateKeeper.usbDevice?.name
|
||||
|
||||
for (trial in 0..1) {
|
||||
val aumsDev = StateKeeper.usbDevice!!.getMassStorageDevices(this)[0]
|
||||
try {
|
||||
StateKeeper.usbMassStorageDevice!!.init()
|
||||
val blockDev = StateKeeper.usbMassStorageDevice!!.blockDevice
|
||||
aumsDev.init()
|
||||
val blockDev = aumsDev.blockDevices[0]
|
||||
|
||||
if (blockDev != null) {
|
||||
val devSize = (blockDev.size.toLong() * blockDev.blockSize.toLong())
|
||||
@@ -112,6 +120,7 @@ class ConfirmationActivity : ActivityBase() {
|
||||
confirm_extra_info.text = getString(R.string.cant_read_usbdev)
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Unable to query size from the USB device:", e)
|
||||
if (trial == 0) {
|
||||
continue
|
||||
} else {
|
||||
@@ -119,7 +128,7 @@ class ConfirmationActivity : ActivityBase() {
|
||||
break
|
||||
}
|
||||
} finally {
|
||||
StateKeeper.usbMassStorageDevice!!.close()
|
||||
aumsDev.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-6
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.activities
|
||||
package eu.depau.etchdroid.ui.activities
|
||||
|
||||
import android.os.Bundle
|
||||
import eu.depau.etchdroid.R
|
||||
@@ -11,10 +11,6 @@ class ErrorActivity : ActivityBase() {
|
||||
setContentView(R.layout.activity_error)
|
||||
val msg = intent.getStringExtra("error")
|
||||
error_message.text = msg
|
||||
|
||||
if ("errno 88" in msg)
|
||||
troubleshooting_info.text = getString(R.string.troubleshoot_sock_op_on_non_sock)
|
||||
else
|
||||
troubleshooting_info.text = getString(R.string.unknown_error)
|
||||
troubleshooting_info.text = getString(R.string.unknown_error)
|
||||
}
|
||||
}
|
||||
+38
-15
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.activities
|
||||
package eu.depau.etchdroid.ui.activities
|
||||
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
@@ -8,11 +8,13 @@ import android.os.Environment
|
||||
import android.view.View
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.codekidlabs.storagechooser.StorageChooser
|
||||
import com.github.mjdev.libaums.usb.UsbCommunicationFactory
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.StateKeeper
|
||||
import eu.depau.etchdroid.enums.FlashMethod
|
||||
import eu.depau.etchdroid.utils.DoNotShowAgainDialogFragment
|
||||
import eu.depau.etchdroid.ui.misc.DoNotShowAgainDialogFragment
|
||||
import eu.depau.etchdroid.utils.enums.FlashMethod
|
||||
import kotlinx.android.synthetic.main.activity_start.*
|
||||
import me.jahnen.libaums.libusbcommunication.LibusbCommunicationCreator
|
||||
import java.io.File
|
||||
|
||||
|
||||
@@ -37,33 +39,53 @@ class StartActivity : ActivityBase() {
|
||||
setContentView(R.layout.activity_start)
|
||||
btn_image_raw.setOnClickListener(this::onButtonClicked)
|
||||
btn_image_dmg.setOnClickListener(this::onButtonClicked)
|
||||
btn_broken_usb.setOnClickListener(this::onButtonClicked)
|
||||
|
||||
if (!StateKeeper.libusbRegistered) {
|
||||
UsbCommunicationFactory.apply {
|
||||
registerCommunication(LibusbCommunicationCreator())
|
||||
underlyingUsbCommunication = UsbCommunicationFactory.UnderlyingUsbCommunication.OTHER
|
||||
}
|
||||
StateKeeper.libusbRegistered = true
|
||||
}
|
||||
}
|
||||
|
||||
fun onButtonClicked(view: View) = onButtonClicked(view, true)
|
||||
|
||||
private fun openBrokenUsbPage() {
|
||||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://etchdroid.depau.eu/broken_usb/"))
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
private fun onButtonClicked(view: View?, showDMGDialog: Boolean = true, showAndroidPieDialog: Boolean = true) {
|
||||
if (view != null)
|
||||
if (view != null) {
|
||||
if (view.id == R.id.btn_broken_usb) {
|
||||
openBrokenUsbPage()
|
||||
return
|
||||
}
|
||||
|
||||
StateKeeper.flashMethod = when (view.id) {
|
||||
R.id.btn_image_raw -> FlashMethod.FLASH_API
|
||||
R.id.btn_image_dmg -> FlashMethod.FLASH_DMG_API
|
||||
else -> null
|
||||
else -> null
|
||||
}
|
||||
|
||||
if (showAndroidPieDialog && shouldShowAndroidPieAlertDialog) {
|
||||
showAndroidPieAlertDialog { onButtonClicked(view, showDMGDialog, false) }
|
||||
return
|
||||
}
|
||||
// if (showAndroidPieDialog && shouldShowAndroidPieAlertDialog) {
|
||||
// showAndroidPieAlertDialog { onButtonClicked(view, showDMGDialog, false) }
|
||||
// return
|
||||
// }
|
||||
|
||||
if (showDMGDialog && shouldShowDMGAlertDialog && StateKeeper.flashMethod == FlashMethod.FLASH_DMG_API) {
|
||||
showDMGBetaAlertDialog { onButtonClicked(view, false, showAndroidPieDialog) }
|
||||
return
|
||||
}
|
||||
if (showDMGDialog && shouldShowDMGAlertDialog && StateKeeper.flashMethod == FlashMethod.FLASH_DMG_API) {
|
||||
showDMGBetaAlertDialog { onButtonClicked(view, false, showAndroidPieDialog) }
|
||||
return
|
||||
}
|
||||
|
||||
showFilePicker()
|
||||
showFilePicker()
|
||||
}
|
||||
}
|
||||
|
||||
fun showDMGBetaAlertDialog(callback: () -> Unit) {
|
||||
val dialogFragment = DoNotShowAgainDialogFragment(nightModeHelper.nightMode)
|
||||
val dialogFragment = DoNotShowAgainDialogFragment(isNightMode)
|
||||
dialogFragment.title = getString(R.string.here_be_dragons)
|
||||
dialogFragment.message = getString(R.string.dmg_alert_dialog_text)
|
||||
dialogFragment.positiveButton = getString(R.string.i_understand)
|
||||
@@ -130,6 +152,7 @@ class StartActivity : ActivityBase() {
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
if (requestCode == READ_REQUEST_CODE && resultCode == AppCompatActivity.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
|
||||
+9
-9
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.activities
|
||||
package eu.depau.etchdroid.ui.activities
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.BroadcastReceiver
|
||||
@@ -19,12 +19,13 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||
import com.github.mjdev.libaums.UsbMassStorageDevice
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.StateKeeper
|
||||
import eu.depau.etchdroid.adapters.UsbDrivesRecyclerViewAdapter
|
||||
import eu.depau.etchdroid.enums.FlashMethod
|
||||
import eu.depau.etchdroid.kotlin_exts.*
|
||||
import eu.depau.etchdroid.utils.ClickListener
|
||||
import eu.depau.etchdroid.utils.EmptyRecyclerView
|
||||
import eu.depau.etchdroid.utils.RecyclerViewTouchListener
|
||||
import eu.depau.etchdroid.libaums_wrapper.EtchDroidUsbMassStorageDevice.Companion.getMassStorageDevices
|
||||
import eu.depau.etchdroid.ui.adapters.UsbDrivesRecyclerViewAdapter
|
||||
import eu.depau.etchdroid.utils.enums.FlashMethod
|
||||
import eu.depau.etchdroid.utils.ktexts.*
|
||||
import eu.depau.etchdroid.ui.misc.ClickListener
|
||||
import eu.depau.etchdroid.ui.misc.EmptyRecyclerView
|
||||
import eu.depau.etchdroid.ui.misc.RecyclerViewTouchListener
|
||||
import kotlinx.android.synthetic.main.activity_usb_drive_picker.*
|
||||
import java.io.File
|
||||
|
||||
@@ -120,7 +121,7 @@ class UsbDrivePickerActivity : ActivityBase(), SwipeRefreshLayout.OnRefreshListe
|
||||
}
|
||||
|
||||
viewManager = LinearLayoutManager(this)
|
||||
recyclerView = usbdevs_recycler_view
|
||||
recyclerView = usbdevs_recycler_view as EmptyRecyclerView
|
||||
recyclerView.emptyView = usbdevs_recycler_empty_view
|
||||
|
||||
recyclerView.addOnItemTouchListener(RecyclerViewTouchListener(this, recyclerView, object : ClickListener {
|
||||
@@ -182,7 +183,6 @@ class UsbDrivePickerActivity : ActivityBase(), SwipeRefreshLayout.OnRefreshListe
|
||||
}
|
||||
|
||||
StateKeeper.usbDevice = device
|
||||
StateKeeper.usbMassStorageDevice = UsbMassStorageDevice.getMassStorageDevices(context).find { it.usbDevice == device }
|
||||
|
||||
nextStep()
|
||||
}
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
package eu.depau.etchdroid.adapters
|
||||
package eu.depau.etchdroid.ui.adapters
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import android.widget.LinearLayout
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.kotlin_exts.toHRSize
|
||||
import eu.depau.etchdroid.utils.ktexts.toHRSize
|
||||
import eu.depau.etchdroid.utils.Partition
|
||||
import kotlinx.android.synthetic.main.part_data_keyvalue.view.*
|
||||
import kotlinx.android.synthetic.main.partition_row.view.*
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.adapters
|
||||
package eu.depau.etchdroid.ui.adapters
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Build
|
||||
@@ -8,7 +8,7 @@ import android.widget.RelativeLayout
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.github.mjdev.libaums.UsbMassStorageDevice
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.kotlin_exts.vidpid
|
||||
import eu.depau.etchdroid.utils.ktexts.vidpid
|
||||
import kotlinx.android.synthetic.main.usb_device_row.view.*
|
||||
|
||||
class UsbDrivesRecyclerViewAdapter(private val dataset: Array<UsbMassStorageDevice>) : RecyclerView.Adapter<UsbDrivesRecyclerViewAdapter.ViewHolder>() {
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.utils
|
||||
package eu.depau.etchdroid.ui.misc
|
||||
|
||||
import android.view.View
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.utils
|
||||
package eu.depau.etchdroid.ui.misc
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Dialog
|
||||
@@ -21,7 +21,7 @@ class DoNotShowAgainDialogFragment(nightMode: Boolean) : DialogFragment() {
|
||||
constructor() : this(false)
|
||||
|
||||
init {
|
||||
setStyle(DialogFragment.STYLE_NORMAL, dialogTheme)
|
||||
setStyle(STYLE_NORMAL, dialogTheme)
|
||||
}
|
||||
|
||||
interface DialogListener {
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.utils
|
||||
package eu.depau.etchdroid.ui.misc
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.utils
|
||||
package eu.depau.etchdroid.ui.misc
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.content.res.Configuration
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.utils
|
||||
package eu.depau.etchdroid.ui.misc
|
||||
|
||||
import android.content.Context
|
||||
import android.view.GestureDetector
|
||||
@@ -0,0 +1,19 @@
|
||||
package eu.depau.etchdroid.ui.misc
|
||||
|
||||
import java.io.File
|
||||
import java.io.FileDescriptor
|
||||
import java.io.RandomAccessFile
|
||||
|
||||
class SparseFile : RandomAccessFile {
|
||||
constructor(file: File, mode: String) : super(file, mode)
|
||||
constructor(path: String, mode: String) : super(path, mode)
|
||||
|
||||
fun lseek(fd: FileDescriptor, offset: Long, whence: Int): Long {
|
||||
val libcore = Class.forName("libcore.io.Libcore")
|
||||
val os = libcore.getField("os").get(null)
|
||||
val lseek = os.javaClass.getMethod(
|
||||
"lseek", FileDescriptor::class.java, Long::class.java, Int::class.java
|
||||
)
|
||||
return lseek.invoke(null, fd, offset, whence) as Long
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package eu.depau.etchdroid.ui.notifications
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.kotlet.android.extensions.notification.NotificationImportanceCompat
|
||||
import eu.depau.kotlet.android.extensions.notification.registerNotificationChannel
|
||||
|
||||
object AppNotifications {
|
||||
const val asyncProgressChannelId = "eu.depau.etchdroid.notifications.ASYNC)WORKER_PROGRESS"
|
||||
const val asyncResultChannelId = "eu.depau.etchdroid.notifications.ASYNC_WORKER_RESULT"
|
||||
|
||||
private var channelsRegistered = false
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.O)
|
||||
fun registerChannels(context: Context) {
|
||||
if (channelsRegistered || Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
||||
return
|
||||
|
||||
val notificationManager = context.getSystemService(NotificationManager::class.java)!!
|
||||
|
||||
notificationManager.registerNotificationChannel(
|
||||
asyncProgressChannelId,
|
||||
context.getString(R.string.notchan_writestatus_title),
|
||||
context.getString(R.string.notchan_writestatus_desc),
|
||||
NotificationImportanceCompat.IMPORTANCE_LOW
|
||||
)
|
||||
|
||||
notificationManager.registerNotificationChannel(
|
||||
asyncResultChannelId,
|
||||
context.getString(R.string.result_channel_name),
|
||||
context.getString(R.string.result_channel_desc),
|
||||
NotificationImportanceCompat.IMPORTANCE_DEFAULT
|
||||
)
|
||||
|
||||
channelsRegistered = true
|
||||
}
|
||||
|
||||
fun getNotificationBuilder(context: Context, channel: String): NotificationCompat.Builder {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
NotificationCompat.Builder(context, channel)
|
||||
} else {
|
||||
NotificationCompat.Builder(context)
|
||||
}
|
||||
}
|
||||
|
||||
fun buildAsyncWorkerProgressNotification(
|
||||
context: Context,
|
||||
contentTitle: String? = null,
|
||||
contentText: String? = null,
|
||||
subText: String? = null,
|
||||
ongoing: Boolean = true,
|
||||
progressPercent: Int = 0,
|
||||
indeterminate: Boolean = false,
|
||||
smallIcon: Int = R.drawable.ic_usb_white_24dp
|
||||
): Notification {
|
||||
|
||||
val builder = getNotificationBuilder(context, asyncProgressChannelId)
|
||||
|
||||
if (contentTitle != null)
|
||||
builder.setContentTitle(contentTitle)
|
||||
if (subText != null)
|
||||
builder.setSubText(subText)
|
||||
if (contentText != null)
|
||||
builder.setContentText(contentText)
|
||||
else
|
||||
builder.setContentText(context.getString(R.string.notif_initializing))
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
|
||||
builder.setSmallIcon(smallIcon)
|
||||
|
||||
builder.setOngoing(ongoing)
|
||||
builder.setProgress(100, progressPercent, indeterminate)
|
||||
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package eu.depau.etchdroid.utils
|
||||
|
||||
import android.net.Uri
|
||||
|
||||
data class License(
|
||||
val name: String,
|
||||
val url: Uri,
|
||||
val license: String,
|
||||
val description: String? = null
|
||||
)
|
||||
@@ -1,7 +1,7 @@
|
||||
package eu.depau.etchdroid.utils
|
||||
|
||||
import eu.depau.etchdroid.enums.FilesystemType
|
||||
import eu.depau.etchdroid.enums.PartitionType
|
||||
import eu.depau.etchdroid.utils.enums.FilesystemType
|
||||
import eu.depau.etchdroid.utils.enums.PartitionType
|
||||
|
||||
data class Partition(
|
||||
val number: Int?,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package eu.depau.etchdroid.utils
|
||||
|
||||
import eu.depau.etchdroid.enums.FilesystemType
|
||||
import eu.depau.etchdroid.enums.PartitionType
|
||||
import eu.depau.etchdroid.utils.enums.FilesystemType
|
||||
import eu.depau.etchdroid.utils.enums.PartitionType
|
||||
|
||||
class PartitionBuilder {
|
||||
var number: Int? = null
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package eu.depau.etchdroid.utils
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.os.Parcel
|
||||
import eu.depau.kotlet.android.parcelable.KotletParcelable
|
||||
import eu.depau.kotlet.android.parcelable.parcelableCreator
|
||||
import java.io.Serializable
|
||||
|
||||
class StringResBuilder(private val resId: Int, private vararg val formatArgs: Any) : Serializable, KotletParcelable {
|
||||
@SuppressLint("ParcelClassLoader")
|
||||
constructor(parcel: Parcel) : this(
|
||||
parcel.readInt(),
|
||||
parcel.readArray(null) as Array<Any>
|
||||
)
|
||||
|
||||
constructor(resId: Int) : this(resId, emptyArray<Any>())
|
||||
|
||||
fun build(context: Context): String {
|
||||
if (formatArgs.isEmpty())
|
||||
return context.getString(resId)
|
||||
|
||||
// Unwrap inner StringResBuilders
|
||||
val formatArgsUnwrapped = formatArgs.map {
|
||||
if (it is StringResBuilder)
|
||||
it.build(context)
|
||||
else
|
||||
it
|
||||
}.toTypedArray()
|
||||
|
||||
return context.getString(resId, formatArgsUnwrapped)
|
||||
}
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.apply {
|
||||
writeInt(resId)
|
||||
writeArray(formatArgs)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val CREATOR = parcelableCreator(::StringResBuilder)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.enums
|
||||
package eu.depau.etchdroid.utils.enums
|
||||
|
||||
import android.content.Context
|
||||
import eu.depau.etchdroid.R
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.enums
|
||||
package eu.depau.etchdroid.utils.enums
|
||||
|
||||
enum class FlashMethod {
|
||||
FLASH_API,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.enums
|
||||
package eu.depau.etchdroid.utils.enums
|
||||
|
||||
enum class ImageLocation {
|
||||
REMOTE,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.enums
|
||||
package eu.depau.etchdroid.utils.enums
|
||||
|
||||
import android.content.Context
|
||||
import eu.depau.etchdroid.R
|
||||
@@ -0,0 +1,3 @@
|
||||
package eu.depau.etchdroid.utils.enums
|
||||
|
||||
enum class PartitionType
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.enums
|
||||
package eu.depau.etchdroid.utils.enums
|
||||
|
||||
enum class WizardStep {
|
||||
SELECT_FLASH_METHOD,
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
package eu.depau.etchdroid.exceptions
|
||||
package eu.depau.etchdroid.utils.exception
|
||||
|
||||
class CannotGetFilePathException(cause: Exception) : Exception(cause)
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package eu.depau.etchdroid.exceptions
|
||||
package eu.depau.etchdroid.utils.exception
|
||||
|
||||
import eu.depau.etchdroid.kotlin_exts.toHRSize
|
||||
import eu.depau.etchdroid.utils.ktexts.toHRSize
|
||||
import java.io.IOException
|
||||
|
||||
class UsbWriteException(offset: Long, writtenBytes: Long, exc: Exception) : IOException(
|
||||
@@ -0,0 +1,153 @@
|
||||
package eu.depau.etchdroid.utils.imagetypes
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import eu.depau.etchdroid.utils.Partition
|
||||
import eu.depau.etchdroid.utils.PartitionBuilder
|
||||
import eu.depau.etchdroid.utils.enums.FilesystemType
|
||||
import eu.depau.etchdroid.utils.enums.PartitionTableType
|
||||
import eu.depau.etchdroid.utils.ktexts.getBinary
|
||||
import eu.depau.etchdroid.utils.ktexts.readString
|
||||
import eu.depau.etchdroid.utils.streams.AbstractSizedInputStream
|
||||
import eu.depau.etchdroid.utils.streams.SizedInputStream
|
||||
import java.io.File
|
||||
|
||||
val SECTOR_SIZE = 512
|
||||
private val partRegex = Regex("partition (\\d+): begin=(\\d+), size=(\\d+), decoded=(\\d+), firstsector=(\\d+), sectorcount=(\\d+), blocksruncount=(\\d+)\\s+(.*) \\((.+) : \\d+\\)", RegexOption.MULTILINE)
|
||||
private val partitionListRegex = Regex("\\s*partition (\\d+): begin=(\\d+), size=(\\d+), decoded=(\\d+), firstsector=(\\d+), sectorcount=(\\d+), blocksruncount=(\\d+)\\s*")
|
||||
|
||||
class DMGImage(private val uri: Uri, private val context: Context) : Image {
|
||||
private val libDir: String = context.applicationInfo.nativeLibraryDir
|
||||
private var loaded: Boolean = false
|
||||
private var partTable: List<Partition>? = null
|
||||
private var partTableType: PartitionTableType? = null
|
||||
private var imgSize: Long? = null
|
||||
|
||||
private fun readInfo() {
|
||||
if (loaded)
|
||||
return
|
||||
val triple = readPartitionTable(context, libDir, File(uri.path))
|
||||
loaded = true
|
||||
partTableType = triple.first
|
||||
partTable = triple.second
|
||||
imgSize = triple.third
|
||||
}
|
||||
|
||||
override val partitionTable: List<Partition>?
|
||||
get() {
|
||||
readInfo()
|
||||
return partTable
|
||||
}
|
||||
|
||||
|
||||
override val tableType: PartitionTableType?
|
||||
get() {
|
||||
readInfo()
|
||||
return partTableType
|
||||
}
|
||||
|
||||
override val size: Long?
|
||||
get() {
|
||||
readInfo()
|
||||
return imgSize
|
||||
}
|
||||
|
||||
override val inputStream: AbstractSizedInputStream
|
||||
get() = getRawImageInputStream(context, uri)
|
||||
|
||||
companion object {
|
||||
private fun getDmg2ImgProcessBuilder(context: Context, vararg args: String): ProcessBuilder =
|
||||
ProcessBuilder(context.getBinary("dmg2img").path, *args)
|
||||
.apply {
|
||||
environment()["LD_LIBRARY_PATH"] = context.applicationInfo.nativeLibraryDir
|
||||
}
|
||||
|
||||
private fun readPartitionTable(context: Context, libDir: String, file: File): Triple<PartitionTableType?, List<Partition>?, Long?> {
|
||||
val pt = ArrayList<Partition>()
|
||||
var ptType: PartitionTableType? = null
|
||||
var imgSize = 0L
|
||||
|
||||
val pb = getDmg2ImgProcessBuilder(context, "-v", "-l", file.path)
|
||||
pb.redirectErrorStream(true)
|
||||
|
||||
val p = pb.start()
|
||||
val out = p.inputStream.readString()
|
||||
System.err.println(out)
|
||||
p.waitFor()
|
||||
val matches = partRegex.findAll(out)
|
||||
|
||||
matchloop@ for (m in matches) {
|
||||
val (
|
||||
number, begin, size,
|
||||
decoded, firstsector,
|
||||
sectorcount, blocksruncount,
|
||||
label, type
|
||||
) = m.destructured
|
||||
|
||||
val pb = PartitionBuilder()
|
||||
|
||||
pb.number = number.toInt()
|
||||
pb.size = SECTOR_SIZE * sectorcount.toLong()
|
||||
imgSize += pb.size!!
|
||||
|
||||
if (label.isNotEmpty())
|
||||
pb.fsLabel = label
|
||||
|
||||
pb.partLabel = type
|
||||
|
||||
when (type) {
|
||||
"Apple_partition_map" -> {
|
||||
ptType = PartitionTableType.MAC
|
||||
}
|
||||
"MBR" -> {
|
||||
ptType = PartitionTableType.MSDOS
|
||||
continue@matchloop
|
||||
}
|
||||
}
|
||||
|
||||
pb.fsType = when {
|
||||
type == "Apple_Empty" || type == "Apple_Scratch" || type == "Apple_Free" -> FilesystemType.FREE
|
||||
type == "Apple_HFS" -> FilesystemType.HFSPLUS
|
||||
type == "Apple_HFSX" -> FilesystemType.HFSPLUS
|
||||
type == "Windows_FAT_32" -> FilesystemType.FAT32
|
||||
type.startsWith("Apple_") || type == "DDM" -> FilesystemType.APT_DATA
|
||||
else -> FilesystemType.UNKNOWN
|
||||
}
|
||||
|
||||
pt.add(pb.build())
|
||||
}
|
||||
|
||||
return Triple(ptType, pt, imgSize)
|
||||
}
|
||||
|
||||
fun getRawImageInputStream(
|
||||
context: Context,
|
||||
uri: Uri,
|
||||
sectorSize: Int = 512
|
||||
): AbstractSizedInputStream {
|
||||
val pb = getDmg2ImgProcessBuilder(context, "-v", uri.path!!, "-")
|
||||
val process = pb.start()
|
||||
val errReader = process.errorStream.bufferedReader()
|
||||
|
||||
// Read blocksruncount
|
||||
var matched = false
|
||||
var lastSector = 0L
|
||||
|
||||
while (true) {
|
||||
val line = errReader.readLine() ?: break
|
||||
val match = partitionListRegex.find(line) ?: if (matched) break else continue
|
||||
matched = true
|
||||
|
||||
val (begin, size, decoded, firstsector, sectorcount, blocksruncount) = match.destructured
|
||||
|
||||
val partLastSector = firstsector.toLong() + sectorcount.toLong()
|
||||
if (partLastSector > lastSector)
|
||||
lastSector = partLastSector
|
||||
}
|
||||
|
||||
val bytesTotal = lastSector * sectorSize
|
||||
return SizedInputStream(bytesTotal, process.inputStream)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package eu.depau.etchdroid.utils.imagetypes
|
||||
|
||||
import android.content.Context
|
||||
import eu.depau.etchdroid.utils.Partition
|
||||
import eu.depau.etchdroid.utils.enums.PartitionTableType
|
||||
import eu.depau.etchdroid.utils.streams.AbstractSizedInputStream
|
||||
|
||||
interface Image {
|
||||
val partitionTable: List<Partition>?
|
||||
val tableType: PartitionTableType?
|
||||
val size: Long?
|
||||
val inputStream: AbstractSizedInputStream
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package eu.depau.etchdroid.utils.imagetypes
|
||||
|
||||
import eu.depau.etchdroid.utils.Partition
|
||||
import eu.depau.etchdroid.utils.enums.PartitionTableType
|
||||
import eu.depau.etchdroid.utils.streams.AbstractSizedInputStream
|
||||
|
||||
class RawImage: Image {
|
||||
override val partitionTable: List<Partition>?
|
||||
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
|
||||
override val tableType: PartitionTableType?
|
||||
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
|
||||
override val size: Long?
|
||||
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
|
||||
override val inputStream: AbstractSizedInputStream
|
||||
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package eu.depau.etchdroid.utils.job
|
||||
|
||||
import android.os.Parcelable
|
||||
import eu.depau.etchdroid.utils.StringResBuilder
|
||||
import eu.depau.etchdroid.utils.job.enums.SharedDataType
|
||||
import eu.depau.etchdroid.utils.worker.IAsyncWorker
|
||||
import java.io.Serializable
|
||||
|
||||
interface IJobAction : Parcelable, Serializable {
|
||||
/**
|
||||
* Name of the action instance
|
||||
*/
|
||||
val name: StringResBuilder
|
||||
|
||||
/**
|
||||
* Weight of this action's progress on the total progress
|
||||
*/
|
||||
val progressWeight: Double
|
||||
|
||||
/**
|
||||
* Checkpoint data
|
||||
*/
|
||||
val checkpoint: Serializable?
|
||||
|
||||
/**
|
||||
* Whether the GUI should mention it
|
||||
*/
|
||||
val showInGUI: Boolean
|
||||
|
||||
/**
|
||||
* Whether this action always needs to be run, regardless of whether there was a checkpoint or
|
||||
* not, or whether the previous workers have failed or not
|
||||
*/
|
||||
val runAlways: Boolean
|
||||
|
||||
/**
|
||||
* Build a worker instance that performs the action encoded in this object
|
||||
*
|
||||
* @param sharedData a mutable map that workers can use to share data among each other
|
||||
* @return a worker instance that performs the encoded action
|
||||
*/
|
||||
fun getWorker(sharedData: MutableMap<SharedDataType, Any?>): IAsyncWorker
|
||||
|
||||
/**
|
||||
* Check action preconditions and return a list of StringResBuilders, which will then be
|
||||
* built and rendered in the UI.
|
||||
*
|
||||
* @return a list of failed preconditions descriptions
|
||||
*/
|
||||
fun checkPreconditions(): List<StringResBuilder>
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package eu.depau.etchdroid.utils.job
|
||||
|
||||
import android.os.Parcelable
|
||||
import eu.depau.etchdroid.utils.StringResBuilder
|
||||
import java.io.Serializable
|
||||
|
||||
interface IJobProcedure : List<IJobAction>, Parcelable, Serializable {
|
||||
val name: StringResBuilder
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package eu.depau.etchdroid.utils.job
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Parcelable
|
||||
|
||||
interface IJobProcedureBuilder : Parcelable {
|
||||
fun build(context: Context): IJobProcedure
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package eu.depau.etchdroid.utils.job
|
||||
|
||||
/**
|
||||
* Base interface for a class that listens to progress events from a JobService
|
||||
*/
|
||||
interface IJobProgressListener {
|
||||
/**
|
||||
* Called by the worker service to update the forwarder on the current ACTION index.
|
||||
*
|
||||
* @param actionIndex the current ACTION index within the JobProcedure
|
||||
*/
|
||||
fun onActionStart(actionIndex: Int)
|
||||
|
||||
/**
|
||||
* Called by the worker service to notify the forwarder that the current ACTION has finished
|
||||
* running. Whether it has finished successfully or not is unspecified.
|
||||
*
|
||||
* If it failed, a call to onProcedureError will follow with an Exception reference.
|
||||
*/
|
||||
fun onActionDone()
|
||||
|
||||
/**
|
||||
* Called by the worker service to notify the forwarder that the currently referenced procedure
|
||||
* is starting.
|
||||
*
|
||||
* @param startActionIndex the ACTION index the procedure is starting with (which is > 0 if it's
|
||||
* being bootstrapped from a checkpoint)
|
||||
*/
|
||||
fun onProcedureStart(startActionIndex: Int = 0)
|
||||
|
||||
/**
|
||||
* Called by the worker service to notify the forwarder that the currently referenced procedure
|
||||
* has finished successfully.
|
||||
*/
|
||||
fun onProcedureDone()
|
||||
|
||||
/**
|
||||
* Called by the worker service to notify the forwarder that the currently referenced procedure
|
||||
* has failed with the provided exception.
|
||||
*/
|
||||
fun onProcedureError(error: Throwable)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package eu.depau.etchdroid.utils.job
|
||||
|
||||
interface IJobProgressSender {
|
||||
fun attachJobProgressListener(listener: IJobProgressListener): Boolean
|
||||
fun detachJobProgressListener(listener: IJobProgressListener): Boolean
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package eu.depau.etchdroid.utils.job.enums
|
||||
|
||||
enum class SharedDataType {
|
||||
CONTEXT,
|
||||
INPUT_STREAM,
|
||||
OUTPUT_STREAM,
|
||||
SIZE
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package eu.depau.etchdroid.utils.job.impl
|
||||
|
||||
import eu.depau.etchdroid.utils.job.IJobProgressListener
|
||||
import eu.depau.etchdroid.utils.job.IJobProgressSender
|
||||
|
||||
abstract class AbstractJobProgressSender : IJobProgressSender {
|
||||
private val listeners = ArrayList<IJobProgressListener>()
|
||||
|
||||
override fun attachJobProgressListener(listener: IJobProgressListener): Boolean =
|
||||
listeners.add(listener)
|
||||
|
||||
override fun detachJobProgressListener(listener: IJobProgressListener): Boolean =
|
||||
listeners.remove(listener)
|
||||
|
||||
protected fun notifyJobActionStart(actionIndex: Int) =
|
||||
listeners.forEach { it.onActionStart(actionIndex) }
|
||||
|
||||
protected fun notifyJobActionDone() =
|
||||
listeners.forEach { it.onActionDone() }
|
||||
|
||||
protected fun notifyJobProcedureStart(startActionIndex: Int = 0) =
|
||||
listeners.forEach { it.onProcedureStart(startActionIndex) }
|
||||
|
||||
protected fun notifyJobProcedureDone() =
|
||||
listeners.forEach { it.onProcedureDone() }
|
||||
|
||||
protected fun notifyJobProcedureError(error: Throwable) =
|
||||
listeners.forEach { it.onProcedureError(error) }
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package eu.depau.etchdroid.utils.job.impl
|
||||
|
||||
import android.os.Parcel
|
||||
import eu.depau.etchdroid.utils.StringResBuilder
|
||||
import eu.depau.etchdroid.utils.job.IJobAction
|
||||
import eu.depau.etchdroid.utils.job.IJobProcedure
|
||||
import eu.depau.kotlet.android.parcelable.KotletParcelable
|
||||
import eu.depau.kotlet.android.parcelable.parcelableCreator
|
||||
import java.io.Serializable
|
||||
|
||||
class JobProcedure(override val name: StringResBuilder) :
|
||||
ArrayList<IJobAction>(), IJobProcedure, KotletParcelable, Serializable {
|
||||
|
||||
constructor(parcel: Parcel) : this(
|
||||
(parcel.readParcelable(StringResBuilder::class.java.classLoader) as StringResBuilder?)!!
|
||||
) {
|
||||
parcel.readList(this as List<IJobAction>, IJobAction::class.java.classLoader)
|
||||
}
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.apply {
|
||||
writeParcelable(name, flags)
|
||||
writeList(this@JobProcedure as List<IJobAction>)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val CREATOR = parcelableCreator(::JobProcedure)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.Toast
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.IOException
|
||||
@@ -0,0 +1,11 @@
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
|
||||
fun Intent.sendBroadcast(context: Context) = context.sendBroadcast(this)
|
||||
fun Intent.sendBroadcast(context: Context, receiverPermission: String) = context.sendBroadcast(this, receiverPermission)
|
||||
|
||||
fun Intent.sendLocalBroadcast(context: Context) = LocalBroadcastManager.getInstance(context).sendBroadcast(this)
|
||||
fun Intent.sendLocalBroadcastSync(context: Context) = LocalBroadcastManager.getInstance(context).sendBroadcastSync(this)
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
// https://stackoverflow.com/a/3758880/1124621
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
private val timeStrings = arrayOf("s", "m", "h", "d")
|
||||
private val timeDivs = arrayOf(60, 60, 24)
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
import android.content.ContentResolver
|
||||
import android.net.Uri
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
import android.content.ContentUris
|
||||
import android.content.Context
|
||||
@@ -7,7 +7,7 @@ import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.provider.DocumentsContract
|
||||
import android.provider.MediaStore
|
||||
import eu.depau.etchdroid.exceptions.CannotGetFilePathException
|
||||
import eu.depau.etchdroid.utils.exception.CannotGetFilePathException
|
||||
|
||||
|
||||
/**
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
import android.content.ContentResolver
|
||||
import android.content.Context
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.os.Build
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package eu.depau.etchdroid.kotlin_exts
|
||||
package eu.depau.etchdroid.utils.ktexts
|
||||
|
||||
import android.view.View
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
@@ -0,0 +1,7 @@
|
||||
package eu.depau.etchdroid.utils.streams
|
||||
|
||||
import java.io.InputStream
|
||||
|
||||
abstract class AbstractSizedInputStream: InputStream() {
|
||||
abstract val size: Long
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package eu.depau.etchdroid.utils.streams
|
||||
|
||||
import java.io.OutputStream
|
||||
|
||||
abstract class AbstractSizedOutputStream: OutputStream() {
|
||||
abstract val size: Long
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package eu.depau.etchdroid.utils.streams
|
||||
|
||||
import java.io.InputStream
|
||||
|
||||
/**
|
||||
* This class simply wraps an existing InputStream adding some provided size field.
|
||||
*/
|
||||
class SizedInputStream(
|
||||
override val size: Long,
|
||||
private val inputStream: InputStream
|
||||
) : AbstractSizedInputStream() {
|
||||
|
||||
override fun skip(n: Long): Long {
|
||||
return inputStream.skip(n)
|
||||
}
|
||||
|
||||
override fun available(): Int {
|
||||
return inputStream.available()
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
inputStream.reset()
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
inputStream.close()
|
||||
}
|
||||
|
||||
override fun mark(readlimit: Int) {
|
||||
inputStream.mark(readlimit)
|
||||
}
|
||||
|
||||
override fun markSupported(): Boolean {
|
||||
return inputStream.markSupported()
|
||||
}
|
||||
|
||||
override fun read(): Int {
|
||||
return inputStream.read()
|
||||
}
|
||||
|
||||
override fun read(b: ByteArray?): Int {
|
||||
return inputStream.read(b)
|
||||
}
|
||||
|
||||
override fun read(b: ByteArray?, off: Int, len: Int): Int {
|
||||
return inputStream.read(b, off, len)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package eu.depau.etchdroid.utils.streams
|
||||
|
||||
import java.io.OutputStream
|
||||
|
||||
/**
|
||||
* This class simply wraps an existing OutputStream adding some provided size field.
|
||||
*/
|
||||
class SizedOutputStream(
|
||||
override val size: Long,
|
||||
private val outputStream: OutputStream
|
||||
) : AbstractSizedOutputStream() {
|
||||
override fun write(b: Int) {
|
||||
outputStream.write(b)
|
||||
}
|
||||
|
||||
override fun write(b: ByteArray?) {
|
||||
outputStream.write(b)
|
||||
}
|
||||
|
||||
override fun write(b: ByteArray?, off: Int, len: Int) {
|
||||
outputStream.write(b, off, len)
|
||||
}
|
||||
|
||||
override fun flush() {
|
||||
outputStream.flush()
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
outputStream.close()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package eu.depau.etchdroid.utils.worker
|
||||
|
||||
interface IAsyncWorker: IWorkerProgressSender {
|
||||
/**
|
||||
* Make the worker run do one step. Returns whether there are more steps to do
|
||||
*
|
||||
* @return whether more work needs to be done
|
||||
*/
|
||||
fun runStep(): Boolean
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package eu.depau.etchdroid.utils.worker
|
||||
|
||||
import eu.depau.etchdroid.utils.worker.dto.ProgressUpdateDTO
|
||||
|
||||
interface IWorkerProgressListener {
|
||||
/**
|
||||
* Called by the current worker to update the progress listener on its status relative to
|
||||
* the current JobAction that describes its task.
|
||||
*
|
||||
* @param dto a ProgressUpdateDTO describing the current status of the worker
|
||||
*/
|
||||
fun onWorkerProgress(dto: ProgressUpdateDTO)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package eu.depau.etchdroid.utils.worker
|
||||
|
||||
interface IWorkerProgressSender {
|
||||
fun attachWorkerProgressListener(listener: IWorkerProgressListener): Boolean
|
||||
fun detachWorkerProgressListener(listener: IWorkerProgressListener): Boolean
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package eu.depau.etchdroid.utils.worker.dto
|
||||
|
||||
import android.os.Parcel
|
||||
import eu.depau.etchdroid.utils.StringResBuilder
|
||||
import eu.depau.etchdroid.utils.worker.enums.RateUnit
|
||||
import eu.depau.kotlet.android.parcelable.*
|
||||
|
||||
data class ProgressUpdateDTO(
|
||||
val progress: Double?,
|
||||
val rate: Double?,
|
||||
val rateUnit: RateUnit?,
|
||||
val message: StringResBuilder? = null
|
||||
) : KotletParcelable {
|
||||
constructor(parcel: Parcel) : this(
|
||||
progress = parcel.readNullable { parcel.readDouble() },
|
||||
rate = parcel.readNullable { parcel.readDouble() },
|
||||
rateUnit = parcel.readNullable { parcel.readEnum<RateUnit>() },
|
||||
message = parcel.readNullable {
|
||||
(parcel.readParcelable(StringResBuilder::class.java.classLoader) as StringResBuilder?)!!
|
||||
}
|
||||
)
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.apply {
|
||||
writeNullable(progress) { writeDouble(it) }
|
||||
writeNullable(rate) { writeDouble(it) }
|
||||
writeNullable(rateUnit) { writeEnum(rateUnit) }
|
||||
writeNullable(message) { writeParcelable(message, flags) }
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val CREATOR = parcelableCreator(::ProgressUpdateDTO)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package eu.depau.etchdroid.utils.worker.enums
|
||||
|
||||
enum class RateUnit {
|
||||
BYTES_PER_SECOND,
|
||||
FURLONGS_PER_FORTNIGHT
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package eu.depau.etchdroid.utils.worker.impl
|
||||
|
||||
import eu.depau.etchdroid.utils.worker.IAsyncWorker
|
||||
|
||||
abstract class AbstractAsyncWorker : IAsyncWorker, AbstractWorkerProgressSender()
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package eu.depau.etchdroid.utils.worker.impl
|
||||
|
||||
import eu.depau.etchdroid.utils.worker.dto.ProgressUpdateDTO
|
||||
import eu.depau.etchdroid.utils.worker.enums.RateUnit
|
||||
|
||||
|
||||
abstract class AbstractAutoProgressAsyncWorker(
|
||||
private val alreadyDone: Long,
|
||||
private val totalToDo: Long,
|
||||
private val rateUnit: RateUnit
|
||||
) : AbstractAsyncWorker() {
|
||||
|
||||
private var doneAccumulator = alreadyDone
|
||||
private var doneSinceLastUpdate = 0L
|
||||
private var lastUpdateTime = 0L
|
||||
|
||||
fun progressUpdate(stepDone: Long) {
|
||||
val currentTime = System.currentTimeMillis()
|
||||
|
||||
doneSinceLastUpdate += stepDone
|
||||
doneAccumulator += stepDone
|
||||
|
||||
if (currentTime > lastUpdateTime + UPDATE_INTERVAL) {
|
||||
val progress = doneAccumulator.toDouble() / totalToDo
|
||||
val speedUnitPerMillis = doneSinceLastUpdate.toDouble() / (currentTime - lastUpdateTime)
|
||||
|
||||
val dto = ProgressUpdateDTO(
|
||||
progress = progress,
|
||||
rate = speedUnitPerMillis * 1000,
|
||||
rateUnit = rateUnit
|
||||
)
|
||||
|
||||
notifyWorkerProgress(dto)
|
||||
|
||||
doneSinceLastUpdate = 0
|
||||
lastUpdateTime = currentTime
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val UPDATE_INTERVAL = 500
|
||||
}
|
||||
}
|
||||
Loaded 100 of 142 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user