blob: 7d4bb198230af8dee205279b7a3a6248185310ac [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;
Jeff Sharkey14f61ab2009-08-05 21:02:37 -070042import android.view.ViewGroup;
Evan Millar7911ff52009-07-21 15:55:18 -070043import android.view.Window;
44import android.widget.CheckBox;
45import android.widget.ImageView;
46import android.widget.TextView;
47
48/**
49 * The base Activity class for viewing and editing a contact.
50 */
51public abstract class BaseContactCardActivity extends Activity
Evan Millar1ea3bf72009-07-30 13:46:19 -070052 implements QueryCompleteListener, OnTabSelectionChangedListener {
Evan Millar7911ff52009-07-21 15:55:18 -070053
54 private static final String TAG = "BaseContactCardActivity";
55
56 private SparseArray<Long> mTabRawContactIdMap;
57 protected Uri mUri;
Evan Millar7911ff52009-07-21 15:55:18 -070058 protected ScrollingTabWidget mTabWidget;
Evan Millar1ea3bf72009-07-30 13:46:19 -070059 protected ContactHeaderWidget mContactHeaderWidget;
Evan Millar7911ff52009-07-21 15:55:18 -070060 private NotifyingAsyncQueryHandler mHandler;
Evan Millar7911ff52009-07-21 15:55:18 -070061
62 protected LayoutInflater mInflater;
63
64 //Projection used for the query that determines which tabs to add.
65 protected static final String[] TAB_PROJECTION = new String[] {
66 RawContacts._ID,
67 RawContacts.ACCOUNT_NAME,
68 RawContacts.ACCOUNT_TYPE
69 };
70 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
71 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
72 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
73
Evan Millar1ea3bf72009-07-30 13:46:19 -070074 private static final int TOKEN_TABS = 0;
Evan Millar7911ff52009-07-21 15:55:18 -070075
76 @Override
77 protected void onCreate(Bundle icicle) {
78 super.onCreate(icicle);
79
80 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
81
82 final Intent intent = getIntent();
83 mUri = intent.getData();
Evan Millar7911ff52009-07-21 15:55:18 -070084
85 requestWindowFeature(Window.FEATURE_NO_TITLE);
86 setContentView(R.layout.contact_card_layout);
87
Evan Millar1ea3bf72009-07-30 13:46:19 -070088 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
89 mContactHeaderWidget.showStar(true);
90 mContactHeaderWidget.bindFromContactId(ContentUris.parseId(mUri));
Evan Millar7911ff52009-07-21 15:55:18 -070091 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
Evan Millar7911ff52009-07-21 15:55:18 -070092
93 mTabWidget.setTabSelectionListener(this);
94 mTabRawContactIdMap = new SparseArray<Long>();
95
Evan Millar7911ff52009-07-21 15:55:18 -070096 mHandler = new NotifyingAsyncQueryHandler(this, this);
97
98 setupTabs();
Evan Millar7911ff52009-07-21 15:55:18 -070099 }
100
101 private void setupTabs() {
Dmitri Plotnikov8832a642009-08-06 17:24:34 -0700102 long contactId = ContentUris.parseId(mUri);
103 mHandler.startQuery(TOKEN_TABS, null, RawContacts.CONTENT_URI, TAB_PROJECTION,
104 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Evan Millar7911ff52009-07-21 15:55:18 -0700105 }
106
107 /**
108 * Return the contactId associated with the tab at an index.
109 *
110 * @param index The index of the tab in question.
111 * @return The contactId associated with the tab at the specified index.
112 */
113 protected long getTabRawContactId(int index) {
114 return mTabRawContactIdMap.get(index);
115 }
116
117 /** {@inheritDoc} */
118 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Evan Millar7911ff52009-07-21 15:55:18 -0700119 try{
Evan Millar1ea3bf72009-07-30 13:46:19 -0700120 if (token == TOKEN_TABS) {
Evan Millar7911ff52009-07-21 15:55:18 -0700121 clearCurrentTabs();
122 bindTabs(cursor);
123 }
124 } finally {
Evan Millar1ea3bf72009-07-30 13:46:19 -0700125 if (cursor != null) {
126 cursor.close();
127 }
Evan Millar7911ff52009-07-21 15:55:18 -0700128 }
129 }
130
131 /**
132 * Adds a tab for each {@link RawContact} associated with this contact.
133 * Override this method if you want to additional tabs and/or different
134 * tabs for your activity.
135 *
136 * @param tabsCursor A cursor over all the RawContacts associated with
137 * the contact being displayed. Use {@link TAB_CONTACT_ID_COLUMN_INDEX},
138 * {@link TAB_ACCOUNT_NAME_COLUMN_INDEX}, {@link TAB_ACCOUNT_TYPE_COLUMN_INDEX},
139 * and {@link TAB_PACKAGE_COLUMN_INDEX} as column indexes on the cursor.
140 */
141 protected void bindTabs(Cursor tabsCursor) {
142 while (tabsCursor.moveToNext()) {
143 long contactId = tabsCursor.getLong(TAB_CONTACT_ID_COLUMN_INDEX);
144
145 //TODO: figure out how to get the icon
146 Drawable tabIcon = null;
147 addTab(contactId, null, tabIcon);
148 }
149 selectDefaultTab();
150
151 }
152
Evan Millar7911ff52009-07-21 15:55:18 -0700153 /**
154 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
155 *
156 * @param contactId The contact id associated with the tab.
157 * @param label A label to display in the tab indicator.
158 * @param icon An icon to display in the tab indicator.
159 */
160 protected void addTab(long contactId, String label, Drawable icon) {
Jeff Sharkey14f61ab2009-08-05 21:02:37 -0700161 addTab(contactId, createTabIndicatorView(mTabWidget, label, icon));
Evan Millar7911ff52009-07-21 15:55:18 -0700162 }
163
164 /**
165 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
166 *
167 * @param contactId The contact id associated with the tab.
168 * @param view A view to use as the tab indicator.
169 */
170 protected void addTab(long contactId, View view) {
171 mTabRawContactIdMap.put(mTabWidget.getTabCount(), contactId);
172 mTabWidget.addTab(view);
173 }
174
175
176 protected void clearCurrentTabs() {
177 mTabRawContactIdMap.clear();
178 mTabWidget.removeAllTabs();
179 }
180
181 /**
182 * Makes the default tab selection. This is called after the tabs have been
183 * bound for the first time, and whenever a new intent is received. Override
184 * this method if you want to customize the default tab behavior.
185 */
186 protected void selectDefaultTab() {
187 // Select the first tab.
188 mTabWidget.setCurrentTab(0);
189 }
190
191 @Override
192 public void onNewIntent(Intent newIntent) {
193 setIntent(newIntent);
194 selectDefaultTab();
195 mUri = newIntent.getData();
196 }
197
198 /**
199 * Utility for creating a standard tab indicator view.
200 *
201 * @param label The label to display in the tab indicator. If null, not label will be displayed.
202 * @param icon The icon to display. If null, no icon will be displayed.
203 * @return The tab indicator View.
204 */
Jeff Sharkey14f61ab2009-08-05 21:02:37 -0700205 public static View createTabIndicatorView(ViewGroup parent, CharSequence label, Drawable icon) {
206 final LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(
207 Context.LAYOUT_INFLATER_SERVICE);
208 final View tabIndicator = inflater.inflate(R.layout.tab_indicator, parent, false);
Evan Millar7911ff52009-07-21 15:55:18 -0700209
210 final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title);
211 tv.setText(label);
212
213 final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon);
214 iconView.setImageDrawable(icon);
215
216 return tabIndicator;
217 }
218
219}