Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 1 | /* |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 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 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 21 | import com.android.contacts.ScrollingTabWidget.OnTabSelectionChangedListener; |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 22 | import com.android.contacts.model.ContactsSource; |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 23 | import com.android.contacts.util.NotifyingAsyncQueryHandler; |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 24 | import com.android.contacts.model.Sources; |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 25 | import com.android.internal.widget.ContactHeaderWidget; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 26 | |
| 27 | import android.app.Activity; |
| 28 | import android.content.ContentUris; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 30 | import android.content.Entity; |
| 31 | import android.content.EntityIterator; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 32 | import android.content.Intent; |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageManager; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 34 | import android.database.Cursor; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 35 | import android.graphics.drawable.Drawable; |
| 36 | import android.net.Uri; |
| 37 | import android.os.Bundle; |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 38 | import android.os.RemoteException; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 39 | import android.provider.ContactsContract.RawContacts; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 40 | import android.util.Log; |
| 41 | import android.util.SparseArray; |
| 42 | import android.view.LayoutInflater; |
| 43 | import android.view.View; |
Jeff Sharkey | 14f61ab | 2009-08-05 21:02:37 -0700 | [diff] [blame] | 44 | import android.view.ViewGroup; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 45 | import android.view.Window; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 46 | import android.widget.ImageView; |
| 47 | import android.widget.TextView; |
| 48 | |
| 49 | /** |
| 50 | * The base Activity class for viewing and editing a contact. |
| 51 | */ |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 52 | public abstract class BaseContactCardActivity extends Activity implements |
| 53 | NotifyingAsyncQueryHandler.AsyncQueryListener, OnTabSelectionChangedListener { |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 54 | |
| 55 | private static final String TAG = "BaseContactCardActivity"; |
| 56 | |
| 57 | private SparseArray<Long> mTabRawContactIdMap; |
| 58 | protected Uri mUri; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 59 | protected ScrollingTabWidget mTabWidget; |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 60 | protected ContactHeaderWidget mContactHeaderWidget; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 61 | private NotifyingAsyncQueryHandler mHandler; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 62 | |
| 63 | protected LayoutInflater mInflater; |
| 64 | |
| 65 | //Projection used for the query that determines which tabs to add. |
| 66 | protected static final String[] TAB_PROJECTION = new String[] { |
| 67 | RawContacts._ID, |
| 68 | RawContacts.ACCOUNT_NAME, |
| 69 | RawContacts.ACCOUNT_TYPE |
| 70 | }; |
| 71 | protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0; |
| 72 | protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1; |
| 73 | protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2; |
| 74 | |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame^] | 75 | protected static final String SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContact"; |
| 76 | protected long mSelectedRawContactId; |
| 77 | |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 78 | private static final int TOKEN_TABS = 0; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 79 | |
| 80 | @Override |
| 81 | protected void onCreate(Bundle icicle) { |
| 82 | super.onCreate(icicle); |
| 83 | |
| 84 | mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 85 | |
| 86 | final Intent intent = getIntent(); |
| 87 | mUri = intent.getData(); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 88 | |
| 89 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 90 | setContentView(R.layout.contact_card_layout); |
| 91 | |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 92 | mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget); |
| 93 | mContactHeaderWidget.showStar(true); |
| 94 | mContactHeaderWidget.bindFromContactId(ContentUris.parseId(mUri)); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 95 | mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 96 | |
| 97 | mTabWidget.setTabSelectionListener(this); |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 98 | mTabWidget.setVisibility(View.INVISIBLE); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 99 | mTabRawContactIdMap = new SparseArray<Long>(); |
| 100 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 101 | mHandler = new NotifyingAsyncQueryHandler(this, this); |
| 102 | |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 103 | // TODO: turn this into async call instead of blocking ui |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 104 | asyncSetupTabs(); |
| 105 | } |
| 106 | |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame^] | 107 | |
| 108 | @Override |
| 109 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 110 | super.onRestoreInstanceState(savedInstanceState); |
| 111 | mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | protected void onSaveInstanceState(Bundle outState) { |
| 116 | super.onSaveInstanceState(outState); |
| 117 | outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId); |
| 118 | } |
| 119 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 120 | private void asyncSetupTabs() { |
Dmitri Plotnikov | 8832a64 | 2009-08-06 17:24:34 -0700 | [diff] [blame] | 121 | long contactId = ContentUris.parseId(mUri); |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 122 | mHandler.startQueryEntities(TOKEN_TABS, null, |
| 123 | RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame^] | 127 | * Return the RawContact id associated with the tab at an index. |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 128 | * |
| 129 | * @param index The index of the tab in question. |
| 130 | * @return The contactId associated with the tab at the specified index. |
| 131 | */ |
| 132 | protected long getTabRawContactId(int index) { |
| 133 | return mTabRawContactIdMap.get(index); |
| 134 | } |
| 135 | |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame^] | 136 | /** |
| 137 | * Return the tab index associated with the RawContact id. |
| 138 | * |
| 139 | * @param index The index of the tab in question. |
| 140 | * @return The contactId associated with the tab at the specified index. |
| 141 | */ |
| 142 | protected int getTabIndexForRawContactId(long rawContactId) { |
| 143 | int numTabs = mTabRawContactIdMap.size(); |
| 144 | for (int i=0; i < numTabs; i++) { |
| 145 | if (mTabRawContactIdMap.get(i) == rawContactId) { |
| 146 | return i; |
| 147 | } |
| 148 | } |
| 149 | return -1; |
| 150 | } |
| 151 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 152 | /** {@inheritDoc} */ |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 153 | public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) { |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 154 | try{ |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 155 | if (token == TOKEN_TABS) { |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 156 | clearCurrentTabs(); |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 157 | bindTabs(readEntities(iterator)); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 158 | } |
| 159 | } finally { |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 160 | if (iterator != null) { |
| 161 | iterator.close(); |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 162 | } |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 166 | /** {@inheritDoc} */ |
| 167 | public void onQueryComplete(int token, Object cookie, Cursor cursor) { |
| 168 | // Emtpy |
| 169 | } |
| 170 | |
| 171 | private ArrayList<Entity> readEntities(EntityIterator iterator) { |
| 172 | ArrayList<Entity> entities = new ArrayList<Entity>(); |
| 173 | try { |
| 174 | while (iterator.hasNext()) { |
| 175 | entities.add(iterator.next()); |
| 176 | } |
| 177 | } catch (RemoteException e) { |
| 178 | } |
| 179 | |
| 180 | return entities; |
| 181 | } |
| 182 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Adds a tab for each {@link RawContact} associated with this contact. |
| 185 | * Override this method if you want to additional tabs and/or different |
| 186 | * tabs for your activity. |
| 187 | * |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 188 | * @param entities An {@link ArrayList} of {@link Entity}s of all the RawContacts |
| 189 | * associated with the contact being displayed. |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 190 | */ |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 191 | protected void bindTabs(ArrayList<Entity> entities) { |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 192 | final Sources sources = Sources.getInstance(this); |
| 193 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 194 | for (Entity entity : entities) { |
| 195 | final String accountType = entity.getEntityValues(). |
| 196 | getAsString(RawContacts.ACCOUNT_TYPE); |
| 197 | final Long rawContactId = entity.getEntityValues(). |
| 198 | getAsLong(RawContacts._ID); |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 199 | |
| 200 | // TODO: ensure inflation on background task so we don't block UI thread here |
| 201 | final ContactsSource source = sources.getInflatedSource(accountType, |
| 202 | ContactsSource.LEVEL_SUMMARY); |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 203 | addTab(rawContactId, createTabIndicatorView(mTabWidget, source)); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 204 | } |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame^] | 205 | |
| 206 | selectInitialTab(); |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 207 | mTabWidget.setVisibility(View.VISIBLE); |
| 208 | mTabWidget.postInvalidate(); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 211 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Add a tab to be displayed in the {@link ScrollingTabWidget}. |
| 214 | * |
| 215 | * @param contactId The contact id associated with the tab. |
| 216 | * @param label A label to display in the tab indicator. |
| 217 | * @param icon An icon to display in the tab indicator. |
| 218 | */ |
| 219 | protected void addTab(long contactId, String label, Drawable icon) { |
Jeff Sharkey | 14f61ab | 2009-08-05 21:02:37 -0700 | [diff] [blame] | 220 | addTab(contactId, createTabIndicatorView(mTabWidget, label, icon)); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Add a tab to be displayed in the {@link ScrollingTabWidget}. |
| 225 | * |
| 226 | * @param contactId The contact id associated with the tab. |
| 227 | * @param view A view to use as the tab indicator. |
| 228 | */ |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 229 | protected void addTab(long rawContactId, View view) { |
| 230 | mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 231 | mTabWidget.addTab(view); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | protected void clearCurrentTabs() { |
| 236 | mTabRawContactIdMap.clear(); |
| 237 | mTabWidget.removeAllTabs(); |
| 238 | } |
| 239 | |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame^] | 240 | protected void selectInitialTab() { |
| 241 | int selectedTabIndex = -1; |
| 242 | if (mSelectedRawContactId > 0) { |
| 243 | selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId); |
| 244 | } |
| 245 | if (selectedTabIndex >= 0) { |
| 246 | mTabWidget.setCurrentTab(selectedTabIndex); |
| 247 | } else { |
| 248 | selectDefaultTab(); |
| 249 | } |
| 250 | } |
| 251 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 252 | /** |
| 253 | * Makes the default tab selection. This is called after the tabs have been |
| 254 | * bound for the first time, and whenever a new intent is received. Override |
| 255 | * this method if you want to customize the default tab behavior. |
| 256 | */ |
| 257 | protected void selectDefaultTab() { |
| 258 | // Select the first tab. |
| 259 | mTabWidget.setCurrentTab(0); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void onNewIntent(Intent newIntent) { |
| 264 | setIntent(newIntent); |
| 265 | selectDefaultTab(); |
| 266 | mUri = newIntent.getData(); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Utility for creating a standard tab indicator view. |
| 271 | * |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 272 | * @param parent The parent ViewGroup to attach the new view to. |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 273 | * @param label The label to display in the tab indicator. If null, not label will be displayed. |
| 274 | * @param icon The icon to display. If null, no icon will be displayed. |
| 275 | * @return The tab indicator View. |
| 276 | */ |
Jeff Sharkey | 14f61ab | 2009-08-05 21:02:37 -0700 | [diff] [blame] | 277 | public static View createTabIndicatorView(ViewGroup parent, CharSequence label, Drawable icon) { |
| 278 | final LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService( |
| 279 | Context.LAYOUT_INFLATER_SERVICE); |
| 280 | final View tabIndicator = inflater.inflate(R.layout.tab_indicator, parent, false); |
Evan Millar | 7466091 | 2009-08-19 17:36:33 -0700 | [diff] [blame] | 281 | tabIndicator.getBackground().setDither(true); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 282 | |
| 283 | final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title); |
| 284 | tv.setText(label); |
| 285 | |
| 286 | final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon); |
| 287 | iconView.setImageDrawable(icon); |
| 288 | |
| 289 | return tabIndicator; |
| 290 | } |
| 291 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 292 | /** |
| 293 | * Utility for creating a standard tab indicator view. |
| 294 | * |
| 295 | * @param context The label to display in the tab indicator. If null, not label will be displayed. |
| 296 | * @param parent The parent ViewGroup to attach the new view to. |
| 297 | * @param source The {@link ContactsSource} to build the tab view from. |
| 298 | * @return The tab indicator View. |
| 299 | */ |
| 300 | public static View createTabIndicatorView(ViewGroup parent, ContactsSource source) { |
| 301 | Drawable icon = null; |
| 302 | if (source != null) { |
| 303 | final String packageName = source.resPackageName; |
| 304 | if (source.iconRes > 0) { |
| 305 | try { |
| 306 | final Context authContext = parent.getContext(). |
| 307 | createPackageContext(packageName, 0); |
| 308 | icon = authContext.getResources().getDrawable(source.iconRes); |
| 309 | |
| 310 | } catch (PackageManager.NameNotFoundException e) { |
| 311 | Log.d(TAG, "error getting the Package Context for " + packageName, e); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | return createTabIndicatorView(parent, null, icon); |
| 316 | } |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 317 | } |