The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 19 | import com.android.contacts.Collapser.Collapsible; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 20 | import com.android.contacts.ScrollingTabWidget.OnTabSelectionChangedListener; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 21 | import com.android.contacts.SplitAggregateView.OnContactSelectedListener; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 22 | import com.android.contacts.model.ContactsSource; |
| 23 | import com.android.contacts.model.Sources; |
| 24 | import com.android.contacts.model.ContactsSource.DataKind; |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 25 | import com.android.contacts.util.Constants; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 26 | import com.android.contacts.util.NotifyingAsyncQueryHandler; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 27 | import com.android.internal.telephony.ITelephony; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 28 | import com.android.internal.widget.ContactHeaderWidget; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 29 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 30 | import android.app.Activity; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 31 | import android.app.AlertDialog; |
| 32 | import android.app.Dialog; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 33 | import android.content.ActivityNotFoundException; |
| 34 | import android.content.ContentResolver; |
| 35 | import android.content.ContentUris; |
| 36 | import android.content.ContentValues; |
| 37 | import android.content.Context; |
| 38 | import android.content.DialogInterface; |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 39 | import android.content.Entity; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 40 | import android.content.EntityIterator; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 41 | import android.content.Intent; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 42 | import android.content.DialogInterface.OnClickListener; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 43 | import android.content.Entity.NamedContentValues; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 44 | import android.content.res.Resources; |
| 45 | import android.database.ContentObserver; |
| 46 | import android.database.Cursor; |
| 47 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 48 | import android.net.Uri; |
| 49 | import android.os.Bundle; |
| 50 | import android.os.Handler; |
| 51 | import android.os.RemoteException; |
| 52 | import android.os.ServiceManager; |
Dmitri Plotnikov | 4e3a043 | 2009-09-15 13:54:40 -0700 | [diff] [blame] | 53 | import android.provider.ContactsContract; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 54 | import android.provider.ContactsContract.AggregationExceptions; |
| 55 | import android.provider.ContactsContract.CommonDataKinds; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 56 | import android.provider.ContactsContract.Contacts; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 57 | import android.provider.ContactsContract.Data; |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 58 | import android.provider.ContactsContract.Presence; |
Dmitri Plotnikov | 3946659 | 2009-07-27 11:23:51 -0700 | [diff] [blame] | 59 | import android.provider.ContactsContract.RawContacts; |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 60 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 61 | import android.telephony.PhoneNumberUtils; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 62 | import android.text.TextUtils; |
| 63 | import android.util.Log; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 64 | import android.util.SparseArray; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 65 | import android.view.ContextMenu; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 66 | import android.view.ContextThemeWrapper; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 67 | import android.view.KeyEvent; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 68 | import android.view.LayoutInflater; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 69 | import android.view.Menu; |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 70 | import android.view.MenuInflater; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 71 | import android.view.MenuItem; |
| 72 | import android.view.View; |
| 73 | import android.view.ViewGroup; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 74 | import android.view.Window; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 75 | import android.view.ContextMenu.ContextMenuInfo; |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 76 | import android.view.animation.Animation; |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 77 | import android.view.animation.TranslateAnimation; |
| 78 | import android.view.animation.Animation.AnimationListener; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 79 | import android.widget.AdapterView; |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 80 | import android.widget.FrameLayout; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 81 | import android.widget.ImageView; |
| 82 | import android.widget.ListView; |
| 83 | import android.widget.TextView; |
| 84 | import android.widget.Toast; |
| 85 | |
| 86 | import java.util.ArrayList; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Displays the details of a specific contact. |
| 90 | */ |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 91 | public class ViewContactActivity extends Activity |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 92 | implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener, |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 93 | AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener, |
| 94 | OnTabSelectionChangedListener { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 95 | private static final String TAG = "ViewContact"; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 96 | |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 97 | public static final String RAW_CONTACT_ID_EXTRA = "rawContactIdExtra"; |
| 98 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 99 | private static final boolean SHOW_SEPARATORS = false; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 100 | |
| 101 | private static final int DIALOG_CONFIRM_DELETE = 1; |
| 102 | |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 103 | private static final int REQUEST_JOIN_CONTACT = 1; |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 104 | private static final int REQUEST_EDIT_CONTACT = 2; |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 105 | |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 106 | public static final int MENU_ITEM_MAKE_DEFAULT = 3; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 107 | |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 108 | private static final String SPLIT_MIMETYPE = "split_mimetype"; |
| 109 | |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 110 | protected Uri mLookupUri; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 111 | private Uri mUri; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 112 | private ContentResolver mResolver; |
| 113 | private ViewAdapter mAdapter; |
| 114 | private int mNumPhoneNumbers = 0; |
| 115 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 116 | /** |
Dmitri Plotnikov | e1cd679 | 2009-07-27 20:28:17 -0700 | [diff] [blame] | 117 | * A list of distinct contact IDs included in the current contact. |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 118 | */ |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 119 | private ArrayList<Long> mRawContactIds = new ArrayList<Long>(); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 120 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 121 | /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>(); |
| 122 | /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>(); |
| 123 | /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>(); |
| 124 | /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>(); |
| 125 | /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>(); |
| 126 | /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>(); |
The Android Open Source Project | cac191e | 2009-03-18 22:20:27 -0700 | [diff] [blame] | 127 | /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 128 | /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>(); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 129 | /* package */ ArrayList<ViewEntry> mSplitEntry = new ArrayList<ViewEntry>(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 130 | /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>(); |
| 131 | |
| 132 | private Cursor mCursor; |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 133 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 134 | private SparseArray<Long> mTabRawContactIdMap; |
| 135 | protected ScrollingTabWidget mTabWidget; |
| 136 | protected ContactHeaderWidget mContactHeaderWidget; |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 137 | protected View mBelowHeader; |
Tadashi G. Takaoka | 33aa1c6 | 2009-09-27 14:22:28 -0700 | [diff] [blame] | 138 | protected TextView mAccountName; |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 139 | protected View mBufferView; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 140 | private NotifyingAsyncQueryHandler mHandler; |
| 141 | |
| 142 | protected LayoutInflater mInflater; |
| 143 | |
| 144 | //Projection used for the query that determines which tabs to add. |
| 145 | protected static final String[] TAB_PROJECTION = new String[] { |
| 146 | RawContacts._ID, |
| 147 | RawContacts.ACCOUNT_NAME, |
| 148 | RawContacts.ACCOUNT_TYPE |
| 149 | }; |
| 150 | protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0; |
| 151 | protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1; |
| 152 | protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2; |
| 153 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 154 | private static final String SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContactKey"; |
| 155 | private static final String SAVED_STATE_TABS_VISIBLE_KEY = "tabsVisibleKey"; |
| 156 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 157 | protected Long mSelectedRawContactId = null; |
| 158 | |
| 159 | private static final int TOKEN_QUERY = 0; |
| 160 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 161 | private ContentObserver mObserver = new ContentObserver(new Handler()) { |
| 162 | @Override |
| 163 | public boolean deliverSelfNotifications() { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void onChange(boolean selfChange) { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 169 | if (mCursor != null && !mCursor.isClosed()) { |
| 170 | startEntityQuery(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | public void onClick(DialogInterface dialog, int which) { |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 176 | closeCursor(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 177 | getContentResolver().delete(mUri, null, null); |
| 178 | finish(); |
| 179 | } |
| 180 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 181 | private FrameLayout mTabContentLayout; |
| 182 | private ListView mListView; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 183 | private boolean mShowSmsLinksForAllPhones; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 184 | private ArrayList<Entity> mEntities = null; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 185 | |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 186 | private boolean mTabsVisible; |
| 187 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 188 | @Override |
| 189 | protected void onCreate(Bundle icicle) { |
| 190 | super.onCreate(icicle); |
| 191 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 192 | final Intent intent = getIntent(); |
Dmitri Plotnikov | 4e3a043 | 2009-09-15 13:54:40 -0700 | [diff] [blame] | 193 | Uri data = intent.getData(); |
| 194 | String authority = data.getAuthority(); |
| 195 | if (ContactsContract.AUTHORITY.equals(authority)) { |
| 196 | mLookupUri = data; |
| 197 | } else if (android.provider.Contacts.AUTHORITY.equals(authority)) { |
| 198 | final long rawContactId = ContentUris.parseId(data); |
| 199 | mLookupUri = RawContacts.getContactLookupUri(getContentResolver(), |
| 200 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId)); |
Dmitri Plotnikov | 83129f0 | 2009-09-02 19:04:41 -0700 | [diff] [blame] | 201 | |
Dmitri Plotnikov | 4e3a043 | 2009-09-15 13:54:40 -0700 | [diff] [blame] | 202 | } |
Dmitri Plotnikov | 83129f0 | 2009-09-02 19:04:41 -0700 | [diff] [blame] | 203 | mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 204 | |
| 205 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 206 | setContentView(R.layout.contact_card_layout); |
| 207 | |
| 208 | mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget); |
| 209 | mContactHeaderWidget.showStar(true); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 210 | mContactHeaderWidget.setExcludeMimes(new String[] { |
| 211 | Contacts.CONTENT_ITEM_TYPE |
| 212 | }); |
| 213 | |
| 214 | mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 215 | mTabWidget.setTabSelectionListener(this); |
Evan Millar | 8586311 | 2009-09-21 14:35:18 -0700 | [diff] [blame] | 216 | mTabWidget.setVisibility(View.GONE); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 217 | mTabsVisible = false; |
| 218 | |
| 219 | mBelowHeader = findViewById(R.id.below_header); |
Tadashi G. Takaoka | 33aa1c6 | 2009-09-27 14:22:28 -0700 | [diff] [blame] | 220 | mAccountName = (TextView) findViewById(R.id.account_name); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 221 | |
| 222 | mTabRawContactIdMap = new SparseArray<Long>(); |
| 223 | |
| 224 | mHandler = new NotifyingAsyncQueryHandler(this, this); |
| 225 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 226 | mListView = new ListView(this); |
| 227 | mListView.setOnCreateContextMenuListener(this); |
| 228 | mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY); |
| 229 | mListView.setOnItemClickListener(this); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 230 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 231 | mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent); |
| 232 | mTabContentLayout.addView(mListView); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 233 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 234 | mResolver = getContentResolver(); |
| 235 | |
| 236 | // Build the list of sections. The order they're added to mSections dictates the |
| 237 | // order they are displayed in the list. |
| 238 | mSections.add(mPhoneEntries); |
| 239 | mSections.add(mSmsEntries); |
| 240 | mSections.add(mEmailEntries); |
| 241 | mSections.add(mImEntries); |
| 242 | mSections.add(mPostalEntries); |
| 243 | mSections.add(mOrganizationEntries); |
The Android Open Source Project | cac191e | 2009-03-18 22:20:27 -0700 | [diff] [blame] | 244 | mSections.add(mGroupEntries); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 245 | mSections.add(mOtherEntries); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 246 | mSections.add(mSplitEntry); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 247 | |
| 248 | //TODO Read this value from a preference |
| 249 | mShowSmsLinksForAllPhones = true; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 250 | } |
| 251 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 252 | @Override |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 253 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 254 | super.onRestoreInstanceState(savedInstanceState); |
| 255 | long restoredRawContactId = savedInstanceState.getLong( |
| 256 | SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY, -1); |
| 257 | mSelectedRawContactId = restoredRawContactId != -1 ? restoredRawContactId : null; |
| 258 | mTabsVisible = savedInstanceState.getBoolean(SAVED_STATE_TABS_VISIBLE_KEY); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | protected void onSaveInstanceState(Bundle outState) { |
| 263 | super.onSaveInstanceState(outState); |
| 264 | if (mSelectedRawContactId != null) { |
| 265 | outState.putLong(SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId); |
| 266 | |
| 267 | } |
| 268 | outState.putBoolean(SAVED_STATE_TABS_VISIBLE_KEY, mTabsVisible); |
| 269 | } |
| 270 | |
| 271 | @Override |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 272 | protected void onResume() { |
| 273 | super.onResume(); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 274 | startEntityQuery(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Override |
| 278 | protected void onPause() { |
| 279 | super.onPause(); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 280 | closeCursor(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Override |
| 284 | protected void onDestroy() { |
| 285 | super.onDestroy(); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 286 | closeCursor(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | @Override |
| 290 | protected Dialog onCreateDialog(int id) { |
| 291 | switch (id) { |
| 292 | case DIALOG_CONFIRM_DELETE: |
| 293 | return new AlertDialog.Builder(this) |
| 294 | .setTitle(R.string.deleteConfirmation_title) |
| 295 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 296 | .setMessage(R.string.deleteConfirmation) |
| 297 | .setNegativeButton(android.R.string.cancel, null) |
| 298 | .setPositiveButton(android.R.string.ok, this) |
| 299 | .setCancelable(false) |
| 300 | .create(); |
| 301 | } |
| 302 | return null; |
| 303 | } |
| 304 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 305 | |
| 306 | // TAB CODE // |
| 307 | /** |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 308 | * Adds a tab for each {@link RawContacts} associated with this contact. |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 309 | * Override this method if you want to additional tabs and/or different |
| 310 | * tabs for your activity. |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 311 | */ |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 312 | protected void bindTabs() { |
| 313 | final Sources sources = Sources.getInstance(this); |
| 314 | |
| 315 | for (Entity entity : mEntities) { |
| 316 | final String accountType = entity.getEntityValues(). |
| 317 | getAsString(RawContacts.ACCOUNT_TYPE); |
| 318 | final Long rawContactId = entity.getEntityValues(). |
| 319 | getAsLong(RawContacts._ID); |
| 320 | |
| 321 | // TODO: ensure inflation on background task so we don't block UI thread here |
| 322 | final ContactsSource source = sources.getInflatedSource(accountType, |
| 323 | ContactsSource.LEVEL_SUMMARY); |
| 324 | addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(), |
| 325 | source)); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Add a tab to be displayed in the {@link ScrollingTabWidget}. |
| 331 | * |
| 332 | * @param rawContactId The contact id associated with the tab. |
| 333 | * @param view A view to use as the tab indicator. |
| 334 | */ |
| 335 | protected void addTab(long rawContactId, View view) { |
| 336 | mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId); |
| 337 | mTabWidget.addTab(view); |
| 338 | } |
| 339 | |
| 340 | |
| 341 | protected void clearCurrentTabs() { |
| 342 | mTabRawContactIdMap.clear(); |
| 343 | mTabWidget.removeAllTabs(); |
| 344 | } |
| 345 | |
| 346 | protected void selectInitialTab() { |
| 347 | int selectedTabIndex = 0; |
| 348 | |
| 349 | if (mSelectedRawContactId != null) { |
| 350 | selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId); |
| 351 | if (selectedTabIndex == -1) { |
| 352 | // If there was no matching tab, just select the first; |
| 353 | selectedTabIndex = 0; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | mTabWidget.setCurrentTab(selectedTabIndex); |
| 358 | onTabSelectionChanged(selectedTabIndex, false); |
| 359 | } |
| 360 | |
| 361 | public void onTabSelectionChanged(int tabIndex, boolean clicked) { |
| 362 | Long rawContactId = getTabRawContactId(tabIndex); |
| 363 | if (rawContactId != null) { |
| 364 | mSelectedRawContactId = rawContactId; |
| 365 | bindData(); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Return the RawContact id associated with the tab at an index. |
| 371 | * |
| 372 | * @param index The index of the tab in question. |
| 373 | * @return The contactId associated with the tab at the specified index. |
| 374 | */ |
| 375 | protected Long getTabRawContactId(int index) { |
| 376 | return mTabRawContactIdMap.get(index); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Return the tab index associated with the RawContact id. |
| 381 | * |
| 382 | * @param index The index of the tab in question. |
| 383 | * @return The contactId associated with the tab at the specified index. |
| 384 | */ |
| 385 | protected int getTabIndexForRawContactId(long rawContactId) { |
| 386 | int numTabs = mTabRawContactIdMap.size(); |
| 387 | for (int i=0; i < numTabs; i++) { |
| 388 | if (mTabRawContactIdMap.get(i) == rawContactId) { |
| 389 | return i; |
| 390 | } |
| 391 | } |
| 392 | return -1; |
| 393 | } |
| 394 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 395 | protected void showTabs(boolean show) { |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 396 | if (mTabsVisible == show) { |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | float tabHeight = getResources().getDimension(R.dimen.tab_height); |
| 401 | if (show) { |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 402 | TranslateAnimation showAnimation = new TranslateAnimation( |
| 403 | Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, |
| 404 | Animation.ABSOLUTE, -tabHeight, Animation.ABSOLUTE, 0); |
| 405 | showAnimation.setDuration(getResources().getInteger( |
| 406 | android.R.integer.config_longAnimTime)); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 407 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 408 | showAnimation.setAnimationListener(new AnimationListener() { |
| 409 | public void onAnimationEnd(Animation animation) { |
| 410 | selectInitialTab(); |
| 411 | bindData(); |
| 412 | } |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 413 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 414 | public void onAnimationRepeat(Animation animation) { |
| 415 | } |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 416 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 417 | public void onAnimationStart(Animation animation) { |
| 418 | } |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 419 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 420 | }); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 421 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 422 | mBelowHeader.startAnimation(showAnimation); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 423 | mTabWidget.setVisibility(View.VISIBLE); |
| 424 | mTabsVisible = true; |
| 425 | clearCurrentTabs(); |
| 426 | bindTabs(); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 427 | } else { |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 428 | TranslateAnimation hideTabsAnimation = new TranslateAnimation( |
| 429 | Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, |
| 430 | Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -tabHeight); |
| 431 | hideTabsAnimation.setDuration(getResources().getInteger( |
| 432 | android.R.integer.config_longAnimTime)); |
| 433 | hideTabsAnimation.setAnimationListener(new AnimationListener() { |
| 434 | public void onAnimationEnd(Animation animation) { |
| 435 | bindData(); |
| 436 | } |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 437 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 438 | public void onAnimationRepeat(Animation animation) { |
| 439 | } |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 440 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 441 | public void onAnimationStart(Animation animation) { |
| 442 | } |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 443 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 444 | }); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 445 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 446 | TranslateAnimation hideListAnimation = new TranslateAnimation( |
| 447 | Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, |
| 448 | Animation.ABSOLUTE, tabHeight, Animation.ABSOLUTE, 0); |
| 449 | hideListAnimation.setDuration(getResources().getInteger( |
| 450 | android.R.integer.config_longAnimTime)); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 451 | |
| 452 | |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 453 | mTabWidget.startAnimation(hideTabsAnimation); |
| 454 | mTabContentLayout.startAnimation(hideListAnimation); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 455 | mTabWidget.setVisibility(View.GONE); |
| 456 | mTabsVisible = false; |
| 457 | mSelectedRawContactId = null; |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 460 | |
| 461 | |
| 462 | // QUERY CODE // |
| 463 | /** {@inheritDoc} */ |
| 464 | public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) { |
| 465 | try{ |
| 466 | if (token == TOKEN_QUERY) { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 467 | mEntities = readEntities(iterator); |
Evan Millar | 8586311 | 2009-09-21 14:35:18 -0700 | [diff] [blame] | 468 | // Show the aggregate badge if this contact is aggregated. |
Evan Millar | cc87fb9 | 2009-09-22 15:27:47 -0700 | [diff] [blame] | 469 | boolean isAggregate = mEntities.size() > 1; |
| 470 | mContactHeaderWidget.showAggregateBadge(isAggregate); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 471 | if (mTabsVisible) { |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 472 | mTabWidget.setVisibility(View.VISIBLE); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 473 | clearCurrentTabs(); |
| 474 | bindTabs(); |
| 475 | selectInitialTab(); |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 476 | } else { |
| 477 | mTabWidget.setVisibility(View.GONE); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 478 | } |
Evan Millar | 8586311 | 2009-09-21 14:35:18 -0700 | [diff] [blame] | 479 | bindData(); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 480 | } |
| 481 | } finally { |
| 482 | if (iterator != null) { |
| 483 | iterator.close(); |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /** {@inheritDoc} */ |
| 489 | public void onQueryComplete(int token, Object cookie, Cursor cursor) { |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 490 | // Empty |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | private ArrayList<Entity> readEntities(EntityIterator iterator) { |
| 494 | ArrayList<Entity> entities = new ArrayList<Entity>(); |
| 495 | try { |
| 496 | while (iterator.hasNext()) { |
| 497 | entities.add(iterator.next()); |
| 498 | } |
| 499 | } catch (RemoteException e) { |
| 500 | } |
| 501 | |
| 502 | return entities; |
| 503 | } |
| 504 | |
| 505 | private void startEntityQuery() { |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 506 | closeCursor(); |
| 507 | |
Dmitri Plotnikov | 0306bbd | 2009-09-03 14:37:15 -0700 | [diff] [blame] | 508 | mUri = null; |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 509 | if (mLookupUri != null) { |
| 510 | mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri); |
| 511 | if (mLookupUri != null) { |
| 512 | mUri = Contacts.lookupContact(getContentResolver(), mLookupUri); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (mUri == null) { |
| 517 | |
| 518 | // TODO either figure out a way to prevent a flash of black background or |
| 519 | // use some other UI than a toast |
| 520 | Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show(); |
| 521 | Log.e(TAG, "invalid contact uri: " + mLookupUri); |
| 522 | finish(); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY), |
| 527 | new String[] {Contacts.DISPLAY_NAME}, null, null, null); |
| 528 | mCursor.registerContentObserver(mObserver); |
| 529 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 530 | long contactId = ContentUris.parseId(mUri); |
| 531 | mHandler.startQueryEntities(TOKEN_QUERY, null, |
| 532 | RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 533 | |
Evan Millar | 9b1a124 | 2009-09-16 10:26:10 -0700 | [diff] [blame] | 534 | mContactHeaderWidget.bindFromContactLookupUri(mLookupUri); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | private void closeCursor() { |
| 538 | if (mCursor != null) { |
| 539 | mCursor.unregisterContentObserver(mObserver); |
| 540 | mCursor.close(); |
| 541 | mCursor = null; |
| 542 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | private void bindData() { |
| 546 | |
| 547 | // Build up the contact entries |
| 548 | buildEntries(); |
| 549 | |
| 550 | // Collapse similar data items in select sections. |
| 551 | Collapser.collapseList(mPhoneEntries); |
| 552 | Collapser.collapseList(mSmsEntries); |
| 553 | Collapser.collapseList(mEmailEntries); |
| 554 | Collapser.collapseList(mPostalEntries); |
| 555 | |
| 556 | if (mAdapter == null) { |
| 557 | mAdapter = new ViewAdapter(this, mSections); |
| 558 | mListView.setAdapter(mAdapter); |
| 559 | } else { |
| 560 | mAdapter.setSections(mSections, SHOW_SEPARATORS); |
| 561 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | @Override |
| 565 | public boolean onCreateOptionsMenu(Menu menu) { |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 566 | super.onCreateOptionsMenu(menu); |
| 567 | |
| 568 | final MenuInflater inflater = getMenuInflater(); |
| 569 | inflater.inflate(R.menu.view, menu); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 570 | return true; |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 575 | super.onPrepareOptionsMenu(menu); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 576 | |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 577 | // If tabWidget is not shown enable "show sources", otherwise |
| 578 | // enable "hide sources" |
| 579 | if (mTabsVisible) { |
| 580 | menu.findItem(R.id.menu_show_sources).setVisible(false); |
| 581 | menu.findItem(R.id.menu_hide_sources).setVisible(true); |
| 582 | } else { |
| 583 | menu.findItem(R.id.menu_show_sources).setVisible(true); |
| 584 | menu.findItem(R.id.menu_hide_sources).setVisible(false); |
| 585 | } |
| 586 | |
Evan Millar | 51514db | 2009-09-22 11:42:35 -0700 | [diff] [blame] | 587 | // Only allow edit when we have at least one raw_contact id |
| 588 | final boolean hasRawContact = (mRawContactIds.size() > 0); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 589 | menu.findItem(R.id.menu_edit).setEnabled(hasRawContact); |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 590 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 591 | return true; |
| 592 | } |
| 593 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 594 | @Override |
| 595 | public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { |
| 596 | AdapterView.AdapterContextMenuInfo info; |
| 597 | try { |
| 598 | info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
| 599 | } catch (ClassCastException e) { |
| 600 | Log.e(TAG, "bad menuInfo", e); |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | // This can be null sometimes, don't crash... |
| 605 | if (info == null) { |
| 606 | Log.e(TAG, "bad menuInfo"); |
| 607 | return; |
| 608 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 609 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 610 | ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS); |
Neel Parekh | a8fb68a | 2009-09-25 18:05:18 -0700 | [diff] [blame] | 611 | menu.setHeaderTitle(R.string.contactOptionsTitle); |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 612 | if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) { |
| 613 | menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent); |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 614 | menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent); |
| 615 | if (!entry.isPrimary) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 616 | menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 617 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 618 | } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) { |
| 619 | menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent); |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 620 | if (!entry.isPrimary) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 621 | menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 622 | } |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 623 | } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 624 | menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent); |
| 625 | } |
| 626 | // TODO(emillar): add back with group support. |
| 627 | /* else if (entry.mimetype.equals()) { |
| 628 | menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent); |
| 629 | } */ |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | @Override |
| 633 | public boolean onOptionsItemSelected(MenuItem item) { |
| 634 | switch (item.getItemId()) { |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 635 | case R.id.menu_show_sources: { |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 636 | showTabs(true); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 637 | break; |
| 638 | } |
| 639 | case R.id.menu_hide_sources: { |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 640 | showTabs(false); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 641 | break; |
| 642 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 643 | case R.id.menu_edit: { |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 644 | Long rawContactIdToEdit = mSelectedRawContactId; |
| 645 | if (rawContactIdToEdit == null) { |
| 646 | if (mRawContactIds.size() > 0) { |
| 647 | rawContactIdToEdit = mRawContactIds.get(0); |
| 648 | } else { |
| 649 | // There is no rawContact to edit. |
| 650 | break; |
| 651 | } |
Evan Millar | db5d88c | 2009-08-28 09:31:57 -0700 | [diff] [blame] | 652 | } |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 653 | Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, |
| 654 | rawContactIdToEdit); |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 655 | startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri), |
| 656 | REQUEST_EDIT_CONTACT); |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 657 | break; |
| 658 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 659 | case R.id.menu_delete: { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 660 | // Get confirmation |
| 661 | showDialog(DIALOG_CONFIRM_DELETE); |
| 662 | return true; |
| 663 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 664 | case R.id.menu_join: { |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 665 | showJoinAggregateActivity(); |
| 666 | return true; |
| 667 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 668 | case R.id.menu_options: { |
Dmitri Plotnikov | ef03872 | 2009-06-24 18:51:47 -0700 | [diff] [blame] | 669 | showOptionsActivity(); |
| 670 | return true; |
| 671 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 672 | case R.id.menu_share: { |
Jeff Sharkey | 9304931 | 2009-09-27 17:28:13 -0700 | [diff] [blame] | 673 | // TODO: Keep around actual LOOKUP_KEY, or formalize method of extracting |
| 674 | final String lookupKey = mLookupUri.getPathSegments().get(2); |
| 675 | final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey); |
| 676 | |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 677 | final Intent intent = new Intent(Intent.ACTION_SEND); |
Jeff Sharkey | 9304931 | 2009-09-27 17:28:13 -0700 | [diff] [blame] | 678 | intent.setType(Contacts.CONTENT_VCARD_TYPE); |
| 679 | intent.putExtra(Intent.EXTRA_STREAM, shareUri); |
Dmitri Plotnikov | ef03872 | 2009-06-24 18:51:47 -0700 | [diff] [blame] | 680 | |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 681 | // Launch chooser to share contact via |
| 682 | final CharSequence chooseTitle = getText(R.string.share_via); |
| 683 | final Intent chooseIntent = Intent.createChooser(intent, chooseTitle); |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 684 | |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 685 | try { |
| 686 | startActivity(chooseIntent); |
| 687 | } catch (ActivityNotFoundException ex) { |
| 688 | Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 689 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 690 | return true; |
| 691 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 692 | } |
| 693 | return super.onOptionsItemSelected(item); |
| 694 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 695 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 696 | @Override |
| 697 | public boolean onContextItemSelected(MenuItem item) { |
| 698 | switch (item.getItemId()) { |
| 699 | case MENU_ITEM_MAKE_DEFAULT: { |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 700 | if (makeItemDefault(item)) { |
| 701 | return true; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 702 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 703 | break; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 704 | } |
| 705 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 706 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 707 | return super.onContextItemSelected(item); |
| 708 | } |
| 709 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 710 | private boolean makeItemDefault(MenuItem item) { |
| 711 | ViewEntry entry = getViewEntryForMenuItem(item); |
| 712 | if (entry == null) { |
| 713 | return false; |
| 714 | } |
| 715 | |
| 716 | // Update the primary values in the data record. |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 717 | ContentValues values = new ContentValues(1); |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 718 | values.put(Data.IS_SUPER_PRIMARY, 1); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 719 | getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id), |
| 720 | values, null, null); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 721 | startEntityQuery(); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 722 | return true; |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * Shows a dialog that contains a list of all constituent contacts in this aggregate. |
| 727 | * The user picks a contact to be split into its own aggregate or clicks Cancel. |
| 728 | */ |
| 729 | private void showSplitAggregateDialog() { |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 730 | // Wrap this dialog in a specific theme so that list items have correct text color. |
| 731 | final ContextThemeWrapper dialogContext = |
| 732 | new ContextThemeWrapper(this, android.R.style.Theme_Light); |
| 733 | AlertDialog.Builder builder = |
| 734 | new AlertDialog.Builder(dialogContext); |
| 735 | builder.setTitle(getString(R.string.splitAggregate_title)); |
| 736 | |
| 737 | final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri); |
| 738 | builder.setView(view); |
| 739 | |
| 740 | builder.setInverseBackgroundForced(true); |
| 741 | builder.setCancelable(true); |
| 742 | builder.setNegativeButton(android.R.string.cancel, |
| 743 | new OnClickListener() { |
| 744 | public void onClick(DialogInterface dialog, int which) { |
| 745 | dialog.dismiss(); |
| 746 | } |
| 747 | }); |
| 748 | final AlertDialog dialog = builder.create(); |
| 749 | |
| 750 | view.setOnContactSelectedListener(new OnContactSelectedListener() { |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 751 | public void onContactSelected(long rawContactId) { |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 752 | dialog.dismiss(); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 753 | splitContact(rawContactId); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 754 | } |
| 755 | }); |
| 756 | |
| 757 | dialog.show(); |
| 758 | } |
| 759 | |
| 760 | /** |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 761 | * Shows a list of aggregates that can be joined into the currently viewed aggregate. |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 762 | */ |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 763 | public void showJoinAggregateActivity() { |
| 764 | Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE); |
| 765 | intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri)); |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 766 | startActivityForResult(intent, REQUEST_JOIN_CONTACT); |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 767 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 768 | |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 769 | @Override |
| 770 | protected void onActivityResult(int requestCode, int resultCode, Intent intent) { |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 771 | if (requestCode == REQUEST_JOIN_CONTACT) { |
| 772 | if (resultCode == RESULT_OK && intent != null) { |
| 773 | final long contactId = ContentUris.parseId(intent.getData()); |
| 774 | joinAggregate(contactId); |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 775 | } |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 776 | } else if (requestCode == REQUEST_EDIT_CONTACT) { |
| 777 | mTabsVisible = false; |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 781 | private void splitContact(long rawContactId) { |
Dmitri Plotnikov | f40ccdf | 2009-09-04 17:37:15 -0700 | [diff] [blame] | 782 | setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 783 | |
| 784 | // The split operation may have removed the original aggregate contact, so we need |
| 785 | // to requery everything |
Dmitri Plotnikov | 040dc15 | 2009-09-03 15:17:56 -0700 | [diff] [blame] | 786 | Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show(); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 787 | startEntityQuery(); |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Dmitri Plotnikov | f40ccdf | 2009-09-04 17:37:15 -0700 | [diff] [blame] | 790 | private void joinAggregate(final long contactId) { |
Dmitri Plotnikov | 3946659 | 2009-07-27 11:23:51 -0700 | [diff] [blame] | 791 | Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID}, |
Dmitri Plotnikov | f40ccdf | 2009-09-04 17:37:15 -0700 | [diff] [blame] | 792 | RawContacts.CONTACT_ID + "=" + contactId, null, null); |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 793 | |
| 794 | try { |
| 795 | while(c.moveToNext()) { |
Dmitri Plotnikov | f40ccdf | 2009-09-04 17:37:15 -0700 | [diff] [blame] | 796 | long rawContactId = c.getLong(0); |
| 797 | setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER); |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 798 | } |
| 799 | } finally { |
| 800 | c.close(); |
| 801 | } |
| 802 | |
Dmitri Plotnikov | 040dc15 | 2009-09-03 15:17:56 -0700 | [diff] [blame] | 803 | Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show(); |
| 804 | startEntityQuery(); |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /** |
| 808 | * Given a contact ID sets an aggregation exception to either join the contact with the |
| 809 | * current aggregate or split off. |
| 810 | */ |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 811 | protected void setAggregationException(long rawContactId, int exceptionType) { |
Dmitri Plotnikov | d09f75c | 2009-06-16 11:59:22 -0700 | [diff] [blame] | 812 | ContentValues values = new ContentValues(3); |
Dmitri Plotnikov | f40ccdf | 2009-09-04 17:37:15 -0700 | [diff] [blame] | 813 | for (long aRawContactId : mRawContactIds) { |
| 814 | if (aRawContactId != rawContactId) { |
| 815 | values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId); |
| 816 | values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId); |
| 817 | values.put(AggregationExceptions.TYPE, exceptionType); |
| 818 | mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null); |
| 819 | } |
| 820 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Dmitri Plotnikov | ef03872 | 2009-06-24 18:51:47 -0700 | [diff] [blame] | 823 | private void showOptionsActivity() { |
| 824 | final Intent intent = new Intent(this, ContactOptionsActivity.class); |
| 825 | intent.setData(mUri); |
| 826 | startActivity(intent); |
| 827 | } |
| 828 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 829 | private ViewEntry getViewEntryForMenuItem(MenuItem item) { |
| 830 | AdapterView.AdapterContextMenuInfo info; |
| 831 | try { |
| 832 | info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); |
| 833 | } catch (ClassCastException e) { |
| 834 | Log.e(TAG, "bad menuInfo", e); |
| 835 | return null; |
| 836 | } |
| 837 | |
| 838 | return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS); |
| 839 | } |
| 840 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 841 | @Override |
| 842 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 843 | switch (keyCode) { |
| 844 | case KeyEvent.KEYCODE_CALL: { |
| 845 | try { |
| 846 | ITelephony phone = ITelephony.Stub.asInterface( |
| 847 | ServiceManager.checkService("phone")); |
| 848 | if (phone != null && !phone.isIdle()) { |
| 849 | // Skip out and let the key be handled at a higher level |
| 850 | break; |
| 851 | } |
| 852 | } catch (RemoteException re) { |
| 853 | // Fall through and try to call the contact |
| 854 | } |
| 855 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 856 | int index = mListView.getSelectedItemPosition(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 857 | if (index != -1) { |
| 858 | ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 859 | if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) { |
| 860 | startActivity(entry.intent); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 861 | } |
| 862 | } else if (mNumPhoneNumbers != 0) { |
| 863 | // There isn't anything selected, call the default number |
| 864 | Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri); |
| 865 | startActivity(intent); |
| 866 | } |
| 867 | return true; |
| 868 | } |
| 869 | |
| 870 | case KeyEvent.KEYCODE_DEL: { |
| 871 | showDialog(DIALOG_CONFIRM_DELETE); |
| 872 | return true; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | return super.onKeyDown(keyCode, event); |
| 877 | } |
| 878 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 879 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 880 | ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS); |
| 881 | if (entry != null) { |
| 882 | Intent intent = entry.intent; |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 883 | if (entry.mimetype == SPLIT_MIMETYPE) { |
| 884 | splitContact(entry.id); |
| 885 | } else if (intent != null) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 886 | try { |
| 887 | startActivity(intent); |
| 888 | } catch (ActivityNotFoundException e) { |
| 889 | Log.e(TAG, "No activity found for intent: " + intent); |
| 890 | signalError(); |
| 891 | } |
| 892 | } else { |
| 893 | signalError(); |
| 894 | } |
| 895 | } else { |
| 896 | signalError(); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * Signal an error to the user via a beep, or some other method. |
| 902 | */ |
| 903 | private void signalError() { |
| 904 | //TODO: implement this when we have the sonification APIs |
| 905 | } |
| 906 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 907 | private Uri constructImToUrl(String host, String data) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 908 | // don't encode the url, because the Activity Manager can't find using the encoded url |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 909 | StringBuilder buf = new StringBuilder("imto://"); |
| 910 | buf.append(host); |
| 911 | buf.append('/'); |
| 912 | buf.append(data); |
| 913 | return Uri.parse(buf.toString()); |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * Build up the entries to display on the screen. |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 918 | * |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 919 | * @param personCursor the URI for the contact being displayed |
| 920 | */ |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 921 | private final void buildEntries() { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 922 | // Clear out the old entries |
| 923 | final int numSections = mSections.size(); |
| 924 | for (int i = 0; i < numSections; i++) { |
| 925 | mSections.get(i).clear(); |
| 926 | } |
| 927 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 928 | mRawContactIds.clear(); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 929 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 930 | Sources sources = Sources.getInstance(this); |
| 931 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 932 | // Build up method entries |
| 933 | if (mUri != null) { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 934 | for (Entity entity: mEntities) { |
| 935 | final ContentValues entValues = entity.getEntityValues(); |
| 936 | final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE); |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 937 | final long rawContactId = entValues.getAsLong(RawContacts._ID); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 938 | |
Dmitri Plotnikov | 2a8cef0 | 2009-09-23 19:01:15 -0700 | [diff] [blame] | 939 | if (!mRawContactIds.contains(rawContactId)) { |
| 940 | mRawContactIds.add(rawContactId); |
| 941 | } |
| 942 | |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 943 | // This performs the tab filtering |
| 944 | if (mSelectedRawContactId != null && mSelectedRawContactId != rawContactId) { |
| 945 | continue; |
| 946 | } |
Evan Millar | 6621045 | 2009-09-21 10:53:43 -0700 | [diff] [blame] | 947 | |
Tadashi G. Takaoka | 33aa1c6 | 2009-09-27 14:22:28 -0700 | [diff] [blame] | 948 | if (mTabsVisible) { |
| 949 | final String accountName = entValues.getAsString(RawContacts.ACCOUNT_NAME); |
| 950 | mAccountName.setText(getString(R.string.account_name_format, accountName)); |
| 951 | mAccountName.setVisibility(View.VISIBLE); |
| 952 | } else { |
| 953 | mAccountName.setVisibility(View.GONE); |
| 954 | } |
| 955 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 956 | for (NamedContentValues subValue : entity.getSubValues()) { |
| 957 | ViewEntry entry = new ViewEntry(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 958 | |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 959 | final ContentValues entryValues = subValue.values; |
| 960 | entryValues.put(Data.RAW_CONTACT_ID, rawContactId); |
| 961 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 962 | final String mimetype = entryValues.getAsString(Data.MIMETYPE); |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 963 | if (mimetype == null) continue; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 964 | |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 965 | final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this, |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 966 | ContactsSource.LEVEL_MIMETYPES); |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 967 | if (kind == null) continue; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 968 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 969 | final long id = entryValues.getAsLong(Data._ID); |
| 970 | final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id); |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 971 | entry.contactId = rawContactId; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 972 | entry.id = id; |
| 973 | entry.uri = uri; |
| 974 | entry.mimetype = mimetype; |
Daisuke Miyakawa | 999db91 | 2009-09-17 15:55:15 -0700 | [diff] [blame] | 975 | entry.label = buildActionString(kind, entryValues, false); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 976 | entry.data = buildDataString(kind, entryValues); |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 977 | if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 978 | entry.type = entryValues.getAsInteger(kind.typeColumn); |
| 979 | } |
| 980 | if (kind.iconRes > 0) { |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 981 | entry.resPackageName = kind.resPackageName; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 982 | entry.actionIcon = kind.iconRes; |
| 983 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 984 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 985 | // Don't crash if the data is bogus |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 986 | if (TextUtils.isEmpty(entry.data)) { |
Alex Kennberg | 87fc317 | 2009-03-28 06:43:06 -0700 | [diff] [blame] | 987 | continue; |
| 988 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 989 | |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 990 | final boolean isSuperPrimary = entryValues.getAsInteger( |
| 991 | Data.IS_SUPER_PRIMARY) != 0; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 992 | |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 993 | if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 994 | // Build phone entries |
| 995 | mNumPhoneNumbers++; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 996 | |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 997 | entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, |
| 998 | Uri.fromParts("tel", entry.data, null)); |
| 999 | entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO, |
| 1000 | Uri.fromParts("sms", entry.data, null)); |
| 1001 | entry.data = PhoneNumberUtils.stripSeparators(entry.data); |
Evan Millar | 49714ee | 2009-09-02 16:42:47 -0700 | [diff] [blame] | 1002 | |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 1003 | entry.isPrimary = isSuperPrimary; |
| 1004 | mPhoneEntries.add(entry); |
| 1005 | |
| 1006 | if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE |
| 1007 | || mShowSmsLinksForAllPhones) { |
| 1008 | // Add an SMS entry |
| 1009 | if (kind.iconAltRes > 0) { |
| 1010 | entry.secondaryActionIcon = kind.iconAltRes; |
Evan Millar | 49714ee | 2009-09-02 16:42:47 -0700 | [diff] [blame] | 1011 | } |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 1012 | } |
| 1013 | } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 1014 | // Build email entries |
| 1015 | entry.intent = new Intent(Intent.ACTION_SENDTO, |
| 1016 | Uri.fromParts("mailto", entry.data, null)); |
| 1017 | entry.isPrimary = isSuperPrimary; |
| 1018 | mEmailEntries.add(entry); |
| 1019 | } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE. |
| 1020 | equals(mimetype)) { |
| 1021 | // Build postal entries |
| 1022 | entry.maxLines = 4; |
| 1023 | entry.intent = new Intent(Intent.ACTION_VIEW, uri); |
| 1024 | mPostalEntries.add(entry); |
| 1025 | } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 1026 | // Build im entries |
| 1027 | Object protocolObj = entryValues.getAsInteger(CommonDataKinds.Im.PROTOCOL); |
| 1028 | String host = null; |
Evan Millar | 49714ee | 2009-09-02 16:42:47 -0700 | [diff] [blame] | 1029 | |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 1030 | if (TextUtils.isEmpty(entry.label)) { |
| 1031 | entry.label = getString(R.string.chat).toLowerCase(); |
| 1032 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1033 | |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 1034 | if (protocolObj instanceof Number) { |
| 1035 | int protocol = ((Number) protocolObj).intValue(); |
| 1036 | host = ContactsUtils.lookupProviderNameFromId(protocol); |
| 1037 | if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK |
| 1038 | || protocol == CommonDataKinds.Im.PROTOCOL_MSN) { |
| 1039 | entry.maxLabelLines = 2; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1040 | } |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 1041 | } else if (protocolObj != null) { |
| 1042 | String providerName = (String) protocolObj; |
| 1043 | host = providerName.toLowerCase(); |
| 1044 | } |
| 1045 | |
| 1046 | // Only add the intent if there is a valid host |
| 1047 | // host is null for CommonDataKinds.Im.PROTOCOL_CUSTOM |
| 1048 | if (!TextUtils.isEmpty(host)) { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1049 | entry.intent = new Intent(Intent.ACTION_SENDTO, |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 1050 | constructImToUrl(host.toLowerCase(), entry.data)); |
| 1051 | } |
| 1052 | //TODO(emillar) Add in presence info |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1053 | /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) { |
| 1054 | entry.presenceIcon = Presence.getPresenceIconResourceId( |
| 1055 | aggCursor.getInt(METHODS_STATUS_COLUMN)); |
| 1056 | entry.status = ... |
| 1057 | }*/ |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 1058 | mImEntries.add(entry); |
| 1059 | } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype) |
| 1060 | || CommonDataKinds.Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 1061 | // Build organization and note entries |
| 1062 | entry.uri = null; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1063 | mOrganizationEntries.add(entry); |
| 1064 | } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 1065 | // Build note entries |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1066 | entry.uri = null; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1067 | entry.maxLines = 10; |
| 1068 | mOtherEntries.add(entry); |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 1069 | } else { |
| 1070 | // Handle showing custom |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 1071 | entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri); |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 1072 | mOtherEntries.add(entry); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | if (mSelectedRawContactId != null && |
| 1078 | mSelectedRawContactId == rawContactId |
| 1079 | && mEntities.size() > 1) { |
| 1080 | ViewEntry entry = new ViewEntry(); |
| 1081 | entry.mimetype = SPLIT_MIMETYPE; |
| 1082 | entry.id = rawContactId; |
| 1083 | entry.label = getString(R.string.split_label); |
| 1084 | entry.data = getString(R.string.split_explanation); |
| 1085 | entry.actionIcon = R.drawable.ic_list_split; |
| 1086 | mSplitEntry.add(entry); |
| 1087 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1088 | // TODO(emillar) Add group entries |
| 1089 | // // Build the group entries |
| 1090 | // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY); |
| 1091 | // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION, |
| 1092 | // null, null, Groups.DEFAULT_SORT_ORDER); |
| 1093 | // if (groupCursor != null) { |
| 1094 | // try { |
| 1095 | // StringBuilder sb = new StringBuilder(); |
| 1096 | // |
| 1097 | // while (groupCursor.moveToNext()) { |
| 1098 | // String systemId = groupCursor.getString( |
| 1099 | // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID); |
| 1100 | // |
| 1101 | // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) { |
| 1102 | // continue; |
| 1103 | // } |
| 1104 | // |
| 1105 | // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME); |
| 1106 | // if (!TextUtils.isEmpty(name)) { |
| 1107 | // if (sb.length() == 0) { |
| 1108 | // sb.append(name); |
| 1109 | // } else { |
| 1110 | // sb.append(getString(R.string.group_list, name)); |
| 1111 | // } |
| 1112 | // } |
| 1113 | // } |
| 1114 | // |
| 1115 | // if (sb.length() > 0) { |
| 1116 | // ViewEntry entry = new ViewEntry(); |
| 1117 | // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP; |
| 1118 | // entry.label = getString(R.string.label_groups); |
| 1119 | // entry.data = sb.toString(); |
| 1120 | // entry.intent = new Intent(Intent.ACTION_EDIT, mUri); |
| 1121 | // |
| 1122 | // // TODO: Add an icon for the groups item. |
| 1123 | // |
| 1124 | // mGroupEntries.add(entry); |
| 1125 | // } |
| 1126 | // } finally { |
| 1127 | // groupCursor.close(); |
| 1128 | // } |
| 1129 | // } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 1130 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 1131 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1135 | String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) { |
| 1136 | if (kind.actionHeader == null) { |
| 1137 | return null; |
Jeff Hamilton | 8350e5b | 2009-03-24 21:01:34 -0700 | [diff] [blame] | 1138 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1139 | CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values); |
| 1140 | if (actionHeader == null) { |
| 1141 | return null; |
| 1142 | } |
| 1143 | return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString(); |
| 1144 | } |
Jeff Hamilton | 8350e5b | 2009-03-24 21:01:34 -0700 | [diff] [blame] | 1145 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1146 | String buildDataString(DataKind kind, ContentValues values) { |
| 1147 | if (kind.actionBody == null) { |
| 1148 | return null; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1149 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1150 | CharSequence actionBody = kind.actionBody.inflateUsing(this, values); |
| 1151 | return actionBody == null ? null : actionBody.toString(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1152 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 1153 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1154 | /** |
| 1155 | * A basic structure with the data for a contact entry in the list. |
| 1156 | */ |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 1157 | class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> { |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 1158 | public String resPackageName = null; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1159 | public int actionIcon = -1; |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1160 | public boolean isPrimary = false; |
| 1161 | public int presenceIcon = -1; |
| 1162 | public int secondaryActionIcon = -1; |
| 1163 | public Intent intent; |
| 1164 | public Intent secondaryIntent = null; |
| 1165 | public int status = -1; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1166 | public int maxLabelLines = 1; |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1167 | public ArrayList<Long> ids = new ArrayList<Long>(); |
| 1168 | public int collapseCount = 0; |
| 1169 | |
| 1170 | public boolean collapseWith(ViewEntry entry) { |
| 1171 | // assert equal collapse keys |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 1172 | if (!shouldCollapseWith(entry)) { |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1173 | return false; |
| 1174 | } |
| 1175 | |
| 1176 | // Choose the label associated with the highest type precedence. |
| 1177 | if (TypePrecedence.getTypePrecedence(mimetype, type) |
| 1178 | > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) { |
| 1179 | type = entry.type; |
| 1180 | label = entry.label; |
| 1181 | } |
| 1182 | |
| 1183 | // Choose the max of the maxLines and maxLabelLines values. |
| 1184 | maxLines = Math.max(maxLines, entry.maxLines); |
| 1185 | maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines); |
| 1186 | |
| 1187 | // Choose the presence with the highest precedence. |
| 1188 | if (Presence.getPresencePrecedence(status) |
| 1189 | < Presence.getPresencePrecedence(entry.status)) { |
| 1190 | status = entry.status; |
| 1191 | } |
| 1192 | |
| 1193 | // If any of the collapsed entries are primary make the whole thing primary. |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1194 | isPrimary = entry.isPrimary ? true : isPrimary; |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1195 | |
| 1196 | // uri, and contactdId, shouldn't make a difference. Just keep the original. |
| 1197 | |
| 1198 | // Keep track of all the ids that have been collapsed with this one. |
| 1199 | ids.add(entry.id); |
| 1200 | collapseCount++; |
| 1201 | return true; |
| 1202 | } |
| 1203 | |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 1204 | public boolean shouldCollapseWith(ViewEntry entry) { |
| 1205 | if (entry == null) { |
| 1206 | return false; |
| 1207 | } |
| 1208 | |
| 1209 | if (Phone.CONTENT_ITEM_TYPE.equals(mimetype) |
| 1210 | && Phone.CONTENT_ITEM_TYPE.equals(entry.mimetype)) { |
| 1211 | if (!PhoneNumberUtils.compare(ViewContactActivity.this, data, entry.data)) { |
| 1212 | return false; |
| 1213 | } |
| 1214 | } else { |
| 1215 | if (!equals(data, entry.data)) { |
| 1216 | return false; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | if (!equals(mimetype, entry.mimetype) |
| 1221 | || !intentCollapsible(intent, entry.intent) |
| 1222 | || !intentCollapsible(secondaryIntent, entry.secondaryIntent) |
| 1223 | || actionIcon != entry.actionIcon) { |
| 1224 | return false; |
| 1225 | } |
| 1226 | |
| 1227 | return true; |
| 1228 | } |
| 1229 | |
| 1230 | private boolean equals(Object a, Object b) { |
| 1231 | return a==b || (a != null && a.equals(b)); |
| 1232 | } |
| 1233 | |
| 1234 | private boolean intentCollapsible(Intent a, Intent b) { |
| 1235 | if (a == b) { |
| 1236 | return true; |
| 1237 | } else if ((a != null && b != null) && equals(a.getAction(), b.getAction())) { |
| 1238 | return true; |
| 1239 | } |
| 1240 | return false; |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1241 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1244 | /** Cache of the children views of a row */ |
| 1245 | static class ViewCache { |
| 1246 | public TextView label; |
| 1247 | public TextView data; |
| 1248 | public ImageView actionIcon; |
| 1249 | public ImageView presenceIcon; |
| 1250 | public ImageView primaryIcon; |
| 1251 | public ImageView secondaryActionButton; |
| 1252 | public View secondaryActionDivider; |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 1253 | |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1254 | // Need to keep track of this too |
| 1255 | ViewEntry entry; |
| 1256 | } |
| 1257 | |
| 1258 | private final class ViewAdapter extends ContactEntryAdapter<ViewEntry> |
| 1259 | implements View.OnClickListener { |
| 1260 | |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 1261 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1262 | ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) { |
| 1263 | super(context, sections, SHOW_SEPARATORS); |
| 1264 | } |
| 1265 | |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1266 | public void onClick(View v) { |
| 1267 | Intent intent = (Intent) v.getTag(); |
| 1268 | startActivity(intent); |
| 1269 | } |
| 1270 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1271 | @Override |
| 1272 | public View getView(int position, View convertView, ViewGroup parent) { |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 1273 | ViewEntry entry = getEntry(mSections, position, false); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1274 | View v; |
| 1275 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1276 | ViewCache views; |
| 1277 | |
| 1278 | // Check to see if we can reuse convertView |
| 1279 | if (convertView != null) { |
| 1280 | v = convertView; |
| 1281 | views = (ViewCache) v.getTag(); |
| 1282 | } else { |
| 1283 | // Create a new view if needed |
| 1284 | v = mInflater.inflate(R.layout.list_item_text_icons, parent, false); |
| 1285 | |
| 1286 | // Cache the children |
| 1287 | views = new ViewCache(); |
| 1288 | views.label = (TextView) v.findViewById(android.R.id.text1); |
| 1289 | views.data = (TextView) v.findViewById(android.R.id.text2); |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1290 | views.actionIcon = (ImageView) v.findViewById(R.id.action_icon); |
| 1291 | views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon); |
| 1292 | views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon); |
| 1293 | views.secondaryActionButton = (ImageView) v.findViewById( |
| 1294 | R.id.secondary_action_button); |
| 1295 | views.secondaryActionButton.setOnClickListener(this); |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1296 | views.secondaryActionDivider = v.findViewById(R.id.divider); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1297 | v.setTag(views); |
| 1298 | } |
| 1299 | |
| 1300 | // Update the entry in the view cache |
| 1301 | views.entry = entry; |
| 1302 | |
| 1303 | // Bind the data to the view |
| 1304 | bindView(v, entry); |
| 1305 | return v; |
| 1306 | } |
| 1307 | |
| 1308 | @Override |
| 1309 | protected View newView(int position, ViewGroup parent) { |
| 1310 | // getView() handles this |
| 1311 | throw new UnsupportedOperationException(); |
| 1312 | } |
| 1313 | |
| 1314 | @Override |
| 1315 | protected void bindView(View view, ViewEntry entry) { |
| 1316 | final Resources resources = mContext.getResources(); |
| 1317 | ViewCache views = (ViewCache) view.getTag(); |
| 1318 | |
| 1319 | // Set the label |
| 1320 | TextView label = views.label; |
| 1321 | setMaxLines(label, entry.maxLabelLines); |
| 1322 | label.setText(entry.label); |
| 1323 | |
| 1324 | // Set the data |
| 1325 | TextView data = views.data; |
| 1326 | if (data != null) { |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1327 | if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE) |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 1328 | || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) { |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1329 | data.setText(PhoneNumberUtils.formatNumber(entry.data)); |
| 1330 | } else { |
| 1331 | data.setText(entry.data); |
| 1332 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1333 | setMaxLines(data, entry.maxLines); |
| 1334 | } |
| 1335 | |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1336 | // Set the primary icon |
| 1337 | views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE); |
| 1338 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1339 | // Set the action icon |
| 1340 | ImageView action = views.actionIcon; |
| 1341 | if (entry.actionIcon != -1) { |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 1342 | Drawable actionIcon; |
| 1343 | if (entry.resPackageName != null) { |
| 1344 | // Load external resources through PackageManager |
| 1345 | actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName, |
| 1346 | entry.actionIcon, null); |
| 1347 | } else { |
| 1348 | actionIcon = resources.getDrawable(entry.actionIcon); |
| 1349 | } |
| 1350 | action.setImageDrawable(actionIcon); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1351 | action.setVisibility(View.VISIBLE); |
| 1352 | } else { |
| 1353 | // Things should still line up as if there was an icon, so make it invisible |
| 1354 | action.setVisibility(View.INVISIBLE); |
| 1355 | } |
| 1356 | |
| 1357 | // Set the presence icon |
| 1358 | Drawable presenceIcon = null; |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1359 | if (entry.presenceIcon != -1) { |
| 1360 | presenceIcon = resources.getDrawable(entry.presenceIcon); |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1361 | } else if (entry.status != -1) { |
| 1362 | presenceIcon = resources.getDrawable( |
| 1363 | Presence.getPresenceIconResourceId(entry.status)); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1364 | } |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1365 | ImageView presenceIconView = views.presenceIcon; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1366 | if (presenceIcon != null) { |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1367 | presenceIconView.setImageDrawable(presenceIcon); |
| 1368 | presenceIconView.setVisibility(View.VISIBLE); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1369 | } else { |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1370 | presenceIconView.setVisibility(View.GONE); |
| 1371 | } |
| 1372 | |
| 1373 | // Set the secondary action button |
| 1374 | ImageView secondaryActionView = views.secondaryActionButton; |
| 1375 | Drawable secondaryActionIcon = null; |
| 1376 | if (entry.secondaryActionIcon != -1) { |
| 1377 | secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon); |
| 1378 | } |
| 1379 | if (entry.secondaryIntent != null && secondaryActionIcon != null) { |
| 1380 | secondaryActionView.setImageDrawable(secondaryActionIcon); |
| 1381 | secondaryActionView.setTag(entry.secondaryIntent); |
| 1382 | secondaryActionView.setVisibility(View.VISIBLE); |
| 1383 | views.secondaryActionDivider.setVisibility(View.VISIBLE); |
| 1384 | } else { |
| 1385 | secondaryActionView.setVisibility(View.GONE); |
| 1386 | views.secondaryActionDivider.setVisibility(View.GONE); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | private void setMaxLines(TextView textView, int maxLines) { |
| 1391 | if (maxLines == 1) { |
| 1392 | textView.setSingleLine(true); |
| 1393 | textView.setEllipsize(TextUtils.TruncateAt.END); |
| 1394 | } else { |
| 1395 | textView.setSingleLine(false); |
| 1396 | textView.setMaxLines(maxLines); |
| 1397 | textView.setEllipsize(null); |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | } |