Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Dmitri Plotnikov | 18ffaa2 | 2010-12-03 14:28:00 -0800 | [diff] [blame] | 17 | package com.android.contacts.editor; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 18 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 22 | import android.database.Cursor; |
| 23 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Makoto Onuki | 93a0025 | 2012-01-11 16:48:40 -0800 | [diff] [blame] | 24 | import android.text.TextUtils; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | import android.view.View; |
| 27 | import android.view.View.OnClickListener; |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 28 | import android.view.ViewGroup; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 29 | import android.widget.AdapterView; |
| 30 | import android.widget.AdapterView.OnItemClickListener; |
| 31 | import android.widget.ArrayAdapter; |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 32 | import android.widget.CheckedTextView; |
Brian Attwell | 043fba6 | 2014-10-30 11:11:56 -0700 | [diff] [blame^] | 33 | import android.widget.ImageView; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 34 | import android.widget.LinearLayout; |
| 35 | import android.widget.ListPopupWindow; |
| 36 | import android.widget.ListView; |
| 37 | import android.widget.TextView; |
| 38 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 39 | import com.android.contacts.GroupMetaDataLoader; |
| 40 | import com.android.contacts.R; |
Chiao Cheng | ba09d4c | 2012-11-14 15:52:32 -0800 | [diff] [blame] | 41 | import com.android.contacts.common.model.dataitem.DataKind; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 42 | import com.android.contacts.interactions.GroupCreationDialogFragment; |
| 43 | import com.android.contacts.interactions.GroupCreationDialogFragment.OnGroupCreatedListener; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 44 | import com.android.contacts.common.model.RawContactDelta; |
Chiao Cheng | 738ff86 | 2012-11-30 12:06:03 -0800 | [diff] [blame] | 45 | import com.android.contacts.common.model.ValuesDelta; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 46 | import com.android.contacts.common.model.RawContactModifier; |
Chiao Cheng | 619ac16 | 2012-12-20 14:29:03 -0800 | [diff] [blame] | 47 | import com.android.contacts.util.UiClosables; |
Chiao Cheng | ba09d4c | 2012-11-14 15:52:32 -0800 | [diff] [blame] | 48 | import com.google.common.base.Objects; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 49 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 50 | import java.util.ArrayList; |
| 51 | |
| 52 | /** |
| 53 | * An editor for group membership. Displays the current group membership list and |
| 54 | * brings up a dialog to change it. |
| 55 | */ |
| 56 | public class GroupMembershipView extends LinearLayout |
| 57 | implements OnClickListener, OnItemClickListener { |
| 58 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 59 | private static final int CREATE_NEW_GROUP_GROUP_ID = 133; |
| 60 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 61 | public static final class GroupSelectionItem { |
| 62 | private final long mGroupId; |
| 63 | private final String mTitle; |
| 64 | private boolean mChecked; |
| 65 | |
| 66 | public GroupSelectionItem(long groupId, String title, boolean checked) { |
| 67 | this.mGroupId = groupId; |
| 68 | this.mTitle = title; |
| 69 | mChecked = checked; |
| 70 | } |
| 71 | |
| 72 | public long getGroupId() { |
| 73 | return mGroupId; |
| 74 | } |
| 75 | |
| 76 | public boolean isChecked() { |
| 77 | return mChecked; |
| 78 | } |
| 79 | |
| 80 | public void setChecked(boolean checked) { |
| 81 | mChecked = checked; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public String toString() { |
| 86 | return mTitle; |
| 87 | } |
| 88 | } |
| 89 | |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Extends the array adapter to show checkmarks on all but the last list item for |
| 92 | * the group membership popup. Note that this is highly specific to the fact that the |
| 93 | * group_membership_list_item.xml is a CheckedTextView object. |
| 94 | */ |
| 95 | private class GroupMembershipAdapter<T> extends ArrayAdapter<T> { |
| 96 | |
| 97 | public GroupMembershipAdapter(Context context, int textViewResourceId) { |
| 98 | super(context, textViewResourceId); |
| 99 | } |
| 100 | |
| 101 | public boolean getItemIsCheckable(int position) { |
| 102 | // Item is checkable if it is NOT the last one in the list |
| 103 | return position != getCount()-1; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int getItemViewType(int position) { |
| 108 | return getItemIsCheckable(position) ? 0 : 1; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public int getViewTypeCount() { |
| 113 | return 2; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public View getView(int position, View convertView, ViewGroup parent) { |
| 118 | final View itemView = super.getView(position, convertView, parent); |
| 119 | |
| 120 | // Hide the checkable drawable. This assumes that the item views |
| 121 | // are CheckedTextView objects |
| 122 | final CheckedTextView checkedTextView = (CheckedTextView)itemView; |
| 123 | if (!getItemIsCheckable(position)) { |
| 124 | checkedTextView.setCheckMarkDrawable(null); |
| 125 | } |
| 126 | |
| 127 | return checkedTextView; |
| 128 | } |
| 129 | } |
| 130 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 131 | private RawContactDelta mState; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 132 | private Cursor mGroupMetaData; |
| 133 | private String mAccountName; |
| 134 | private String mAccountType; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 135 | private String mDataSet; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 136 | private TextView mGroupList; |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 137 | private GroupMembershipAdapter<GroupSelectionItem> mAdapter; |
Dmitri Plotnikov | 025838f | 2010-09-22 17:52:42 -0700 | [diff] [blame] | 138 | private long mDefaultGroupId; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 139 | private long mFavoritesGroupId; |
| 140 | private ListPopupWindow mPopup; |
| 141 | private DataKind mKind; |
Dmitri Plotnikov | ad9659e | 2010-10-06 16:46:39 -0700 | [diff] [blame] | 142 | private boolean mDefaultGroupVisibilityKnown; |
| 143 | private boolean mDefaultGroupVisible; |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 144 | private boolean mCreatedNewGroup; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 145 | |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 146 | private String mNoGroupString; |
| 147 | private int mPrimaryTextColor; |
| 148 | private int mSecondaryTextColor; |
| 149 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 150 | public GroupMembershipView(Context context) { |
| 151 | super(context); |
| 152 | } |
| 153 | |
| 154 | public GroupMembershipView(Context context, AttributeSet attrs) { |
| 155 | super(context, attrs); |
| 156 | } |
| 157 | |
Dmitri Plotnikov | 02bb125 | 2010-12-06 09:39:05 -0800 | [diff] [blame] | 158 | @Override |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 159 | protected void onFinishInflate() { |
| 160 | super.onFinishInflate(); |
| 161 | Resources resources = mContext.getResources(); |
| 162 | mPrimaryTextColor = resources.getColor(R.color.primary_text_color); |
| 163 | mSecondaryTextColor = resources.getColor(R.color.secondary_text_color); |
Katherine Kuan | 9574314 | 2011-08-10 17:56:11 -0700 | [diff] [blame] | 164 | mNoGroupString = mContext.getString(R.string.group_edit_field_hint_text); |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
Dmitri Plotnikov | 02bb125 | 2010-12-06 09:39:05 -0800 | [diff] [blame] | 168 | public void setEnabled(boolean enabled) { |
| 169 | super.setEnabled(enabled); |
| 170 | if (mGroupList != null) { |
| 171 | mGroupList.setEnabled(enabled); |
| 172 | } |
| 173 | } |
| 174 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 175 | public void setKind(DataKind kind) { |
| 176 | mKind = kind; |
Brian Attwell | 043fba6 | 2014-10-30 11:11:56 -0700 | [diff] [blame^] | 177 | final ImageView imageView = (ImageView) findViewById(R.id.kind_icon); |
| 178 | imageView.setContentDescription(getResources().getString(kind.titleRes)); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | public void setGroupMetaData(Cursor groupMetaData) { |
| 182 | this.mGroupMetaData = groupMetaData; |
| 183 | updateView(); |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 184 | // Open up the list of groups if a new group was just created. |
| 185 | if (mCreatedNewGroup) { |
| 186 | mCreatedNewGroup = false; |
| 187 | onClick(this); // This causes the popup to open. |
| 188 | if (mPopup != null) { |
| 189 | // Ensure that the newly created group is checked. |
| 190 | int position = mAdapter.getCount() - 2; |
| 191 | ListView listView = mPopup.getListView(); |
Jay Shrauner | 9813b71 | 2014-02-21 13:09:09 -0800 | [diff] [blame] | 192 | if (listView != null && !listView.isItemChecked(position)) { |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 193 | // Newly created group is not checked, so check it. |
| 194 | listView.setItemChecked(position, true); |
| 195 | onItemClick(listView, null, position, listView.getItemIdAtPosition(position)); |
| 196 | } |
| 197 | } |
| 198 | } |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 201 | public void setState(RawContactDelta state) { |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 202 | mState = state; |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 203 | mAccountType = mState.getAccountType(); |
| 204 | mAccountName = mState.getAccountName(); |
| 205 | mDataSet = mState.getDataSet(); |
Dmitri Plotnikov | ad9659e | 2010-10-06 16:46:39 -0700 | [diff] [blame] | 206 | mDefaultGroupVisibilityKnown = false; |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 207 | mCreatedNewGroup = false; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 208 | updateView(); |
| 209 | } |
| 210 | |
| 211 | private void updateView() { |
Dmitri Plotnikov | 08d2334 | 2010-09-16 15:43:33 -0700 | [diff] [blame] | 212 | if (mGroupMetaData == null || mGroupMetaData.isClosed() || mAccountType == null |
| 213 | || mAccountName == null) { |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 214 | setVisibility(GONE); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | boolean accountHasGroups = false; |
Dmitri Plotnikov | 025838f | 2010-09-22 17:52:42 -0700 | [diff] [blame] | 219 | mFavoritesGroupId = 0; |
| 220 | mDefaultGroupId = 0; |
| 221 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 222 | StringBuilder sb = new StringBuilder(); |
| 223 | mGroupMetaData.moveToPosition(-1); |
| 224 | while (mGroupMetaData.moveToNext()) { |
| 225 | String accountName = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_NAME); |
| 226 | String accountType = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 227 | String dataSet = mGroupMetaData.getString(GroupMetaDataLoader.DATA_SET); |
| 228 | if (accountName.equals(mAccountName) && accountType.equals(mAccountType) |
| 229 | && Objects.equal(dataSet, mDataSet)) { |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 230 | long groupId = mGroupMetaData.getLong(GroupMetaDataLoader.GROUP_ID); |
| 231 | if (!mGroupMetaData.isNull(GroupMetaDataLoader.FAVORITES) |
| 232 | && mGroupMetaData.getInt(GroupMetaDataLoader.FAVORITES) != 0) { |
| 233 | mFavoritesGroupId = groupId; |
Dmitri Plotnikov | 025838f | 2010-09-22 17:52:42 -0700 | [diff] [blame] | 234 | } else if (!mGroupMetaData.isNull(GroupMetaDataLoader.AUTO_ADD) |
| 235 | && mGroupMetaData.getInt(GroupMetaDataLoader.AUTO_ADD) != 0) { |
| 236 | mDefaultGroupId = groupId; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 237 | } else { |
| 238 | accountHasGroups = true; |
| 239 | } |
| 240 | |
| 241 | // Exclude favorites from the list - they are handled with special UI (star) |
Dmitri Plotnikov | 5d11ccd | 2010-09-26 17:16:12 -0700 | [diff] [blame] | 242 | // Also exclude the default group. |
| 243 | if (groupId != mFavoritesGroupId && groupId != mDefaultGroupId |
| 244 | && hasMembership(groupId)) { |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 245 | String title = mGroupMetaData.getString(GroupMetaDataLoader.TITLE); |
Makoto Onuki | 93a0025 | 2012-01-11 16:48:40 -0800 | [diff] [blame] | 246 | if (!TextUtils.isEmpty(title)) { |
| 247 | if (sb.length() != 0) { |
| 248 | sb.append(", "); |
| 249 | } |
| 250 | sb.append(title); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 251 | } |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (!accountHasGroups) { |
| 257 | setVisibility(GONE); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | if (mGroupList == null) { |
| 262 | mGroupList = (TextView) findViewById(R.id.group_list); |
Daniel Lehmann | 82b49b8 | 2010-11-05 15:17:58 -0700 | [diff] [blame] | 263 | mGroupList.setOnClickListener(this); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Dmitri Plotnikov | 02bb125 | 2010-12-06 09:39:05 -0800 | [diff] [blame] | 266 | mGroupList.setEnabled(isEnabled()); |
Dmitri Plotnikov | 135b44c | 2011-01-19 11:05:04 -0800 | [diff] [blame] | 267 | if (sb.length() == 0) { |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 268 | mGroupList.setText(mNoGroupString); |
| 269 | mGroupList.setTextColor(mSecondaryTextColor); |
Dmitri Plotnikov | 135b44c | 2011-01-19 11:05:04 -0800 | [diff] [blame] | 270 | } else { |
| 271 | mGroupList.setText(sb); |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 272 | mGroupList.setTextColor(mPrimaryTextColor); |
Dmitri Plotnikov | 135b44c | 2011-01-19 11:05:04 -0800 | [diff] [blame] | 273 | } |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 274 | setVisibility(VISIBLE); |
Dmitri Plotnikov | ad9659e | 2010-10-06 16:46:39 -0700 | [diff] [blame] | 275 | |
| 276 | if (!mDefaultGroupVisibilityKnown) { |
| 277 | // Only show the default group (My Contacts) if the contact is NOT in it |
| 278 | mDefaultGroupVisible = mDefaultGroupId != 0 && !hasMembership(mDefaultGroupId); |
| 279 | mDefaultGroupVisibilityKnown = true; |
| 280 | } |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void onClick(View v) { |
Chiao Cheng | 619ac16 | 2012-12-20 14:29:03 -0800 | [diff] [blame] | 285 | if (UiClosables.closeQuietly(mPopup)) { |
Jay Shrauner | 9813b71 | 2014-02-21 13:09:09 -0800 | [diff] [blame] | 286 | mPopup = null; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 287 | return; |
| 288 | } |
| 289 | |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 290 | mAdapter = new GroupMembershipAdapter<GroupSelectionItem>( |
Dmitri Plotnikov | 2b95e62 | 2011-01-24 16:52:59 -0800 | [diff] [blame] | 291 | getContext(), R.layout.group_membership_list_item); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 292 | |
| 293 | mGroupMetaData.moveToPosition(-1); |
| 294 | while (mGroupMetaData.moveToNext()) { |
| 295 | String accountName = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_NAME); |
| 296 | String accountType = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 297 | String dataSet = mGroupMetaData.getString(GroupMetaDataLoader.DATA_SET); |
| 298 | if (accountName.equals(mAccountName) && accountType.equals(mAccountType) |
| 299 | && Objects.equal(dataSet, mDataSet)) { |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 300 | long groupId = mGroupMetaData.getLong(GroupMetaDataLoader.GROUP_ID); |
Dmitri Plotnikov | ad9659e | 2010-10-06 16:46:39 -0700 | [diff] [blame] | 301 | if (groupId != mFavoritesGroupId |
| 302 | && (groupId != mDefaultGroupId || mDefaultGroupVisible)) { |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 303 | String title = mGroupMetaData.getString(GroupMetaDataLoader.TITLE); |
| 304 | boolean checked = hasMembership(groupId); |
| 305 | mAdapter.add(new GroupSelectionItem(groupId, title, checked)); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 310 | mAdapter.add(new GroupSelectionItem(CREATE_NEW_GROUP_GROUP_ID, |
| 311 | getContext().getString(R.string.create_group_item_label), false)); |
| 312 | |
Dmitri Plotnikov | b93d626 | 2010-09-26 18:24:40 -0700 | [diff] [blame] | 313 | mPopup = new ListPopupWindow(getContext(), null); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 314 | mPopup.setAnchorView(mGroupList); |
| 315 | mPopup.setAdapter(mAdapter); |
Dmitri Plotnikov | 5d11ccd | 2010-09-26 17:16:12 -0700 | [diff] [blame] | 316 | mPopup.setModal(true); |
Daniel Lehmann | 6d17280 | 2012-04-25 18:17:20 -0700 | [diff] [blame] | 317 | mPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 318 | mPopup.show(); |
| 319 | |
| 320 | ListView listView = mPopup.getListView(); |
| 321 | listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); |
Katherine Kuan | d85b986 | 2011-09-25 19:37:38 -0700 | [diff] [blame] | 322 | listView.setOverScrollMode(OVER_SCROLL_ALWAYS); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 323 | int count = mAdapter.getCount(); |
| 324 | for (int i = 0; i < count; i++) { |
| 325 | listView.setItemChecked(i, mAdapter.getItem(i).isChecked()); |
| 326 | } |
| 327 | |
| 328 | listView.setOnItemClickListener(this); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | protected void onDetachedFromWindow() { |
| 333 | super.onDetachedFromWindow(); |
Chiao Cheng | 619ac16 | 2012-12-20 14:29:03 -0800 | [diff] [blame] | 334 | UiClosables.closeQuietly(mPopup); |
| 335 | mPopup = null; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 340 | ListView list = (ListView) parent; |
| 341 | int count = mAdapter.getCount(); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 342 | |
| 343 | if (list.isItemChecked(count - 1)) { |
| 344 | list.setItemChecked(count - 1, false); |
| 345 | createNewGroup(); |
| 346 | return; |
| 347 | } |
| 348 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 349 | for (int i = 0; i < count; i++) { |
| 350 | mAdapter.getItem(i).setChecked(list.isItemChecked(i)); |
| 351 | } |
| 352 | |
| 353 | // First remove the memberships that have been unchecked |
| 354 | ArrayList<ValuesDelta> entries = mState.getMimeEntries(GroupMembership.CONTENT_ITEM_TYPE); |
| 355 | if (entries != null) { |
| 356 | for (ValuesDelta entry : entries) { |
Dmitri Plotnikov | 025838f | 2010-09-22 17:52:42 -0700 | [diff] [blame] | 357 | if (!entry.isDelete()) { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 358 | Long groupId = entry.getGroupRowId(); |
Dmitri Plotnikov | 025838f | 2010-09-22 17:52:42 -0700 | [diff] [blame] | 359 | if (groupId != null && groupId != mFavoritesGroupId |
Dmitri Plotnikov | a61e0fb | 2010-11-02 18:38:42 -0700 | [diff] [blame] | 360 | && (groupId != mDefaultGroupId || mDefaultGroupVisible) |
Dmitri Plotnikov | 025838f | 2010-09-22 17:52:42 -0700 | [diff] [blame] | 361 | && !isGroupChecked(groupId)) { |
| 362 | entry.markDeleted(); |
| 363 | } |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // Now add the newly selected items |
| 369 | for (int i = 0; i < count; i++) { |
| 370 | GroupSelectionItem item = mAdapter.getItem(i); |
| 371 | long groupId = item.getGroupId(); |
| 372 | if (item.isChecked() && !hasMembership(groupId)) { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 373 | ValuesDelta entry = RawContactModifier.insertChild(mState, mKind); |
Jay Shrauner | f54c9f2 | 2014-01-10 12:41:55 -0800 | [diff] [blame] | 374 | if (entry != null) { |
| 375 | entry.setGroupRowId(groupId); |
| 376 | } |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | updateView(); |
| 381 | } |
| 382 | |
| 383 | private boolean isGroupChecked(long groupId) { |
| 384 | int count = mAdapter.getCount(); |
| 385 | for (int i = 0; i < count; i++) { |
| 386 | GroupSelectionItem item = mAdapter.getItem(i); |
| 387 | if (groupId == item.getGroupId()) { |
| 388 | return item.isChecked(); |
| 389 | } |
| 390 | } |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | private boolean hasMembership(long groupId) { |
Dmitri Plotnikov | 025838f | 2010-09-22 17:52:42 -0700 | [diff] [blame] | 395 | if (groupId == mDefaultGroupId && mState.isContactInsert()) { |
| 396 | return true; |
| 397 | } |
| 398 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 399 | ArrayList<ValuesDelta> entries = mState.getMimeEntries(GroupMembership.CONTENT_ITEM_TYPE); |
| 400 | if (entries != null) { |
| 401 | for (ValuesDelta values : entries) { |
Dmitri Plotnikov | 025838f | 2010-09-22 17:52:42 -0700 | [diff] [blame] | 402 | if (!values.isDelete()) { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 403 | Long id = values.getGroupRowId(); |
Dmitri Plotnikov | 025838f | 2010-09-22 17:52:42 -0700 | [diff] [blame] | 404 | if (id != null && id == groupId) { |
| 405 | return true; |
| 406 | } |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } |
| 410 | return false; |
| 411 | } |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 412 | |
| 413 | private void createNewGroup() { |
Chiao Cheng | 619ac16 | 2012-12-20 14:29:03 -0800 | [diff] [blame] | 414 | UiClosables.closeQuietly(mPopup); |
| 415 | mPopup = null; |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 416 | |
| 417 | GroupCreationDialogFragment.show( |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 418 | ((Activity) getContext()).getFragmentManager(), |
| 419 | mAccountType, |
| 420 | mAccountName, |
| 421 | mDataSet, |
| 422 | new OnGroupCreatedListener() { |
| 423 | @Override |
| 424 | public void onGroupCreated() { |
| 425 | mCreatedNewGroup = true; |
| 426 | } |
| 427 | }); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 428 | } |
Maurice Chu | b03e6a2 | 2012-05-04 12:46:25 -0700 | [diff] [blame] | 429 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 430 | } |