From 5bc1846375b43fe17b9b23bf8a1e4ffa8dbaf029 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Mon, 7 Jan 2019 15:13:39 -0800 Subject: [PATCH] Adding support for putting arbitary attributes in InvariantDeviceProfile This would allow derivative projects to profile profile specific customization options Change-Id: Id4703dc54d649a8d8a930f72c836c4ec23ffc45d --- .../launcher3/InvariantDeviceProfile.java | 20 +++++++++++++++++++ .../android/launcher3/folder/FolderShape.java | 5 ++++- src/com/android/launcher3/util/Themes.java | 13 +++++++----- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/com/android/launcher3/InvariantDeviceProfile.java b/src/com/android/launcher3/InvariantDeviceProfile.java index 5c842a57dd..935dda609f 100644 --- a/src/com/android/launcher3/InvariantDeviceProfile.java +++ b/src/com/android/launcher3/InvariantDeviceProfile.java @@ -30,12 +30,16 @@ import android.text.TextUtils; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.Log; +import android.util.SparseArray; +import android.util.TypedValue; import android.util.Xml; import android.view.Display; import android.view.WindowManager; import com.android.launcher3.util.ConfigMonitor; +import com.android.launcher3.util.IntArray; import com.android.launcher3.util.MainThreadInitializedObject; +import com.android.launcher3.util.Themes; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -44,6 +48,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; public class InvariantDeviceProfile { @@ -91,6 +96,8 @@ public class InvariantDeviceProfile { public int fillResIconDpi; public float iconTextSize; + private SparseArray mExtraAttrs; + /** * Number of icons inside the hotseat area. */ @@ -122,6 +129,7 @@ public class InvariantDeviceProfile { numHotseatIcons = p.numHotseatIcons; defaultLayoutId = p.defaultLayoutId; demoModeLayoutId = p.demoModeLayoutId; + mExtraAttrs = p.mExtraAttrs; } @TargetApi(23) @@ -171,6 +179,8 @@ public class InvariantDeviceProfile { demoModeLayoutId = closestProfile.demoModeLayoutId; numFolderRows = closestProfile.numFolderRows; numFolderColumns = closestProfile.numFolderColumns; + mExtraAttrs = closestProfile.extraAttrs; + if (!closestProfile.name.equals(gridName)) { Utilities.getPrefs(context).edit() .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply(); @@ -210,6 +220,11 @@ public class InvariantDeviceProfile { } } + @Nullable + public TypedValue getAttrValue(int attr) { + return mExtraAttrs == null ? null : mExtraAttrs.get(attr); + } + public void addOnChangeListener(OnIDPChangeListener listener) { mChangeListeners.add(listener); } @@ -436,6 +451,8 @@ public class InvariantDeviceProfile { private final int defaultLayoutId; private final int demoModeLayoutId; + private final SparseArray extraAttrs; + GridOption(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.GridDisplayOption); @@ -454,6 +471,9 @@ public class InvariantDeviceProfile { numFolderColumns = a.getInt( R.styleable.GridDisplayOption_numFolderColumns, numColumns); a.recycle(); + + extraAttrs = Themes.createValueMap(context, attrs, + IntArray.wrap(R.styleable.GridDisplayOption)); } } diff --git a/src/com/android/launcher3/folder/FolderShape.java b/src/com/android/launcher3/folder/FolderShape.java index f7cdb778e3..4b06ddac8c 100644 --- a/src/com/android/launcher3/folder/FolderShape.java +++ b/src/com/android/launcher3/folder/FolderShape.java @@ -49,6 +49,7 @@ import com.android.launcher3.MainThreadExecutor; import com.android.launcher3.R; import com.android.launcher3.Utilities; import com.android.launcher3.anim.RoundedRectRevealOutlineProvider; +import com.android.launcher3.util.IntArray; import com.android.launcher3.util.Themes; import org.xmlpull.v1.XmlPullParser; @@ -387,6 +388,8 @@ public abstract class FolderShape { final int depth = parser.getDepth(); int[] radiusAttr = new int[] {R.attr.folderIconRadius}; + IntArray keysToIgnore = new IntArray(0); + while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { @@ -396,7 +399,7 @@ public abstract class FolderShape { FolderShape shape = getShapeDefinition(parser.getName(), a.getFloat(0, 1)); a.recycle(); - shape.mAttrs = Themes.createValueMap(context, attrs); + shape.mAttrs = Themes.createValueMap(context, attrs, keysToIgnore); result.add(shape); } } diff --git a/src/com/android/launcher3/util/Themes.java b/src/com/android/launcher3/util/Themes.java index e3ab1a5a08..675e2f4b74 100644 --- a/src/com/android/launcher3/util/Themes.java +++ b/src/com/android/launcher3/util/Themes.java @@ -112,16 +112,19 @@ public class Themes { * Creates a map for attribute-name to value for all the values in {@param attrs} which can be * held in memory for later use. */ - public static SparseArray createValueMap(Context context, AttributeSet attrSet) { + public static SparseArray createValueMap(Context context, AttributeSet attrSet, + IntArray keysToIgnore) { int count = attrSet.getAttributeCount(); - int[] attrNames = new int[count]; + IntArray attrNameArray = new IntArray(count); for (int i = 0; i < count; i++) { - attrNames[i] = attrSet.getAttributeNameResource(i); + attrNameArray.add(attrSet.getAttributeNameResource(i)); } + attrNameArray.removeAllValues(keysToIgnore); - SparseArray result = new SparseArray<>(count); + int[] attrNames = attrNameArray.toArray(); + SparseArray result = new SparseArray<>(attrNames.length); TypedArray ta = context.obtainStyledAttributes(attrSet, attrNames); - for (int i = 0; i < count; i++) { + for (int i = 0; i < attrNames.length; i++) { TypedValue tv = new TypedValue(); ta.getValue(i, tv); result.put(attrNames[i], tv);