blob: f545e7b6c63170445bf159e29770674a315819b0 [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.group.GroupNameEditDialogFragment;
Gary Mai69c182a2016-12-05 13:07:03 -080043import com.android.contacts.model.RawContactDelta;
Gary Mai69c182a2016-12-05 13:07:03 -080044import com.android.contacts.model.RawContactModifier;
Gary Mai0a49afa2016-12-05 15:53:58 -080045import com.android.contacts.model.ValuesDelta;
46import com.android.contacts.model.account.AccountWithDataSet;
47import com.android.contacts.model.dataitem.DataKind;
Chiao Cheng619ac162012-12-20 14:29:03 -080048import com.android.contacts.util.UiClosables;
Gary Mai0a49afa2016-12-05 15:53:58 -080049
Chiao Chengba09d4c2012-11-14 15:52:32 -080050import com.google.common.base.Objects;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070051
Dmitri Plotnikove843f912010-09-16 15:21:48 -070052import java.util.ArrayList;
53
54/**
55 * An editor for group membership. Displays the current group membership list and
56 * brings up a dialog to change it.
57 */
58public class GroupMembershipView extends LinearLayout
59 implements OnClickListener, OnItemClickListener {
60
Marcus Hagerottbc66bba2016-08-01 17:31:38 -070061 public static final String TAG_CREATE_GROUP_FRAGMENT = "createGroupDialog";
62
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -080063 private static final int CREATE_NEW_GROUP_GROUP_ID = 133;
64
Dmitri Plotnikove843f912010-09-16 15:21:48 -070065 public static final class GroupSelectionItem {
66 private final long mGroupId;
67 private final String mTitle;
68 private boolean mChecked;
69
70 public GroupSelectionItem(long groupId, String title, boolean checked) {
71 this.mGroupId = groupId;
72 this.mTitle = title;
73 mChecked = checked;
74 }
75
76 public long getGroupId() {
77 return mGroupId;
78 }
79
80 public boolean isChecked() {
81 return mChecked;
82 }
83
84 public void setChecked(boolean checked) {
85 mChecked = checked;
86 }
87
88 @Override
89 public String toString() {
90 return mTitle;
91 }
92 }
93
Maurice Chub03e6a22012-05-04 12:46:25 -070094 /**
95 * Extends the array adapter to show checkmarks on all but the last list item for
96 * the group membership popup. Note that this is highly specific to the fact that the
97 * group_membership_list_item.xml is a CheckedTextView object.
98 */
99 private class GroupMembershipAdapter<T> extends ArrayAdapter<T> {
100
Wenyi Wang05f47b52016-06-03 14:18:40 -0700101 // The position of the group with the largest group ID
102 private int mNewestGroupPosition;
103
Maurice Chub03e6a22012-05-04 12:46:25 -0700104 public GroupMembershipAdapter(Context context, int textViewResourceId) {
105 super(context, textViewResourceId);
106 }
107
108 public boolean getItemIsCheckable(int position) {
109 // Item is checkable if it is NOT the last one in the list
110 return position != getCount()-1;
111 }
112
113 @Override
114 public int getItemViewType(int position) {
115 return getItemIsCheckable(position) ? 0 : 1;
116 }
117
118 @Override
119 public int getViewTypeCount() {
120 return 2;
121 }
122
123 @Override
124 public View getView(int position, View convertView, ViewGroup parent) {
125 final View itemView = super.getView(position, convertView, parent);
Brian Attwelld690dff2014-12-02 15:04:56 -0800126 if (itemView == null) {
127 return null;
128 }
Maurice Chub03e6a22012-05-04 12:46:25 -0700129
130 // Hide the checkable drawable. This assumes that the item views
131 // are CheckedTextView objects
132 final CheckedTextView checkedTextView = (CheckedTextView)itemView;
133 if (!getItemIsCheckable(position)) {
134 checkedTextView.setCheckMarkDrawable(null);
135 }
Brian Attwelld690dff2014-12-02 15:04:56 -0800136 checkedTextView.setTextColor(mPrimaryTextColor);
Maurice Chub03e6a22012-05-04 12:46:25 -0700137
138 return checkedTextView;
139 }
Wenyi Wang05f47b52016-06-03 14:18:40 -0700140
141 public int getNewestGroupPosition() {
142 return mNewestGroupPosition;
143 }
144
145 public void setNewestGroupPosition(int newestGroupPosition) {
146 mNewestGroupPosition = newestGroupPosition;
147 }
148
Maurice Chub03e6a22012-05-04 12:46:25 -0700149 }
150
Maurice Chu851222a2012-06-21 11:43:08 -0700151 private RawContactDelta mState;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700152 private Cursor mGroupMetaData;
Tingting Wang6dc36b02015-11-15 21:36:22 -0800153 private boolean mAccountHasGroups;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700154 private String mAccountName;
155 private String mAccountType;
Dave Santoro2b3f3c52011-07-26 17:35:42 -0700156 private String mDataSet;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700157 private TextView mGroupList;
Maurice Chub03e6a22012-05-04 12:46:25 -0700158 private GroupMembershipAdapter<GroupSelectionItem> mAdapter;
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700159 private long mDefaultGroupId;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700160 private long mFavoritesGroupId;
161 private ListPopupWindow mPopup;
162 private DataKind mKind;
Dmitri Plotnikovad9659e2010-10-06 16:46:39 -0700163 private boolean mDefaultGroupVisibilityKnown;
164 private boolean mDefaultGroupVisible;
Maurice Chub03e6a22012-05-04 12:46:25 -0700165 private boolean mCreatedNewGroup;
James Laskeyb2d2e422016-11-17 15:30:41 -0800166 private GroupNameEditDialogFragment mGroupNameEditDialogFragment;
167 private GroupNameEditDialogFragment.Listener mListener =
168 new GroupNameEditDialogFragment.Listener() {
169 @Override
170 public void onGroupNameEditCancelled() {
171 }
172
173 @Override
174 public void onGroupNameEditCompleted(String name) {
175 mCreatedNewGroup = true;
176 }
177 };
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700178
Katherine Kuanfd709032011-08-02 14:18:16 -0700179 private String mNoGroupString;
180 private int mPrimaryTextColor;
Brian Attwelld690dff2014-12-02 15:04:56 -0800181 private int mHintTextColor;
Katherine Kuanfd709032011-08-02 14:18:16 -0700182
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700183 public GroupMembershipView(Context context) {
184 super(context);
185 }
186
187 public GroupMembershipView(Context context, AttributeSet attrs) {
188 super(context, attrs);
189 }
190
Dmitri Plotnikov02bb1252010-12-06 09:39:05 -0800191 @Override
Katherine Kuanfd709032011-08-02 14:18:16 -0700192 protected void onFinishInflate() {
193 super.onFinishInflate();
Brian Attwellf1402272014-12-16 16:00:08 -0800194 Resources resources = getContext().getResources();
Katherine Kuanfd709032011-08-02 14:18:16 -0700195 mPrimaryTextColor = resources.getColor(R.color.primary_text_color);
Brian Attwelld690dff2014-12-02 15:04:56 -0800196 mHintTextColor = resources.getColor(R.color.editor_disabled_text_color);
Brian Attwellf1402272014-12-16 16:00:08 -0800197 mNoGroupString = getContext().getString(R.string.group_edit_field_hint_text);
Gary Maif8622332016-09-09 12:00:30 -0700198 setFocusable(true);
199 setFocusableInTouchMode(true);
Katherine Kuanfd709032011-08-02 14:18:16 -0700200 }
201
James Laskeyb2d2e422016-11-17 15:30:41 -0800202 private void setGroupNameEditDialogFragment() {
203 final FragmentManager fragmentManager = ((Activity) getContext()).getFragmentManager();
204 mGroupNameEditDialogFragment = (GroupNameEditDialogFragment)
205 fragmentManager.findFragmentByTag(TAG_CREATE_GROUP_FRAGMENT);
206 if (mGroupNameEditDialogFragment != null) {
207 mGroupNameEditDialogFragment.setListener(mListener);
208 }
209 }
210
Katherine Kuanfd709032011-08-02 14:18:16 -0700211 @Override
Dmitri Plotnikov02bb1252010-12-06 09:39:05 -0800212 public void setEnabled(boolean enabled) {
213 super.setEnabled(enabled);
214 if (mGroupList != null) {
215 mGroupList.setEnabled(enabled);
216 }
217 }
218
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700219 public void setKind(DataKind kind) {
220 mKind = kind;
Brian Attwell043fba62014-10-30 11:11:56 -0700221 final ImageView imageView = (ImageView) findViewById(R.id.kind_icon);
222 imageView.setContentDescription(getResources().getString(kind.titleRes));
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700223 }
224
225 public void setGroupMetaData(Cursor groupMetaData) {
226 this.mGroupMetaData = groupMetaData;
227 updateView();
Maurice Chub03e6a22012-05-04 12:46:25 -0700228 // Open up the list of groups if a new group was just created.
229 if (mCreatedNewGroup) {
230 mCreatedNewGroup = false;
231 onClick(this); // This causes the popup to open.
232 if (mPopup != null) {
233 // Ensure that the newly created group is checked.
Wenyi Wang05f47b52016-06-03 14:18:40 -0700234 final int position = mAdapter.getNewestGroupPosition();
Maurice Chub03e6a22012-05-04 12:46:25 -0700235 ListView listView = mPopup.getListView();
Jay Shrauner9813b712014-02-21 13:09:09 -0800236 if (listView != null && !listView.isItemChecked(position)) {
Maurice Chub03e6a22012-05-04 12:46:25 -0700237 // Newly created group is not checked, so check it.
238 listView.setItemChecked(position, true);
239 onItemClick(listView, null, position, listView.getItemIdAtPosition(position));
240 }
241 }
242 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700243 }
244
Walter Jang79658e12015-09-24 10:36:26 -0700245 /** Whether {@link #setGroupMetaData} has been invoked yet. */
246 public boolean wasGroupMetaDataBound() {
247 return mGroupMetaData != null;
248 }
249
Tingting Wang6dc36b02015-11-15 21:36:22 -0800250 /**
251 * Return true if the account has groups to edit group membership for contacts
252 * belong to the account.
253 */
254 public boolean accountHasGroups() {
255 return mAccountHasGroups;
256 }
257
Maurice Chu851222a2012-06-21 11:43:08 -0700258 public void setState(RawContactDelta state) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700259 mState = state;
Maurice Chu851222a2012-06-21 11:43:08 -0700260 mAccountType = mState.getAccountType();
261 mAccountName = mState.getAccountName();
262 mDataSet = mState.getDataSet();
Dmitri Plotnikovad9659e2010-10-06 16:46:39 -0700263 mDefaultGroupVisibilityKnown = false;
Maurice Chub03e6a22012-05-04 12:46:25 -0700264 mCreatedNewGroup = false;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700265 updateView();
James Laskeyb2d2e422016-11-17 15:30:41 -0800266 setGroupNameEditDialogFragment();
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700267 }
268
269 private void updateView() {
Dmitri Plotnikov08d23342010-09-16 15:43:33 -0700270 if (mGroupMetaData == null || mGroupMetaData.isClosed() || mAccountType == null
271 || mAccountName == null) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700272 setVisibility(GONE);
273 return;
274 }
275
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700276 mFavoritesGroupId = 0;
277 mDefaultGroupId = 0;
278
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700279 StringBuilder sb = new StringBuilder();
280 mGroupMetaData.moveToPosition(-1);
281 while (mGroupMetaData.moveToNext()) {
282 String accountName = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_NAME);
283 String accountType = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_TYPE);
Dave Santoro2b3f3c52011-07-26 17:35:42 -0700284 String dataSet = mGroupMetaData.getString(GroupMetaDataLoader.DATA_SET);
285 if (accountName.equals(mAccountName) && accountType.equals(mAccountType)
286 && Objects.equal(dataSet, mDataSet)) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700287 long groupId = mGroupMetaData.getLong(GroupMetaDataLoader.GROUP_ID);
288 if (!mGroupMetaData.isNull(GroupMetaDataLoader.FAVORITES)
289 && mGroupMetaData.getInt(GroupMetaDataLoader.FAVORITES) != 0) {
290 mFavoritesGroupId = groupId;
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700291 } else if (!mGroupMetaData.isNull(GroupMetaDataLoader.AUTO_ADD)
292 && mGroupMetaData.getInt(GroupMetaDataLoader.AUTO_ADD) != 0) {
293 mDefaultGroupId = groupId;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700294 } else {
Tingting Wang6dc36b02015-11-15 21:36:22 -0800295 mAccountHasGroups = true;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700296 }
297
298 // Exclude favorites from the list - they are handled with special UI (star)
Dmitri Plotnikov5d11ccd2010-09-26 17:16:12 -0700299 // Also exclude the default group.
300 if (groupId != mFavoritesGroupId && groupId != mDefaultGroupId
301 && hasMembership(groupId)) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700302 String title = mGroupMetaData.getString(GroupMetaDataLoader.TITLE);
Makoto Onuki93a00252012-01-11 16:48:40 -0800303 if (!TextUtils.isEmpty(title)) {
304 if (sb.length() != 0) {
305 sb.append(", ");
306 }
307 sb.append(title);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700308 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700309 }
310 }
311 }
312
Tingting Wang6dc36b02015-11-15 21:36:22 -0800313 if (!mAccountHasGroups) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700314 setVisibility(GONE);
315 return;
316 }
317
318 if (mGroupList == null) {
319 mGroupList = (TextView) findViewById(R.id.group_list);
Daniel Lehmann82b49b82010-11-05 15:17:58 -0700320 mGroupList.setOnClickListener(this);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700321 }
322
Dmitri Plotnikov02bb1252010-12-06 09:39:05 -0800323 mGroupList.setEnabled(isEnabled());
Dmitri Plotnikov135b44c2011-01-19 11:05:04 -0800324 if (sb.length() == 0) {
Katherine Kuanfd709032011-08-02 14:18:16 -0700325 mGroupList.setText(mNoGroupString);
Brian Attwelld690dff2014-12-02 15:04:56 -0800326 mGroupList.setTextColor(mHintTextColor);
Dmitri Plotnikov135b44c2011-01-19 11:05:04 -0800327 } else {
328 mGroupList.setText(sb);
Katherine Kuanfd709032011-08-02 14:18:16 -0700329 mGroupList.setTextColor(mPrimaryTextColor);
Dmitri Plotnikov135b44c2011-01-19 11:05:04 -0800330 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700331 setVisibility(VISIBLE);
Dmitri Plotnikovad9659e2010-10-06 16:46:39 -0700332
333 if (!mDefaultGroupVisibilityKnown) {
334 // Only show the default group (My Contacts) if the contact is NOT in it
335 mDefaultGroupVisible = mDefaultGroupId != 0 && !hasMembership(mDefaultGroupId);
336 mDefaultGroupVisibilityKnown = true;
337 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700338 }
339
340 @Override
341 public void onClick(View v) {
Chiao Cheng619ac162012-12-20 14:29:03 -0800342 if (UiClosables.closeQuietly(mPopup)) {
Jay Shrauner9813b712014-02-21 13:09:09 -0800343 mPopup = null;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700344 return;
345 }
346
Gary Maif8622332016-09-09 12:00:30 -0700347 requestFocus();
Maurice Chub03e6a22012-05-04 12:46:25 -0700348 mAdapter = new GroupMembershipAdapter<GroupSelectionItem>(
Dmitri Plotnikov2b95e622011-01-24 16:52:59 -0800349 getContext(), R.layout.group_membership_list_item);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700350
Wenyi Wang05f47b52016-06-03 14:18:40 -0700351 long newestGroupId = -1;
352
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700353 mGroupMetaData.moveToPosition(-1);
354 while (mGroupMetaData.moveToNext()) {
355 String accountName = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_NAME);
356 String accountType = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_TYPE);
Dave Santoro2b3f3c52011-07-26 17:35:42 -0700357 String dataSet = mGroupMetaData.getString(GroupMetaDataLoader.DATA_SET);
358 if (accountName.equals(mAccountName) && accountType.equals(mAccountType)
359 && Objects.equal(dataSet, mDataSet)) {
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700360 long groupId = mGroupMetaData.getLong(GroupMetaDataLoader.GROUP_ID);
Dmitri Plotnikovad9659e2010-10-06 16:46:39 -0700361 if (groupId != mFavoritesGroupId
362 && (groupId != mDefaultGroupId || mDefaultGroupVisible)) {
Wenyi Wang05f47b52016-06-03 14:18:40 -0700363 if (groupId > newestGroupId) {
364 newestGroupId = groupId;
365 mAdapter.setNewestGroupPosition(mAdapter.getCount());
366 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700367 String title = mGroupMetaData.getString(GroupMetaDataLoader.TITLE);
368 boolean checked = hasMembership(groupId);
369 mAdapter.add(new GroupSelectionItem(groupId, title, checked));
370 }
371 }
372 }
373
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -0800374 mAdapter.add(new GroupSelectionItem(CREATE_NEW_GROUP_GROUP_ID,
375 getContext().getString(R.string.create_group_item_label), false));
376
Dmitri Plotnikovb93d6262010-09-26 18:24:40 -0700377 mPopup = new ListPopupWindow(getContext(), null);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700378 mPopup.setAnchorView(mGroupList);
379 mPopup.setAdapter(mAdapter);
Dmitri Plotnikov5d11ccd2010-09-26 17:16:12 -0700380 mPopup.setModal(true);
Daniel Lehmann6d172802012-04-25 18:17:20 -0700381 mPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700382 mPopup.show();
383
384 ListView listView = mPopup.getListView();
385 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Katherine Kuand85b9862011-09-25 19:37:38 -0700386 listView.setOverScrollMode(OVER_SCROLL_ALWAYS);
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700387 int count = mAdapter.getCount();
388 for (int i = 0; i < count; i++) {
389 listView.setItemChecked(i, mAdapter.getItem(i).isChecked());
390 }
391
392 listView.setOnItemClickListener(this);
393 }
394
395 @Override
396 protected void onDetachedFromWindow() {
397 super.onDetachedFromWindow();
Chiao Cheng619ac162012-12-20 14:29:03 -0800398 UiClosables.closeQuietly(mPopup);
399 mPopup = null;
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700400 }
401
402 @Override
403 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
404 ListView list = (ListView) parent;
405 int count = mAdapter.getCount();
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -0800406
407 if (list.isItemChecked(count - 1)) {
408 list.setItemChecked(count - 1, false);
409 createNewGroup();
410 return;
411 }
412
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700413 for (int i = 0; i < count; i++) {
414 mAdapter.getItem(i).setChecked(list.isItemChecked(i));
415 }
416
417 // First remove the memberships that have been unchecked
418 ArrayList<ValuesDelta> entries = mState.getMimeEntries(GroupMembership.CONTENT_ITEM_TYPE);
419 if (entries != null) {
420 for (ValuesDelta entry : entries) {
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700421 if (!entry.isDelete()) {
Maurice Chu851222a2012-06-21 11:43:08 -0700422 Long groupId = entry.getGroupRowId();
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700423 if (groupId != null && groupId != mFavoritesGroupId
Dmitri Plotnikova61e0fb2010-11-02 18:38:42 -0700424 && (groupId != mDefaultGroupId || mDefaultGroupVisible)
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700425 && !isGroupChecked(groupId)) {
426 entry.markDeleted();
427 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700428 }
429 }
430 }
431
432 // Now add the newly selected items
433 for (int i = 0; i < count; i++) {
434 GroupSelectionItem item = mAdapter.getItem(i);
435 long groupId = item.getGroupId();
436 if (item.isChecked() && !hasMembership(groupId)) {
Maurice Chu851222a2012-06-21 11:43:08 -0700437 ValuesDelta entry = RawContactModifier.insertChild(mState, mKind);
Jay Shraunerf54c9f22014-01-10 12:41:55 -0800438 if (entry != null) {
439 entry.setGroupRowId(groupId);
440 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700441 }
442 }
443
444 updateView();
445 }
446
447 private boolean isGroupChecked(long groupId) {
448 int count = mAdapter.getCount();
449 for (int i = 0; i < count; i++) {
450 GroupSelectionItem item = mAdapter.getItem(i);
451 if (groupId == item.getGroupId()) {
452 return item.isChecked();
453 }
454 }
455 return false;
456 }
457
458 private boolean hasMembership(long groupId) {
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700459 if (groupId == mDefaultGroupId && mState.isContactInsert()) {
460 return true;
461 }
462
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700463 ArrayList<ValuesDelta> entries = mState.getMimeEntries(GroupMembership.CONTENT_ITEM_TYPE);
464 if (entries != null) {
465 for (ValuesDelta values : entries) {
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700466 if (!values.isDelete()) {
Maurice Chu851222a2012-06-21 11:43:08 -0700467 Long id = values.getGroupRowId();
Dmitri Plotnikov025838f2010-09-22 17:52:42 -0700468 if (id != null && id == groupId) {
469 return true;
470 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700471 }
472 }
473 }
474 return false;
475 }
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -0800476
477 private void createNewGroup() {
Chiao Cheng619ac162012-12-20 14:29:03 -0800478 UiClosables.closeQuietly(mPopup);
479 mPopup = null;
James Laskeyb2d2e422016-11-17 15:30:41 -0800480 mGroupNameEditDialogFragment =
481 GroupNameEditDialogFragment.newInstanceForCreation(
482 new AccountWithDataSet(mAccountName, mAccountType, mDataSet), null);
483 mGroupNameEditDialogFragment.setListener(mListener);
484 mGroupNameEditDialogFragment.show(
Maurice Chub03e6a22012-05-04 12:46:25 -0700485 ((Activity) getContext()).getFragmentManager(),
Marcus Hagerottbc66bba2016-08-01 17:31:38 -0700486 TAG_CREATE_GROUP_FRAGMENT);
Dmitri Plotnikov1ac58b62010-11-19 16:12:09 -0800487 }
Dmitri Plotnikove843f912010-09-16 15:21:48 -0700488}