diff --git a/library/main/res/layout/suw_glif_header.xml b/library/main/res/layout/suw_glif_header.xml new file mode 100644 index 0000000..71283b2 --- /dev/null +++ b/library/main/res/layout/suw_glif_header.xml @@ -0,0 +1,36 @@ + + + + + + + + + + diff --git a/library/main/res/layout/suw_glif_list_template.xml b/library/main/res/layout/suw_glif_list_template.xml new file mode 100644 index 0000000..3756898 --- /dev/null +++ b/library/main/res/layout/suw_glif_list_template.xml @@ -0,0 +1,24 @@ + + + + diff --git a/library/main/res/layout/suw_glif_template.xml b/library/main/res/layout/suw_glif_template.xml index d9be0b8..f867e50 100644 --- a/library/main/res/layout/suw_glif_template.xml +++ b/library/main/res/layout/suw_glif_template.xml @@ -27,18 +27,7 @@ android:layout_height="wrap_content" android:orientation="vertical"> - - - + + + + + diff --git a/library/main/src/com/android/setupwizardlib/GlifListLayout.java b/library/main/src/com/android/setupwizardlib/GlifListLayout.java new file mode 100644 index 0000000..99df2a4 --- /dev/null +++ b/library/main/src/com/android/setupwizardlib/GlifListLayout.java @@ -0,0 +1,110 @@ +/* + * 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; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.res.TypedArray; +import android.os.Build.VERSION_CODES; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ListAdapter; +import android.widget.ListView; + +import com.android.setupwizardlib.items.ItemAdapter; +import com.android.setupwizardlib.items.ItemGroup; +import com.android.setupwizardlib.items.ItemInflater; + +/** + * A GLIF themed layout with a ListView. {@code android:entries} can also be used to specify an + * {@link com.android.setupwizardlib.items.ItemHierarchy} to be used with this layout in XML. + */ +public class GlifListLayout extends GlifLayout { + + private static final String TAG = "GlifListLayout"; + private ListView mListView; + + public GlifListLayout(Context context) { + this(context, 0, 0); + } + + public GlifListLayout(Context context, int template) { + this(context, template, 0); + } + + public GlifListLayout(Context context, int template, int containerId) { + super(context, template, containerId); + init(context, null, 0); + } + + public GlifListLayout(Context context, AttributeSet attrs) { + super(context, attrs); + init(context, attrs, 0); + } + + @TargetApi(VERSION_CODES.HONEYCOMB) + public GlifListLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(context, attrs, defStyleAttr); + } + + private void init(Context context, AttributeSet attrs, int defStyleAttr) { + final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwGlifListLayout, + defStyleAttr, 0); + final int xml = a.getResourceId(R.styleable.SuwGlifListLayout_android_entries, 0); + if (xml != 0) { + final ItemGroup inflated = (ItemGroup) new ItemInflater(context).inflate(xml); + setAdapter(new ItemAdapter(inflated)); + } + a.recycle(); + } + + @Override + protected View onInflateTemplate(LayoutInflater inflater, int template) { + if (template == 0) { + template = R.layout.suw_glif_list_template; + } + return super.onInflateTemplate(inflater, template); + } + + @Override + protected ViewGroup findContainer(int containerId) { + if (containerId == 0) { + containerId = android.R.id.list; + } + return super.findContainer(containerId); + } + + @Override + protected void onTemplateInflated() { + mListView = (ListView) findViewById(android.R.id.list); + } + + public ListView getListView() { + return mListView; + } + + public void setAdapter(ListAdapter adapter) { + getListView().setAdapter(adapter); + } + + public ListAdapter getAdapter() { + return getListView().getAdapter(); + } +} diff --git a/library/test/res/layout/test_glif_list_layout.xml b/library/test/res/layout/test_glif_list_layout.xml new file mode 100644 index 0000000..77e9733 --- /dev/null +++ b/library/test/res/layout/test_glif_list_layout.xml @@ -0,0 +1,20 @@ + + + diff --git a/library/test/src/com/android/setupwizardlib/test/GlifListLayoutTest.java b/library/test/src/com/android/setupwizardlib/test/GlifListLayoutTest.java new file mode 100644 index 0000000..71e5369 --- /dev/null +++ b/library/test/src/com/android/setupwizardlib/test/GlifListLayoutTest.java @@ -0,0 +1,107 @@ +/* + * 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.content.Context; +import android.test.InstrumentationTestCase; +import android.test.suitebuilder.annotation.SmallTest; +import android.view.ContextThemeWrapper; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.HeaderViewListAdapter; +import android.widget.ListAdapter; +import android.widget.ListView; +import android.widget.TextView; + +import com.android.setupwizardlib.GlifListLayout; + +public class GlifListLayoutTest extends InstrumentationTestCase { + + private Context mContext; + + @Override + protected void setUp() throws Exception { + super.setUp(); + mContext = new ContextThemeWrapper(getInstrumentation().getContext(), + R.style.SuwThemeGlif_Light); + } + + @SmallTest + public void testDefaultTemplate() { + GlifListLayout layout = new GlifListLayout(mContext); + assertListTemplateInflated(layout); + } + + @SmallTest + public void testAddView() { + GlifListLayout layout = new GlifListLayout(mContext); + TextView tv = new TextView(mContext); + try { + layout.addView(tv); + fail("Adding view to ListLayout should throw"); + } catch (UnsupportedOperationException e) { + // Expected exception + } + } + + @SmallTest + public void testInflateFromXml() { + LayoutInflater inflater = LayoutInflater.from(mContext); + GlifListLayout layout = (GlifListLayout) + inflater.inflate(R.layout.test_glif_list_layout, null); + assertListTemplateInflated(layout); + } + + @SmallTest + public void testGetListView() { + GlifListLayout layout = new GlifListLayout(mContext); + assertListTemplateInflated(layout); + assertNotNull("getListView should not be null", layout.getListView()); + } + + @SmallTest + public void testAdapter() { + GlifListLayout layout = new GlifListLayout(mContext); + assertListTemplateInflated(layout); + + final ArrayAdapter adapter = + new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1); + adapter.add("Abracadabra"); + layout.setAdapter(adapter); + + final ListAdapter gotAdapter = layout.getAdapter(); + if (gotAdapter instanceof HeaderViewListAdapter) { + assertSame("Adapter got from GlifListLayout should be same as set", + adapter, ((HeaderViewListAdapter) gotAdapter).getWrappedAdapter()); + } else { + assertSame("Adapter got from GlifListLayout should be same as set", + adapter, gotAdapter); + } + } + + private void assertListTemplateInflated(GlifListLayout layout) { + View title = layout.findViewById(R.id.suw_layout_title); + assertNotNull("@id/suw_layout_title should not be null", title); + + View icon = layout.findViewById(R.id.suw_layout_icon); + assertNotNull("@id/suw_layout_icon should not be null", icon); + + View listView = layout.findViewById(android.R.id.list); + assertTrue("@android:id/list should be a ListView", listView instanceof ListView); + } +}