[SuwLib] Add SwitchItem
Bug: 27600894 Change-Id: Idca6f2fad550561ed71f9eb1bcae8ba54c5dae33
This commit is contained in:
parent
a8c0365b2a
commit
6e55f30c31
75
library/eclair-mr1/res/layout/suw_items_switch.xml
Normal file
75
library/eclair-mr1/res/layout/suw_items_switch.xml
Normal file
|
@ -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>
|
24
library/eclair-mr1/res/values/attrs.xml
Normal file
24
library/eclair-mr1/res/values/attrs.xml
Normal file
|
@ -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>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -96,8 +96,12 @@ android {
|
||||||
res.srcDirs = ['test/res']
|
res.srcDirs = ['test/res']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
androidTestEclairMr1Compat {
|
||||||
|
java.srcDirs = ['eclair-mr1/test/src']
|
||||||
|
}
|
||||||
|
|
||||||
androidTestFullSupport {
|
androidTestFullSupport {
|
||||||
java.srcDirs = ['full-support/test/src']
|
java.srcDirs = ['full-support/test/src', 'eclair-mr1/test/src']
|
||||||
res.srcDirs = ['full-support/test/res']
|
res.srcDirs = ['full-support/test/res']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue