Adding API to set the current grid

Bug: 122262084
Change-Id: Ie356d4a90cf1e5a3715fde62cd3502384856e8b5
diff --git a/src/com/android/launcher3/InvariantDeviceProfile.java b/src/com/android/launcher3/InvariantDeviceProfile.java
index 45bdea8..dafd5bb 100644
--- a/src/com/android/launcher3/InvariantDeviceProfile.java
+++ b/src/com/android/launcher3/InvariantDeviceProfile.java
@@ -247,7 +247,6 @@
     }
 
     public void verifyConfigChangedInBackground(final Context context) {
-
         String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
         // Good place to check if grid size changed in themepicker when launcher was dead.
         if (savedIconMaskPath.isEmpty()) {
@@ -260,6 +259,12 @@
         }
     }
 
+    public void setCurrentGrid(Context context, String gridName) {
+        Context appContext = context.getApplicationContext();
+        Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
+        new MainThreadExecutor().execute(() -> onConfigChanged(appContext));
+    }
+
     private void onConfigChanged(Context context) {
         // Config changes, what shall we do?
         InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
@@ -300,7 +305,8 @@
             int type;
             while (((type = parser.next()) != XmlPullParser.END_TAG ||
                     parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
-                if ((type == XmlPullParser.START_TAG) && "grid-option".equals(parser.getName())) {
+                if ((type == XmlPullParser.START_TAG)
+                        && GridOption.TAG_NAME.equals(parser.getName())) {
 
                     GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
                     final int displayDepth = parser.getDepth();
@@ -451,6 +457,8 @@
 
     public static final class GridOption {
 
+        public static final String TAG_NAME = "grid-option";
+
         public final String name;
         public final int numRows;
         public final int numColumns;
diff --git a/src/com/android/launcher3/graphics/GridOptionsProvider.java b/src/com/android/launcher3/graphics/GridOptionsProvider.java
index 9b907ba..efd39ee 100644
--- a/src/com/android/launcher3/graphics/GridOptionsProvider.java
+++ b/src/com/android/launcher3/graphics/GridOptionsProvider.java
@@ -25,6 +25,8 @@
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.Future;
 
@@ -40,6 +42,9 @@
  *          is_default: true if this grid is currently active
  *
  *     /preview: Opens a file stream for the grid preview
+ *
+ *     /default_grid: Call update to set the current grid, with values
+ *          name: name of the grid to apply
  */
 public class GridOptionsProvider extends ContentProvider {
 
@@ -51,6 +56,9 @@
     private static final String KEY_PREVIEW_COUNT = "preview_count";
     private static final String KEY_IS_DEFAULT = "is_default";
 
+    private static final String KEY_LIST_OPTIONS = "/list_options";
+    private static final String KEY_DEFAULT_GRID = "/default_grid";
+
     private static final String KEY_PREVIEW = "preview";
     private static final String MIME_TYPE_PNG = "image/png";
 
@@ -75,33 +83,41 @@
     @Override
     public Cursor query(Uri uri, String[] projection, String selection,
             String[] selectionArgs, String sortOrder) {
-        // TODO: Validate the query uri
+        if (!KEY_LIST_OPTIONS.equals(uri.getPath())) {
+            return null;
+        }
         MatrixCursor cursor = new MatrixCursor(new String[] {
                 KEY_NAME, KEY_ROWS, KEY_COLS, KEY_PREVIEW_COUNT, KEY_IS_DEFAULT});
         InvariantDeviceProfile idp = InvariantDeviceProfile.INSTANCE.get(getContext());
+        for (GridOption gridOption : parseAllGridOptions()) {
+            cursor.newRow()
+                    .add(KEY_NAME, gridOption.name)
+                    .add(KEY_ROWS, gridOption.numRows)
+                    .add(KEY_COLS, gridOption.numColumns)
+                    .add(KEY_PREVIEW_COUNT, 1)
+                    .add(KEY_IS_DEFAULT, idp.numColumns == gridOption.numColumns
+                            && idp.numRows == gridOption.numRows);
+        }
+        return cursor;
+    }
+
+    private List<GridOption> parseAllGridOptions() {
+        List<GridOption> result = new ArrayList<>();
         try (XmlResourceParser parser = getContext().getResources().getXml(R.xml.device_profiles)) {
             final int depth = parser.getDepth();
             int type;
             while (((type = parser.next()) != XmlPullParser.END_TAG ||
                     parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
-                if ((type == XmlPullParser.START_TAG) && "grid-option".equals(parser.getName())) {
-                    GridOption gridOption = new GridOption(
-                            getContext(), Xml.asAttributeSet(parser));
-
-                    cursor.newRow()
-                            .add(KEY_NAME, gridOption.name)
-                            .add(KEY_ROWS, gridOption.numRows)
-                            .add(KEY_COLS, gridOption.numColumns)
-                            .add(KEY_PREVIEW_COUNT, 1)
-                            .add(KEY_IS_DEFAULT, idp.numColumns == gridOption.numColumns
-                                    && idp.numRows == gridOption.numRows);
+                if ((type == XmlPullParser.START_TAG)
+                        && GridOption.TAG_NAME.equals(parser.getName())) {
+                    result.add(new GridOption(getContext(), Xml.asAttributeSet(parser)));
                 }
             }
         } catch (IOException | XmlPullParserException e) {
             Log.e(TAG, "Error parsing device profile", e);
+            return Collections.emptyList();
         }
-
-        return cursor;
+        return result;
     }
 
     @Override
@@ -125,7 +141,25 @@
 
     @Override
     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
-        return 0;
+        if (!KEY_DEFAULT_GRID.equals(uri.getPath())) {
+            return 0;
+        }
+
+        String gridName = values.getAsString(KEY_NAME);
+        // Verify that this is a valid grid option
+        GridOption match = null;
+        for (GridOption option : parseAllGridOptions()) {
+            if (option.name.equals(gridName)) {
+                match = option;
+                break;
+            }
+        }
+        if (match == null) {
+            return 0;
+        }
+
+        InvariantDeviceProfile.INSTANCE.get(getContext()).setCurrentGrid(getContext(), gridName);
+        return 1;
     }
 
     @Override