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