1 /* 2 * Copyright (C) 2018 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 20 import android.content.ContentResolver; 21 import android.os.Handler; 22 import android.os.UserHandle; 23 import android.provider.Settings; 24 import android.util.KeyValueListParser; 25 import android.util.KeyValueSettingObserver; 26 27 import com.android.internal.annotations.GuardedBy; 28 import com.android.internal.annotations.VisibleForTesting; 29 30 /** 31 * Configure backup and restore agent timeouts. 32 * 33 * <p>These timeout parameters are stored in Settings.Global to be configurable flags with P/H. They 34 * are represented as a comma-delimited key value list. 35 */ 36 public class BackupAgentTimeoutParameters extends KeyValueSettingObserver { 37 @VisibleForTesting 38 public static final String SETTING = Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS; 39 40 @VisibleForTesting 41 public static final String SETTING_KV_BACKUP_AGENT_TIMEOUT_MILLIS = 42 "kv_backup_agent_timeout_millis"; 43 44 @VisibleForTesting 45 public static final String SETTING_FULL_BACKUP_AGENT_TIMEOUT_MILLIS = 46 "full_backup_agent_timeout_millis"; 47 48 @VisibleForTesting 49 public static final String SETTING_SHARED_BACKUP_AGENT_TIMEOUT_MILLIS = 50 "shared_backup_agent_timeout_millis"; 51 52 @VisibleForTesting 53 public static final String SETTING_RESTORE_AGENT_TIMEOUT_MILLIS = 54 "restore_agent_timeout_millis"; 55 56 @VisibleForTesting 57 public static final String SETTING_RESTORE_SYSTEM_AGENT_TIMEOUT_MILLIS = 58 "restore_system_agent_timeout_millis"; 59 60 @VisibleForTesting 61 public static final String SETTING_RESTORE_AGENT_FINISHED_TIMEOUT_MILLIS = 62 "restore_agent_finished_timeout_millis"; 63 64 @VisibleForTesting 65 public static final String SETTING_RESTORE_SESSION_TIMEOUT_MILLIS = 66 "restore_session_timeout_millis"; 67 68 @VisibleForTesting 69 public static final String SETTING_QUOTA_EXCEEDED_TIMEOUT_MILLIS = 70 "quota_exceeded_timeout_millis"; 71 72 // Default values 73 @VisibleForTesting public static final long DEFAULT_KV_BACKUP_AGENT_TIMEOUT_MILLIS = 30 * 1000; 74 75 @VisibleForTesting 76 public static final long DEFAULT_FULL_BACKUP_AGENT_TIMEOUT_MILLIS = 5 * 60 * 1000; 77 78 @VisibleForTesting 79 public static final long DEFAULT_SHARED_BACKUP_AGENT_TIMEOUT_MILLIS = 30 * 60 * 1000; 80 81 @VisibleForTesting public static final long DEFAULT_RESTORE_AGENT_TIMEOUT_MILLIS = 60 * 1000; 82 83 @VisibleForTesting public static final long DEFAULT_RESTORE_SYSTEM_AGENT_TIMEOUT_MILLIS = 84 180 * 1000; 85 86 @VisibleForTesting 87 public static final long DEFAULT_RESTORE_AGENT_FINISHED_TIMEOUT_MILLIS = 30 * 1000; 88 89 @VisibleForTesting public static final long DEFAULT_RESTORE_SESSION_TIMEOUT_MILLIS = 60 * 1000; 90 91 @VisibleForTesting 92 public static final long DEFAULT_QUOTA_EXCEEDED_TIMEOUT_MILLIS = 3 * 1000; 93 94 @GuardedBy("mLock") 95 private long mKvBackupAgentTimeoutMillis; 96 97 @GuardedBy("mLock") 98 private long mFullBackupAgentTimeoutMillis; 99 100 @GuardedBy("mLock") 101 private long mSharedBackupAgentTimeoutMillis; 102 103 @GuardedBy("mLock") 104 private long mRestoreAgentTimeoutMillis; 105 106 @GuardedBy("mLock") 107 private long mRestoreSystemAgentTimeoutMillis; 108 109 @GuardedBy("mLock") 110 private long mRestoreSessionTimeoutMillis; 111 112 @GuardedBy("mLock") 113 private long mRestoreAgentFinishedTimeoutMillis; 114 115 @GuardedBy("mLock") 116 private long mQuotaExceededTimeoutMillis; 117 118 private final Object mLock = new Object(); 119 BackupAgentTimeoutParameters(Handler handler, ContentResolver resolver)120 public BackupAgentTimeoutParameters(Handler handler, ContentResolver resolver) { 121 super(handler, resolver, Settings.Global.getUriFor(SETTING)); 122 } 123 getSettingValue(ContentResolver resolver)124 public String getSettingValue(ContentResolver resolver) { 125 return Settings.Global.getString(resolver, SETTING); 126 } 127 update(KeyValueListParser parser)128 public void update(KeyValueListParser parser) { 129 synchronized (mLock) { 130 mKvBackupAgentTimeoutMillis = 131 parser.getLong( 132 SETTING_KV_BACKUP_AGENT_TIMEOUT_MILLIS, 133 DEFAULT_KV_BACKUP_AGENT_TIMEOUT_MILLIS); 134 mFullBackupAgentTimeoutMillis = 135 parser.getLong( 136 SETTING_FULL_BACKUP_AGENT_TIMEOUT_MILLIS, 137 DEFAULT_FULL_BACKUP_AGENT_TIMEOUT_MILLIS); 138 mSharedBackupAgentTimeoutMillis = 139 parser.getLong( 140 SETTING_SHARED_BACKUP_AGENT_TIMEOUT_MILLIS, 141 DEFAULT_SHARED_BACKUP_AGENT_TIMEOUT_MILLIS); 142 mRestoreAgentTimeoutMillis = 143 parser.getLong( 144 SETTING_RESTORE_AGENT_TIMEOUT_MILLIS, 145 DEFAULT_RESTORE_AGENT_TIMEOUT_MILLIS); 146 mRestoreSystemAgentTimeoutMillis = 147 parser.getLong( 148 SETTING_RESTORE_SYSTEM_AGENT_TIMEOUT_MILLIS, 149 DEFAULT_RESTORE_SYSTEM_AGENT_TIMEOUT_MILLIS); 150 mRestoreAgentFinishedTimeoutMillis = 151 parser.getLong( 152 SETTING_RESTORE_AGENT_FINISHED_TIMEOUT_MILLIS, 153 DEFAULT_RESTORE_AGENT_FINISHED_TIMEOUT_MILLIS); 154 mRestoreSessionTimeoutMillis = 155 parser.getLong( 156 SETTING_RESTORE_SESSION_TIMEOUT_MILLIS, 157 DEFAULT_RESTORE_SESSION_TIMEOUT_MILLIS); 158 mQuotaExceededTimeoutMillis = 159 parser.getLong( 160 SETTING_QUOTA_EXCEEDED_TIMEOUT_MILLIS, 161 DEFAULT_QUOTA_EXCEEDED_TIMEOUT_MILLIS); 162 } 163 } 164 getKvBackupAgentTimeoutMillis()165 public long getKvBackupAgentTimeoutMillis() { 166 synchronized (mLock) { 167 return mKvBackupAgentTimeoutMillis; 168 } 169 } 170 getFullBackupAgentTimeoutMillis()171 public long getFullBackupAgentTimeoutMillis() { 172 synchronized (mLock) { 173 return mFullBackupAgentTimeoutMillis; 174 } 175 } 176 getSharedBackupAgentTimeoutMillis()177 public long getSharedBackupAgentTimeoutMillis() { 178 synchronized (mLock) { 179 return mSharedBackupAgentTimeoutMillis; 180 } 181 } 182 183 /** 184 * @param applicationUid UID of the application for which to get restore timeout 185 * @return restore timeout in milliseconds 186 */ getRestoreAgentTimeoutMillis(int applicationUid)187 public long getRestoreAgentTimeoutMillis(int applicationUid) { 188 synchronized (mLock) { 189 return UserHandle.isCore(applicationUid) ? mRestoreSystemAgentTimeoutMillis : 190 mRestoreAgentTimeoutMillis; 191 } 192 } 193 getRestoreSessionTimeoutMillis()194 public long getRestoreSessionTimeoutMillis() { 195 synchronized (mLock) { 196 return mRestoreSessionTimeoutMillis; 197 } 198 } 199 getRestoreAgentFinishedTimeoutMillis()200 public long getRestoreAgentFinishedTimeoutMillis() { 201 synchronized (mLock) { 202 return mRestoreAgentFinishedTimeoutMillis; 203 } 204 } 205 getQuotaExceededTimeoutMillis()206 public long getQuotaExceededTimeoutMillis() { 207 synchronized (mLock) { 208 return mQuotaExceededTimeoutMillis; 209 } 210 } 211 } 212