Rewrite raw insert()s and some raw query()s of moved-to-global keys

The Settings put*() APIs fix up references via the old namespaces,
but the raw insert() interface didn't.  Now it does.  Also, when
possible we fix up direct query() operations on the old namespace
to point to the correct one.  At present that is only done for
query() operations with Uris of the form

    content://secure/adb_enabled

There is no rewriting done on queries of the form

    content://secure WHERE name='adb_enabled'

since the app-supplied WHERE clause can be arbitrarily complex.

Bug 7267568

Change-Id: I5c8cecbea7f5b1da6247a53b1428d3effb0bbca5
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index ad35f7f..b1c218d 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -134,6 +134,7 @@
         /** Operate on existing rows. */
         SqlArguments(Uri url, String where, String[] args) {
             if (url.getPathSegments().size() == 1) {
+                // of the form content://settings/secure, arbitrary where clause
                 this.table = url.getPathSegments().get(0);
                 if (!DatabaseHelper.isValidTable(this.table)) {
                     throw new IllegalArgumentException("Bad root path: " + this.table);
@@ -145,6 +146,7 @@
             } else if (!TextUtils.isEmpty(where)) {
                 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
             } else {
+                // of the form content://settings/secure/element_name, no where clause
                 this.table = url.getPathSegments().get(0);
                 if (!DatabaseHelper.isValidTable(this.table)) {
                     throw new IllegalArgumentException("Bad root path: " + this.table);
@@ -152,8 +154,16 @@
                 if (TABLE_SYSTEM.equals(this.table) || TABLE_SECURE.equals(this.table) ||
                     TABLE_GLOBAL.equals(this.table)) {
                     this.where = Settings.NameValueTable.NAME + "=?";
-                    this.args = new String[] { url.getPathSegments().get(1) };
+                    final String name = url.getPathSegments().get(1);
+                    this.args = new String[] { name };
+                    // Rewrite the table for known-migrated names
+                    if (TABLE_SYSTEM.equals(this.table) || TABLE_SECURE.equals(this.table)) {
+                        if (sSecureGlobalKeys.contains(name) || sSystemGlobalKeys.contains(name)) {
+                            this.table = TABLE_GLOBAL;
+                        }
+                    }
                 } else {
+                    // of the form content://bookmarks/19
                     this.where = "_id=" + ContentUris.parseId(url);
                     this.args = null;
                 }
@@ -838,6 +848,17 @@
             if (!parseProviderList(url, initialValues)) return null;
         }
 
+        // If this is an insert() of a key that has been migrated to the global store,
+        // redirect the operation to that store
+        if (name != null) {
+            if (sSecureGlobalKeys.contains(name) || sSystemGlobalKeys.contains(name)) {
+                if (!TABLE_GLOBAL.equals(args.table)) {
+                    if (LOCAL_LOGV) Slog.i(TAG, "Rewrite of insert() of now-global key " + name);
+                }
+                args.table = TABLE_GLOBAL;  // next condition will rewrite the user handle
+            }
+        }
+
         // The global table is stored under the owner, always
         if (TABLE_GLOBAL.equals(args.table)) {
             desiredUserHandle = UserHandle.USER_OWNER;