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