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