Supports themed icon options from Launcher

Adds the support of themed icon options from Launcher.

Bug: 186590551
Test: Manual
Change-Id: Ie64a410564a751ee8df37f032309033dfa435ae4
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 88b3907..eb36162 100755
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -227,4 +227,8 @@
 
     <!-- Accessibility for custom style name page [CHAR_LIMIT=50] -->
     <string name="accessibility_custom_name_title">Custom style name</string>
+
+    <!--Name of metadata in the main launcher Activity which values contains the authority
+    corresponding to a ContentProvider in launcher to query or change themed icon option  -->
+    <string name="themed_icon_metadata_key" translatable="false">com.android.launcher3.themedicon.option</string>
 </resources>
diff --git a/src/com/android/customization/model/themedicon/ThemedIconSwitchProvider.java b/src/com/android/customization/model/themedicon/ThemedIconSwitchProvider.java
new file mode 100644
index 0000000..1a57223
--- /dev/null
+++ b/src/com/android/customization/model/themedicon/ThemedIconSwitchProvider.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.customization.model.themedicon;
+
+import android.content.ContentResolver;
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+
+import androidx.annotation.WorkerThread;
+
+/**
+ * Retrieves the themed icon switch by {@link ContentResolver}
+ */
+public class ThemedIconSwitchProvider {
+
+    private static final String GET_ICON_THEMED = "get_icon_themed";
+    private static final String SET_ICON_THEMED = "set_icon_themed";
+    private static final int ENABLED = 1;
+    private static final String COL_ICON_THEMED_VALUE = "boolean_value";
+
+    private final Context mContext;
+    private final ThemedIconUtils mThemedIconUtils;
+
+    public ThemedIconSwitchProvider(Context context, ThemedIconUtils themedIconUtils) {
+        mContext = context;
+        mThemedIconUtils = themedIconUtils;
+    }
+
+    public boolean isThemedIconAvailable() {
+        return mThemedIconUtils.isThemedIconAvailable();
+    }
+
+    @WorkerThread
+    protected boolean fetchThemedIconEnabled() {
+        ContentResolver contentResolver = mContext.getContentResolver();
+        try (Cursor cursor = contentResolver.query(
+                mThemedIconUtils.getUriForPath(GET_ICON_THEMED), /* projection= */
+                null, /* selection= */ null, /* selectionArgs= */ null, /* sortOrder= */ null)) {
+            if (cursor != null && cursor.moveToNext()) {
+                int themedIconEnabled = cursor.getInt(cursor.getColumnIndex(COL_ICON_THEMED_VALUE));
+                return themedIconEnabled == ENABLED;
+            }
+        }
+        return false;
+    }
+
+    protected int setThemedIconEnabled(boolean enabled) {
+        ContentValues values = new ContentValues();
+        values.put(COL_ICON_THEMED_VALUE, enabled);
+        return mContext.getContentResolver().update(mThemedIconUtils.getUriForPath(SET_ICON_THEMED),
+                values, /* where= */ null, /* selectionArgs= */ null);
+    }
+}
diff --git a/src/com/android/customization/model/themedicon/ThemedIconUtils.java b/src/com/android/customization/model/themedicon/ThemedIconUtils.java
new file mode 100644
index 0000000..8f1bdee
--- /dev/null
+++ b/src/com/android/customization/model/themedicon/ThemedIconUtils.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.customization.model.themedicon;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ProviderInfo;
+import android.content.pm.ResolveInfo;
+import android.net.Uri;
+import android.text.TextUtils;
+
+/**
+ * Utility class for themed icon.
+ */
+public class ThemedIconUtils {
+
+    private final Context mContext;
+    private final String mProviderAuthority;
+
+    private ProviderInfo mProviderInfo;
+
+    public ThemedIconUtils(Context context, String authorityMetaKey) {
+        mContext = context;
+        Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
+        ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(homeIntent,
+                PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA);
+
+        if (resolveInfo != null && resolveInfo.activityInfo.metaData != null) {
+            mProviderAuthority = resolveInfo.activityInfo.metaData.getString(authorityMetaKey);
+        } else {
+            mProviderAuthority = null;
+        }
+        mProviderInfo = TextUtils.isEmpty(mProviderAuthority) ? null :
+                mContext.getPackageManager().resolveContentProvider(mProviderAuthority, 0);
+        if (mProviderInfo != null && !TextUtils.isEmpty(mProviderInfo.readPermission)) {
+            if (mContext.checkSelfPermission(mProviderInfo.readPermission)
+                    != PackageManager.PERMISSION_GRANTED) {
+                mProviderInfo = null;
+            }
+        }
+    }
+
+    /**
+     * Gets the Uri for this provider's authority with path information.
+     *
+     * @param path the path segment of {@link Uri}
+     * @return {@link Uri} with path information
+     */
+    Uri getUriForPath(String path) {
+        return new Uri.Builder()
+                .scheme(ContentResolver.SCHEME_CONTENT)
+                .authority(mProviderInfo.authority)
+                .appendPath(path)
+                .build();
+    }
+
+    /**
+     * Returns if themed icon is available.
+     *
+     * @return true if themed icon feature is available, false otherwise.
+     */
+    boolean isThemedIconAvailable() {
+        return mProviderInfo != null;
+    }
+}
diff --git a/src/com/android/customization/model/themedicon/ThemedIconViewModel.kt b/src/com/android/customization/model/themedicon/ThemedIconViewModel.kt
new file mode 100644
index 0000000..f8906ff
--- /dev/null
+++ b/src/com/android/customization/model/themedicon/ThemedIconViewModel.kt
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.customization.model.themedicon
+
+import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.ViewModel
+
+/**
+ * ViewModel class to keep track of themed icon
+ */
+class ThemedIconViewModel : ViewModel() {
+
+    /**
+     * Flag for the themed icon enabled or not
+     */
+    val themedIconEnabled: MutableLiveData<Boolean> by lazy {
+        MutableLiveData<Boolean>()
+    }
+}