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 | |
| 21 | import com.android.contacts.NotifyingAsyncQueryHandler.AsyncQueryListener; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 22 | import com.android.contacts.ScrollingTabWidget.OnTabSelectionChangedListener; |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 23 | import com.android.contacts.model.ContactsSource; |
| 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 | */ |
| 52 | public abstract class BaseContactCardActivity extends Activity |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 53 | implements AsyncQueryListener, OnTabSelectionChangedListener, |
| 54 | Sources.SourcesCompleteListener { |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 55 | |
| 56 | private static final String TAG = "BaseContactCardActivity"; |
| 57 | |
| 58 | private SparseArray<Long> mTabRawContactIdMap; |
| 59 | protected Uri mUri; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 60 | protected ScrollingTabWidget mTabWidget; |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 61 | protected ContactHeaderWidget mContactHeaderWidget; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 62 | private NotifyingAsyncQueryHandler mHandler; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 63 | |
| 64 | protected LayoutInflater mInflater; |
| 65 | |
| 66 | //Projection used for the query that determines which tabs to add. |
| 67 | protected static final String[] TAB_PROJECTION = new String[] { |
| 68 | RawContacts._ID, |
| 69 | RawContacts.ACCOUNT_NAME, |
| 70 | RawContacts.ACCOUNT_TYPE |
| 71 | }; |
| 72 | protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0; |
| 73 | protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1; |
| 74 | protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2; |
| 75 | |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 76 | private static final int TOKEN_TABS = 0; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 77 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 78 | private Sources mSources; |
| 79 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 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 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 103 | Sources.requestInstance(this, this); |
| 104 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 107 | public void onSourcesComplete(Sources sources) { |
| 108 | mSources = sources; |
| 109 | asyncSetupTabs(); |
| 110 | } |
| 111 | |
| 112 | private void asyncSetupTabs() { |
Dmitri Plotnikov | 8832a64 | 2009-08-06 17:24:34 -0700 | [diff] [blame] | 113 | long contactId = ContentUris.parseId(mUri); |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 114 | mHandler.startQueryEntities(TOKEN_TABS, null, |
| 115 | RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Return the contactId associated with the tab at an index. |
| 120 | * |
| 121 | * @param index The index of the tab in question. |
| 122 | * @return The contactId associated with the tab at the specified index. |
| 123 | */ |
| 124 | protected long getTabRawContactId(int index) { |
| 125 | return mTabRawContactIdMap.get(index); |
| 126 | } |
| 127 | |
| 128 | /** {@inheritDoc} */ |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 129 | public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) { |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 130 | try{ |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 131 | if (token == TOKEN_TABS) { |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 132 | clearCurrentTabs(); |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 133 | bindTabs(readEntities(iterator)); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 134 | } |
| 135 | } finally { |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 136 | if (iterator != null) { |
| 137 | iterator.close(); |
Evan Millar | 1ea3bf7 | 2009-07-30 13:46:19 -0700 | [diff] [blame] | 138 | } |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 142 | /** {@inheritDoc} */ |
| 143 | public void onQueryComplete(int token, Object cookie, Cursor cursor) { |
| 144 | // Emtpy |
| 145 | } |
| 146 | |
| 147 | private ArrayList<Entity> readEntities(EntityIterator iterator) { |
| 148 | ArrayList<Entity> entities = new ArrayList<Entity>(); |
| 149 | try { |
| 150 | while (iterator.hasNext()) { |
| 151 | entities.add(iterator.next()); |
| 152 | } |
| 153 | } catch (RemoteException e) { |
| 154 | } |
| 155 | |
| 156 | return entities; |
| 157 | } |
| 158 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 159 | /** |
| 160 | * Adds a tab for each {@link RawContact} associated with this contact. |
| 161 | * Override this method if you want to additional tabs and/or different |
| 162 | * tabs for your activity. |
| 163 | * |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 164 | * @param entities An {@link ArrayList} of {@link Entity}s of all the RawContacts |
| 165 | * associated with the contact being displayed. |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 166 | */ |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 167 | protected void bindTabs(ArrayList<Entity> entities) { |
| 168 | for (Entity entity : entities) { |
| 169 | final String accountType = entity.getEntityValues(). |
| 170 | getAsString(RawContacts.ACCOUNT_TYPE); |
| 171 | final Long rawContactId = entity.getEntityValues(). |
| 172 | getAsLong(RawContacts._ID); |
| 173 | ContactsSource source = mSources.getSourceForType(accountType); |
| 174 | addTab(rawContactId, createTabIndicatorView(mTabWidget, source)); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 175 | } |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 176 | mTabWidget.setCurrentTab(0); |
| 177 | mTabWidget.setVisibility(View.VISIBLE); |
| 178 | mTabWidget.postInvalidate(); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 181 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Add a tab to be displayed in the {@link ScrollingTabWidget}. |
| 184 | * |
| 185 | * @param contactId The contact id associated with the tab. |
| 186 | * @param label A label to display in the tab indicator. |
| 187 | * @param icon An icon to display in the tab indicator. |
| 188 | */ |
| 189 | protected void addTab(long contactId, String label, Drawable icon) { |
Jeff Sharkey | 14f61ab | 2009-08-05 21:02:37 -0700 | [diff] [blame] | 190 | addTab(contactId, createTabIndicatorView(mTabWidget, label, icon)); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Add a tab to be displayed in the {@link ScrollingTabWidget}. |
| 195 | * |
| 196 | * @param contactId The contact id associated with the tab. |
| 197 | * @param view A view to use as the tab indicator. |
| 198 | */ |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 199 | protected void addTab(long rawContactId, View view) { |
| 200 | mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 201 | mTabWidget.addTab(view); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | protected void clearCurrentTabs() { |
| 206 | mTabRawContactIdMap.clear(); |
| 207 | mTabWidget.removeAllTabs(); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Makes the default tab selection. This is called after the tabs have been |
| 212 | * bound for the first time, and whenever a new intent is received. Override |
| 213 | * this method if you want to customize the default tab behavior. |
| 214 | */ |
| 215 | protected void selectDefaultTab() { |
| 216 | // Select the first tab. |
| 217 | mTabWidget.setCurrentTab(0); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void onNewIntent(Intent newIntent) { |
| 222 | setIntent(newIntent); |
| 223 | selectDefaultTab(); |
| 224 | mUri = newIntent.getData(); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Utility for creating a standard tab indicator view. |
| 229 | * |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 230 | * @param parent The parent ViewGroup to attach the new view to. |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 231 | * @param label The label to display in the tab indicator. If null, not label will be displayed. |
| 232 | * @param icon The icon to display. If null, no icon will be displayed. |
| 233 | * @return The tab indicator View. |
| 234 | */ |
Jeff Sharkey | 14f61ab | 2009-08-05 21:02:37 -0700 | [diff] [blame] | 235 | public static View createTabIndicatorView(ViewGroup parent, CharSequence label, Drawable icon) { |
| 236 | final LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService( |
| 237 | Context.LAYOUT_INFLATER_SERVICE); |
| 238 | final View tabIndicator = inflater.inflate(R.layout.tab_indicator, parent, false); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 239 | |
| 240 | final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title); |
| 241 | tv.setText(label); |
| 242 | |
| 243 | final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon); |
| 244 | iconView.setImageDrawable(icon); |
| 245 | |
| 246 | return tabIndicator; |
| 247 | } |
| 248 | |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 249 | /** |
| 250 | * Utility for creating a standard tab indicator view. |
| 251 | * |
| 252 | * @param context The label to display in the tab indicator. If null, not label will be displayed. |
| 253 | * @param parent The parent ViewGroup to attach the new view to. |
| 254 | * @param source The {@link ContactsSource} to build the tab view from. |
| 255 | * @return The tab indicator View. |
| 256 | */ |
| 257 | public static View createTabIndicatorView(ViewGroup parent, ContactsSource source) { |
| 258 | Drawable icon = null; |
| 259 | if (source != null) { |
| 260 | final String packageName = source.resPackageName; |
| 261 | if (source.iconRes > 0) { |
| 262 | try { |
| 263 | final Context authContext = parent.getContext(). |
| 264 | createPackageContext(packageName, 0); |
| 265 | icon = authContext.getResources().getDrawable(source.iconRes); |
| 266 | |
| 267 | } catch (PackageManager.NameNotFoundException e) { |
| 268 | Log.d(TAG, "error getting the Package Context for " + packageName, e); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | return createTabIndicatorView(parent, null, icon); |
| 273 | } |
| 274 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 275 | } |