Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts; |
| 18 | |
| 19 | import com.android.contacts.ScrollingTabWidget.OnTabSelectionChangedListener; |
| 20 | import com.android.contacts.NotifyingAsyncQueryHandler.QueryCompleteListener; |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 21 | import com.android.internal.widget.ContactHeaderWidget; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 22 | |
| 23 | import android.app.Activity; |
| 24 | import android.content.ContentUris; |
| 25 | import android.content.ContentValues; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.database.Cursor; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.drawable.Drawable; |
| 31 | import android.net.Uri; |
| 32 | import android.os.Bundle; |
| 33 | import android.os.SystemClock; |
| 34 | import android.provider.SocialContract; |
| 35 | import android.provider.ContactsContract.Contacts; |
| 36 | import android.provider.ContactsContract.RawContacts; |
| 37 | import android.provider.SocialContract.Activities; |
| 38 | import android.util.Log; |
| 39 | import android.util.SparseArray; |
| 40 | import android.view.LayoutInflater; |
| 41 | import android.view.View; |
Jeff Sharkey | 14f61ab | 2009-08-05 21:02:37 -0700 | [diff] [blame^] | 42 | import android.view.ViewGroup; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 43 | import android.view.Window; |
| 44 | import android.widget.CheckBox; |
| 45 | import android.widget.ImageView; |
| 46 | import android.widget.TextView; |
| 47 | |
| 48 | /** |
| 49 | * The base Activity class for viewing and editing a contact. |
| 50 | */ |
| 51 | public abstract class BaseContactCardActivity extends Activity |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 52 | implements QueryCompleteListener, OnTabSelectionChangedListener { |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 53 | |
| 54 | private static final String TAG = "BaseContactCardActivity"; |
| 55 | |
| 56 | private SparseArray<Long> mTabRawContactIdMap; |
| 57 | protected Uri mUri; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 58 | protected ScrollingTabWidget mTabWidget; |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 59 | protected ContactHeaderWidget mContactHeaderWidget; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 60 | private NotifyingAsyncQueryHandler mHandler; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 61 | |
| 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 Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 74 | private static final int TOKEN_TABS = 0; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 75 | |
| 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 Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 84 | |
| 85 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 86 | setContentView(R.layout.contact_card_layout); |
| 87 | |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 88 | mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget); |
| 89 | mContactHeaderWidget.showStar(true); |
| 90 | mContactHeaderWidget.bindFromContactId(ContentUris.parseId(mUri)); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 91 | mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 92 | |
| 93 | mTabWidget.setTabSelectionListener(this); |
| 94 | mTabRawContactIdMap = new SparseArray<Long>(); |
| 95 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 96 | mHandler = new NotifyingAsyncQueryHandler(this, this); |
| 97 | |
| 98 | setupTabs(); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private void setupTabs() { |
Dmitri Plotnikov | 8832a64 | 2009-08-06 17:24:34 -0700 | [diff] [blame] | 102 | long contactId = ContentUris.parseId(mUri); |
| 103 | mHandler.startQuery(TOKEN_TABS, null, RawContacts.CONTENT_URI, TAB_PROJECTION, |
| 104 | RawContacts.CONTACT_ID + "=" + contactId, null, null); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 105 | } |
| 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 Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 119 | try{ |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 120 | if (token == TOKEN_TABS) { |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 121 | clearCurrentTabs(); |
| 122 | bindTabs(cursor); |
| 123 | } |
| 124 | } finally { |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 125 | if (cursor != null) { |
| 126 | cursor.close(); |
| 127 | } |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 128 | } |
| 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 Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 153 | /** |
| 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 Sharkey | 14f61ab | 2009-08-05 21:02:37 -0700 | [diff] [blame^] | 161 | addTab(contactId, createTabIndicatorView(mTabWidget, label, icon)); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 162 | } |
| 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 Sharkey | 14f61ab | 2009-08-05 21:02:37 -0700 | [diff] [blame^] | 205 | 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 Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 209 | |
| 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 | } |