blob: a2bd2e98acd6033dc0eb1ef0faeb7260d75bfac1 [file] [log] [blame]
Dmitri Plotnikove843f912010-09-16 15:21:48 -07001/*
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 Plotnikov18ffaa22010-12-03 14:28:00 -080017package com.android.contacts.editor;
Dmitri Plotnikove843f912010-09-16 15:21:48 -070018
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -080019import android.app.Activity;
James Laskeyb2d2e422016-11-17 15:30:41 -080020import android.app.FragmentManager;
Dmitri Plotnikove843f912010-09-16 15:21:48 -070021import android.content.Context;
Katherine Kuanfd709032011-08-02 14:18:16 -070022import android.content.res.Resources;
Dmitri Plotnikove843f912010-09-16 15:21:48 -070023import android.database.Cursor;
24import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
Makoto Onuki93a00252012-01-11 16:48:40 -080025import android.text.TextUtils;
Dmitri Plotnikove843f912010-09-16 15:21:48 -070026import android.util.AttributeSet;
27import android.view.View;
28import android.view.View.OnClickListener;
Maurice Chub03e6a22012-05-04 12:46:25 -070029import android.view.ViewGroup;
Dmitri Plotnikove843f912010-09-16 15:21:48 -070030import android.widget.AdapterView;
31import android.widget.AdapterView.OnItemClickListener;
32import android.widget.ArrayAdapter;
Maurice Chub03e6a22012-05-04 12:46:25 -070033import android.widget.CheckedTextView;
Brian Attwell043fba62014-10-30 11:11:56 -070034import android.widget.ImageView;
Dmitri Plotnikove843f912010-09-16 15:21:48 -070035import android.widget.LinearLayout;
36import android.widget.ListPopupWindow;
37import android.widget.ListView;
38import android.widget.TextView;
39
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070040import com.android.contacts.GroupMetaDataLoader;
41import com.android.contacts.R;
Marcus Hagerottbc66bba2016-08-01 17:31:38 -070042import com.android.contacts.common.model.account.AccountWithDataSet;
Chiao Chengba09d4c2012-11-14 15:52:32 -080043import com.android.contacts.common.model.dataitem.DataKind;
Marcus Hagerottbc66bba2016-08-01 17:31:38 -070044import com.android.contacts.group.GroupNameEditDialogFragment;
Yorke Leecd321f62013-10-28 15:20:15 -070045import com.android.contacts.common.model.RawContactDelta;
Chiao Cheng738ff862012-11-30 12:06:03 -080046import com.android.contacts.common.model.ValuesDelta;
Yorke Leecd321f62013-10-28 15:20:15 -070047import com.android.contacts.common.model.RawContactModifier;
Chiao Cheng619ac162012-12-20 14:29:03 -080048import com.android.contacts.util.UiClosables;
Chiao Chengba09d4c2012-11-14 15:52:32 -080049import com.google.common.base.Objects;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070050
Dmitri Plotnikove843f912010-09-16 15:21:48 -070051import java.util.ArrayList;
52
53/**
54 * An editor for group membership. Displays the current group membership list and
55 * brings up a dialog to change it.
56 */
57public class GroupMembershipView extends LinearLayout
58 implements OnClickListener, OnItemClickListener {
59
Marcus Hagerottbc66bba2016-08-01 17:31:38 -070060 public static final String TAG_CREATE_GROUP_FRAGMENT = "createGroupDialog";
61
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -080062 private static final int CREATE_NEW_GROUP_GROUP_ID = 133;
63
Dmitri Plotnikove843f912010-09-16 15:21:48 -070064 public static final class GroupSelectionItem {
65 private final long mGroupId;
66 private final String mTitle;
67 private boolean mChecked;
68
69 public GroupSelectionItem(long groupId, String title, boolean checked) {
70 this.mGroupId = groupId;
71 this.mTitle = title;
72 mChecked = checked;
73 }
74
75 public long getGroupId() {
76 return mGroupId;
77 }
78
79 public boolean isChecked() {
80 return mChecked;
81 }
82
83 public void setChecked(boolean checked) {
84 mChecked = checked;
85 }
86
87 @Override
88 public String toString() {
89 return mTitle;
90 }
91 }
92
Maurice Chub03e6a22012-05-04 12:46:25 -070093 /**
94 * Extends the array adapter to show checkmarks on all but the last list item for
95 * the group membership popup. Note that this is highly specific to the fact that the
96 * group_membership_list_item.xml is a CheckedTextView object.
97 */
98 private class GroupMembershipAdapter<T> extends ArrayAdapter<T> {
99
Wenyi Wang05f47b52016-06-03 14:18:40 -0700100 // The position of the group with the largest group ID
101 private int mNewestGroupPosition;
102
Maurice Chub03e6a22012-05-04 12:46:25 -0700103 public GroupMembershipAdapter(Context context, int textViewResourceId) {
104 super(context, textViewResourceId);
105 }
106
107 public boolean getItemIsCheckable(int position) {
108 // Item is checkable if it is NOT the last one in the list
109 return position != getCount()-1;
110 }
111
112 @Override
113 public int getItemViewType(int position) {
114 return getItemIsCheckable(position) ? 0 : 1;
115 }
116
117 @Override
118 public int getViewTypeCount() {
119 return 2;
120 }
121
122 @Override
123 public View getView(int position, View convertView, ViewGroup parent) {
124 final View itemView = super.getView(position, convertView, parent);
Brian Attwelld690dff2014-12-02 15:04:56 -0800125 if (itemView == null) {
126 return null;
127 }
Maurice Chub03e6a22012-05-04 12:46:25 -0700128
129 // Hide the checkable drawable. This assumes that the item views
130 // are CheckedTextView objects
131 final CheckedTextView checkedTextView = (CheckedTextView)itemView;
132 if (!getItemIsCheckable(position)) {
133 checkedTextView.setCheckMarkDrawable(null);
134 }
Brian Attwelld690dff2014-12-02 15:04:56 -0800135 checkedTextView.setTextColor(mPrimaryTextColor);
Maurice Chub03e6a22012-05-04 12:46:25 -0700136
137 return checkedTextView;
138 }
Wenyi Wang05f47b52016-06-03 14:18:40 -0700139
140 public int getNewestGroupPosition() {
141 return mNewestGroupPosition;
142 }
143
144 public void setNewestGroupPosition(int newestGroupPosition) {
145 mNewestGroupPosition = newestGroupPosition;
146 }
147
Maurice Chub03e6a22012-05-04 12:46:25 -0700148 }
149
Maurice Chu851222a2012-06-21 11:43:08 -0700150 private RawContactDelta mState;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700151 private Cursor mGroupMetaData;
Tingting Wang6dc36b02015-11-15 21:36:22 -0800152 private boolean mAccountHasGroups;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700153 private String mAccountName;
154 private String mAccountType;
Dave Santoro2b3f3c52011-07-26 17:35:42 -0700155 private String mDataSet;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700156 private TextView mGroupList;
Maurice Chub03e6a22012-05-04 12:46:25 -0700157 private GroupMembershipAdapter<GroupSelectionItem> mAdapter;
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700158 private long mDefaultGroupId;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700159 private long mFavoritesGroupId;
160 private ListPopupWindow mPopup;
161 private DataKind mKind;
Dmitri Plotnikovad9659e2010-10-06 16:46:39 -0700162 private boolean mDefaultGroupVisibilityKnown;
163 private boolean mDefaultGroupVisible;
Maurice Chub03e6a22012-05-04 12:46:25 -0700164 private boolean mCreatedNewGroup;
James Laskeyb2d2e422016-11-17 15:30:41 -0800165 private GroupNameEditDialogFragment mGroupNameEditDialogFragment;
166 private GroupNameEditDialogFragment.Listener mListener =
167 new GroupNameEditDialogFragment.Listener() {
168 @Override
169 public void onGroupNameEditCancelled() {
170 }
171
172 @Override
173 public void onGroupNameEditCompleted(String name) {
174 mCreatedNewGroup = true;
175 }
176 };
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700177
Katherine Kuanfd709032011-08-02 14:18:16 -0700178 private String mNoGroupString;
179 private int mPrimaryTextColor;
Brian Attwelld690dff2014-12-02 15:04:56 -0800180 private int mHintTextColor;
Katherine Kuanfd709032011-08-02 14:18:16 -0700181
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700182 public GroupMembershipView(Context context) {
183 super(context);
184 }
185
186 public GroupMembershipView(Context context, AttributeSet attrs) {
187 super(context, attrs);
188 }
189
Dmitri Plotnikov02bb1252010-12-06 09:39:05 -0800190 @Override
Katherine Kuanfd709032011-08-02 14:18:16 -0700191 protected void onFinishInflate() {
192 super.onFinishInflate();
Brian Attwellf1402272014-12-16 16:00:08 -0800193 Resources resources = getContext().getResources();
Katherine Kuanfd709032011-08-02 14:18:16 -0700194 mPrimaryTextColor = resources.getColor(R.color.primary_text_color);
Brian Attwelld690dff2014-12-02 15:04:56 -0800195 mHintTextColor = resources.getColor(R.color.editor_disabled_text_color);
Brian Attwellf1402272014-12-16 16:00:08 -0800196 mNoGroupString = getContext().getString(R.string.group_edit_field_hint_text);
Gary Maif8622332016-09-09 12:00:30 -0700197 setFocusable(true);
198 setFocusableInTouchMode(true);
Katherine Kuanfd709032011-08-02 14:18:16 -0700199 }
200
James Laskeyb2d2e422016-11-17 15:30:41 -0800201 private void setGroupNameEditDialogFragment() {
202 final FragmentManager fragmentManager = ((Activity) getContext()).getFragmentManager();
203 mGroupNameEditDialogFragment = (GroupNameEditDialogFragment)
204 fragmentManager.findFragmentByTag(TAG_CREATE_GROUP_FRAGMENT);
205 if (mGroupNameEditDialogFragment != null) {
206 mGroupNameEditDialogFragment.setListener(mListener);
207 }
208 }
209
Katherine Kuanfd709032011-08-02 14:18:16 -0700210 @Override
Dmitri Plotnikov02bb1252010-12-06 09:39:05 -0800211 public void setEnabled(boolean enabled) {
212 super.setEnabled(enabled);
213 if (mGroupList != null) {
214 mGroupList.setEnabled(enabled);
215 }
216 }
217
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700218 public void setKind(DataKind kind) {
219 mKind = kind;
Brian Attwell043fba62014-10-30 11:11:56 -0700220 final ImageView imageView = (ImageView) findViewById(R.id.kind_icon);
221 imageView.setContentDescription(getResources().getString(kind.titleRes));
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700222 }
223
224 public void setGroupMetaData(Cursor groupMetaData) {
225 this.mGroupMetaData = groupMetaData;
226 updateView();
Maurice Chub03e6a22012-05-04 12:46:25 -0700227 // Open up the list of groups if a new group was just created.
228 if (mCreatedNewGroup) {
229 mCreatedNewGroup = false;
230 onClick(this); // This causes the popup to open.
231 if (mPopup != null) {
232 // Ensure that the newly created group is checked.
Wenyi Wang05f47b52016-06-03 14:18:40 -0700233 final int position = mAdapter.getNewestGroupPosition();
Maurice Chub03e6a22012-05-04 12:46:25 -0700234 ListView listView = mPopup.getListView();
Jay Shrauner9813b712014-02-21 13:09:09 -0800235 if (listView != null && !listView.isItemChecked(position)) {
Maurice Chub03e6a22012-05-04 12:46:25 -0700236 // Newly created group is not checked, so check it.
237 listView.setItemChecked(position, true);
238 onItemClick(listView, null, position, listView.getItemIdAtPosition(position));
239 }
240 }
241 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700242 }
243
Walter Jang79658e12015-09-24 10:36:26 -0700244 /** Whether {@link #setGroupMetaData} has been invoked yet. */
245 public boolean wasGroupMetaDataBound() {
246 return mGroupMetaData != null;
247 }
248
Tingting Wang6dc36b02015-11-15 21:36:22 -0800249 /**
250 * Return true if the account has groups to edit group membership for contacts
251 * belong to the account.
252 */
253 public boolean accountHasGroups() {
254 return mAccountHasGroups;
255 }
256
Maurice Chu851222a2012-06-21 11:43:08 -0700257 public void setState(RawContactDelta state) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700258 mState = state;
Maurice Chu851222a2012-06-21 11:43:08 -0700259 mAccountType = mState.getAccountType();
260 mAccountName = mState.getAccountName();
261 mDataSet = mState.getDataSet();
Dmitri Plotnikovad9659e2010-10-06 16:46:39 -0700262 mDefaultGroupVisibilityKnown = false;
Maurice Chub03e6a22012-05-04 12:46:25 -0700263 mCreatedNewGroup = false;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700264 updateView();
James Laskeyb2d2e422016-11-17 15:30:41 -0800265 setGroupNameEditDialogFragment();
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700266 }
267
268 private void updateView() {
Dmitri Plotnikov08d23342010-09-16 15:43:33 -0700269 if (mGroupMetaData == null || mGroupMetaData.isClosed() || mAccountType == null
270 || mAccountName == null) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700271 setVisibility(GONE);
272 return;
273 }
274
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700275 mFavoritesGroupId = 0;
276 mDefaultGroupId = 0;
277
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700278 StringBuilder sb = new StringBuilder();
279 mGroupMetaData.moveToPosition(-1);
280 while (mGroupMetaData.moveToNext()) {
281 String accountName = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_NAME);
282 String accountType = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_TYPE);
Dave Santoro2b3f3c52011-07-26 17:35:42 -0700283 String dataSet = mGroupMetaData.getString(GroupMetaDataLoader.DATA_SET);
284 if (accountName.equals(mAccountName) && accountType.equals(mAccountType)
285 && Objects.equal(dataSet, mDataSet)) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700286 long groupId = mGroupMetaData.getLong(GroupMetaDataLoader.GROUP_ID);
287 if (!mGroupMetaData.isNull(GroupMetaDataLoader.FAVORITES)
288 && mGroupMetaData.getInt(GroupMetaDataLoader.FAVORITES) != 0) {
289 mFavoritesGroupId = groupId;
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700290 } else if (!mGroupMetaData.isNull(GroupMetaDataLoader.AUTO_ADD)
291 && mGroupMetaData.getInt(GroupMetaDataLoader.AUTO_ADD) != 0) {
292 mDefaultGroupId = groupId;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700293 } else {
Tingting Wang6dc36b02015-11-15 21:36:22 -0800294 mAccountHasGroups = true;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700295 }
296
297 // Exclude favorites from the list - they are handled with special UI (star)
Dmitri Plotnikov5d11ccd2010-09-26 17:16:12 -0700298 // Also exclude the default group.
299 if (groupId != mFavoritesGroupId && groupId != mDefaultGroupId
300 && hasMembership(groupId)) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700301 String title = mGroupMetaData.getString(GroupMetaDataLoader.TITLE);
Makoto Onuki93a00252012-01-11 16:48:40 -0800302 if (!TextUtils.isEmpty(title)) {
303 if (sb.length() != 0) {
304 sb.append(", ");
305 }
306 sb.append(title);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700307 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700308 }
309 }
310 }
311
Tingting Wang6dc36b02015-11-15 21:36:22 -0800312 if (!mAccountHasGroups) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700313 setVisibility(GONE);
314 return;
315 }
316
317 if (mGroupList == null) {
318 mGroupList = (TextView) findViewById(R.id.group_list);
Daniel Lehmann82b49b82010-11-05 15:17:58 -0700319 mGroupList.setOnClickListener(this);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700320 }
321
Dmitri Plotnikov02bb1252010-12-06 09:39:05 -0800322 mGroupList.setEnabled(isEnabled());
Dmitri Plotnikov135b44c2011-01-19 11:05:04 -0800323 if (sb.length() == 0) {
Katherine Kuanfd709032011-08-02 14:18:16 -0700324 mGroupList.setText(mNoGroupString);
Brian Attwelld690dff2014-12-02 15:04:56 -0800325 mGroupList.setTextColor(mHintTextColor);
Dmitri Plotnikov135b44c2011-01-19 11:05:04 -0800326 } else {
327 mGroupList.setText(sb);
Katherine Kuanfd709032011-08-02 14:18:16 -0700328 mGroupList.setTextColor(mPrimaryTextColor);
Dmitri Plotnikov135b44c2011-01-19 11:05:04 -0800329 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700330 setVisibility(VISIBLE);
Dmitri Plotnikovad9659e2010-10-06 16:46:39 -0700331
332 if (!mDefaultGroupVisibilityKnown) {
333 // Only show the default group (My Contacts) if the contact is NOT in it
334 mDefaultGroupVisible = mDefaultGroupId != 0 && !hasMembership(mDefaultGroupId);
335 mDefaultGroupVisibilityKnown = true;
336 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700337 }
338
339 @Override
340 public void onClick(View v) {
Chiao Cheng619ac162012-12-20 14:29:03 -0800341 if (UiClosables.closeQuietly(mPopup)) {
Jay Shrauner9813b712014-02-21 13:09:09 -0800342 mPopup = null;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700343 return;
344 }
345
Gary Maif8622332016-09-09 12:00:30 -0700346 requestFocus();
Maurice Chub03e6a22012-05-04 12:46:25 -0700347 mAdapter = new GroupMembershipAdapter<GroupSelectionItem>(
Dmitri Plotnikov2b95e622011-01-24 16:52:59 -0800348 getContext(), R.layout.group_membership_list_item);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700349
Wenyi Wang05f47b52016-06-03 14:18:40 -0700350 long newestGroupId = -1;
351
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700352 mGroupMetaData.moveToPosition(-1);
353 while (mGroupMetaData.moveToNext()) {
354 String accountName = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_NAME);
355 String accountType = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_TYPE);
Dave Santoro2b3f3c52011-07-26 17:35:42 -0700356 String dataSet = mGroupMetaData.getString(GroupMetaDataLoader.DATA_SET);
357 if (accountName.equals(mAccountName) && accountType.equals(mAccountType)
358 && Objects.equal(dataSet, mDataSet)) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700359 long groupId = mGroupMetaData.getLong(GroupMetaDataLoader.GROUP_ID);
Dmitri Plotnikovad9659e2010-10-06 16:46:39 -0700360 if (groupId != mFavoritesGroupId
361 && (groupId != mDefaultGroupId || mDefaultGroupVisible)) {
Wenyi Wang05f47b52016-06-03 14:18:40 -0700362 if (groupId > newestGroupId) {
363 newestGroupId = groupId;
364 mAdapter.setNewestGroupPosition(mAdapter.getCount());
365 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700366 String title = mGroupMetaData.getString(GroupMetaDataLoader.TITLE);
367 boolean checked = hasMembership(groupId);
368 mAdapter.add(new GroupSelectionItem(groupId, title, checked));
369 }
370 }
371 }
372
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -0800373 mAdapter.add(new GroupSelectionItem(CREATE_NEW_GROUP_GROUP_ID,
374 getContext().getString(R.string.create_group_item_label), false));
375
Dmitri Plotnikovb93d6262010-09-26 18:24:40 -0700376 mPopup = new ListPopupWindow(getContext(), null);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700377 mPopup.setAnchorView(mGroupList);
378 mPopup.setAdapter(mAdapter);
Dmitri Plotnikov5d11ccd2010-09-26 17:16:12 -0700379 mPopup.setModal(true);
Daniel Lehmann6d172802012-04-25 18:17:20 -0700380 mPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700381 mPopup.show();
382
383 ListView listView = mPopup.getListView();
384 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Katherine Kuand85b9862011-09-25 19:37:38 -0700385 listView.setOverScrollMode(OVER_SCROLL_ALWAYS);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700386 int count = mAdapter.getCount();
387 for (int i = 0; i < count; i++) {
388 listView.setItemChecked(i, mAdapter.getItem(i).isChecked());
389 }
390
391 listView.setOnItemClickListener(this);
392 }
393
394 @Override
395 protected void onDetachedFromWindow() {
396 super.onDetachedFromWindow();
Chiao Cheng619ac162012-12-20 14:29:03 -0800397 UiClosables.closeQuietly(mPopup);
398 mPopup = null;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700399 }
400
401 @Override
402 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
403 ListView list = (ListView) parent;
404 int count = mAdapter.getCount();
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -0800405
406 if (list.isItemChecked(count - 1)) {
407 list.setItemChecked(count - 1, false);
408 createNewGroup();
409 return;
410 }
411
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700412 for (int i = 0; i < count; i++) {
413 mAdapter.getItem(i).setChecked(list.isItemChecked(i));
414 }
415
416 // First remove the memberships that have been unchecked
417 ArrayList<ValuesDelta> entries = mState.getMimeEntries(GroupMembership.CONTENT_ITEM_TYPE);
418 if (entries != null) {
419 for (ValuesDelta entry : entries) {
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700420 if (!entry.isDelete()) {
Maurice Chu851222a2012-06-21 11:43:08 -0700421 Long groupId = entry.getGroupRowId();
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700422 if (groupId != null && groupId != mFavoritesGroupId
Dmitri Plotnikova61e0fb2010-11-02 18:38:42 -0700423 && (groupId != mDefaultGroupId || mDefaultGroupVisible)
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700424 && !isGroupChecked(groupId)) {
425 entry.markDeleted();
426 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700427 }
428 }
429 }
430
431 // Now add the newly selected items
432 for (int i = 0; i < count; i++) {
433 GroupSelectionItem item = mAdapter.getItem(i);
434 long groupId = item.getGroupId();
435 if (item.isChecked() && !hasMembership(groupId)) {
Maurice Chu851222a2012-06-21 11:43:08 -0700436 ValuesDelta entry = RawContactModifier.insertChild(mState, mKind);
Jay Shraunerf54c9f22014-01-10 12:41:55 -0800437 if (entry != null) {
438 entry.setGroupRowId(groupId);
439 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700440 }
441 }
442
443 updateView();
444 }
445
446 private boolean isGroupChecked(long groupId) {
447 int count = mAdapter.getCount();
448 for (int i = 0; i < count; i++) {
449 GroupSelectionItem item = mAdapter.getItem(i);
450 if (groupId == item.getGroupId()) {
451 return item.isChecked();
452 }
453 }
454 return false;
455 }
456
457 private boolean hasMembership(long groupId) {
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700458 if (groupId == mDefaultGroupId && mState.isContactInsert()) {
459 return true;
460 }
461
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700462 ArrayList<ValuesDelta> entries = mState.getMimeEntries(GroupMembership.CONTENT_ITEM_TYPE);
463 if (entries != null) {
464 for (ValuesDelta values : entries) {
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700465 if (!values.isDelete()) {
Maurice Chu851222a2012-06-21 11:43:08 -0700466 Long id = values.getGroupRowId();
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700467 if (id != null && id == groupId) {
468 return true;
469 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700470 }
471 }
472 }
473 return false;
474 }
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -0800475
476 private void createNewGroup() {
Chiao Cheng619ac162012-12-20 14:29:03 -0800477 UiClosables.closeQuietly(mPopup);
478 mPopup = null;
James Laskeyb2d2e422016-11-17 15:30:41 -0800479 mGroupNameEditDialogFragment =
480 GroupNameEditDialogFragment.newInstanceForCreation(
481 new AccountWithDataSet(mAccountName, mAccountType, mDataSet), null);
482 mGroupNameEditDialogFragment.setListener(mListener);
483 mGroupNameEditDialogFragment.show(
Maurice Chub03e6a22012-05-04 12:46:25 -0700484 ((Activity) getContext()).getFragmentManager(),
Marcus Hagerottbc66bba2016-08-01 17:31:38 -0700485 TAG_CREATE_GROUP_FRAGMENT);
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -0800486 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700487}