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