Home | History | Annotate | Download | only in widget
      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 
     17 package com.android.contacts.widget;
     18 
     19 import android.app.ListActivity;
     20 import android.content.Context;
     21 import android.database.Cursor;
     22 import android.database.MatrixCursor;
     23 import android.os.Bundle;
     24 import android.os.Handler;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.TextView;
     29 
     30 import com.android.contacts.R;
     31 import com.android.contacts.common.list.PinnedHeaderListAdapter;
     32 
     33 /**
     34  * An activity that demonstrates various use cases for the {@link com.android.contacts.common.list.PinnedHeaderListView}.
     35  * If we decide to move PinnedHeaderListView to the framework, this class could go
     36  * to API demos.
     37  */
     38 public class PinnedHeaderListDemoActivity extends ListActivity {
     39 
     40     public final static class TestPinnedHeaderListAdapter extends PinnedHeaderListAdapter {
     41 
     42         public TestPinnedHeaderListAdapter(Context context) {
     43             super(context);
     44             setPinnedPartitionHeadersEnabled(true);
     45         }
     46 
     47         private String[] mHeaders;
     48         private int mPinnedHeaderCount;
     49 
     50         public void setHeaders(String[] headers) {
     51             this.mHeaders = headers;
     52         }
     53 
     54         @Override
     55         protected View newHeaderView(Context context, int partition, Cursor cursor,
     56                 ViewGroup parent) {
     57             LayoutInflater inflater = LayoutInflater.from(context);
     58             return inflater.inflate(R.layout.list_section, null);
     59         }
     60 
     61         @Override
     62         protected void bindHeaderView(View view, int parition, Cursor cursor) {
     63             TextView headerText = (TextView)view.findViewById(R.id.header_text);
     64             headerText.setText(mHeaders[parition]);
     65         }
     66 
     67         @Override
     68         protected View newView(Context context, int partition, Cursor cursor, int position,
     69                 ViewGroup parent) {
     70             LayoutInflater inflater = LayoutInflater.from(context);
     71             return inflater.inflate(android.R.layout.simple_list_item_1, null);
     72         }
     73 
     74         @Override
     75         protected void bindView(View v, int partition, Cursor cursor, int position) {
     76             TextView text = (TextView)v.findViewById(android.R.id.text1);
     77             text.setText(cursor.getString(1));
     78         }
     79 
     80         @Override
     81         public View getPinnedHeaderView(int viewIndex, View convertView, ViewGroup parent) {
     82             LayoutInflater inflater = LayoutInflater.from(getContext());
     83             View view = inflater.inflate(R.layout.list_section, parent, false);
     84             view.setFocusable(false);
     85             view.setEnabled(false);
     86             bindHeaderView(view, viewIndex, null);
     87             return view;
     88         }
     89 
     90         @Override
     91         public int getPinnedHeaderCount() {
     92             return mPinnedHeaderCount;
     93         }
     94     }
     95 
     96     private Handler mHandler = new Handler();
     97 
     98     @Override
     99     protected void onCreate(Bundle bundle) {
    100         super.onCreate(bundle);
    101 
    102         setContentView(R.layout.pinned_header_list_demo);
    103 
    104         final TestPinnedHeaderListAdapter adapter = new TestPinnedHeaderListAdapter(this);
    105 
    106         Bundle extras = getIntent().getExtras();
    107         int[] counts = extras.getIntArray("counts");
    108         String[] names = extras.getStringArray("names");
    109         boolean[] showIfEmpty = extras.getBooleanArray("showIfEmpty");
    110         boolean[] hasHeader = extras.getBooleanArray("headers");
    111         int[] delays = extras.getIntArray("delays");
    112 
    113         if (counts == null || names == null || showIfEmpty == null || delays == null) {
    114             throw new IllegalArgumentException("Missing required extras");
    115         }
    116 
    117         adapter.setHeaders(names);
    118         for (int i = 0; i < counts.length; i++) {
    119             adapter.addPartition(showIfEmpty[i], names[i] != null);
    120             adapter.mPinnedHeaderCount = names.length;
    121         }
    122         setListAdapter(adapter);
    123         for (int i = 0; i < counts.length; i++) {
    124             final int sectionId = i;
    125             final Cursor cursor = makeCursor(names[i], counts[i]);
    126             mHandler.postDelayed(new Runnable() {
    127 
    128                 public void run() {
    129                     adapter.changeCursor(sectionId, cursor);
    130 
    131                 }
    132             }, delays[i]);
    133         }
    134     }
    135 
    136     private Cursor makeCursor(String name, int count) {
    137         MatrixCursor cursor = new MatrixCursor(new String[]{"_id", name});
    138         for (int i = 0; i < count; i++) {
    139             cursor.addRow(new Object[]{i, name + "[" + i + "]"});
    140         }
    141         return cursor;
    142     }
    143 }
    144