blob: d6ed5fd50ca51470e2405553ccc715654d56660b [file] [log] [blame]
Evan Millar7911ff52009-07-21 15:55:18 -07001/*
2 * Copyright (C) 2008 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;
18
19import com.android.contacts.ScrollingTabWidget.OnTabSelectionChangedListener;
20import com.android.contacts.NotifyingAsyncQueryHandler.QueryCompleteListener;
21
22import android.app.Activity;
23import android.content.ContentUris;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
27import android.database.Cursor;
28import android.graphics.Bitmap;
29import android.graphics.drawable.Drawable;
30import android.net.Uri;
31import android.os.Bundle;
32import android.os.SystemClock;
33import android.provider.SocialContract;
34import android.provider.ContactsContract.Contacts;
35import android.provider.ContactsContract.RawContacts;
36import android.provider.SocialContract.Activities;
37import android.util.Log;
38import android.util.SparseArray;
39import android.view.LayoutInflater;
40import android.view.View;
41import android.view.Window;
42import android.widget.CheckBox;
43import android.widget.ImageView;
44import android.widget.TextView;
45
46/**
47 * The base Activity class for viewing and editing a contact.
48 */
49public abstract class BaseContactCardActivity extends Activity
50 implements QueryCompleteListener, OnTabSelectionChangedListener, View.OnClickListener {
51
52 private static final String TAG = "BaseContactCardActivity";
53
54 private SparseArray<Long> mTabRawContactIdMap;
55 protected Uri mUri;
56 protected long mContactId;
57 protected ScrollingTabWidget mTabWidget;
58 private NotifyingAsyncQueryHandler mHandler;
59 private TextView mDisplayNameView;
60 private TextView mPhoneticNameView;
61 private CheckBox mStarredView;
62 private ImageView mPhotoView;
63 private TextView mStatusView;
64
65 private int mNoPhotoResource;
66
67 protected LayoutInflater mInflater;
68
69 //Projection used for the query that determines which tabs to add.
70 protected static final String[] TAB_PROJECTION = new String[] {
71 RawContacts._ID,
72 RawContacts.ACCOUNT_NAME,
73 RawContacts.ACCOUNT_TYPE
74 };
75 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
76 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
77 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
78
79 //Projection used for the summary info in the header.
80 protected static final String[] HEADER_PROJECTION = new String[] {
81 Contacts.DISPLAY_NAME,
82 Contacts.STARRED,
83 Contacts.PHOTO_ID,
84 };
85 protected static final int HEADER_DISPLAY_NAME_COLUMN_INDEX = 0;
86 //TODO: We need to figure out how we're going to get the phonetic name.
87 //static final int HEADER_PHONETIC_NAME_COLUMN_INDEX
88 protected static final int HEADER_STARRED_COLUMN_INDEX = 1;
89 protected static final int HEADER_PHOTO_ID_COLUMN_INDEX = 2;
90
91 //Projection used for finding the most recent social status.
92 protected static final String[] SOCIAL_PROJECTION = new String[] {
93 Activities.TITLE,
94 Activities.PUBLISHED,
95 };
96 protected static final int SOCIAL_TITLE_COLUMN_INDEX = 0;
97 protected static final int SOCIAL_PUBLISHED_COLUMN_INDEX = 1;
98
99 private static final int TOKEN_HEADER = 0;
100 private static final int TOKEN_SOCIAL = 1;
101 private static final int TOKEN_TABS = 2;
102
103 @Override
104 protected void onCreate(Bundle icicle) {
105 super.onCreate(icicle);
106
107 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
108
109 final Intent intent = getIntent();
110 mUri = intent.getData();
111 mContactId = ContentUris.parseId(mUri);
112
113 requestWindowFeature(Window.FEATURE_NO_TITLE);
114 setContentView(R.layout.contact_card_layout);
115
116 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
117 mDisplayNameView = (TextView) findViewById(R.id.name);
118 mPhoneticNameView = (TextView) findViewById(R.id.phonetic_name);
119 mStarredView = (CheckBox) findViewById(R.id.star);
120 mStarredView.setOnClickListener(this);
121 mPhotoView = (ImageView) findViewById(R.id.photo);
122 mStatusView = (TextView) findViewById(R.id.status);
123
124 mTabWidget.setTabSelectionListener(this);
125 mTabRawContactIdMap = new SparseArray<Long>();
126
127 // Set the photo with a random "no contact" image
128 long now = SystemClock.elapsedRealtime();
129 int num = (int) now & 0xf;
130 if (num < 9) {
131 // Leaning in from right, common
132 mNoPhotoResource = R.drawable.ic_contact_picture;
133 } else if (num < 14) {
134 // Leaning in from left uncommon
135 mNoPhotoResource = R.drawable.ic_contact_picture_2;
136 } else {
137 // Coming in from the top, rare
138 mNoPhotoResource = R.drawable.ic_contact_picture_3;
139 }
140
141 mHandler = new NotifyingAsyncQueryHandler(this, this);
142
143 setupTabs();
144 setupHeader();
145 }
146
147 private void setupHeader() {
148 Uri headerUri = Uri.withAppendedPath(mUri, "data");
149
150 Uri socialUri = ContentUris.withAppendedId(
151 SocialContract.Activities.CONTENT_CONTACT_STATUS_URI, mContactId);
152
153 mHandler.startQuery(TOKEN_HEADER, null, headerUri, HEADER_PROJECTION, null, null, null);
154 mHandler.startQuery(TOKEN_SOCIAL, null, socialUri, SOCIAL_PROJECTION, null, null, null);
155 }
156
157 private void setupTabs() {
158 Uri tabsUri = Uri.withAppendedPath(mUri, "raw_contacts");
159 mHandler.startQuery(TOKEN_TABS, null, tabsUri, TAB_PROJECTION, null, null, null);
160 }
161
162 /**
163 * Return the contactId associated with the tab at an index.
164 *
165 * @param index The index of the tab in question.
166 * @return The contactId associated with the tab at the specified index.
167 */
168 protected long getTabRawContactId(int index) {
169 return mTabRawContactIdMap.get(index);
170 }
171
172 /** {@inheritDoc} */
173 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
174 if (cursor == null) {
175 return;
176 }
177 try{
178 if (token == TOKEN_HEADER) {
179 bindHeader(cursor);
180 } else if (token == TOKEN_SOCIAL) {
181 bindSocial(cursor);
182 } else if (token == TOKEN_TABS) {
183 clearCurrentTabs();
184 bindTabs(cursor);
185 }
186 } finally {
187 cursor.close();
188 }
189 }
190
191 /**
192 * Adds a tab for each {@link RawContact} associated with this contact.
193 * Override this method if you want to additional tabs and/or different
194 * tabs for your activity.
195 *
196 * @param tabsCursor A cursor over all the RawContacts associated with
197 * the contact being displayed. Use {@link TAB_CONTACT_ID_COLUMN_INDEX},
198 * {@link TAB_ACCOUNT_NAME_COLUMN_INDEX}, {@link TAB_ACCOUNT_TYPE_COLUMN_INDEX},
199 * and {@link TAB_PACKAGE_COLUMN_INDEX} as column indexes on the cursor.
200 */
201 protected void bindTabs(Cursor tabsCursor) {
202 while (tabsCursor.moveToNext()) {
203 long contactId = tabsCursor.getLong(TAB_CONTACT_ID_COLUMN_INDEX);
204
205 //TODO: figure out how to get the icon
206 Drawable tabIcon = null;
207 addTab(contactId, null, tabIcon);
208 }
209 selectDefaultTab();
210
211 }
212
213 //TODO: This will be part of the ContactHeaderWidget eventually.
214 protected void bindHeader(Cursor c) {
215 if (c == null) {
216 return;
217 }
218 if (c.moveToFirst()) {
219 //Set name
220 String displayName = c.getString(HEADER_DISPLAY_NAME_COLUMN_INDEX);
221 Log.i(TAG, displayName);
222 mDisplayNameView.setText(displayName);
223 //TODO: Bring back phonetic name
224 /*if (mPhoneticNameView != null) {
225 String phoneticName = c.getString(CONTACT_PHONETIC_NAME_COLUMN);
226 mPhoneticNameView.setText(phoneticName);
227 }*/
228
229 //Set starred
230 boolean starred = c.getInt(HEADER_STARRED_COLUMN_INDEX) == 1;
231 mStarredView.setChecked(starred);
232
233 //Set the photo
234 long photoId = c.getLong(HEADER_PHOTO_ID_COLUMN_INDEX);
235 Bitmap photoBitmap = ContactsUtils.loadContactPhoto(
236 this, photoId, null);
237 if (photoBitmap == null) {
238 photoBitmap = ContactsUtils.loadPlaceholderPhoto(mNoPhotoResource, this, null);
239 }
240 mPhotoView.setImageBitmap(photoBitmap);
241 }
242 }
243
244 //TODO: This will be part of the ContactHeaderWidget eventually.
245 protected void bindSocial(Cursor c) {
246 if (c == null) {
247 return;
248 }
249 if (c.moveToFirst()) {
250 String status = c.getString(SOCIAL_TITLE_COLUMN_INDEX);
251 mStatusView.setText(status);
252 }
253 }
254
255 //TODO: This will be part of the ContactHeaderWidget eventually.
256 public void onClick(View view) {
257 switch (view.getId()) {
258 case R.id.star: {
259 Uri uri = Uri.withAppendedPath(mUri, "data");
260 Cursor c = getContentResolver().query(uri, HEADER_PROJECTION, null, null, null);
261 try {
262 c.moveToFirst();
263 int oldStarredState = c.getInt(HEADER_STARRED_COLUMN_INDEX);
264 ContentValues values = new ContentValues(1);
265 values.put(Contacts.STARRED, oldStarredState == 1 ? 0 : 1);
266 getContentResolver().update(mUri, values, null, null);
267 } finally {
268 c.close();
269 }
270 setupHeader();
271 break;
272 }
273 }
274 }
275
276 /**
277 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
278 *
279 * @param contactId The contact id associated with the tab.
280 * @param label A label to display in the tab indicator.
281 * @param icon An icon to display in the tab indicator.
282 */
283 protected void addTab(long contactId, String label, Drawable icon) {
284 addTab(contactId, createTabIndicatorView(label, icon));
285 }
286
287 /**
288 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
289 *
290 * @param contactId The contact id associated with the tab.
291 * @param view A view to use as the tab indicator.
292 */
293 protected void addTab(long contactId, View view) {
294 mTabRawContactIdMap.put(mTabWidget.getTabCount(), contactId);
295 mTabWidget.addTab(view);
296 }
297
298
299 protected void clearCurrentTabs() {
300 mTabRawContactIdMap.clear();
301 mTabWidget.removeAllTabs();
302 }
303
304 /**
305 * Makes the default tab selection. This is called after the tabs have been
306 * bound for the first time, and whenever a new intent is received. Override
307 * this method if you want to customize the default tab behavior.
308 */
309 protected void selectDefaultTab() {
310 // Select the first tab.
311 mTabWidget.setCurrentTab(0);
312 }
313
314 @Override
315 public void onNewIntent(Intent newIntent) {
316 setIntent(newIntent);
317 selectDefaultTab();
318 mUri = newIntent.getData();
319 }
320
321 /**
322 * Utility for creating a standard tab indicator view.
323 *
324 * @param label The label to display in the tab indicator. If null, not label will be displayed.
325 * @param icon The icon to display. If null, no icon will be displayed.
326 * @return The tab indicator View.
327 */
328 protected View createTabIndicatorView(String label, Drawable icon) {
329 View tabIndicator = mInflater.inflate(R.layout.tab_indicator, mTabWidget, false);
330
331 final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title);
332 tv.setText(label);
333
334 final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon);
335 iconView.setImageDrawable(icon);
336
337 return tabIndicator;
338 }
339
340}