Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [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 | |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 19 | |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 20 | import com.android.contacts.list.JoinContactListAdapter; |
Dmitri Plotnikov | 59fb48e | 2010-04-26 17:09:19 -0700 | [diff] [blame] | 21 | import com.android.contacts.list.JoinContactListFragment; |
Dmitri Plotnikov | 807a0fe | 2010-04-20 12:03:10 -0700 | [diff] [blame] | 22 | |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 23 | import android.content.ContentUris; |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 24 | import android.content.Intent; |
| 25 | import android.database.Cursor; |
| 26 | import android.database.MatrixCursor; |
| 27 | import android.net.Uri; |
| 28 | import android.net.Uri.Builder; |
| 29 | import android.provider.ContactsContract; |
| 30 | import android.provider.ContactsContract.Contacts; |
| 31 | import android.provider.ContactsContract.Contacts.AggregationSuggestions; |
| 32 | import android.text.TextUtils; |
| 33 | import android.util.Log; |
Dmitri Plotnikov | caf498b | 2010-04-23 14:58:08 -0700 | [diff] [blame] | 34 | import android.widget.ListView; |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 35 | import android.widget.TextView; |
| 36 | |
| 37 | /** |
| 38 | * An activity that shows a list of contacts that can be joined with the target contact. |
| 39 | */ |
| 40 | public class JoinContactActivity extends ContactsListActivity { |
| 41 | |
| 42 | private static final String TAG = "JoinContactActivity"; |
| 43 | |
| 44 | /** |
| 45 | * The action for the join contact activity. |
| 46 | * <p> |
| 47 | * Input: extra field {@link #EXTRA_TARGET_CONTACT_ID} is the aggregate ID. |
| 48 | * TODO: move to {@link ContactsContract}. |
| 49 | */ |
| 50 | public static final String JOIN_CONTACT = "com.android.contacts.action.JOIN_CONTACT"; |
| 51 | |
| 52 | /** |
| 53 | * Used with {@link #JOIN_CONTACT} to give it the target for aggregation. |
| 54 | * <p> |
| 55 | * Type: LONG |
| 56 | */ |
| 57 | public static final String EXTRA_TARGET_CONTACT_ID = "com.android.contacts.action.CONTACT_ID"; |
| 58 | |
| 59 | /** Maximum number of suggestions shown for joining aggregates */ |
| 60 | private static final int MAX_SUGGESTIONS = 4; |
| 61 | |
| 62 | private long mTargetContactId; |
| 63 | |
| 64 | /** |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 65 | * The ID of the special item described above. |
| 66 | */ |
| 67 | private static final long JOIN_MODE_SHOW_ALL_CONTACTS_ID = -2; |
| 68 | |
| 69 | private boolean mLoadingJoinSuggestions; |
| 70 | |
| 71 | private JoinContactListAdapter mAdapter; |
| 72 | |
| 73 | @Override |
Dmitri Plotnikov | 59fb48e | 2010-04-26 17:09:19 -0700 | [diff] [blame] | 74 | protected boolean resolveIntent(Intent intent) { |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 75 | mMode = MODE_PICK_CONTACT; |
| 76 | mTargetContactId = intent.getLongExtra(EXTRA_TARGET_CONTACT_ID, -1); |
| 77 | if (mTargetContactId == -1) { |
| 78 | Log.e(TAG, "Intent " + intent.getAction() + " is missing required extra: " |
| 79 | + EXTRA_TARGET_CONTACT_ID); |
| 80 | setResult(RESULT_CANCELED); |
| 81 | finish(); |
Dmitri Plotnikov | 59fb48e | 2010-04-26 17:09:19 -0700 | [diff] [blame] | 82 | return false; |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 83 | } |
Dmitri Plotnikov | 59fb48e | 2010-04-26 17:09:19 -0700 | [diff] [blame] | 84 | |
| 85 | mListFragment = new JoinContactListFragment(); |
| 86 | |
| 87 | return true; |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Dmitri Plotnikov | 59fb48e | 2010-04-26 17:09:19 -0700 | [diff] [blame] | 91 | protected void onResume() { |
| 92 | super.onResume(); |
Dmitri Plotnikov | caf498b | 2010-04-23 14:58:08 -0700 | [diff] [blame] | 93 | |
Dmitri Plotnikov | 59fb48e | 2010-04-26 17:09:19 -0700 | [diff] [blame] | 94 | // TODO move this to onAttach of the corresponding fragment |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 95 | TextView blurbView = (TextView)findViewById(R.id.join_contact_blurb); |
| 96 | |
| 97 | String blurb = getString(R.string.blurbJoinContactDataWith, |
| 98 | getContactDisplayName(mTargetContactId)); |
| 99 | blurbView.setText(blurb); |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 100 | |
Dmitri Plotnikov | caf498b | 2010-04-23 14:58:08 -0700 | [diff] [blame] | 101 | ListView listView = (ListView)findViewById(android.R.id.list); |
| 102 | mAdapter = (JoinContactListAdapter)listView.getAdapter(); |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 103 | mAdapter.setJoinModeShowAllContacts(true); |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 107 | public void onListItemClick(int position, long id) { |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 108 | if (id == JOIN_MODE_SHOW_ALL_CONTACTS_ID) { |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 109 | mAdapter.setJoinModeShowAllContacts(false); |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 110 | startQuery(); |
| 111 | } else { |
| 112 | final Uri uri = getSelectedUri(position); |
| 113 | returnPickerResult(null, null, uri); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | protected Uri getUriToQuery() { |
| 119 | return getJoinSuggestionsUri(null); |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * TODO: move to a background thread. |
| 124 | */ |
| 125 | private String getContactDisplayName(long contactId) { |
| 126 | String contactName = null; |
| 127 | Cursor c = getContentResolver().query( |
| 128 | ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId), |
| 129 | new String[] {Contacts.DISPLAY_NAME}, null, null, null); |
| 130 | try { |
| 131 | if (c != null && c.moveToFirst()) { |
| 132 | contactName = c.getString(0); |
| 133 | } |
| 134 | } finally { |
| 135 | if (c != null) { |
| 136 | c.close(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (contactName == null) { |
| 141 | contactName = ""; |
| 142 | } |
| 143 | |
| 144 | return contactName; |
| 145 | } |
| 146 | |
| 147 | private Uri getJoinSuggestionsUri(String filter) { |
| 148 | Builder builder = Contacts.CONTENT_URI.buildUpon(); |
| 149 | builder.appendEncodedPath(String.valueOf(mTargetContactId)); |
| 150 | builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY); |
| 151 | if (!TextUtils.isEmpty(filter)) { |
| 152 | builder.appendEncodedPath(Uri.encode(filter)); |
| 153 | } |
| 154 | builder.appendQueryParameter("limit", String.valueOf(MAX_SUGGESTIONS)); |
| 155 | return builder.build(); |
| 156 | } |
| 157 | |
| 158 | @Override |
Dmitri Plotnikov | 807a0fe | 2010-04-20 12:03:10 -0700 | [diff] [blame] | 159 | public |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 160 | Cursor doFilter(String filter) { |
| 161 | throw new UnsupportedOperationException(); |
| 162 | } |
| 163 | |
| 164 | private Cursor getShowAllContactsLabelCursor(String[] projection) { |
| 165 | MatrixCursor matrixCursor = new MatrixCursor(projection); |
| 166 | Object[] row = new Object[projection.length]; |
| 167 | // The only columns we care about is the id |
| 168 | row[SUMMARY_ID_COLUMN_INDEX] = JOIN_MODE_SHOW_ALL_CONTACTS_ID; |
| 169 | matrixCursor.addRow(row); |
| 170 | return matrixCursor; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | protected void startQuery(Uri uri, String[] projection) { |
| 175 | mLoadingJoinSuggestions = true; |
| 176 | startQuery(uri, projection, null, null, null); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | protected void onQueryComplete(Cursor cursor) { |
| 181 | // Whenever we get a suggestions cursor, we need to immediately kick off |
| 182 | // another query for the complete list of contacts |
| 183 | if (cursor != null && mLoadingJoinSuggestions) { |
| 184 | mLoadingJoinSuggestions = false; |
| 185 | if (cursor.getCount() > 0) { |
| 186 | mAdapter.setSuggestionsCursor(cursor); |
| 187 | } else { |
| 188 | cursor.close(); |
| 189 | mAdapter.setSuggestionsCursor(null); |
| 190 | } |
| 191 | |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame] | 192 | if (mAdapter.getSuggestionsCursorCount() == 0 |
| 193 | || !mAdapter.isJoinModeShowAllContacts()) { |
Dmitri Plotnikov | e3fbfd9 | 2010-04-28 11:20:52 -0700 | [diff] [blame^] | 194 | startQuery(getContactFilterUri(mListFragment.getQueryString()), |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 195 | CONTACTS_SUMMARY_PROJECTION, |
| 196 | Contacts._ID + " != " + mTargetContactId |
| 197 | + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1", null, |
| 198 | getSortOrder(CONTACTS_SUMMARY_PROJECTION)); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | cursor = getShowAllContactsLabelCursor(CONTACTS_SUMMARY_PROJECTION); |
| 203 | } |
| 204 | |
| 205 | super.onQueryComplete(cursor); |
| 206 | } |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 207 | } |