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