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