1 /* 2 * Copyright (C) 2017 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.server.backup; 18 19 import static com.android.server.backup.BackupManagerService.DEBUG_SCHEDULING; 20 21 import android.app.AlarmManager; 22 import android.app.job.JobInfo; 23 import android.content.ContentResolver; 24 import android.os.Handler; 25 import android.provider.Settings; 26 import android.text.TextUtils; 27 import android.util.KeyValueListParser; 28 import android.util.KeyValueSettingObserver; 29 import android.util.Slog; 30 31 import com.android.internal.annotations.VisibleForTesting; 32 33 /** 34 * Class to access backup manager constants. 35 * 36 * <p>The backup manager constants are encoded as a key value list separated by commas and stored as 37 * a Settings.Secure. 38 */ 39 public class BackupManagerConstants extends KeyValueSettingObserver { 40 private static final String TAG = "BackupManagerConstants"; 41 private static final String SETTING = Settings.Secure.BACKUP_MANAGER_CONSTANTS; 42 43 // Key names stored in the secure settings value. 44 @VisibleForTesting 45 public static final String KEY_VALUE_BACKUP_INTERVAL_MILLISECONDS = 46 "key_value_backup_interval_milliseconds"; 47 48 @VisibleForTesting 49 public static final String KEY_VALUE_BACKUP_FUZZ_MILLISECONDS = 50 "key_value_backup_fuzz_milliseconds"; 51 52 @VisibleForTesting 53 public static final String KEY_VALUE_BACKUP_REQUIRE_CHARGING = 54 "key_value_backup_require_charging"; 55 56 @VisibleForTesting 57 public static final String KEY_VALUE_BACKUP_REQUIRED_NETWORK_TYPE = 58 "key_value_backup_required_network_type"; 59 60 @VisibleForTesting 61 public static final String FULL_BACKUP_INTERVAL_MILLISECONDS = 62 "full_backup_interval_milliseconds"; 63 64 @VisibleForTesting 65 public static final String FULL_BACKUP_REQUIRE_CHARGING = "full_backup_require_charging"; 66 67 @VisibleForTesting 68 public static final String FULL_BACKUP_REQUIRED_NETWORK_TYPE = 69 "full_backup_required_network_type"; 70 71 @VisibleForTesting 72 public static final String BACKUP_FINISHED_NOTIFICATION_RECEIVERS = 73 "backup_finished_notification_receivers"; 74 75 // Hard coded default values. 76 @VisibleForTesting 77 public static final long DEFAULT_KEY_VALUE_BACKUP_INTERVAL_MILLISECONDS = 78 4 * AlarmManager.INTERVAL_HOUR; 79 80 @VisibleForTesting 81 public static final long DEFAULT_KEY_VALUE_BACKUP_FUZZ_MILLISECONDS = 10 * 60 * 1000; 82 83 @VisibleForTesting public static final boolean DEFAULT_KEY_VALUE_BACKUP_REQUIRE_CHARGING = true; 84 @VisibleForTesting 85 public static final int DEFAULT_KEY_VALUE_BACKUP_REQUIRED_NETWORK_TYPE = 86 JobInfo.NETWORK_TYPE_ANY; 87 88 @VisibleForTesting 89 public static final long DEFAULT_FULL_BACKUP_INTERVAL_MILLISECONDS = 90 24 * AlarmManager.INTERVAL_HOUR; 91 92 @VisibleForTesting public static final boolean DEFAULT_FULL_BACKUP_REQUIRE_CHARGING = true; 93 @VisibleForTesting 94 public static final int DEFAULT_FULL_BACKUP_REQUIRED_NETWORK_TYPE = 95 JobInfo.NETWORK_TYPE_UNMETERED; 96 97 @VisibleForTesting 98 public static final String DEFAULT_BACKUP_FINISHED_NOTIFICATION_RECEIVERS = ""; 99 100 // Backup manager constants. 101 private long mKeyValueBackupIntervalMilliseconds; 102 private long mKeyValueBackupFuzzMilliseconds; 103 private boolean mKeyValueBackupRequireCharging; 104 private int mKeyValueBackupRequiredNetworkType; 105 private long mFullBackupIntervalMilliseconds; 106 private boolean mFullBackupRequireCharging; 107 private int mFullBackupRequiredNetworkType; 108 private String[] mBackupFinishedNotificationReceivers; 109 BackupManagerConstants(Handler handler, ContentResolver resolver)110 public BackupManagerConstants(Handler handler, ContentResolver resolver) { 111 super(handler, resolver, Settings.Secure.getUriFor(SETTING)); 112 } 113 getSettingValue(ContentResolver resolver)114 public String getSettingValue(ContentResolver resolver) { 115 return Settings.Secure.getStringForUser(resolver, SETTING, resolver.getUserId()); 116 } 117 update(KeyValueListParser parser)118 public synchronized void update(KeyValueListParser parser) { 119 mKeyValueBackupIntervalMilliseconds = 120 parser.getLong( 121 KEY_VALUE_BACKUP_INTERVAL_MILLISECONDS, 122 DEFAULT_KEY_VALUE_BACKUP_INTERVAL_MILLISECONDS); 123 mKeyValueBackupFuzzMilliseconds = 124 parser.getLong( 125 KEY_VALUE_BACKUP_FUZZ_MILLISECONDS, 126 DEFAULT_KEY_VALUE_BACKUP_FUZZ_MILLISECONDS); 127 mKeyValueBackupRequireCharging = 128 parser.getBoolean( 129 KEY_VALUE_BACKUP_REQUIRE_CHARGING, 130 DEFAULT_KEY_VALUE_BACKUP_REQUIRE_CHARGING); 131 mKeyValueBackupRequiredNetworkType = 132 parser.getInt( 133 KEY_VALUE_BACKUP_REQUIRED_NETWORK_TYPE, 134 DEFAULT_KEY_VALUE_BACKUP_REQUIRED_NETWORK_TYPE); 135 mFullBackupIntervalMilliseconds = 136 parser.getLong( 137 FULL_BACKUP_INTERVAL_MILLISECONDS, 138 DEFAULT_FULL_BACKUP_INTERVAL_MILLISECONDS); 139 mFullBackupRequireCharging = 140 parser.getBoolean( 141 FULL_BACKUP_REQUIRE_CHARGING, DEFAULT_FULL_BACKUP_REQUIRE_CHARGING); 142 mFullBackupRequiredNetworkType = 143 parser.getInt( 144 FULL_BACKUP_REQUIRED_NETWORK_TYPE, 145 DEFAULT_FULL_BACKUP_REQUIRED_NETWORK_TYPE); 146 String backupFinishedNotificationReceivers = 147 parser.getString( 148 BACKUP_FINISHED_NOTIFICATION_RECEIVERS, 149 DEFAULT_BACKUP_FINISHED_NOTIFICATION_RECEIVERS); 150 if (backupFinishedNotificationReceivers.isEmpty()) { 151 mBackupFinishedNotificationReceivers = new String[] {}; 152 } else { 153 mBackupFinishedNotificationReceivers = backupFinishedNotificationReceivers.split(":"); 154 } 155 } 156 157 // The following are access methods for the individual parameters. 158 // To be sure to retrieve values from the same set of settings, 159 // group the calls of these methods in a block syncrhonized on 160 // a reference of this object. getKeyValueBackupIntervalMilliseconds()161 public synchronized long getKeyValueBackupIntervalMilliseconds() { 162 if (DEBUG_SCHEDULING) { 163 Slog.v( 164 TAG, 165 "getKeyValueBackupIntervalMilliseconds(...) returns " 166 + mKeyValueBackupIntervalMilliseconds); 167 } 168 return mKeyValueBackupIntervalMilliseconds; 169 } 170 getKeyValueBackupFuzzMilliseconds()171 public synchronized long getKeyValueBackupFuzzMilliseconds() { 172 if (DEBUG_SCHEDULING) { 173 Slog.v( 174 TAG, 175 "getKeyValueBackupFuzzMilliseconds(...) returns " 176 + mKeyValueBackupFuzzMilliseconds); 177 } 178 return mKeyValueBackupFuzzMilliseconds; 179 } 180 getKeyValueBackupRequireCharging()181 public synchronized boolean getKeyValueBackupRequireCharging() { 182 if (DEBUG_SCHEDULING) { 183 Slog.v( 184 TAG, 185 "getKeyValueBackupRequireCharging(...) returns " 186 + mKeyValueBackupRequireCharging); 187 } 188 return mKeyValueBackupRequireCharging; 189 } 190 getKeyValueBackupRequiredNetworkType()191 public synchronized int getKeyValueBackupRequiredNetworkType() { 192 if (DEBUG_SCHEDULING) { 193 Slog.v( 194 TAG, 195 "getKeyValueBackupRequiredNetworkType(...) returns " 196 + mKeyValueBackupRequiredNetworkType); 197 } 198 return mKeyValueBackupRequiredNetworkType; 199 } 200 getFullBackupIntervalMilliseconds()201 public synchronized long getFullBackupIntervalMilliseconds() { 202 if (DEBUG_SCHEDULING) { 203 Slog.v( 204 TAG, 205 "getFullBackupIntervalMilliseconds(...) returns " 206 + mFullBackupIntervalMilliseconds); 207 } 208 return mFullBackupIntervalMilliseconds; 209 } 210 getFullBackupRequireCharging()211 public synchronized boolean getFullBackupRequireCharging() { 212 if (DEBUG_SCHEDULING) { 213 Slog.v(TAG, "getFullBackupRequireCharging(...) returns " + mFullBackupRequireCharging); 214 } 215 return mFullBackupRequireCharging; 216 } 217 getFullBackupRequiredNetworkType()218 public synchronized int getFullBackupRequiredNetworkType() { 219 if (DEBUG_SCHEDULING) { 220 Slog.v( 221 TAG, 222 "getFullBackupRequiredNetworkType(...) returns " 223 + mFullBackupRequiredNetworkType); 224 } 225 return mFullBackupRequiredNetworkType; 226 } 227 228 /** Returns an array of package names that should be notified whenever a backup finishes. */ getBackupFinishedNotificationReceivers()229 public synchronized String[] getBackupFinishedNotificationReceivers() { 230 if (DEBUG_SCHEDULING) { 231 Slog.v( 232 TAG, 233 "getBackupFinishedNotificationReceivers(...) returns " 234 + TextUtils.join(", ", mBackupFinishedNotificationReceivers)); 235 } 236 return mBackupFinishedNotificationReceivers; 237 } 238 } 239