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 | |
| 20 | import com.android.contacts.list.ContactEntryListConfiguration; |
| 21 | import com.android.contacts.list.JoinContactListAdapter; |
| 22 | import com.android.contacts.list.JoinContactListConfiguration; |
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 | 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 | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame^] | 74 | protected ContactEntryListConfiguration 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 | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame^] | 82 | return null; |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 83 | } |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame^] | 84 | return new JoinContactListConfiguration(this, this); |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void initContentView() { |
| 89 | setContentView(R.layout.contacts_list_content_join); |
| 90 | TextView blurbView = (TextView)findViewById(R.id.join_contact_blurb); |
| 91 | |
| 92 | String blurb = getString(R.string.blurbJoinContactDataWith, |
| 93 | getContactDisplayName(mTargetContactId)); |
| 94 | blurbView.setText(blurb); |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame^] | 95 | mConfig.configureListView(getListView()); |
| 96 | |
| 97 | mAdapter = (JoinContactListAdapter)getListView().getAdapter(); |
| 98 | mAdapter.setJoinModeShowAllContacts(true); |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame^] | 102 | public void onListItemClick(int position, long id) { |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 103 | if (id == JOIN_MODE_SHOW_ALL_CONTACTS_ID) { |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame^] | 104 | mAdapter.setJoinModeShowAllContacts(false); |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 105 | startQuery(); |
| 106 | } else { |
| 107 | final Uri uri = getSelectedUri(position); |
| 108 | returnPickerResult(null, null, uri); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected Uri getUriToQuery() { |
| 114 | return getJoinSuggestionsUri(null); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * TODO: move to a background thread. |
| 119 | */ |
| 120 | private String getContactDisplayName(long contactId) { |
| 121 | String contactName = null; |
| 122 | Cursor c = getContentResolver().query( |
| 123 | ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId), |
| 124 | new String[] {Contacts.DISPLAY_NAME}, null, null, null); |
| 125 | try { |
| 126 | if (c != null && c.moveToFirst()) { |
| 127 | contactName = c.getString(0); |
| 128 | } |
| 129 | } finally { |
| 130 | if (c != null) { |
| 131 | c.close(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (contactName == null) { |
| 136 | contactName = ""; |
| 137 | } |
| 138 | |
| 139 | return contactName; |
| 140 | } |
| 141 | |
| 142 | private Uri getJoinSuggestionsUri(String filter) { |
| 143 | Builder builder = Contacts.CONTENT_URI.buildUpon(); |
| 144 | builder.appendEncodedPath(String.valueOf(mTargetContactId)); |
| 145 | builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY); |
| 146 | if (!TextUtils.isEmpty(filter)) { |
| 147 | builder.appendEncodedPath(Uri.encode(filter)); |
| 148 | } |
| 149 | builder.appendQueryParameter("limit", String.valueOf(MAX_SUGGESTIONS)); |
| 150 | return builder.build(); |
| 151 | } |
| 152 | |
| 153 | @Override |
Dmitri Plotnikov | 807a0fe | 2010-04-20 12:03:10 -0700 | [diff] [blame] | 154 | public |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 155 | Cursor doFilter(String filter) { |
| 156 | throw new UnsupportedOperationException(); |
| 157 | } |
| 158 | |
| 159 | private Cursor getShowAllContactsLabelCursor(String[] projection) { |
| 160 | MatrixCursor matrixCursor = new MatrixCursor(projection); |
| 161 | Object[] row = new Object[projection.length]; |
| 162 | // The only columns we care about is the id |
| 163 | row[SUMMARY_ID_COLUMN_INDEX] = JOIN_MODE_SHOW_ALL_CONTACTS_ID; |
| 164 | matrixCursor.addRow(row); |
| 165 | return matrixCursor; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | protected void startQuery(Uri uri, String[] projection) { |
| 170 | mLoadingJoinSuggestions = true; |
| 171 | startQuery(uri, projection, null, null, null); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | protected void onQueryComplete(Cursor cursor) { |
| 176 | // Whenever we get a suggestions cursor, we need to immediately kick off |
| 177 | // another query for the complete list of contacts |
| 178 | if (cursor != null && mLoadingJoinSuggestions) { |
| 179 | mLoadingJoinSuggestions = false; |
| 180 | if (cursor.getCount() > 0) { |
| 181 | mAdapter.setSuggestionsCursor(cursor); |
| 182 | } else { |
| 183 | cursor.close(); |
| 184 | mAdapter.setSuggestionsCursor(null); |
| 185 | } |
| 186 | |
Dmitri Plotnikov | 6e2009d | 2010-04-22 16:03:53 -0700 | [diff] [blame^] | 187 | if (mAdapter.getSuggestionsCursorCount() == 0 |
| 188 | || !mAdapter.isJoinModeShowAllContacts()) { |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 189 | startQuery(getContactFilterUri(getTextFilter()), |
| 190 | CONTACTS_SUMMARY_PROJECTION, |
| 191 | Contacts._ID + " != " + mTargetContactId |
| 192 | + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1", null, |
| 193 | getSortOrder(CONTACTS_SUMMARY_PROJECTION)); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | cursor = getShowAllContactsLabelCursor(CONTACTS_SUMMARY_PROJECTION); |
| 198 | } |
| 199 | |
| 200 | super.onQueryComplete(cursor); |
| 201 | } |
Dmitri Plotnikov | 501b7ea | 2010-04-07 17:20:49 -0700 | [diff] [blame] | 202 | } |