diff options
author | Maurice Lam <yukl@google.com> | 2015-12-18 16:48:29 -0800 |
---|---|---|
committer | Maurice Lam <yukl@google.com> | 2015-12-21 20:29:12 +0000 |
commit | 38a3832bf55aeb5dc24904d4d1ddf391dffa7d7b (patch) | |
tree | 3f7689da95e395532b5c47571053296b20014ce0 /library | |
parent | 21a04dc2c4d43b455dc1c1250a90dc84738b2a00 (diff) | |
download | setupwizard-38a3832bf55aeb5dc24904d4d1ddf391dffa7d7b.tar.gz |
[SuwLib] Add preference integration support
Change-Id: I2e756bcea1512886958ec70c80ae6f4616c9ed81
Diffstat (limited to 'library')
9 files changed, 222 insertions, 6 deletions
diff --git a/library/eclair-mr1/res/values/styles.xml b/library/eclair-mr1/res/values/styles.xml index fa66c53..3c3cb46 100644 --- a/library/eclair-mr1/res/values/styles.xml +++ b/library/eclair-mr1/res/values/styles.xml @@ -66,6 +66,8 @@ <!-- Specify the indeterminateTintMode to work around a bug in Lollipop --> <item name="android:indeterminateTintMode" tools:ignore="NewApi">src_in</item> <item name="android:listPreferredItemHeight">@dimen/suw_items_preferred_height</item> + <item name="android:listPreferredItemPaddingEnd" tools:ignore="NewApi">?attr/suwMarginSides</item> + <item name="android:listPreferredItemPaddingStart" tools:ignore="NewApi">?attr/suwMarginSides</item> <item name="android:navigationBarColor" tools:ignore="NewApi">@android:color/black</item> <item name="android:statusBarColor" tools:ignore="NewApi">@android:color/transparent</item> <item name="android:textColorLink">@color/suw_link_color_light</item> diff --git a/library/full-support/src/com/android/setupwizardlib/util/GlifPreferenceDelegate.java b/library/full-support/src/com/android/setupwizardlib/util/GlifPreferenceDelegate.java new file mode 100644 index 0000000..914dca9 --- /dev/null +++ b/library/full-support/src/com/android/setupwizardlib/util/GlifPreferenceDelegate.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2015 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 KIN'D, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.setupwizardlib.util; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.setupwizardlib.R; +import com.android.setupwizardlib.view.HeaderRecyclerView; + +/** + * A helper delegate to integrate GLIF theme with PreferenceFragment v14. To use this, create an + * instance and delegate {@code PreferenceFragment#onCreateRecyclerView} to it. Then call + * {@code PreferenceFragment#setDivider} to {@link #getDividerDrawable(android.content.Context)} in + * order to make sure the correct inset is applied to the dividers. + */ +public class GlifPreferenceDelegate { + + public static final int[] ATTRS_LIST_DIVIDER = new int[]{ android.R.attr.listDivider }; + + private HeaderRecyclerView mRecyclerView; + private boolean mHasIcons; + + public GlifPreferenceDelegate(boolean hasIcons) { + mHasIcons = hasIcons; + } + + public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, + Bundle savedInstanceState) { + final Context inflaterContext = inflater.getContext(); + mRecyclerView = new HeaderRecyclerView(inflaterContext); + mRecyclerView.setLayoutManager(new LinearLayoutManager(inflaterContext)); + final View header = inflater.inflate(R.layout.suw_glif_header, mRecyclerView, false); + mRecyclerView.setHeader(header); + return mRecyclerView; + } + + public Drawable getDividerDrawable(Context context) { + final TypedArray a = context.obtainStyledAttributes(ATTRS_LIST_DIVIDER); + final Drawable defaultDivider = a.getDrawable(0); + a.recycle(); + + final int dividerInset = context.getResources().getDimensionPixelSize( + mHasIcons ? R.dimen.suw_items_icon_divider_inset + : R.dimen.suw_items_text_divider_inset); + return DrawableLayoutDirectionHelper.createRelativeInsetDrawable(defaultDivider, + dividerInset /* start */, 0 /* top */, 0 /* end */, 0 /* bottom */, + context); + } + + public void setHeaderText(CharSequence text) { + final View header = mRecyclerView.getHeader(); + + final View titleView = header.findViewById(R.id.suw_layout_title); + if (titleView instanceof TextView) { + ((TextView) titleView).setText(text); + } + } + + public void setIcon(Drawable icon) { + final View header = mRecyclerView.getHeader(); + + final View iconView = header.findViewById(R.id.suw_layout_icon); + if (iconView instanceof ImageView) { + ((ImageView) iconView).setImageDrawable(icon); + } + } +} diff --git a/library/full-support/src/com/android/setupwizardlib/view/HeaderRecyclerView.java b/library/full-support/src/com/android/setupwizardlib/view/HeaderRecyclerView.java index f164a23..e0c0e46 100644 --- a/library/full-support/src/com/android/setupwizardlib/view/HeaderRecyclerView.java +++ b/library/full-support/src/com/android/setupwizardlib/view/HeaderRecyclerView.java @@ -207,6 +207,14 @@ public class HeaderRecyclerView extends RecyclerView { return mHeader; } + /** + * Set the view to use as the header of this recycler view. + * Note: This must be called before setAdapter. + */ + public void setHeader(View header) { + mHeader = header; + } + @Override public void setLayoutManager(LayoutManager layout) { super.setLayoutManager(layout); diff --git a/library/main/res/layout/suw_glif_blank_template_card.xml b/library/main/res/layout/suw_glif_blank_template_card.xml new file mode 100644 index 0000000..6f1ab9d --- /dev/null +++ b/library/main/res/layout/suw_glif_blank_template_card.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (C) 2015 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. +--> + +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/suw_pattern_bg" + style="@style/SuwGlifCardBackground" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:fitsSystemWindows="true"> + + <FrameLayout + android:id="@+id/suw_layout_content" + style="@style/SuwGlifCardContainer" + android:layout_width="@dimen/suw_glif_card_width" + android:layout_height="@dimen/suw_glif_card_height" + android:layout_centerInParent="true" /> + +</RelativeLayout> diff --git a/library/main/res/layout/suw_glif_blank_template_compact.xml b/library/main/res/layout/suw_glif_blank_template_compact.xml new file mode 100644 index 0000000..7792cd1 --- /dev/null +++ b/library/main/res/layout/suw_glif_blank_template_compact.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (C) 2015 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. +--> + +<com.android.setupwizardlib.view.StatusBarBackgroundLayout + xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/suw_pattern_bg" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <FrameLayout + android:id="@+id/suw_layout_content" + android:layout_width="match_parent" + android:layout_height="match_parent" /> + +</com.android.setupwizardlib.view.StatusBarBackgroundLayout> diff --git a/library/main/res/values-sw600dp/layouts.xml b/library/main/res/values-sw600dp/layouts.xml index 3b304d7..ce7ef60 100644 --- a/library/main/res/values-sw600dp/layouts.xml +++ b/library/main/res/values-sw600dp/layouts.xml @@ -26,6 +26,7 @@ <item name="suw_glif_template" type="layout">@layout/suw_glif_template_card</item> <item name="suw_glif_list_template" type="layout">@layout/suw_glif_list_template_card</item> + <item name="suw_glif_blank_template" type="layout">@layout/suw_glif_blank_template_card</item> </resources> diff --git a/library/main/res/values/layouts.xml b/library/main/res/values/layouts.xml index e4d3327..74a6479 100644 --- a/library/main/res/values/layouts.xml +++ b/library/main/res/values/layouts.xml @@ -26,6 +26,7 @@ <item name="suw_glif_template" type="layout">@layout/suw_glif_template_compact</item> <item name="suw_glif_list_template" type="layout">@layout/suw_glif_list_template_compact</item> + <item name="suw_glif_blank_template" type="layout">@layout/suw_glif_blank_template_compact</item> </resources> diff --git a/library/main/src/com/android/setupwizardlib/util/DrawableLayoutDirectionHelper.java b/library/main/src/com/android/setupwizardlib/util/DrawableLayoutDirectionHelper.java index d0c8310..bf4c0c2 100644 --- a/library/main/src/com/android/setupwizardlib/util/DrawableLayoutDirectionHelper.java +++ b/library/main/src/com/android/setupwizardlib/util/DrawableLayoutDirectionHelper.java @@ -16,6 +16,7 @@ package com.android.setupwizardlib.util; +import android.content.Context; import android.graphics.drawable.Drawable; import android.graphics.drawable.InsetDrawable; import android.os.Build; @@ -32,12 +33,26 @@ public class DrawableLayoutDirectionHelper { */ public static InsetDrawable createRelativeInsetDrawable(Drawable drawable, int insetStart, int insetTop, int insetEnd, int insetBottom, View view) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 - && view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) { - return new InsetDrawable(drawable, insetEnd, insetTop, insetStart, insetBottom); - } else { - return new InsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom); + boolean isRtl = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 + && view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; + return createRelativeInsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom, + isRtl); + } + + /** + * Creates an {@link android.graphics.drawable.InsetDrawable} according to the layout direction + * of {@code context}. + */ + public static InsetDrawable createRelativeInsetDrawable(Drawable drawable, + int insetStart, int insetTop, int insetEnd, int insetBottom, Context context) { + boolean isRtl = false; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + final int layoutDirection = + context.getResources().getConfiguration().getLayoutDirection(); + isRtl = layoutDirection == View.LAYOUT_DIRECTION_RTL; } + return createRelativeInsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom, + isRtl); } /** @@ -46,7 +61,14 @@ public class DrawableLayoutDirectionHelper { */ public static InsetDrawable createRelativeInsetDrawable(Drawable drawable, int insetStart, int insetTop, int insetEnd, int insetBottom, int layoutDirection) { - if (layoutDirection == View.LAYOUT_DIRECTION_RTL) { + //noinspection AndroidLintInlinedApi + return createRelativeInsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom, + layoutDirection == View.LAYOUT_DIRECTION_RTL); + } + + private static InsetDrawable createRelativeInsetDrawable(Drawable drawable, + int insetStart, int insetTop, int insetEnd, int insetBottom, boolean isRtl) { + if (isRtl) { return new InsetDrawable(drawable, insetEnd, insetTop, insetStart, insetBottom); } else { return new InsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom); diff --git a/library/test/src/com/android/setupwizardlib/test/DrawableLayoutDirectionHelperTest.java b/library/test/src/com/android/setupwizardlib/test/DrawableLayoutDirectionHelperTest.java index 5b9b1cb..b14817e 100644 --- a/library/test/src/com/android/setupwizardlib/test/DrawableLayoutDirectionHelperTest.java +++ b/library/test/src/com/android/setupwizardlib/test/DrawableLayoutDirectionHelperTest.java @@ -17,6 +17,7 @@ package com.android.setupwizardlib.test; import android.content.Context; +import android.content.res.Configuration; import android.graphics.Color; import android.graphics.Rect; import android.graphics.drawable.ColorDrawable; @@ -29,6 +30,8 @@ import android.view.View; import com.android.setupwizardlib.util.DrawableLayoutDirectionHelper; +import java.util.Locale; + public class DrawableLayoutDirectionHelperTest extends AndroidTestCase { @SmallTest @@ -87,6 +90,33 @@ public class DrawableLayoutDirectionHelperTest extends AndroidTestCase { } } + @SmallTest + public void testCreateRelativeInsetDrawableContextRtl() { + final Drawable drawable = new ColorDrawable(Color.RED); + Context context = getContext(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + final Configuration config = new Configuration(); + config.setLayoutDirection(new Locale("fa", "IR")); + context = getContext().createConfigurationContext(config); + } + final InsetDrawable insetDrawable = + DrawableLayoutDirectionHelper.createRelativeInsetDrawable(drawable, + 1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */, context); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + assertSame("Drawable from getDrawable() should be same as passed in", drawable, + insetDrawable.getDrawable()); + } + Rect outRect = new Rect(); + insetDrawable.getPadding(outRect); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + assertEquals("InsetDrawable padding should be same as inset", new Rect(3, 2, 1, 4), + outRect); + } else { + assertEquals("InsetDrawable padding should be same as inset", new Rect(1, 2, 3, 4), + outRect); + } + } + private static class ForceRtlView extends View { public ForceRtlView(Context context) { |