seedvault/app/src/main/java/com/stevesoltys/backup/Backup.kt
Torsten Grote 1ee443a3d8
Add a unique ID to the device folder name to avoid collisions
when using several devices of the same model with the same account
2019-09-02 09:47:51 -03:00

61 lines
2 KiB
Kotlin

package com.stevesoltys.backup
import android.Manifest
import android.Manifest.permission.READ_PHONE_STATE
import android.app.Application
import android.app.backup.IBackupManager
import android.content.Context.BACKUP_SERVICE
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Build
import android.os.ServiceManager.getService
import android.util.Log
import com.stevesoltys.backup.crypto.KeyManager
import com.stevesoltys.backup.crypto.KeyManagerImpl
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
const val JOB_ID_BACKGROUND_BACKUP = 1
private val TAG = Backup::class.java.simpleName
/**
* @author Steve Soltys
* @author Torsten Grote
*/
class Backup : Application() {
companion object {
val backupManager: IBackupManager by lazy {
IBackupManager.Stub.asInterface(getService(BACKUP_SERVICE))
}
val keyManager: KeyManager by lazy {
KeyManagerImpl()
}
}
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)
}
}