blob: 4378c45b86994642f61f4d6f6dfa9f99fdd63727 [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;
Evan Millar1ea3bf72009-07-30 13:46:19 -070021import com.android.internal.widget.ContactHeaderWidget;
Evan Millar7911ff52009-07-21 15:55:18 -070022
23import android.app.Activity;
24import android.content.ContentUris;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
28import android.database.Cursor;
29import android.graphics.Bitmap;
30import android.graphics.drawable.Drawable;
31import android.net.Uri;
32import android.os.Bundle;
33import android.os.SystemClock;
34import android.provider.SocialContract;
35import android.provider.ContactsContract.Contacts;
36import android.provider.ContactsContract.RawContacts;
37import android.provider.SocialContract.Activities;
38import android.util.Log;
39import android.util.SparseArray;
40import android.view.LayoutInflater;
41import android.view.View;
42import android.view.Window;
43import android.widget.CheckBox;
44import android.widget.ImageView;
45import android.widget.TextView;
46
47/**
48 * The base Activity class for viewing and editing a contact.
49 */
50public abstract class BaseContactCardActivity extends Activity
Evan Millar1ea3bf72009-07-30 13:46:19 -070051 implements QueryCompleteListener, OnTabSelectionChangedListener {
Evan Millar7911ff52009-07-21 15:55:18 -070052
53 private static final String TAG = "BaseContactCardActivity";
54
55 private SparseArray<Long> mTabRawContactIdMap;
56 protected Uri mUri;
Evan Millar7911ff52009-07-21 15:55:18 -070057 protected ScrollingTabWidget mTabWidget;
Evan Millar1ea3bf72009-07-30 13:46:19 -070058 protected ContactHeaderWidget mContactHeaderWidget;
Evan Millar7911ff52009-07-21 15:55:18 -070059 private NotifyingAsyncQueryHandler mHandler;
Evan Millar7911ff52009-07-21 15:55:18 -070060
61 protected LayoutInflater mInflater;
62
63 //Projection used for the query that determines which tabs to add.
64 protected static final String[] TAB_PROJECTION = new String[] {
65 RawContacts._ID,
66 RawContacts.ACCOUNT_NAME,
67 RawContacts.ACCOUNT_TYPE
68 };
69 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
70 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
71 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
72
Evan Millar1ea3bf72009-07-30 13:46:19 -070073 private static final int TOKEN_TABS = 0;
Evan Millar7911ff52009-07-21 15:55:18 -070074
75 @Override
76 protected void onCreate(Bundle icicle) {
77 super.onCreate(icicle);
78
79 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
80
81 final Intent intent = getIntent();
82 mUri = intent.getData();
Evan Millar7911ff52009-07-21 15:55:18 -070083
84 requestWindowFeature(Window.FEATURE_NO_TITLE);
85 setContentView(R.layout.contact_card_layout);
86
Evan Millar1ea3bf72009-07-30 13:46:19 -070087 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
88 mContactHeaderWidget.showStar(true);
89 mContactHeaderWidget.bindFromContactId(ContentUris.parseId(mUri));
Evan Millar7911ff52009-07-21 15:55:18 -070090 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
Evan Millar7911ff52009-07-21 15:55:18 -070091
92 mTabWidget.setTabSelectionListener(this);
93 mTabRawContactIdMap = new SparseArray<Long>();
94
Evan Millar7911ff52009-07-21 15:55:18 -070095 mHandler = new NotifyingAsyncQueryHandler(this, this);
96
97 setupTabs();
Evan Millar7911ff52009-07-21 15:55:18 -070098 }
99
100 private void setupTabs() {
Dmitri Plotnikov8832a642009-08-06 17:24:34 -0700101 long contactId = ContentUris.parseId(mUri);
102 mHandler.startQuery(TOKEN_TABS, null, RawContacts.CONTENT_URI, TAB_PROJECTION,
103 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Evan Millar7911ff52009-07-21 15:55:18 -0700104 }
105
106 /**
107 * Return the contactId associated with the tab at an index.
108 *
109 * @param index The index of the tab in question.
110 * @return The contactId associated with the tab at the specified index.
111 */
112 protected long getTabRawContactId(int index) {
113 return mTabRawContactIdMap.get(index);
114 }
115
116 /** {@inheritDoc} */
117 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Evan Millar7911ff52009-07-21 15:55:18 -0700118 try{
Evan Millar1ea3bf72009-07-30 13:46:19 -0700119 if (token == TOKEN_TABS) {
Evan Millar7911ff52009-07-21 15:55:18 -0700120 clearCurrentTabs();
121 bindTabs(cursor);
122 }
123 } finally {
Evan Millar1ea3bf72009-07-30 13:46:19 -0700124 if (cursor != null) {
125 cursor.close();
126 }
Evan Millar7911ff52009-07-21 15:55:18 -0700127 }
128 }
129
130 /**
131 * Adds a tab for each {@link RawContact} associated with this contact.
132 * Override this method if you want to additional tabs and/or different
133 * tabs for your activity.
134 *
135 * @param tabsCursor A cursor over all the RawContacts associated with
136 * the contact being displayed. Use {@link TAB_CONTACT_ID_COLUMN_INDEX},
137 * {@link TAB_ACCOUNT_NAME_COLUMN_INDEX}, {@link TAB_ACCOUNT_TYPE_COLUMN_INDEX},
138 * and {@link TAB_PACKAGE_COLUMN_INDEX} as column indexes on the cursor.
139 */
140 protected void bindTabs(Cursor tabsCursor) {
141 while (tabsCursor.moveToNext()) {
142 long contactId = tabsCursor.getLong(TAB_CONTACT_ID_COLUMN_INDEX);
143
144 //TODO: figure out how to get the icon
145 Drawable tabIcon = null;
146 addTab(contactId, null, tabIcon);
147 }
148 selectDefaultTab();
149
150 }
151
Evan Millar7911ff52009-07-21 15:55:18 -0700152 /**
153 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
154 *
155 * @param contactId The contact id associated with the tab.
156 * @param label A label to display in the tab indicator.
157 * @param icon An icon to display in the tab indicator.
158 */
159 protected void addTab(long contactId, String label, Drawable icon) {
160 addTab(contactId, createTabIndicatorView(label, icon));
161 }
162
163 /**
164 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
165 *
166 * @param contactId The contact id associated with the tab.
167 * @param view A view to use as the tab indicator.
168 */
169 protected void addTab(long contactId, View view) {
170 mTabRawContactIdMap.put(mTabWidget.getTabCount(), contactId);
171 mTabWidget.addTab(view);
172 }
173
174
175 protected void clearCurrentTabs() {
176 mTabRawContactIdMap.clear();
177 mTabWidget.removeAllTabs();
178 }
179
180 /**
181 * Makes the default tab selection. This is called after the tabs have been
182 * bound for the first time, and whenever a new intent is received. Override
183 * this method if you want to customize the default tab behavior.
184 */
185 protected void selectDefaultTab() {
186 // Select the first tab.
187 mTabWidget.setCurrentTab(0);
188 }
189
190 @Override
191 public void onNewIntent(Intent newIntent) {
192 setIntent(newIntent);
193 selectDefaultTab();
194 mUri = newIntent.getData();
195 }
196
197 /**
198 * Utility for creating a standard tab indicator view.
199 *
200 * @param label The label to display in the tab indicator. If null, not label will be displayed.
201 * @param icon The icon to display. If null, no icon will be displayed.
202 * @return The tab indicator View.
203 */
204 protected View createTabIndicatorView(String label, Drawable icon) {
205 View tabIndicator = mInflater.inflate(R.layout.tab_indicator, mTabWidget, false);
206
207 final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title);
208 tv.setText(label);
209
210 final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon);
211 iconView.setImageDrawable(icon);
212
213 return tabIndicator;
214 }
215
216}