2019-07-09 09:52:03 +02:00
|
|
|
package com.stevesoltys.backup
|
|
|
|
|
2019-08-01 10:34:31 +02:00
|
|
|
import android.Manifest
|
|
|
|
import android.Manifest.permission.READ_PHONE_STATE
|
2019-07-09 09:52:03 +02:00
|
|
|
import android.app.Application
|
|
|
|
import android.app.backup.IBackupManager
|
|
|
|
import android.content.Context.BACKUP_SERVICE
|
2019-08-01 10:34:31 +02:00
|
|
|
import android.content.pm.PackageManager.PERMISSION_GRANTED
|
|
|
|
import android.os.Build
|
2019-07-09 09:52:03 +02:00
|
|
|
import android.os.ServiceManager.getService
|
2019-08-01 10:34:31 +02:00
|
|
|
import android.util.Log
|
2019-07-09 19:22:24 +02:00
|
|
|
import com.stevesoltys.backup.crypto.KeyManager
|
|
|
|
import com.stevesoltys.backup.crypto.KeyManagerImpl
|
2019-08-01 10:34:31 +02:00
|
|
|
import com.stevesoltys.backup.settings.getDeviceName
|
|
|
|
import com.stevesoltys.backup.settings.setDeviceName
|
|
|
|
import io.github.novacrypto.hashing.Sha256
|
|
|
|
import io.github.novacrypto.hashing.Sha256.sha256
|
|
|
|
import io.github.novacrypto.hashing.Sha256.sha256Twice
|
|
|
|
import java.lang.AssertionError
|
2019-07-09 09:52:03 +02:00
|
|
|
|
|
|
|
const val JOB_ID_BACKGROUND_BACKUP = 1
|
|
|
|
|
2019-08-01 10:34:31 +02:00
|
|
|
private val TAG = Backup::class.java.simpleName
|
|
|
|
|
2019-07-09 09:52:03 +02:00
|
|
|
/**
|
|
|
|
* @author Steve Soltys
|
|
|
|
* @author Torsten Grote
|
|
|
|
*/
|
|
|
|
class Backup : Application() {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
val backupManager: IBackupManager by lazy {
|
|
|
|
IBackupManager.Stub.asInterface(getService(BACKUP_SERVICE))
|
|
|
|
}
|
2019-07-09 19:22:24 +02:00
|
|
|
val keyManager: KeyManager by lazy {
|
|
|
|
KeyManagerImpl()
|
|
|
|
}
|
2019-07-09 09:52:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 10:34:31 +02:00
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
|
|
|
storeDeviceName()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun storeDeviceName() {
|
|
|
|
if (getDeviceName(this) != null) return // we already have a stored device name
|
|
|
|
|
|
|
|
val permission = READ_PHONE_STATE
|
|
|
|
if (checkSelfPermission(permission) != PERMISSION_GRANTED) {
|
|
|
|
throw AssertionError("You need to grant the $permission permission.")
|
|
|
|
}
|
|
|
|
// TODO consider just using a hash for the entire device name and store metadata in an encrypted file
|
|
|
|
val id = sha256Twice(Build.getSerial().toByteArray(Utf8))
|
|
|
|
.copyOfRange(0, 8)
|
|
|
|
.encodeBase64()
|
|
|
|
val name = "${Build.MANUFACTURER} ${Build.MODEL} ($id)"
|
|
|
|
Log.i(TAG, "Initialized device name to: $name")
|
|
|
|
setDeviceName(this, name)
|
|
|
|
}
|
|
|
|
|
2019-07-09 09:52:03 +02:00
|
|
|
}
|