1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts.widget;
18
19import com.android.contacts.R;
20
21import android.app.ListActivity;
22import android.content.Context;
23import android.database.Cursor;
24import android.database.MatrixCursor;
25import android.os.Bundle;
26import android.os.Handler;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.TextView;
31
32/**
33 * An activity that demonstrates various use cases for the {@link PinnedHeaderListView}.
34 * If we decide to move PinnedHeaderListView to the framework, this class could go
35 * to API demos.
36 */
37public class PinnedHeaderListDemoActivity extends ListActivity {
38
39    public final static class TestPinnedHeaderListAdapter extends PinnedHeaderListAdapter {
40
41        public TestPinnedHeaderListAdapter(Context context) {
42            super(context);
43            setPinnedPartitionHeadersEnabled(true);
44        }
45
46        private String[] mHeaders;
47        private int mPinnedHeaderCount;
48
49        public void setHeaders(String[] headers) {
50            this.mHeaders = headers;
51        }
52
53        @Override
54        protected View newHeaderView(Context context, int partition, Cursor cursor,
55                ViewGroup parent) {
56            LayoutInflater inflater = LayoutInflater.from(context);
57            return inflater.inflate(R.layout.list_section, null);
58        }
59
60        @Override
61        protected void bindHeaderView(View view, int parition, Cursor cursor) {
62            TextView headerText = (TextView)view.findViewById(R.id.header_text);
63            headerText.setText(mHeaders[parition]);
64        }
65
66        @Override
67        protected View newView(Context context, int partition, Cursor cursor, int position,
68                ViewGroup parent) {
69            LayoutInflater inflater = LayoutInflater.from(context);
70            return inflater.inflate(android.R.layout.simple_list_item_1, null);
71        }
72
73        @Override
74        protected void bindView(View v, int partition, Cursor cursor, int position) {
75            TextView text = (TextView)v.findViewById(android.R.id.text1);
76            text.setText(cursor.getString(1));
77        }
78
79        @Override
80        public View getPinnedHeaderView(int viewIndex, View convertView, ViewGroup parent) {
81            LayoutInflater inflater = LayoutInflater.from(getContext());
82            View view = inflater.inflate(R.layout.list_section, parent, false);
83            view.setFocusable(false);
84            view.setEnabled(false);
85            bindHeaderView(view, viewIndex, null);
86            return view;
87        }
88
89        @Override
90        public int getPinnedHeaderCount() {
91            return mPinnedHeaderCount;
92        }
93    }
94
95    private Handler mHandler = new Handler();
96
97    @Override
98    protected void onCreate(Bundle bundle) {
99        super.onCreate(bundle);
100
101        setContentView(R.layout.pinned_header_list_demo);
102
103        final TestPinnedHeaderListAdapter adapter = new TestPinnedHeaderListAdapter(this);
104
105        Bundle extras = getIntent().getExtras();
106        int[] counts = extras.getIntArray("counts");
107        String[] names = extras.getStringArray("names");
108        boolean[] showIfEmpty = extras.getBooleanArray("showIfEmpty");
109        boolean[] hasHeader = extras.getBooleanArray("headers");
110        int[] delays = extras.getIntArray("delays");
111
112        if (counts == null || names == null || showIfEmpty == null || delays == null) {
113            throw new IllegalArgumentException("Missing required extras");
114        }
115
116        adapter.setHeaders(names);
117        for (int i = 0; i < counts.length; i++) {
118            adapter.addPartition(showIfEmpty[i], names[i] != null);
119            adapter.mPinnedHeaderCount = names.length;
120        }
121        setListAdapter(adapter);
122        for (int i = 0; i < counts.length; i++) {
123            final int sectionId = i;
124            final Cursor cursor = makeCursor(names[i], counts[i]);
125            mHandler.postDelayed(new Runnable() {
126
127                public void run() {
128                    adapter.changeCursor(sectionId, cursor);
129
130                }
131            }, delays[i]);
132        }
133    }
134
135    private Cursor makeCursor(String name, int count) {
136        MatrixCursor cursor = new MatrixCursor(new String[]{"_id", name});
137        for (int i = 0; i < count; i++) {
138            cursor.addRow(new Object[]{i, name + "[" + i + "]"});
139        }
140        return cursor;
141    }
142}
143