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