diff options
author | Maurice Lam <yukl@google.com> | 2016-03-26 00:17:21 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2016-03-26 00:17:21 +0000 |
commit | eb85a47ae17061a4ed20d5a5fba4c61e24f17013 (patch) | |
tree | fad341ea61e34261384f4ee26ebf574f5ee46ff3 | |
parent | 2def2bf2156581c6bde6d6baa4583ccd97f2f670 (diff) | |
parent | 6e55f30c31f1ac3b35d60f306cf6cef084b1a845 (diff) | |
download | setupwizard-eb85a47ae17061a4ed20d5a5fba4c61e24f17013.tar.gz |
Merge "[SuwLib] Add SwitchItem" into ub-setupwizard-belgarath
5 files changed, 374 insertions, 1 deletions
diff --git a/library/eclair-mr1/res/layout/suw_items_switch.xml b/library/eclair-mr1/res/layout/suw_items_switch.xml new file mode 100644 index 0000000..14c7650 --- /dev/null +++ b/library/eclair-mr1/res/layout/suw_items_switch.xml @@ -0,0 +1,75 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (C) 2016 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. +--> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + style="@style/SuwItemContainer.Verbose" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:descendantFocusability="blocksDescendants" + android:orientation="horizontal"> + + <FrameLayout + android:id="@+id/suw_items_icon_container" + android:layout_width="@dimen/suw_items_icon_container_width" + android:layout_height="wrap_content" + android:layout_gravity="top" + android:gravity="start"> + + <ImageView + android:id="@+id/suw_items_icon" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + tools:ignore="ContentDescription" /> + + </FrameLayout> + + <LinearLayout + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginBottom="@dimen/suw_items_verbose_padding_bottom_extra" + android:layout_weight="1" + android:orientation="vertical"> + + <TextView + android:id="@+id/suw_items_title" + style="@style/SuwItemTitle.Verbose" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:gravity="start" + android:textAlignment="viewStart" + tools:ignore="UnusedAttribute" /> + + <TextView + android:id="@+id/suw_items_summary" + style="@style/SuwItemSummary" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:gravity="start" + android:textAlignment="viewStart" + android:visibility="gone" + tools:ignore="UnusedAttribute" /> + + </LinearLayout> + + <android.support.v7.widget.SwitchCompat + android:id="@+id/suw_items_switch" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center_vertical" /> + +</LinearLayout> diff --git a/library/eclair-mr1/res/values/attrs.xml b/library/eclair-mr1/res/values/attrs.xml new file mode 100644 index 0000000..34cf3da --- /dev/null +++ b/library/eclair-mr1/res/values/attrs.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (C) 2016 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. +--> + +<resources> + + <declare-styleable name="SuwSwitchItem"> + <attr name="android:checked" /> + </declare-styleable> + +</resources> diff --git a/library/eclair-mr1/src/com/android/setupwizardlib/items/SwitchItem.java b/library/eclair-mr1/src/com/android/setupwizardlib/items/SwitchItem.java new file mode 100644 index 0000000..a75431f --- /dev/null +++ b/library/eclair-mr1/src/com/android/setupwizardlib/items/SwitchItem.java @@ -0,0 +1,99 @@ +/* + * 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. + */ + +package com.android.setupwizardlib.items; + +import android.content.Context; +import android.content.res.TypedArray; +import android.support.v7.widget.SwitchCompat; +import android.util.AttributeSet; +import android.view.View; +import android.widget.CompoundButton; + +import com.android.setupwizardlib.R; + +/** + * An Item with a switch, which the user can + */ +public class SwitchItem extends Item implements CompoundButton.OnCheckedChangeListener { + + public interface OnCheckedChangeListener { + void onCheckedChange(SwitchItem item, boolean isChecked); + } + + private boolean mChecked = false; + private OnCheckedChangeListener mListener; + + public SwitchItem() { + super(); + } + + public SwitchItem(Context context, AttributeSet attrs) { + super(context, attrs); + final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwSwitchItem); + mChecked = a.getBoolean(R.styleable.SuwSwitchItem_android_checked, false); + a.recycle(); + } + + public void setChecked(boolean checked) { + if (mChecked != checked) { + mChecked = checked; + notifyChanged(); + if (mListener != null) { + mListener.onCheckedChange(this, checked); + } + } + } + + public boolean isChecked() { + return mChecked; + } + + protected int getDefaultLayoutResource() { + return R.layout.suw_items_switch; + } + + /** + * Toggle the checked state of the switch, without invalidating the entire item. + * + * @param view The root view of this item, typically from the argument of onItemClick. + */ + public void toggle(View view) { + mChecked = !mChecked; + final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch); + switchView.setChecked(mChecked); + } + + @Override + public void onBindView(View view) { + super.onBindView(view); + final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch); + switchView.setChecked(mChecked); + switchView.setOnCheckedChangeListener(this); + switchView.setEnabled(isEnabled()); + } + + public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { + mListener = listener; + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (mListener != null) { + mListener.onCheckedChange(this, isChecked); + } + } +} diff --git a/library/eclair-mr1/test/src/com/android/setupwizardlib/test/SwitchItemTest.java b/library/eclair-mr1/test/src/com/android/setupwizardlib/test/SwitchItemTest.java new file mode 100644 index 0000000..e7c93ba --- /dev/null +++ b/library/eclair-mr1/test/src/com/android/setupwizardlib/test/SwitchItemTest.java @@ -0,0 +1,171 @@ +/* + * 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. + */ + +package com.android.setupwizardlib.test; + +import android.support.v7.widget.SwitchCompat; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; +import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.setupwizardlib.R; +import com.android.setupwizardlib.items.SwitchItem; + +public class SwitchItemTest extends AndroidTestCase { + + private SwitchCompat mSwitch; + + @SmallTest + public void testChecked() { + SwitchItem item = new SwitchItem(); + item.setTitle("TestTitle"); + item.setSummary("TestSummary"); + View view = createLayout(); + + item.setChecked(true); + + item.onBindView(view); + + assertTrue("Switch should be checked", mSwitch.isChecked()); + } + + @SmallTest + public void testNotChecked() { + SwitchItem item = new SwitchItem(); + item.setTitle("TestTitle"); + item.setSummary("TestSummary"); + View view = createLayout(); + + item.setChecked(false); + + item.onBindView(view); + + assertFalse("Switch should be unchecked", mSwitch.isChecked()); + } + + @SmallTest + public void testListener() { + SwitchItem item = new SwitchItem(); + item.setTitle("TestTitle"); + item.setSummary("TestSummary"); + View view = createLayout(); + + item.setChecked(true); + + final TestOnCheckedChangeListener listener = new TestOnCheckedChangeListener(); + item.setOnCheckedChangeListener(listener); + + item.onBindView(view); + + assertTrue("Switch should be checked", mSwitch.isChecked()); + mSwitch.setChecked(false); + + assertTrue("Listener should be called", listener.called); + assertFalse("Listener should not be checked", listener.checked); + + mSwitch.setChecked(true); + + assertTrue("Listener should be called", listener.called); + assertTrue("Listener should be checked", listener.checked); + } + + @SmallTest + public void testListenerSetChecked() { + // Check that calling setChecked on the item will also call the listener. + + SwitchItem item = new SwitchItem(); + item.setTitle("TestTitle"); + item.setSummary("TestSummary"); + View view = createLayout(); + + item.setChecked(true); + + final TestOnCheckedChangeListener listener = new TestOnCheckedChangeListener(); + item.setOnCheckedChangeListener(listener); + + item.onBindView(view); + + assertTrue("Switch should be checked", mSwitch.isChecked()); + item.setChecked(false); + + assertTrue("Listener should be called", listener.called); + assertFalse("Listener should not be checked", listener.checked); + + item.setChecked(true); + + assertTrue("Listener should be called", listener.called); + assertTrue("Listener should be checked", listener.checked); + } + + @SmallTest + public void testToggle() { + SwitchItem item = new SwitchItem(); + item.setTitle("TestTitle"); + item.setSummary("TestSummary"); + View view = createLayout(); + + item.setChecked(true); + item.onBindView(view); + + assertTrue("Switch should be checked", mSwitch.isChecked()); + + item.toggle(view); + + assertFalse("Switch should be unchecked", mSwitch.isChecked()); + } + + private ViewGroup createLayout() { + ViewGroup root = new FrameLayout(mContext); + + TextView titleView = new TextView(mContext); + titleView.setId(R.id.suw_items_title); + root.addView(titleView); + + TextView summaryView = new TextView(mContext); + summaryView.setId(R.id.suw_items_summary); + root.addView(summaryView); + + FrameLayout iconContainer = new FrameLayout(mContext); + iconContainer.setId(R.id.suw_items_icon_container); + root.addView(iconContainer); + + ImageView iconView = new ImageView(mContext); + iconView.setId(R.id.suw_items_icon); + iconContainer.addView(iconView); + + mSwitch = new SwitchCompat(mContext); + mSwitch.setId(R.id.suw_items_switch); + root.addView(mSwitch); + + return root; + } + + private static class TestOnCheckedChangeListener implements SwitchItem.OnCheckedChangeListener { + + public boolean called = false; + public boolean checked = false; + + @Override + public void onCheckedChange(SwitchItem item, boolean isChecked) { + called = true; + checked = isChecked; + } + } +} diff --git a/library/rules.gradle b/library/rules.gradle index e99c994..8efdd5c 100644 --- a/library/rules.gradle +++ b/library/rules.gradle @@ -96,8 +96,12 @@ android { res.srcDirs = ['test/res'] } + androidTestEclairMr1Compat { + java.srcDirs = ['eclair-mr1/test/src'] + } + androidTestFullSupport { - java.srcDirs = ['full-support/test/src'] + java.srcDirs = ['full-support/test/src', 'eclair-mr1/test/src'] res.srcDirs = ['full-support/test/res'] } } |