Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2007 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.phone;
     18 
     19 import static android.view.Window.PROGRESS_VISIBILITY_OFF;
     20 import static android.view.Window.PROGRESS_VISIBILITY_ON;
     21 
     22 import android.app.ListActivity;
     23 import android.content.AsyncQueryHandler;
     24 import android.content.ContentResolver;
     25 import android.content.Intent;
     26 import android.database.Cursor;
     27 import android.net.Uri;
     28 import android.os.Bundle;
     29 import android.util.Log;
     30 import android.view.Window;
     31 import android.widget.CursorAdapter;
     32 import android.widget.SimpleCursorAdapter;
     33 import android.widget.TextView;
     34 
     35 /**
     36  * ADN List activity for the Phone app.
     37  */
     38 public class ADNList extends ListActivity {
     39     protected static final String TAG = "ADNList";
     40     protected static final boolean DBG = false;
     41 
     42     private static final String[] COLUMN_NAMES = new String[] {
     43         "name",
     44         "number",
     45         "emails"
     46     };
     47 
     48     protected static final int NAME_COLUMN = 0;
     49     protected static final int NUMBER_COLUMN = 1;
     50     protected static final int EMAILS_COLUMN = 2;
     51 
     52     private static final int[] VIEW_NAMES = new int[] {
     53         android.R.id.text1,
     54         android.R.id.text2
     55     };
     56 
     57     protected static final int QUERY_TOKEN = 0;
     58     protected static final int INSERT_TOKEN = 1;
     59     protected static final int UPDATE_TOKEN = 2;
     60     protected static final int DELETE_TOKEN = 3;
     61 
     62 
     63     protected QueryHandler mQueryHandler;
     64     protected CursorAdapter mCursorAdapter;
     65     protected Cursor mCursor = null;
     66 
     67     private TextView mEmptyText;
     68 
     69     protected int mInitialSelection = -1;
     70 
     71     @Override
     72     protected void onCreate(Bundle icicle) {
     73         super.onCreate(icicle);
     74         getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
     75         setContentView(R.layout.adn_list);
     76         mEmptyText = (TextView) findViewById(android.R.id.empty);
     77         mQueryHandler = new QueryHandler(getContentResolver());
     78     }
     79 
     80     @Override
     81     protected void onResume() {
     82         super.onResume();
     83         query();
     84     }
     85 
     86     @Override
     87     protected void onStop() {
     88         super.onStop();
     89         if (mCursor != null) {
     90             mCursor.deactivate();
     91         }
     92     }
     93 
     94     protected Uri resolveIntent() {
     95         Intent intent = getIntent();
     96         if (intent.getData() == null) {
     97             intent.setData(Uri.parse("content://icc/adn"));
     98         }
     99 
    100         return intent.getData();
    101     }
    102 
    103     private void query() {
    104         Uri uri = resolveIntent();
    105         if (DBG) log("query: starting an async query");
    106         mQueryHandler.startQuery(QUERY_TOKEN, null, uri, COLUMN_NAMES,
    107                 null, null, null);
    108         displayProgress(true);
    109     }
    110 
    111     private void reQuery() {
    112         query();
    113     }
    114 
    115     private void setAdapter() {
    116         // NOTE:
    117         // As it it written, the positioning code below is NOT working.
    118         // However, this current non-working state is in compliance with
    119         // the UI paradigm, so we can't really do much to change it.
    120 
    121         // In the future, if we wish to get this "positioning" correct,
    122         // we'll need to do the following:
    123         //   1. Change the layout to in the cursor adapter to:
    124         //     android.R.layout.simple_list_item_checked
    125         //   2. replace the selection / focus code with:
    126         //     getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    127         //     getListView().setItemChecked(mInitialSelection, true);
    128 
    129         // Since the positioning is really only useful for the dialer's
    130         // SpecialCharSequence case (dialing '2#' to get to the 2nd
    131         // contact for instance), it doesn't make sense to mess with
    132         // the usability of the activity just for this case.
    133 
    134         // These artifacts include:
    135         //  1. UI artifacts (checkbox and highlight at the same time)
    136         //  2. Allowing the user to edit / create new SIM contacts when
    137         //    the user is simply trying to retrieve a number into the d
    138         //    dialer.
    139 
    140         if (mCursorAdapter == null) {
    141             mCursorAdapter = newAdapter();
    142 
    143             setListAdapter(mCursorAdapter);
    144         } else {
    145             mCursorAdapter.changeCursor(mCursor);
    146         }
    147 
    148         if (mInitialSelection >=0 && mInitialSelection < mCursorAdapter.getCount()) {
    149             setSelection(mInitialSelection);
    150             getListView().setFocusableInTouchMode(true);
    151             boolean gotfocus = getListView().requestFocus();
    152         }
    153     }
    154 
    155     protected CursorAdapter newAdapter() {
    156         return new SimpleCursorAdapter(this,
    157                     android.R.layout.simple_list_item_2,
    158                     mCursor, COLUMN_NAMES, VIEW_NAMES);
    159     }
    160 
    161     private void displayProgress(boolean flag) {
    162         if (DBG) log("displayProgress: " + flag);
    163         mEmptyText.setText(flag ? R.string.simContacts_emptyLoading: R.string.simContacts_empty);
    164         getWindow().setFeatureInt(
    165                 Window.FEATURE_INDETERMINATE_PROGRESS,
    166                 flag ? PROGRESS_VISIBILITY_ON : PROGRESS_VISIBILITY_OFF);
    167     }
    168 
    169     private class QueryHandler extends AsyncQueryHandler {
    170         public QueryHandler(ContentResolver cr) {
    171             super(cr);
    172         }
    173 
    174         @Override
    175         protected void onQueryComplete(int token, Object cookie, Cursor c) {
    176             if (DBG) log("onQueryComplete: cursor.count=" + c.getCount());
    177             mCursor = c;
    178             setAdapter();
    179             displayProgress(false);
    180         }
    181 
    182         @Override
    183         protected void onInsertComplete(int token, Object cookie,
    184                                         Uri uri) {
    185             if (DBG) log("onInsertComplete: requery");
    186             reQuery();
    187         }
    188 
    189         @Override
    190         protected void onUpdateComplete(int token, Object cookie, int result) {
    191             if (DBG) log("onUpdateComplete: requery");
    192             reQuery();
    193         }
    194 
    195         @Override
    196         protected void onDeleteComplete(int token, Object cookie, int result) {
    197             if (DBG) log("onDeleteComplete: requery");
    198             reQuery();
    199         }
    200     }
    201 
    202     protected void log(String msg) {
    203         Log.d(TAG, "[ADNList] " + msg);
    204     }
    205 }
    206