blob: 7d9b8809105c6f9c7a8b0cffc0b56d5fb0c53bc5 [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() {
101 Uri tabsUri = Uri.withAppendedPath(mUri, "raw_contacts");
102 mHandler.startQuery(TOKEN_TABS, null, tabsUri, TAB_PROJECTION, null, null, null);
103 }
104
105 /**
106 * Return the contactId associated with the tab at an index.
107 *
108 * @param index The index of the tab in question.
109 * @return The contactId associated with the tab at the specified index.
110 */
111 protected long getTabRawContactId(int index) {
112 return mTabRawContactIdMap.get(index);
113 }
114
115 /** {@inheritDoc} */
116 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Evan Millar7911ff52009-07-21 15:55:18 -0700117 try{
Evan Millar1ea3bf72009-07-30 13:46:19 -0700118 if (token == TOKEN_TABS) {
Evan Millar7911ff52009-07-21 15:55:18 -0700119 clearCurrentTabs();
120 bindTabs(cursor);
121 }
122 } finally {
Evan Millar1ea3bf72009-07-30 13:46:19 -0700123 if (cursor != null) {
124 cursor.close();
125 }
Evan Millar7911ff52009-07-21 15:55:18 -0700126 }
127 }
128
129 /**
130 * Adds a tab for each {@link RawContact} associated with this contact.
131 * Override this method if you want to additional tabs and/or different
132 * tabs for your activity.
133 *
134 * @param tabsCursor A cursor over all the RawContacts associated with
135 * the contact being displayed. Use {@link TAB_CONTACT_ID_COLUMN_INDEX},
136 * {@link TAB_ACCOUNT_NAME_COLUMN_INDEX}, {@link TAB_ACCOUNT_TYPE_COLUMN_INDEX},
137 * and {@link TAB_PACKAGE_COLUMN_INDEX} as column indexes on the cursor.
138 */
139 protected void bindTabs(Cursor tabsCursor) {
140 while (tabsCursor.moveToNext()) {
141 long contactId = tabsCursor.getLong(TAB_CONTACT_ID_COLUMN_INDEX);
142
143 //TODO: figure out how to get the icon
144 Drawable tabIcon = null;
145 addTab(contactId, null, tabIcon);
146 }
147 selectDefaultTab();
148
149 }
150
Evan Millar7911ff52009-07-21 15:55:18 -0700151 /**
152 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
153 *
154 * @param contactId The contact id associated with the tab.
155 * @param label A label to display in the tab indicator.
156 * @param icon An icon to display in the tab indicator.
157 */
158 protected void addTab(long contactId, String label, Drawable icon) {
159 addTab(contactId, createTabIndicatorView(label, icon));
160 }
161
162 /**
163 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
164 *
165 * @param contactId The contact id associated with the tab.
166 * @param view A view to use as the tab indicator.
167 */
168 protected void addTab(long contactId, View view) {
169 mTabRawContactIdMap.put(mTabWidget.getTabCount(), contactId);
170 mTabWidget.addTab(view);
171 }
172
173
174 protected void clearCurrentTabs() {
175 mTabRawContactIdMap.clear();
176 mTabWidget.removeAllTabs();
177 }
178
179 /**
180 * Makes the default tab selection. This is called after the tabs have been
181 * bound for the first time, and whenever a new intent is received. Override
182 * this method if you want to customize the default tab behavior.
183 */
184 protected void selectDefaultTab() {
185 // Select the first tab.
186 mTabWidget.setCurrentTab(0);
187 }
188
189 @Override
190 public void onNewIntent(Intent newIntent) {
191 setIntent(newIntent);
192 selectDefaultTab();
193 mUri = newIntent.getData();
194 }
195
196 /**
197 * Utility for creating a standard tab indicator view.
198 *
199 * @param label The label to display in the tab indicator. If null, not label will be displayed.
200 * @param icon The icon to display. If null, no icon will be displayed.
201 * @return The tab indicator View.
202 */
203 protected View createTabIndicatorView(String label, Drawable icon) {
204 View tabIndicator = mInflater.inflate(R.layout.tab_indicator, mTabWidget, false);
205
206 final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title);
207 tv.setText(label);
208
209 final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon);
210 iconView.setImageDrawable(icon);
211
212 return tabIndicator;
213 }
214
215}