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 package com.android.server.content;
17 
18 import android.content.Context;
19 import android.database.ContentObserver;
20 import android.provider.Settings;
21 import android.provider.Settings.Global;
22 import android.util.KeyValueListParser;
23 import android.util.Slog;
24 
25 import com.android.internal.os.BackgroundThread;
26 
27 import java.io.PrintWriter;
28 
29 public class SyncManagerConstants extends ContentObserver {
30     private static final String TAG = "SyncManagerConfig";
31 
32     private final Object mLock = new Object();
33     private final Context mContext;
34 
35     private static final String KEY_INITIAL_SYNC_RETRY_TIME_IN_SECONDS =
36             "initial_sync_retry_time_in_seconds";
37     private static final int DEF_INITIAL_SYNC_RETRY_TIME_IN_SECONDS = 30;
38     private int mInitialSyncRetryTimeInSeconds = DEF_INITIAL_SYNC_RETRY_TIME_IN_SECONDS;
39 
40     private static final String KEY_RETRY_TIME_INCREASE_FACTOR =
41             "retry_time_increase_factor";
42     private static final float DEF_RETRY_TIME_INCREASE_FACTOR = 2.0f;
43     private float mRetryTimeIncreaseFactor = DEF_RETRY_TIME_INCREASE_FACTOR;
44 
45     private static final String KEY_MAX_SYNC_RETRY_TIME_IN_SECONDS =
46             "max_sync_retry_time_in_seconds";
47     private static final int DEF_MAX_SYNC_RETRY_TIME_IN_SECONDS = 60 * 60;
48     private int mMaxSyncRetryTimeInSeconds = DEF_MAX_SYNC_RETRY_TIME_IN_SECONDS;
49 
50     private static final String KEY_MAX_RETRIES_WITH_APP_STANDBY_EXEMPTION =
51             "max_retries_with_app_standby_exemption";
52     private static final int DEF_MAX_RETRIES_WITH_APP_STANDBY_EXEMPTION = 5;
53     private int mMaxRetriesWithAppStandbyExemption = DEF_MAX_RETRIES_WITH_APP_STANDBY_EXEMPTION;
54 
55     private static final String KEY_EXEMPTION_TEMP_WHITELIST_DURATION_IN_SECONDS =
56             "exemption_temp_whitelist_duration_in_seconds";
57     private static final int DEF_EXEMPTION_TEMP_WHITELIST_DURATION_IN_SECONDS = 10 * 60;
58     private int mKeyExemptionTempWhitelistDurationInSeconds
59             = DEF_EXEMPTION_TEMP_WHITELIST_DURATION_IN_SECONDS;
60 
SyncManagerConstants(Context context)61     protected SyncManagerConstants(Context context) {
62         super(null);
63         mContext = context;
64     }
65 
start()66     public void start() {
67         BackgroundThread.getHandler().post(() -> {
68             mContext.getContentResolver().registerContentObserver(Settings.Global.getUriFor(
69                     Settings.Global.SYNC_MANAGER_CONSTANTS), false, this);
70             refresh();
71         });
72     }
73 
74     @Override
onChange(boolean selfChange)75     public void onChange(boolean selfChange) {
76         refresh();
77     }
78 
refresh()79     private void refresh() {
80         synchronized (mLock) {
81 
82             String newValue = Settings.Global.getString(mContext.getContentResolver(),
83                     Global.SYNC_MANAGER_CONSTANTS);
84             final KeyValueListParser parser = new KeyValueListParser(',');
85             try {
86                 parser.setString(newValue);
87             } catch (IllegalArgumentException e) {
88                 Slog.wtf(TAG, "Bad constants: " + newValue);
89             }
90 
91             mInitialSyncRetryTimeInSeconds = parser.getInt(
92                     KEY_INITIAL_SYNC_RETRY_TIME_IN_SECONDS,
93                     DEF_INITIAL_SYNC_RETRY_TIME_IN_SECONDS);
94 
95             mMaxSyncRetryTimeInSeconds = parser.getInt(
96                     KEY_MAX_SYNC_RETRY_TIME_IN_SECONDS,
97                     DEF_MAX_SYNC_RETRY_TIME_IN_SECONDS);
98 
99             mRetryTimeIncreaseFactor = parser.getFloat(
100                     KEY_RETRY_TIME_INCREASE_FACTOR,
101                     DEF_RETRY_TIME_INCREASE_FACTOR);
102 
103             mMaxRetriesWithAppStandbyExemption = parser.getInt(
104                     KEY_MAX_RETRIES_WITH_APP_STANDBY_EXEMPTION,
105                     DEF_MAX_RETRIES_WITH_APP_STANDBY_EXEMPTION);
106 
107             mKeyExemptionTempWhitelistDurationInSeconds = parser.getInt(
108                     KEY_EXEMPTION_TEMP_WHITELIST_DURATION_IN_SECONDS,
109                     DEF_EXEMPTION_TEMP_WHITELIST_DURATION_IN_SECONDS);
110 
111         }
112     }
113 
getInitialSyncRetryTimeInSeconds()114     public int getInitialSyncRetryTimeInSeconds() {
115         synchronized (mLock) {
116             return mInitialSyncRetryTimeInSeconds;
117         }
118     }
119 
getRetryTimeIncreaseFactor()120     public float getRetryTimeIncreaseFactor() {
121         synchronized (mLock) {
122             return mRetryTimeIncreaseFactor;
123         }
124     }
125 
getMaxSyncRetryTimeInSeconds()126     public int getMaxSyncRetryTimeInSeconds() {
127         synchronized (mLock) {
128             return mMaxSyncRetryTimeInSeconds;
129         }
130     }
131 
getMaxRetriesWithAppStandbyExemption()132     public int getMaxRetriesWithAppStandbyExemption() {
133         synchronized (mLock) {
134             return mMaxRetriesWithAppStandbyExemption;
135         }
136     }
137 
getKeyExemptionTempWhitelistDurationInSeconds()138     public int getKeyExemptionTempWhitelistDurationInSeconds() {
139         synchronized (mLock) {
140             return mKeyExemptionTempWhitelistDurationInSeconds;
141         }
142     }
143 
dump(PrintWriter pw, String prefix)144     public void dump(PrintWriter pw, String prefix) {
145         synchronized (mLock) {
146             pw.print(prefix);
147             pw.println("SyncManager Config:");
148 
149             pw.print(prefix);
150             pw.print("  mInitialSyncRetryTimeInSeconds=");
151             pw.println(mInitialSyncRetryTimeInSeconds);
152 
153             pw.print(prefix);
154             pw.print("  mRetryTimeIncreaseFactor=");
155             pw.println(mRetryTimeIncreaseFactor);
156 
157             pw.print(prefix);
158             pw.print("  mMaxSyncRetryTimeInSeconds=");
159             pw.println(mMaxSyncRetryTimeInSeconds);
160 
161             pw.print(prefix);
162             pw.print("  mMaxRetriesWithAppStandbyExemption=");
163             pw.println(mMaxRetriesWithAppStandbyExemption);
164 
165             pw.print(prefix);
166             pw.print("  mKeyExemptionTempWhitelistDurationInSeconds=");
167             pw.println(mKeyExemptionTempWhitelistDurationInSeconds);
168         }
169     }
170 }
171