1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.systemui.backup
18
19 import android.app.backup.BackupAgentHelper
20 import android.app.backup.BackupDataInputStream
21 import android.app.backup.BackupDataOutput
22 import android.app.backup.FileBackupHelper
23 import android.app.job.JobScheduler
24 import android.content.Context
25 import android.content.Intent
26 import android.os.Environment
27 import android.os.ParcelFileDescriptor
28 import android.os.UserHandle
29 import android.util.Log
30 import com.android.systemui.controls.controller.AuxiliaryPersistenceWrapper
31 import com.android.systemui.controls.controller.ControlsFavoritePersistenceWrapper
32
33 /**
34 * Helper for backing up elements in SystemUI
35 *
36 * This helper is invoked by BackupManager whenever a backup or restore is required in SystemUI.
37 * The helper can be used to back up any element that is stored in [Context.getFilesDir].
38 *
39 * After restoring is done, a [ACTION_RESTORE_FINISHED] intent will be send to SystemUI user 0,
40 * indicating that restoring is finished for a given user.
41 */
42 class BackupHelper : BackupAgentHelper() {
43
44 companion object {
45 private const val TAG = "BackupHelper"
46 internal const val CONTROLS = ControlsFavoritePersistenceWrapper.FILE_NAME
47 private const val NO_OVERWRITE_FILES_BACKUP_KEY = "systemui.files_no_overwrite"
48 val controlsDataLock = Any()
49 const val ACTION_RESTORE_FINISHED = "com.android.systemui.backup.RESTORE_FINISHED"
50 private const val PERMISSION_SELF = "com.android.systemui.permission.SELF"
51 }
52
onCreatenull53 override fun onCreate() {
54 super.onCreate()
55 // The map in mapOf is guaranteed to be order preserving
56 val controlsMap = mapOf(CONTROLS to getPPControlsFile(this))
57 NoOverwriteFileBackupHelper(controlsDataLock, this, controlsMap).also {
58 addHelper(NO_OVERWRITE_FILES_BACKUP_KEY, it)
59 }
60 }
61
onRestoreFinishednull62 override fun onRestoreFinished() {
63 super.onRestoreFinished()
64 val intent = Intent(ACTION_RESTORE_FINISHED).apply {
65 `package` = packageName
66 putExtra(Intent.EXTRA_USER_ID, userId)
67 flags = Intent.FLAG_RECEIVER_REGISTERED_ONLY
68 }
69 sendBroadcastAsUser(intent, UserHandle.SYSTEM, PERMISSION_SELF)
70 }
71
72 /**
73 * Helper class for restoring files ONLY if they are not present.
74 *
75 * A [Map] between filenames and actions (functions) is passed to indicate post processing
76 * actions to be taken after each file is restored.
77 *
78 * @property lock a lock to hold while backing up and restoring the files.
79 * @property context the context of the [BackupAgent]
80 * @property fileNamesAndPostProcess a map from the filenames to back up and the post processing
81 * actions to take
82 */
83 private class NoOverwriteFileBackupHelper(
84 val lock: Any,
85 val context: Context,
86 val fileNamesAndPostProcess: Map<String, () -> Unit>
87 ) : FileBackupHelper(context, *fileNamesAndPostProcess.keys.toTypedArray()) {
88
restoreEntitynull89 override fun restoreEntity(data: BackupDataInputStream) {
90 val file = Environment.buildPath(context.filesDir, data.key)
91 if (file.exists()) {
92 Log.w(TAG, "File " + data.key + " already exists. Skipping restore.")
93 return
94 }
95 synchronized(lock) {
96 super.restoreEntity(data)
97 fileNamesAndPostProcess.get(data.key)?.invoke()
98 }
99 }
100
performBackupnull101 override fun performBackup(
102 oldState: ParcelFileDescriptor?,
103 data: BackupDataOutput?,
104 newState: ParcelFileDescriptor?
105 ) {
106 synchronized(lock) {
107 super.performBackup(oldState, data, newState)
108 }
109 }
110 }
111 }
getPPControlsFilenull112 private fun getPPControlsFile(context: Context): () -> Unit {
113 return {
114 val filesDir = context.filesDir
115 val file = Environment.buildPath(filesDir, BackupHelper.CONTROLS)
116 if (file.exists()) {
117 val dest = Environment.buildPath(filesDir,
118 AuxiliaryPersistenceWrapper.AUXILIARY_FILE_NAME)
119 file.copyTo(dest)
120 val jobScheduler = context.getSystemService(JobScheduler::class.java)
121 jobScheduler?.schedule(
122 AuxiliaryPersistenceWrapper.DeletionJobService.getJobForContext(context))
123 }
124 }
125 }