2019-07-09 09:52:03 +02:00
|
|
|
package com.stevesoltys.backup
|
|
|
|
|
|
|
|
import android.app.backup.BackupProgress
|
|
|
|
import android.app.backup.IBackupObserver
|
|
|
|
import android.content.Context
|
2019-09-06 17:36:51 +02:00
|
|
|
import android.content.pm.PackageManager
|
2019-07-09 09:52:03 +02:00
|
|
|
import android.util.Log
|
|
|
|
import android.util.Log.INFO
|
|
|
|
import android.util.Log.isLoggable
|
|
|
|
|
2019-07-09 19:22:24 +02:00
|
|
|
private val TAG = NotificationBackupObserver::class.java.simpleName
|
2019-07-09 09:52:03 +02:00
|
|
|
|
2019-09-02 22:01:12 +02:00
|
|
|
class NotificationBackupObserver(context: Context, private val userInitiated: Boolean) : IBackupObserver.Stub() {
|
2019-07-09 09:52:03 +02:00
|
|
|
|
|
|
|
private val pm = context.packageManager
|
2019-09-02 22:01:12 +02:00
|
|
|
private val nm = (context.applicationContext as Backup).notificationManager
|
2019-07-09 09:52:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method could be called several times for packages with full data backup.
|
|
|
|
* It will tell how much of backup data is already saved and how much is expected.
|
|
|
|
*
|
|
|
|
* @param currentBackupPackage The name of the package that now being backed up.
|
|
|
|
* @param backupProgress Current progress of backup for the package.
|
|
|
|
*/
|
|
|
|
override fun onUpdate(currentBackupPackage: String, backupProgress: BackupProgress) {
|
2019-09-02 22:01:12 +02:00
|
|
|
val transferred = backupProgress.bytesTransferred.toInt()
|
|
|
|
val expected = backupProgress.bytesExpected.toInt()
|
2019-07-09 09:52:03 +02:00
|
|
|
if (isLoggable(TAG, INFO)) {
|
|
|
|
Log.i(TAG, "Update. Target: $currentBackupPackage, $transferred/$expected")
|
|
|
|
}
|
2019-09-02 22:01:12 +02:00
|
|
|
val app = getAppName(currentBackupPackage)
|
|
|
|
nm.onBackupUpdate(app, transferred, expected, userInitiated)
|
2019-07-09 09:52:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backup of one package or initialization of one transport has completed. This
|
|
|
|
* method will be called at most one time for each package or transport, and might not
|
|
|
|
* be not called if the operation fails before backupFinished(); for example, if the
|
|
|
|
* requested package/transport does not exist.
|
|
|
|
*
|
|
|
|
* @param target The name of the package that was backed up, or of the transport
|
|
|
|
* that was initialized
|
|
|
|
* @param status Zero on success; a nonzero error code if the backup operation failed.
|
|
|
|
*/
|
|
|
|
override fun onResult(target: String, status: Int) {
|
|
|
|
if (isLoggable(TAG, INFO)) {
|
|
|
|
Log.i(TAG, "Completed. Target: $target, status: $status")
|
|
|
|
}
|
2019-09-02 22:01:12 +02:00
|
|
|
nm.onBackupResult(getAppName(target), status, userInitiated)
|
2019-07-09 09:52:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The backup process has completed. This method will always be called,
|
|
|
|
* even if no individual package backup operations were attempted.
|
|
|
|
*
|
|
|
|
* @param status Zero on success; a nonzero error code if the backup operation
|
|
|
|
* as a whole failed.
|
|
|
|
*/
|
|
|
|
override fun backupFinished(status: Int) {
|
|
|
|
if (isLoggable(TAG, INFO)) {
|
|
|
|
Log.i(TAG, "Backup finished. Status: $status")
|
|
|
|
}
|
2019-09-02 22:01:12 +02:00
|
|
|
nm.onBackupFinished()
|
2019-07-09 09:52:03 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 17:36:51 +02:00
|
|
|
private fun getAppName(packageId: String): CharSequence = getAppName(pm, packageId)
|
|
|
|
|
|
|
|
}
|
2019-07-09 09:52:03 +02:00
|
|
|
|
2019-09-06 17:36:51 +02:00
|
|
|
fun getAppName(pm: PackageManager, packageId: String): CharSequence {
|
|
|
|
if (packageId == "@pm@") return packageId
|
|
|
|
val appInfo = pm.getApplicationInfo(packageId, 0)
|
|
|
|
return pm.getApplicationLabel(appInfo)
|
2019-07-09 09:52:03 +02:00
|
|
|
}
|