blob: 523b95edc3a02b9fb4d073e89478fecc02e47936 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 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
17package com.android.providers.settings;
18
-b master501eec92009-07-06 13:53:11 -070019import java.io.FileNotFoundException;
Doug Zongker4f8ff392010-02-03 10:36:40 -080020import java.security.SecureRandom;
Christopher Tate06efb532012-08-24 15:29:27 -070021import java.util.HashSet;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070022import java.util.concurrent.atomic.AtomicBoolean;
23import java.util.concurrent.atomic.AtomicInteger;
-b master501eec92009-07-06 13:53:11 -070024
Christopher Tated5fe1472012-09-10 15:48:38 -070025import android.app.ActivityManager;
Christopher Tate06efb532012-08-24 15:29:27 -070026import android.app.ActivityManagerNative;
Christopher Tate45281862010-03-05 15:46:30 -080027import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070028import android.content.BroadcastReceiver;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070029import android.content.ContentProvider;
30import android.content.ContentUris;
31import android.content.ContentValues;
32import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070033import android.content.Intent;
34import android.content.IntentFilter;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070035import android.content.pm.PackageManager;
Christopher Tate06efb532012-08-24 15:29:27 -070036import android.content.pm.UserInfo;
Marco Nelissen69f593c2009-07-28 09:55:04 -070037import android.content.res.AssetFileDescriptor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070038import android.database.Cursor;
39import android.database.sqlite.SQLiteDatabase;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080040import android.database.sqlite.SQLiteException;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070041import android.database.sqlite.SQLiteQueryBuilder;
Christopher Tate06efb532012-08-24 15:29:27 -070042import android.database.sqlite.SQLiteStatement;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070043import android.media.RingtoneManager;
44import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070045import android.os.Binder;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080046import android.os.Bundle;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070047import android.os.FileObserver;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070048import android.os.ParcelFileDescriptor;
Christopher Tate06efb532012-08-24 15:29:27 -070049import android.os.RemoteException;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070050import android.os.SystemProperties;
Christopher Tate06efb532012-08-24 15:29:27 -070051import android.os.UserHandle;
52import android.os.UserManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070053import android.provider.DrmStore;
54import android.provider.MediaStore;
55import android.provider.Settings;
56import android.text.TextUtils;
57import android.util.Log;
Jesse Wilson0c7faee2011-02-10 11:33:19 -080058import android.util.LruCache;
Christopher Tate06efb532012-08-24 15:29:27 -070059import android.util.Slog;
60import android.util.SparseArray;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070061
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070062public class SettingsProvider extends ContentProvider {
63 private static final String TAG = "SettingsProvider";
Christopher Tate4dc7a682012-09-11 12:15:49 -070064 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070065
Christopher Tate06efb532012-08-24 15:29:27 -070066 private static final String TABLE_SYSTEM = "system";
67 private static final String TABLE_SECURE = "secure";
68 private static final String TABLE_GLOBAL = "global";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 private static final String TABLE_FAVORITES = "favorites";
70 private static final String TABLE_OLD_FAVORITES = "old_favorites";
71
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080072 private static final String[] COLUMN_VALUE = new String[] { "value" };
73
Christopher Tate06efb532012-08-24 15:29:27 -070074 // Caches for each user's settings, access-ordered for acting as LRU.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080075 // Guarded by themselves.
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070076 private static final int MAX_CACHE_ENTRIES = 200;
Christopher Tate06efb532012-08-24 15:29:27 -070077 private static final SparseArray<SettingsCache> sSystemCaches
78 = new SparseArray<SettingsCache>();
79 private static final SparseArray<SettingsCache> sSecureCaches
80 = new SparseArray<SettingsCache>();
81 private static final SettingsCache sGlobalCache = new SettingsCache(TABLE_GLOBAL);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070082
83 // The count of how many known (handled by SettingsProvider)
Christopher Tate06efb532012-08-24 15:29:27 -070084 // database mutations are currently being handled for this user.
85 // Used by file observers to not reload the database when it's ourselves
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070086 // modifying it.
Christopher Tate06efb532012-08-24 15:29:27 -070087 private static final SparseArray<AtomicInteger> sKnownMutationsInFlight
88 = new SparseArray<AtomicInteger>();
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080089
Brad Fitzpatrick342984a2010-03-09 16:59:30 -080090 // Over this size we don't reject loading or saving settings but
91 // we do consider them broken/malicious and don't keep them in
92 // memory at least:
93 private static final int MAX_CACHE_ENTRY_SIZE = 500;
94
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080095 private static final Bundle NULL_SETTING = Bundle.forPair("value", null);
96
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070097 // Used as a sentinel value in an instance equality test when we
98 // want to cache the existence of a key, but not store its value.
99 private static final Bundle TOO_LARGE_TO_CACHE_MARKER = Bundle.forPair("_dummy", null);
100
Christopher Tate06efb532012-08-24 15:29:27 -0700101 // Each defined user has their own settings
102 protected final SparseArray<DatabaseHelper> mOpenHelpers = new SparseArray<DatabaseHelper>();
103 //protected DatabaseHelper mOpenHelper;
104 private UserManager mUserManager;
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700105 private BackupManager mBackupManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700106
107 /**
Christopher Tate06efb532012-08-24 15:29:27 -0700108 * Settings which need to be treated as global/shared in multi-user environments.
109 */
110 static final HashSet<String> sSecureGlobalKeys;
111 static final HashSet<String> sSystemGlobalKeys;
112 static {
113 // Keys (name column) from the 'secure' table that are now in the owner user's 'global'
114 // table, shared across all users
115 // These must match Settings.Secure.MOVED_TO_GLOBAL
116 sSecureGlobalKeys = new HashSet<String>();
Christopher Tate92198742012-09-07 12:00:13 -0700117 sSecureGlobalKeys.add(Settings.Secure.ADB_ENABLED);
Christopher Tate06efb532012-08-24 15:29:27 -0700118 sSecureGlobalKeys.add(Settings.Secure.ASSISTED_GPS_ENABLED);
Christopher Tate92198742012-09-07 12:00:13 -0700119 sSecureGlobalKeys.add(Settings.Secure.BLUETOOTH_ON);
Christopher Tate06efb532012-08-24 15:29:27 -0700120 sSecureGlobalKeys.add(Settings.Secure.CDMA_CELL_BROADCAST_SMS);
121 sSecureGlobalKeys.add(Settings.Secure.CDMA_ROAMING_MODE);
122 sSecureGlobalKeys.add(Settings.Secure.CDMA_SUBSCRIPTION_MODE);
123 sSecureGlobalKeys.add(Settings.Secure.DATA_ACTIVITY_TIMEOUT_MOBILE);
124 sSecureGlobalKeys.add(Settings.Secure.DATA_ACTIVITY_TIMEOUT_WIFI);
Christopher Tate92198742012-09-07 12:00:13 -0700125 sSecureGlobalKeys.add(Settings.Secure.DATA_ROAMING);
Christopher Tate06efb532012-08-24 15:29:27 -0700126 sSecureGlobalKeys.add(Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED);
Christopher Tate92198742012-09-07 12:00:13 -0700127 sSecureGlobalKeys.add(Settings.Secure.DEVICE_PROVISIONED);
Christopher Tate06efb532012-08-24 15:29:27 -0700128 sSecureGlobalKeys.add(Settings.Secure.DISPLAY_DENSITY_FORCED);
129 sSecureGlobalKeys.add(Settings.Secure.DISPLAY_SIZE_FORCED);
130 sSecureGlobalKeys.add(Settings.Secure.DOWNLOAD_MAX_BYTES_OVER_MOBILE);
131 sSecureGlobalKeys.add(Settings.Secure.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE);
Christopher Tate92198742012-09-07 12:00:13 -0700132 sSecureGlobalKeys.add(Settings.Secure.INSTALL_NON_MARKET_APPS);
Christopher Tate06efb532012-08-24 15:29:27 -0700133 sSecureGlobalKeys.add(Settings.Secure.MOBILE_DATA);
134 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_DEV_BUCKET_DURATION);
135 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_DEV_DELETE_AGE);
136 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_DEV_PERSIST_BYTES);
137 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_DEV_ROTATE_AGE);
138 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_ENABLED);
139 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_GLOBAL_ALERT_BYTES);
140 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_POLL_INTERVAL);
141 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_REPORT_XT_OVER_DEV);
142 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_SAMPLE_ENABLED);
143 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_TIME_CACHE_MAX_AGE);
144 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_UID_BUCKET_DURATION);
145 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_UID_DELETE_AGE);
146 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_UID_PERSIST_BYTES);
147 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_UID_ROTATE_AGE);
148 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_UID_TAG_BUCKET_DURATION);
149 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_UID_TAG_DELETE_AGE);
150 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_UID_TAG_PERSIST_BYTES);
151 sSecureGlobalKeys.add(Settings.Secure.NETSTATS_UID_TAG_ROTATE_AGE);
152 sSecureGlobalKeys.add(Settings.Secure.NETWORK_PREFERENCE);
153 sSecureGlobalKeys.add(Settings.Secure.NITZ_UPDATE_DIFF);
154 sSecureGlobalKeys.add(Settings.Secure.NITZ_UPDATE_SPACING);
155 sSecureGlobalKeys.add(Settings.Secure.NTP_SERVER);
156 sSecureGlobalKeys.add(Settings.Secure.NTP_TIMEOUT);
157 sSecureGlobalKeys.add(Settings.Secure.PDP_WATCHDOG_ERROR_POLL_COUNT);
158 sSecureGlobalKeys.add(Settings.Secure.PDP_WATCHDOG_LONG_POLL_INTERVAL_MS);
159 sSecureGlobalKeys.add(Settings.Secure.PDP_WATCHDOG_MAX_PDP_RESET_FAIL_COUNT);
160 sSecureGlobalKeys.add(Settings.Secure.PDP_WATCHDOG_POLL_INTERVAL_MS);
161 sSecureGlobalKeys.add(Settings.Secure.PDP_WATCHDOG_TRIGGER_PACKET_COUNT);
162 sSecureGlobalKeys.add(Settings.Secure.SAMPLING_PROFILER_MS);
163 sSecureGlobalKeys.add(Settings.Secure.SETUP_PREPAID_DATA_SERVICE_URL);
164 sSecureGlobalKeys.add(Settings.Secure.SETUP_PREPAID_DETECTION_REDIR_HOST);
165 sSecureGlobalKeys.add(Settings.Secure.SETUP_PREPAID_DETECTION_TARGET_URL);
166 sSecureGlobalKeys.add(Settings.Secure.TETHER_DUN_APN);
167 sSecureGlobalKeys.add(Settings.Secure.TETHER_DUN_REQUIRED);
168 sSecureGlobalKeys.add(Settings.Secure.TETHER_SUPPORTED);
169 sSecureGlobalKeys.add(Settings.Secure.THROTTLE_HELP_URI);
170 sSecureGlobalKeys.add(Settings.Secure.THROTTLE_MAX_NTP_CACHE_AGE_SEC);
171 sSecureGlobalKeys.add(Settings.Secure.THROTTLE_NOTIFICATION_TYPE);
172 sSecureGlobalKeys.add(Settings.Secure.THROTTLE_POLLING_SEC);
173 sSecureGlobalKeys.add(Settings.Secure.THROTTLE_RESET_DAY);
174 sSecureGlobalKeys.add(Settings.Secure.THROTTLE_THRESHOLD_BYTES);
175 sSecureGlobalKeys.add(Settings.Secure.THROTTLE_VALUE_KBITSPS);
Christopher Tate92198742012-09-07 12:00:13 -0700176 sSecureGlobalKeys.add(Settings.Secure.USB_MASS_STORAGE_ENABLED);
Christopher Tate06efb532012-08-24 15:29:27 -0700177 sSecureGlobalKeys.add(Settings.Secure.USE_GOOGLE_MAIL);
178 sSecureGlobalKeys.add(Settings.Secure.WEB_AUTOFILL_QUERY_URL);
179 sSecureGlobalKeys.add(Settings.Secure.WIFI_COUNTRY_CODE);
180 sSecureGlobalKeys.add(Settings.Secure.WIFI_FRAMEWORK_SCAN_INTERVAL_MS);
181 sSecureGlobalKeys.add(Settings.Secure.WIFI_FREQUENCY_BAND);
182 sSecureGlobalKeys.add(Settings.Secure.WIFI_IDLE_MS);
183 sSecureGlobalKeys.add(Settings.Secure.WIFI_MAX_DHCP_RETRY_COUNT);
184 sSecureGlobalKeys.add(Settings.Secure.WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS);
185 sSecureGlobalKeys.add(Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON);
186 sSecureGlobalKeys.add(Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY);
187 sSecureGlobalKeys.add(Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT);
188 sSecureGlobalKeys.add(Settings.Secure.WIFI_ON);
189 sSecureGlobalKeys.add(Settings.Secure.WIFI_P2P_DEVICE_NAME);
190 sSecureGlobalKeys.add(Settings.Secure.WIFI_SAVED_STATE);
191 sSecureGlobalKeys.add(Settings.Secure.WIFI_SUPPLICANT_SCAN_INTERVAL_MS);
192 sSecureGlobalKeys.add(Settings.Secure.WIFI_SUSPEND_OPTIMIZATIONS_ENABLED);
193 sSecureGlobalKeys.add(Settings.Secure.WIFI_WATCHDOG_NUM_ARP_PINGS);
194 sSecureGlobalKeys.add(Settings.Secure.WIFI_WATCHDOG_ON);
195 sSecureGlobalKeys.add(Settings.Secure.WIFI_WATCHDOG_POOR_NETWORK_TEST_ENABLED);
196 sSecureGlobalKeys.add(Settings.Secure.WIFI_WATCHDOG_RSSI_FETCH_INTERVAL_MS);
197 sSecureGlobalKeys.add(Settings.Secure.WIMAX_NETWORKS_AVAILABLE_NOTIFICATION_ON);
198 sSecureGlobalKeys.add(Settings.Secure.WTF_IS_FATAL);
199
200 // Keys from the 'system' table now moved to 'global'
201 // These must match Settings.System.MOVED_TO_GLOBAL
202 sSystemGlobalKeys = new HashSet<String>();
Christopher Tate06efb532012-08-24 15:29:27 -0700203
204 sSystemGlobalKeys.add(Settings.System.AIRPLANE_MODE_ON);
205 sSystemGlobalKeys.add(Settings.System.AIRPLANE_MODE_RADIOS);
206 sSystemGlobalKeys.add(Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
207 sSystemGlobalKeys.add(Settings.System.AUTO_TIME);
208 sSystemGlobalKeys.add(Settings.System.AUTO_TIME_ZONE);
209 sSystemGlobalKeys.add(Settings.System.CAR_DOCK_SOUND);
210 sSystemGlobalKeys.add(Settings.System.CAR_UNDOCK_SOUND);
211 sSystemGlobalKeys.add(Settings.System.DESK_DOCK_SOUND);
212 sSystemGlobalKeys.add(Settings.System.DESK_UNDOCK_SOUND);
213 sSystemGlobalKeys.add(Settings.System.DOCK_SOUNDS_ENABLED);
214 sSystemGlobalKeys.add(Settings.System.LOCK_SOUND);
215 sSystemGlobalKeys.add(Settings.System.UNLOCK_SOUND);
216 sSystemGlobalKeys.add(Settings.System.LOW_BATTERY_SOUND);
217 sSystemGlobalKeys.add(Settings.System.POWER_SOUNDS_ENABLED);
Christopher Tate92198742012-09-07 12:00:13 -0700218 sSystemGlobalKeys.add(Settings.System.STAY_ON_WHILE_PLUGGED_IN);
Christopher Tate06efb532012-08-24 15:29:27 -0700219 sSystemGlobalKeys.add(Settings.System.WIFI_SLEEP_POLICY);
220 }
221
222 private boolean settingMovedToGlobal(final String name) {
223 return sSecureGlobalKeys.contains(name) || sSystemGlobalKeys.contains(name);
224 }
225
226 /**
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700227 * Decode a content URL into the table, projection, and arguments
228 * used to access the corresponding database rows.
229 */
230 private static class SqlArguments {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 public String table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700232 public final String where;
233 public final String[] args;
234
235 /** Operate on existing rows. */
236 SqlArguments(Uri url, String where, String[] args) {
237 if (url.getPathSegments().size() == 1) {
238 this.table = url.getPathSegments().get(0);
Dianne Hackborn24117ce2010-07-12 15:54:38 -0700239 if (!DatabaseHelper.isValidTable(this.table)) {
240 throw new IllegalArgumentException("Bad root path: " + this.table);
241 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700242 this.where = where;
243 this.args = args;
244 } else if (url.getPathSegments().size() != 2) {
245 throw new IllegalArgumentException("Invalid URI: " + url);
246 } else if (!TextUtils.isEmpty(where)) {
247 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
248 } else {
249 this.table = url.getPathSegments().get(0);
Dianne Hackborn24117ce2010-07-12 15:54:38 -0700250 if (!DatabaseHelper.isValidTable(this.table)) {
251 throw new IllegalArgumentException("Bad root path: " + this.table);
252 }
Christopher Tate06efb532012-08-24 15:29:27 -0700253 if (TABLE_SYSTEM.equals(this.table) || TABLE_SECURE.equals(this.table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700254 this.where = Settings.NameValueTable.NAME + "=?";
255 this.args = new String[] { url.getPathSegments().get(1) };
256 } else {
257 this.where = "_id=" + ContentUris.parseId(url);
258 this.args = null;
259 }
260 }
261 }
262
263 /** Insert new rows (no where clause allowed). */
264 SqlArguments(Uri url) {
265 if (url.getPathSegments().size() == 1) {
266 this.table = url.getPathSegments().get(0);
Dianne Hackborn24117ce2010-07-12 15:54:38 -0700267 if (!DatabaseHelper.isValidTable(this.table)) {
268 throw new IllegalArgumentException("Bad root path: " + this.table);
269 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700270 this.where = null;
271 this.args = null;
272 } else {
273 throw new IllegalArgumentException("Invalid URI: " + url);
274 }
275 }
276 }
277
278 /**
279 * Get the content URI of a row added to a table.
280 * @param tableUri of the entire table
281 * @param values found in the row
282 * @param rowId of the row
283 * @return the content URI for this particular row
284 */
285 private Uri getUriFor(Uri tableUri, ContentValues values, long rowId) {
286 if (tableUri.getPathSegments().size() != 1) {
287 throw new IllegalArgumentException("Invalid URI: " + tableUri);
288 }
289 String table = tableUri.getPathSegments().get(0);
Christopher Tate06efb532012-08-24 15:29:27 -0700290 if (TABLE_SYSTEM.equals(table) ||
291 TABLE_SECURE.equals(table) ||
292 TABLE_GLOBAL.equals(table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700293 String name = values.getAsString(Settings.NameValueTable.NAME);
294 return Uri.withAppendedPath(tableUri, name);
295 } else {
296 return ContentUris.withAppendedId(tableUri, rowId);
297 }
298 }
299
300 /**
301 * Send a notification when a particular content URI changes.
302 * Modify the system property used to communicate the version of
303 * this table, for tables which have such a property. (The Settings
304 * contract class uses these to provide client-side caches.)
305 * @param uri to send notifications for
306 */
Christopher Tate06efb532012-08-24 15:29:27 -0700307 private void sendNotify(Uri uri, int userHandle) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700308 // Update the system property *first*, so if someone is listening for
309 // a notification and then using the contract class to get their data,
310 // the system property will be updated and they'll get the new data.
311
Amith Yamasanid1582142009-07-08 20:04:55 -0700312 boolean backedUpDataChanged = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700313 String property = null, table = uri.getPathSegments().get(0);
Christopher Tate06efb532012-08-24 15:29:27 -0700314 if (table.equals(TABLE_SYSTEM)) {
315 property = Settings.System.SYS_PROP_SETTING_VERSION + '_' + userHandle;
Amith Yamasanid1582142009-07-08 20:04:55 -0700316 backedUpDataChanged = true;
Christopher Tate06efb532012-08-24 15:29:27 -0700317 } else if (table.equals(TABLE_SECURE)) {
318 property = Settings.Secure.SYS_PROP_SETTING_VERSION + '_' + userHandle;
319 backedUpDataChanged = true;
320 } else if (table.equals(TABLE_GLOBAL)) {
321 property = Settings.Global.SYS_PROP_SETTING_VERSION; // this one is global
Amith Yamasanid1582142009-07-08 20:04:55 -0700322 backedUpDataChanged = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700323 }
324
325 if (property != null) {
326 long version = SystemProperties.getLong(property, 0) + 1;
327 if (LOCAL_LOGV) Log.v(TAG, "property: " + property + "=" + version);
328 SystemProperties.set(property, Long.toString(version));
329 }
330
-b master501eec92009-07-06 13:53:11 -0700331 // Inform the backup manager about a data change
Amith Yamasanid1582142009-07-08 20:04:55 -0700332 if (backedUpDataChanged) {
333 mBackupManager.dataChanged();
334 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700335 // Now send the notification through the content framework.
336
337 String notify = uri.getQueryParameter("notify");
338 if (notify == null || "true".equals(notify)) {
339 getContext().getContentResolver().notifyChange(uri, null);
340 if (LOCAL_LOGV) Log.v(TAG, "notifying: " + uri);
341 } else {
342 if (LOCAL_LOGV) Log.v(TAG, "notification suppressed: " + uri);
343 }
344 }
345
346 /**
347 * Make sure the caller has permission to write this data.
348 * @param args supplied by the caller
349 * @throws SecurityException if the caller is forbidden to write.
350 */
351 private void checkWritePermissions(SqlArguments args) {
Christopher Tate06efb532012-08-24 15:29:27 -0700352 if ((TABLE_SECURE.equals(args.table) || TABLE_GLOBAL.equals(args.table)) &&
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800353 getContext().checkCallingOrSelfPermission(
Doug Zongkeraed8f8e2010-01-07 18:07:50 -0800354 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
355 PackageManager.PERMISSION_GRANTED) {
Brett Chabot16dd82c2009-06-18 17:00:48 -0700356 throw new SecurityException(
Doug Zongkeraed8f8e2010-01-07 18:07:50 -0800357 String.format("Permission denial: writing to secure settings requires %1$s",
358 android.Manifest.permission.WRITE_SECURE_SETTINGS));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700359 }
360 }
361
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700362 // FileObserver for external modifications to the database file.
363 // Note that this is for platform developers only with
364 // userdebug/eng builds who should be able to tinker with the
365 // sqlite database out from under the SettingsProvider, which is
366 // normally the exclusive owner of the database. But we keep this
367 // enabled all the time to minimize development-vs-user
368 // differences in testing.
Christopher Tate06efb532012-08-24 15:29:27 -0700369 private static SparseArray<SettingsFileObserver> sObserverInstances
370 = new SparseArray<SettingsFileObserver>();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700371 private class SettingsFileObserver extends FileObserver {
372 private final AtomicBoolean mIsDirty = new AtomicBoolean(false);
Christopher Tate06efb532012-08-24 15:29:27 -0700373 private final int mUserHandle;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700374 private final String mPath;
375
Christopher Tate06efb532012-08-24 15:29:27 -0700376 public SettingsFileObserver(int userHandle, String path) {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700377 super(path, FileObserver.CLOSE_WRITE |
378 FileObserver.CREATE | FileObserver.DELETE |
379 FileObserver.MOVED_TO | FileObserver.MODIFY);
Christopher Tate06efb532012-08-24 15:29:27 -0700380 mUserHandle = userHandle;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700381 mPath = path;
382 }
383
384 public void onEvent(int event, String path) {
Christopher Tate06efb532012-08-24 15:29:27 -0700385 int modsInFlight = sKnownMutationsInFlight.get(mUserHandle).get();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700386 if (modsInFlight > 0) {
387 // our own modification.
388 return;
389 }
Christopher Tate06efb532012-08-24 15:29:27 -0700390 Log.d(TAG, "User " + mUserHandle + " external modification to " + mPath
391 + "; event=" + event);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700392 if (!mIsDirty.compareAndSet(false, true)) {
393 // already handled. (we get a few update events
394 // during an sqlite write)
395 return;
396 }
Christopher Tate06efb532012-08-24 15:29:27 -0700397 Log.d(TAG, "User " + mUserHandle + " updating our caches for " + mPath);
398 fullyPopulateCaches(mUserHandle);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700399 mIsDirty.set(false);
400 }
401 }
402
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700403 @Override
404 public boolean onCreate() {
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700405 mBackupManager = new BackupManager(getContext());
Christopher Tate06efb532012-08-24 15:29:27 -0700406 mUserManager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
Fred Quintanac70239e2009-12-17 10:28:33 -0800407
Christopher Tate06efb532012-08-24 15:29:27 -0700408 synchronized (this) {
409 establishDbTrackingLocked(UserHandle.USER_OWNER);
410
411 IntentFilter userFilter = new IntentFilter();
412 userFilter.addAction(Intent.ACTION_USER_REMOVED);
413 getContext().registerReceiver(new BroadcastReceiver() {
414 @Override
415 public void onReceive(Context context, Intent intent) {
416 if (intent.getAction().equals(Intent.ACTION_USER_REMOVED)) {
417 final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
418 UserHandle.USER_OWNER);
419 if (userHandle != UserHandle.USER_OWNER) {
420 onUserRemoved(userHandle);
421 }
422 }
423 }
424 }, userFilter);
Fred Quintanac70239e2009-12-17 10:28:33 -0800425 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700426 return true;
427 }
428
Christopher Tate06efb532012-08-24 15:29:27 -0700429 void onUserRemoved(int userHandle) {
430 // the db file itself will be deleted automatically, but we need to tear down
431 // our caches and other internal bookkeeping. Creation/deletion of a user's
432 // settings db infrastructure is synchronized on 'this'
433 synchronized (this) {
434 FileObserver observer = sObserverInstances.get(userHandle);
435 if (observer != null) {
436 observer.stopWatching();
437 sObserverInstances.delete(userHandle);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700438 }
Christopher Tate06efb532012-08-24 15:29:27 -0700439
440 mOpenHelpers.delete(userHandle);
441 sSystemCaches.delete(userHandle);
442 sSecureCaches.delete(userHandle);
443 sKnownMutationsInFlight.delete(userHandle);
444
445 String property = Settings.System.SYS_PROP_SETTING_VERSION + '_' + userHandle;
446 SystemProperties.set(property, "");
447 property = Settings.Secure.SYS_PROP_SETTING_VERSION + '_' + userHandle;
448 SystemProperties.set(property, "");
449 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700450 }
451
Christopher Tate06efb532012-08-24 15:29:27 -0700452 private void establishDbTrackingLocked(int userHandle) {
453 if (LOCAL_LOGV) {
454 Slog.i(TAG, "Installing settings db helper and caches for user " + userHandle);
455 }
456
457 DatabaseHelper dbhelper = new DatabaseHelper(getContext(), userHandle);
458 mOpenHelpers.append(userHandle, dbhelper);
459
460 // Watch for external modifications to the database files,
461 // keeping our caches in sync.
462 sSystemCaches.append(userHandle, new SettingsCache(TABLE_SYSTEM));
463 sSecureCaches.append(userHandle, new SettingsCache(TABLE_SECURE));
464 sKnownMutationsInFlight.append(userHandle, new AtomicInteger(0));
465 SQLiteDatabase db = dbhelper.getWritableDatabase();
466
467 // Now we can start observing it for changes
468 SettingsFileObserver observer = new SettingsFileObserver(userHandle, db.getPath());
469 sObserverInstances.append(userHandle, observer);
470 observer.startWatching();
471
Christopher Tate4dc7a682012-09-11 12:15:49 -0700472 ensureAndroidIdIsSet(userHandle);
473
Christopher Tate06efb532012-08-24 15:29:27 -0700474 startAsyncCachePopulation(userHandle);
475 }
476
477 class CachePrefetchThread extends Thread {
478 private int mUserHandle;
479
480 CachePrefetchThread(int userHandle) {
481 super("populate-settings-caches");
482 mUserHandle = userHandle;
483 }
484
485 @Override
486 public void run() {
487 fullyPopulateCaches(mUserHandle);
488 }
489 }
490
491 private void startAsyncCachePopulation(int userHandle) {
492 new CachePrefetchThread(userHandle).start();
493 }
494
495 private void fullyPopulateCaches(final int userHandle) {
496 DatabaseHelper dbHelper = mOpenHelpers.get(userHandle);
497 // Only populate the globals cache once, for the owning user
498 if (userHandle == UserHandle.USER_OWNER) {
499 fullyPopulateCache(dbHelper, TABLE_GLOBAL, sGlobalCache);
500 }
501 fullyPopulateCache(dbHelper, TABLE_SECURE, sSecureCaches.get(userHandle));
502 fullyPopulateCache(dbHelper, TABLE_SYSTEM, sSystemCaches.get(userHandle));
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700503 }
504
505 // Slurp all values (if sane in number & size) into cache.
Christopher Tate06efb532012-08-24 15:29:27 -0700506 private void fullyPopulateCache(DatabaseHelper dbHelper, String table, SettingsCache cache) {
507 SQLiteDatabase db = dbHelper.getReadableDatabase();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700508 Cursor c = db.query(
509 table,
510 new String[] { Settings.NameValueTable.NAME, Settings.NameValueTable.VALUE },
511 null, null, null, null, null,
512 "" + (MAX_CACHE_ENTRIES + 1) /* limit */);
513 try {
514 synchronized (cache) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -0800515 cache.evictAll();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700516 cache.setFullyMatchesDisk(true); // optimistic
517 int rows = 0;
518 while (c.moveToNext()) {
519 rows++;
520 String name = c.getString(0);
521 String value = c.getString(1);
522 cache.populate(name, value);
523 }
524 if (rows > MAX_CACHE_ENTRIES) {
525 // Somewhat redundant, as removeEldestEntry() will
526 // have already done this, but to be explicit:
527 cache.setFullyMatchesDisk(false);
528 Log.d(TAG, "row count exceeds max cache entries for table " + table);
529 }
Brad Fitzpatrick3a2952b2010-08-26 17:16:14 -0700530 Log.d(TAG, "cache for settings table '" + table + "' rows=" + rows + "; fullycached=" +
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700531 cache.fullyMatchesDisk());
532 }
533 } finally {
534 c.close();
535 }
536 }
537
Christopher Tate4dc7a682012-09-11 12:15:49 -0700538 private boolean ensureAndroidIdIsSet(int userHandle) {
539 final Cursor c = queryForUser(Settings.Secure.CONTENT_URI,
Fred Quintanac70239e2009-12-17 10:28:33 -0800540 new String[] { Settings.NameValueTable.VALUE },
541 Settings.NameValueTable.NAME + "=?",
Christopher Tate4dc7a682012-09-11 12:15:49 -0700542 new String[] { Settings.Secure.ANDROID_ID }, null,
543 userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800544 try {
545 final String value = c.moveToNext() ? c.getString(0) : null;
546 if (value == null) {
Nick Kralevich9bb4ec42010-09-24 11:48:37 -0700547 final SecureRandom random = new SecureRandom();
Fred Quintanac70239e2009-12-17 10:28:33 -0800548 final String newAndroidIdValue = Long.toHexString(random.nextLong());
Fred Quintanac70239e2009-12-17 10:28:33 -0800549 final ContentValues values = new ContentValues();
550 values.put(Settings.NameValueTable.NAME, Settings.Secure.ANDROID_ID);
551 values.put(Settings.NameValueTable.VALUE, newAndroidIdValue);
Christopher Tate4dc7a682012-09-11 12:15:49 -0700552 final Uri uri = insertForUser(Settings.Secure.CONTENT_URI, values, userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800553 if (uri == null) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700554 Slog.e(TAG, "Unable to generate new ANDROID_ID for user " + userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800555 return false;
556 }
Christopher Tate4dc7a682012-09-11 12:15:49 -0700557 Slog.d(TAG, "Generated and saved new ANDROID_ID [" + newAndroidIdValue
558 + "] for user " + userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800559 }
560 return true;
Fred Quintanac70239e2009-12-17 10:28:33 -0800561 } finally {
562 c.close();
563 }
564 }
565
Christopher Tate06efb532012-08-24 15:29:27 -0700566 // Lazy-initialize the settings caches for non-primary users
567 private SettingsCache getOrConstructCache(int callingUser, SparseArray<SettingsCache> which) {
568 synchronized (this) {
569 getOrEstablishDatabaseLocked(callingUser); // ignore return value; we don't need it
570 return which.get(callingUser);
571 }
572 }
573
574 // Lazy initialize the database helper and caches for this user, if necessary
575 private DatabaseHelper getOrEstablishDatabaseLocked(int callingUser) {
576 long oldId = Binder.clearCallingIdentity();
577 try {
578 DatabaseHelper dbHelper = mOpenHelpers.get(callingUser);
579 if (null == dbHelper) {
580 establishDbTrackingLocked(callingUser);
581 dbHelper = mOpenHelpers.get(callingUser);
582 }
583 return dbHelper;
584 } finally {
585 Binder.restoreCallingIdentity(oldId);
586 }
587 }
588
589 public SettingsCache cacheForTable(final int callingUser, String tableName) {
590 if (TABLE_SYSTEM.equals(tableName)) {
591 return getOrConstructCache(callingUser, sSystemCaches);
592 }
593 if (TABLE_SECURE.equals(tableName)) {
594 return getOrConstructCache(callingUser, sSecureCaches);
595 }
596 if (TABLE_GLOBAL.equals(tableName)) {
597 return sGlobalCache;
598 }
599 return null;
600 }
601
602 /**
603 * Used for wiping a whole cache on deletes when we're not
604 * sure what exactly was deleted or changed.
605 */
606 public void invalidateCache(final int callingUser, String tableName) {
607 SettingsCache cache = cacheForTable(callingUser, tableName);
608 if (cache == null) {
609 return;
610 }
611 synchronized (cache) {
612 cache.evictAll();
613 cache.mCacheFullyMatchesDisk = false;
614 }
615 }
616
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800617 /**
618 * Fast path that avoids the use of chatty remoted Cursors.
619 */
620 @Override
621 public Bundle call(String method, String request, Bundle args) {
Christopher Tate06efb532012-08-24 15:29:27 -0700622 int callingUser = UserHandle.getCallingUserId();
623 if (args != null) {
624 int reqUser = args.getInt(Settings.CALL_METHOD_USER_KEY, callingUser);
625 if (reqUser != callingUser) {
Christopher Tated5fe1472012-09-10 15:48:38 -0700626 callingUser = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
627 Binder.getCallingUid(), reqUser, false, true,
628 "get/set setting for user", null);
629 if (LOCAL_LOGV) Slog.v(TAG, " access setting for user " + callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700630 }
631 }
632
633 // Note: we assume that get/put operations for moved-to-global names have already
634 // been directed to the new location on the caller side (otherwise we'd fix them
635 // up here).
636
637 DatabaseHelper dbHelper;
638 SettingsCache cache;
639
640 // Get methods
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800641 if (Settings.CALL_METHOD_GET_SYSTEM.equals(method)) {
Christopher Tate06efb532012-08-24 15:29:27 -0700642 if (LOCAL_LOGV) Slog.v(TAG, "call(system:" + request + ") for " + callingUser);
643 synchronized (this) {
644 dbHelper = getOrEstablishDatabaseLocked(callingUser);
645 cache = sSystemCaches.get(callingUser);
646 }
647 return lookupValue(dbHelper, TABLE_SYSTEM, cache, request);
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800648 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800649 if (Settings.CALL_METHOD_GET_SECURE.equals(method)) {
Christopher Tate06efb532012-08-24 15:29:27 -0700650 if (LOCAL_LOGV) Slog.v(TAG, "call(secure:" + request + ") for " + callingUser);
651 synchronized (this) {
652 dbHelper = getOrEstablishDatabaseLocked(callingUser);
653 cache = sSecureCaches.get(callingUser);
654 }
655 return lookupValue(dbHelper, TABLE_SECURE, cache, request);
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800656 }
Christopher Tate06efb532012-08-24 15:29:27 -0700657 if (Settings.CALL_METHOD_GET_GLOBAL.equals(method)) {
658 if (LOCAL_LOGV) Slog.v(TAG, "call(global:" + request + ") for " + callingUser);
659 // fast path: owner db & cache are immutable after onCreate() so we need not
660 // guard on the attempt to look them up
661 return lookupValue(getOrEstablishDatabaseLocked(UserHandle.USER_OWNER), TABLE_GLOBAL,
662 sGlobalCache, request);
663 }
664
665 // Put methods - new value is in the args bundle under the key named by
666 // the Settings.NameValueTable.VALUE static.
667 final String newValue = (args == null)
668 ? null : args.getString(Settings.NameValueTable.VALUE);
Christopher Tate06efb532012-08-24 15:29:27 -0700669
670 final ContentValues values = new ContentValues();
671 values.put(Settings.NameValueTable.NAME, request);
672 values.put(Settings.NameValueTable.VALUE, newValue);
673 if (Settings.CALL_METHOD_PUT_SYSTEM.equals(method)) {
674 if (LOCAL_LOGV) Slog.v(TAG, "call_put(system:" + request + "=" + newValue + ") for " + callingUser);
675 insert(Settings.System.CONTENT_URI, values);
676 } else if (Settings.CALL_METHOD_PUT_SECURE.equals(method)) {
677 if (LOCAL_LOGV) Slog.v(TAG, "call_put(secure:" + request + "=" + newValue + ") for " + callingUser);
678 insert(Settings.Secure.CONTENT_URI, values);
679 } else if (Settings.CALL_METHOD_PUT_GLOBAL.equals(method)) {
680 if (LOCAL_LOGV) Slog.v(TAG, "call_put(global:" + request + "=" + newValue + ") for " + callingUser);
681 insert(Settings.Global.CONTENT_URI, values);
682 } else {
683 Slog.w(TAG, "call() with invalid method: " + method);
684 }
685
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800686 return null;
687 }
688
689 // Looks up value 'key' in 'table' and returns either a single-pair Bundle,
690 // possibly with a null value, or null on failure.
Christopher Tate06efb532012-08-24 15:29:27 -0700691 private Bundle lookupValue(DatabaseHelper dbHelper, String table,
692 final SettingsCache cache, String key) {
693 if (cache == null) {
694 Slog.e(TAG, "cache is null for user " + UserHandle.getCallingUserId() + " : key=" + key);
695 return null;
696 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800697 synchronized (cache) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -0800698 Bundle value = cache.get(key);
699 if (value != null) {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700700 if (value != TOO_LARGE_TO_CACHE_MARKER) {
701 return value;
702 }
703 // else we fall through and read the value from disk
704 } else if (cache.fullyMatchesDisk()) {
705 // Fast path (very common). Don't even try touch disk
706 // if we know we've slurped it all in. Trying to
707 // touch the disk would mean waiting for yaffs2 to
708 // give us access, which could takes hundreds of
709 // milliseconds. And we're very likely being called
710 // from somebody's UI thread...
711 return NULL_SETTING;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800712 }
713 }
714
Christopher Tate06efb532012-08-24 15:29:27 -0700715 SQLiteDatabase db = dbHelper.getReadableDatabase();
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800716 Cursor cursor = null;
717 try {
718 cursor = db.query(table, COLUMN_VALUE, "name=?", new String[]{key},
719 null, null, null, null);
720 if (cursor != null && cursor.getCount() == 1) {
721 cursor.moveToFirst();
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800722 return cache.putIfAbsent(key, cursor.getString(0));
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800723 }
724 } catch (SQLiteException e) {
725 Log.w(TAG, "settings lookup error", e);
726 return null;
727 } finally {
728 if (cursor != null) cursor.close();
729 }
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800730 cache.putIfAbsent(key, null);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800731 return NULL_SETTING;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800732 }
733
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700734 @Override
735 public Cursor query(Uri url, String[] select, String where, String[] whereArgs, String sort) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700736 return queryForUser(url, select, where, whereArgs, sort, UserHandle.getCallingUserId());
737 }
738
739 public Cursor queryForUser(Uri url, String[] select, String where, String[] whereArgs,
740 String sort, int forUser) {
741 if (LOCAL_LOGV) Slog.v(TAG, "query(" + url + ") for user " + forUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700742 SqlArguments args = new SqlArguments(url, where, whereArgs);
Christopher Tate06efb532012-08-24 15:29:27 -0700743 DatabaseHelper dbH;
744 synchronized (this) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700745 dbH = getOrEstablishDatabaseLocked(
746 TABLE_GLOBAL.equals(args.table) ? UserHandle.USER_OWNER : forUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700747 }
748 SQLiteDatabase db = dbH.getReadableDatabase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800750 // The favorites table was moved from this provider to a provider inside Home
751 // Home still need to query this table to upgrade from pre-cupcake builds
752 // However, a cupcake+ build with no data does not contain this table which will
753 // cause an exception in the SQL stack. The following line is a special case to
754 // let the caller of the query have a chance to recover and avoid the exception
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 if (TABLE_FAVORITES.equals(args.table)) {
756 return null;
757 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
758 args.table = TABLE_FAVORITES;
759 Cursor cursor = db.rawQuery("PRAGMA table_info(favorites);", null);
760 if (cursor != null) {
761 boolean exists = cursor.getCount() > 0;
762 cursor.close();
763 if (!exists) return null;
764 } else {
765 return null;
766 }
767 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800768
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700769 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
770 qb.setTables(args.table);
771
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700772 Cursor ret = qb.query(db, select, args.where, args.args, null, null, sort);
773 ret.setNotificationUri(getContext().getContentResolver(), url);
774 return ret;
775 }
776
777 @Override
778 public String getType(Uri url) {
779 // If SqlArguments supplies a where clause, then it must be an item
780 // (because we aren't supplying our own where clause).
781 SqlArguments args = new SqlArguments(url, null, null);
782 if (TextUtils.isEmpty(args.where)) {
783 return "vnd.android.cursor.dir/" + args.table;
784 } else {
785 return "vnd.android.cursor.item/" + args.table;
786 }
787 }
788
789 @Override
790 public int bulkInsert(Uri uri, ContentValues[] values) {
Christopher Tate06efb532012-08-24 15:29:27 -0700791 final int callingUser = UserHandle.getCallingUserId();
792 if (LOCAL_LOGV) Slog.v(TAG, "bulkInsert() for user " + callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700793 SqlArguments args = new SqlArguments(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 if (TABLE_FAVORITES.equals(args.table)) {
795 return 0;
796 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700797 checkWritePermissions(args);
Christopher Tate06efb532012-08-24 15:29:27 -0700798 SettingsCache cache = cacheForTable(callingUser, args.table);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700799
Christopher Tate06efb532012-08-24 15:29:27 -0700800 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
801 mutationCount.incrementAndGet();
802 DatabaseHelper dbH;
803 synchronized (this) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700804 dbH = getOrEstablishDatabaseLocked(
805 TABLE_GLOBAL.equals(args.table) ? UserHandle.USER_OWNER : callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700806 }
807 SQLiteDatabase db = dbH.getWritableDatabase();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700808 db.beginTransaction();
809 try {
810 int numValues = values.length;
811 for (int i = 0; i < numValues; i++) {
812 if (db.insert(args.table, null, values[i]) < 0) return 0;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800813 SettingsCache.populate(cache, values[i]);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700814 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);
815 }
816 db.setTransactionSuccessful();
817 } finally {
818 db.endTransaction();
Christopher Tate06efb532012-08-24 15:29:27 -0700819 mutationCount.decrementAndGet();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700820 }
821
Christopher Tate06efb532012-08-24 15:29:27 -0700822 sendNotify(uri, callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700823 return values.length;
824 }
825
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700826 /*
827 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
828 * This setting contains a list of the currently enabled location providers.
829 * But helper functions in android.providers.Settings can enable or disable
830 * a single provider by using a "+" or "-" prefix before the provider name.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800831 *
832 * @returns whether the database needs to be updated or not, also modifying
833 * 'initialValues' if needed.
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700834 */
835 private boolean parseProviderList(Uri url, ContentValues initialValues) {
836 String value = initialValues.getAsString(Settings.Secure.VALUE);
837 String newProviders = null;
838 if (value != null && value.length() > 1) {
839 char prefix = value.charAt(0);
840 if (prefix == '+' || prefix == '-') {
841 // skip prefix
842 value = value.substring(1);
843
844 // read list of enabled providers into "providers"
845 String providers = "";
846 String[] columns = {Settings.Secure.VALUE};
847 String where = Settings.Secure.NAME + "=\'" + Settings.Secure.LOCATION_PROVIDERS_ALLOWED + "\'";
848 Cursor cursor = query(url, columns, where, null, null);
849 if (cursor != null && cursor.getCount() == 1) {
850 try {
851 cursor.moveToFirst();
852 providers = cursor.getString(0);
853 } finally {
854 cursor.close();
855 }
856 }
857
858 int index = providers.indexOf(value);
859 int end = index + value.length();
860 // check for commas to avoid matching on partial string
861 if (index > 0 && providers.charAt(index - 1) != ',') index = -1;
862 if (end < providers.length() && providers.charAt(end) != ',') index = -1;
863
864 if (prefix == '+' && index < 0) {
865 // append the provider to the list if not present
866 if (providers.length() == 0) {
867 newProviders = value;
868 } else {
869 newProviders = providers + ',' + value;
870 }
871 } else if (prefix == '-' && index >= 0) {
872 // remove the provider from the list if present
Mike Lockwoodbdc7f892010-04-21 18:24:57 -0400873 // remove leading or trailing comma
874 if (index > 0) {
875 index--;
876 } else if (end < providers.length()) {
877 end++;
878 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700879
880 newProviders = providers.substring(0, index);
881 if (end < providers.length()) {
882 newProviders += providers.substring(end);
883 }
884 } else {
885 // nothing changed, so no need to update the database
886 return false;
887 }
888
889 if (newProviders != null) {
890 initialValues.put(Settings.Secure.VALUE, newProviders);
891 }
892 }
893 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800894
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700895 return true;
896 }
897
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700898 @Override
899 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate06efb532012-08-24 15:29:27 -0700900 return insertForUser(url, initialValues, UserHandle.getCallingUserId());
901 }
902
903 // Settings.put*ForUser() always winds up here, so this is where we apply
904 // policy around permission to write settings for other users.
905 private Uri insertForUser(Uri url, ContentValues initialValues, int desiredUserHandle) {
906 final int callingUser = UserHandle.getCallingUserId();
907 if (callingUser != desiredUserHandle) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700908 getContext().enforceCallingOrSelfPermission(
Christopher Tate06efb532012-08-24 15:29:27 -0700909 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
910 "Not permitted to access settings for other users");
911 }
912
913 if (LOCAL_LOGV) Slog.v(TAG, "insert(" + url + ") for user " + desiredUserHandle
914 + " by " + callingUser);
915
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700916 SqlArguments args = new SqlArguments(url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if (TABLE_FAVORITES.equals(args.table)) {
918 return null;
919 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700920 checkWritePermissions(args);
921
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700922 // Special case LOCATION_PROVIDERS_ALLOWED.
923 // Support enabling/disabling a single provider (using "+" or "-" prefix)
924 String name = initialValues.getAsString(Settings.Secure.NAME);
925 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
926 if (!parseProviderList(url, initialValues)) return null;
927 }
928
Christopher Tate06efb532012-08-24 15:29:27 -0700929 // The global table is stored under the owner, always
930 if (TABLE_GLOBAL.equals(args.table)) {
931 desiredUserHandle = UserHandle.USER_OWNER;
932 }
933
934 SettingsCache cache = cacheForTable(desiredUserHandle, args.table);
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -0800935 String value = initialValues.getAsString(Settings.NameValueTable.VALUE);
936 if (SettingsCache.isRedundantSetValue(cache, name, value)) {
937 return Uri.withAppendedPath(url, name);
938 }
939
Christopher Tate06efb532012-08-24 15:29:27 -0700940 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(desiredUserHandle);
941 mutationCount.incrementAndGet();
942 DatabaseHelper dbH;
943 synchronized (this) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700944 dbH = getOrEstablishDatabaseLocked(desiredUserHandle);
Christopher Tate06efb532012-08-24 15:29:27 -0700945 }
946 SQLiteDatabase db = dbH.getWritableDatabase();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700947 final long rowId = db.insert(args.table, null, initialValues);
Christopher Tate06efb532012-08-24 15:29:27 -0700948 mutationCount.decrementAndGet();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700949 if (rowId <= 0) return null;
950
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800951 SettingsCache.populate(cache, initialValues); // before we notify
952
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700953 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + initialValues);
Christopher Tate06efb532012-08-24 15:29:27 -0700954 // Note that we use the original url here, not the potentially-rewritten table name
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700955 url = getUriFor(url, initialValues, rowId);
Christopher Tate06efb532012-08-24 15:29:27 -0700956 sendNotify(url, desiredUserHandle);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700957 return url;
958 }
959
960 @Override
961 public int delete(Uri url, String where, String[] whereArgs) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700962 int callingUser = UserHandle.getCallingUserId();
Christopher Tate06efb532012-08-24 15:29:27 -0700963 if (LOCAL_LOGV) Slog.v(TAG, "delete() for user " + callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700964 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 if (TABLE_FAVORITES.equals(args.table)) {
966 return 0;
967 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
968 args.table = TABLE_FAVORITES;
Christopher Tate4dc7a682012-09-11 12:15:49 -0700969 } else if (TABLE_GLOBAL.equals(args.table)) {
970 callingUser = UserHandle.USER_OWNER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700972 checkWritePermissions(args);
973
Christopher Tate06efb532012-08-24 15:29:27 -0700974 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
975 mutationCount.incrementAndGet();
976 DatabaseHelper dbH;
977 synchronized (this) {
978 dbH = getOrEstablishDatabaseLocked(callingUser);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800979 }
Christopher Tate06efb532012-08-24 15:29:27 -0700980 SQLiteDatabase db = dbH.getWritableDatabase();
981 int count = db.delete(args.table, args.where, args.args);
982 mutationCount.decrementAndGet();
983 if (count > 0) {
984 invalidateCache(callingUser, args.table); // before we notify
985 sendNotify(url, callingUser);
986 }
987 startAsyncCachePopulation(callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700988 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) deleted");
989 return count;
990 }
991
992 @Override
993 public int update(Uri url, ContentValues initialValues, String where, String[] whereArgs) {
Christopher Tate06efb532012-08-24 15:29:27 -0700994 // NOTE: update() is never called by the front-end Settings API, and updates that
995 // wind up affecting rows in Secure that are globally shared will not have the
996 // intended effect (the update will be invisible to the rest of the system).
997 // This should have no practical effect, since writes to the Secure db can only
998 // be done by system code, and that code should be using the correct API up front.
Christopher Tate4dc7a682012-09-11 12:15:49 -0700999 int callingUser = UserHandle.getCallingUserId();
Christopher Tate06efb532012-08-24 15:29:27 -07001000 if (LOCAL_LOGV) Slog.v(TAG, "update() for user " + callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001001 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 if (TABLE_FAVORITES.equals(args.table)) {
1003 return 0;
Christopher Tate4dc7a682012-09-11 12:15:49 -07001004 } else if (TABLE_GLOBAL.equals(args.table)) {
1005 callingUser = UserHandle.USER_OWNER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001007 checkWritePermissions(args);
1008
Christopher Tate06efb532012-08-24 15:29:27 -07001009 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
1010 mutationCount.incrementAndGet();
1011 DatabaseHelper dbH;
1012 synchronized (this) {
1013 dbH = getOrEstablishDatabaseLocked(callingUser);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001014 }
Christopher Tate06efb532012-08-24 15:29:27 -07001015 SQLiteDatabase db = dbH.getWritableDatabase();
1016 int count = db.update(args.table, initialValues, args.where, args.args);
1017 mutationCount.decrementAndGet();
1018 if (count > 0) {
1019 invalidateCache(callingUser, args.table); // before we notify
1020 sendNotify(url, callingUser);
1021 }
1022 startAsyncCachePopulation(callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001023 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) <- " + initialValues);
1024 return count;
1025 }
1026
1027 @Override
1028 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
1029
1030 /*
1031 * When a client attempts to openFile the default ringtone or
1032 * notification setting Uri, we will proxy the call to the current
1033 * default ringtone's Uri (if it is in the DRM or media provider).
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001034 */
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001035 int ringtoneType = RingtoneManager.getDefaultType(uri);
1036 // Above call returns -1 if the Uri doesn't match a default type
1037 if (ringtoneType != -1) {
1038 Context context = getContext();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001039
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001040 // Get the current value for the default sound
1041 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001042
Marco Nelissen69f593c2009-07-28 09:55:04 -07001043 if (soundUri != null) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001044 // Only proxy the openFile call to drm or media providers
1045 String authority = soundUri.getAuthority();
1046 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
1047 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
1048
1049 if (isDrmAuthority) {
1050 try {
1051 // Check DRM access permission here, since once we
1052 // do the below call the DRM will be checking our
1053 // permission, not our caller's permission
1054 DrmStore.enforceAccessDrmPermission(context);
1055 } catch (SecurityException e) {
1056 throw new FileNotFoundException(e.getMessage());
1057 }
1058 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001059
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001060 return context.getContentResolver().openFileDescriptor(soundUri, mode);
1061 }
1062 }
1063 }
1064
1065 return super.openFile(uri, mode);
1066 }
Marco Nelissen69f593c2009-07-28 09:55:04 -07001067
1068 @Override
1069 public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
1070
1071 /*
1072 * When a client attempts to openFile the default ringtone or
1073 * notification setting Uri, we will proxy the call to the current
1074 * default ringtone's Uri (if it is in the DRM or media provider).
1075 */
1076 int ringtoneType = RingtoneManager.getDefaultType(uri);
1077 // Above call returns -1 if the Uri doesn't match a default type
1078 if (ringtoneType != -1) {
1079 Context context = getContext();
1080
1081 // Get the current value for the default sound
1082 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
1083
1084 if (soundUri != null) {
1085 // Only proxy the openFile call to drm or media providers
1086 String authority = soundUri.getAuthority();
1087 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
1088 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
1089
1090 if (isDrmAuthority) {
1091 try {
1092 // Check DRM access permission here, since once we
1093 // do the below call the DRM will be checking our
1094 // permission, not our caller's permission
1095 DrmStore.enforceAccessDrmPermission(context);
1096 } catch (SecurityException e) {
1097 throw new FileNotFoundException(e.getMessage());
1098 }
1099 }
1100
1101 ParcelFileDescriptor pfd = null;
1102 try {
1103 pfd = context.getContentResolver().openFileDescriptor(soundUri, mode);
1104 return new AssetFileDescriptor(pfd, 0, -1);
1105 } catch (FileNotFoundException ex) {
1106 // fall through and open the fallback ringtone below
1107 }
1108 }
1109
1110 try {
1111 return super.openAssetFile(soundUri, mode);
1112 } catch (FileNotFoundException ex) {
1113 // Since a non-null Uri was specified, but couldn't be opened,
1114 // fall back to the built-in ringtone.
1115 return context.getResources().openRawResourceFd(
1116 com.android.internal.R.raw.fallbackring);
1117 }
1118 }
1119 // no need to fall through and have openFile() try again, since we
1120 // already know that will fail.
1121 throw new FileNotFoundException(); // or return null ?
1122 }
1123
1124 // Note that this will end up calling openFile() above.
1125 return super.openAssetFile(uri, mode);
1126 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001127
1128 /**
1129 * In-memory LRU Cache of system and secure settings, along with
1130 * associated helper functions to keep cache coherent with the
1131 * database.
1132 */
Jesse Wilson0c7faee2011-02-10 11:33:19 -08001133 private static final class SettingsCache extends LruCache<String, Bundle> {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001134
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001135 private final String mCacheName;
1136 private boolean mCacheFullyMatchesDisk = false; // has the whole database slurped.
1137
1138 public SettingsCache(String name) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -08001139 super(MAX_CACHE_ENTRIES);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001140 mCacheName = name;
1141 }
1142
1143 /**
1144 * Is the whole database table slurped into this cache?
1145 */
1146 public boolean fullyMatchesDisk() {
1147 synchronized (this) {
1148 return mCacheFullyMatchesDisk;
1149 }
1150 }
1151
1152 public void setFullyMatchesDisk(boolean value) {
1153 synchronized (this) {
1154 mCacheFullyMatchesDisk = value;
1155 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001156 }
1157
1158 @Override
Jesse Wilson32c80a22011-02-25 17:28:41 -08001159 protected void entryRemoved(boolean evicted, String key, Bundle oldValue, Bundle newValue) {
1160 if (evicted) {
1161 mCacheFullyMatchesDisk = false;
1162 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001163 }
1164
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001165 /**
1166 * Atomic cache population, conditional on size of value and if
1167 * we lost a race.
1168 *
1169 * @returns a Bundle to send back to the client from call(), even
1170 * if we lost the race.
1171 */
1172 public Bundle putIfAbsent(String key, String value) {
1173 Bundle bundle = (value == null) ? NULL_SETTING : Bundle.forPair("value", value);
1174 if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {
1175 synchronized (this) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -08001176 if (get(key) == null) {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001177 put(key, bundle);
1178 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001179 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001180 }
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001181 return bundle;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001182 }
1183
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001184 /**
1185 * Populates a key in a given (possibly-null) cache.
1186 */
1187 public static void populate(SettingsCache cache, ContentValues contentValues) {
1188 if (cache == null) {
1189 return;
1190 }
1191 String name = contentValues.getAsString(Settings.NameValueTable.NAME);
1192 if (name == null) {
1193 Log.w(TAG, "null name populating settings cache.");
1194 return;
1195 }
1196 String value = contentValues.getAsString(Settings.NameValueTable.VALUE);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001197 cache.populate(name, value);
1198 }
1199
1200 public void populate(String name, String value) {
1201 synchronized (this) {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001202 if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001203 put(name, Bundle.forPair(Settings.NameValueTable.VALUE, value));
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001204 } else {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001205 put(name, TOO_LARGE_TO_CACHE_MARKER);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001206 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001207 }
1208 }
1209
1210 /**
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08001211 * For suppressing duplicate/redundant settings inserts early,
1212 * checking our cache first (but without faulting it in),
1213 * before going to sqlite with the mutation.
1214 */
1215 public static boolean isRedundantSetValue(SettingsCache cache, String name, String value) {
1216 if (cache == null) return false;
1217 synchronized (cache) {
1218 Bundle bundle = cache.get(name);
1219 if (bundle == null) return false;
1220 String oldValue = bundle.getPairValue();
1221 if (oldValue == null && value == null) return true;
1222 if ((oldValue == null) != (value == null)) return false;
1223 return oldValue.equals(value);
1224 }
1225 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001226 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001227}