Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 13d848c9 authored by Walter Jang's avatar Walter Jang Committed by Android (Google) Code Review
Browse files

Merge "Delete group members one by one in edit mode"

parents 376849a2 05e7147f
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -23,6 +23,12 @@
        android:title="@string/menu_addToGroup"
        contacts:showAsAction="ifRoom" />

    <item
        android:id="@+id/menu_edit_group"
        android:icon="@drawable/ic_create_24dp"
        android:title="@string/menu_editGroup"
        contacts:showAsAction="ifRoom" />

    <item
        android:id="@+id/menu_rename_group"
        android:title="@string/menu_renameGroup"/>
+1 −1
Original line number Diff line number Diff line
@@ -338,7 +338,7 @@
    <string name="groupUpdatedToast">Label updated</string>

    <!-- Toast displayed when contacts are removed from a label. [CHAR LIMIT=50] -->
    <string name="groupMembersRemovedToast">Removed contacts</string>
    <string name="groupMembersRemovedToast">Removed from label</string>

    <!-- Toast displayed when one or more contacts is added to a label. [CHAR LIMIT=50] -->
    <string name="groupMembersAddedToast">Added to label</string>
+76 −29
Original line number Diff line number Diff line
@@ -71,6 +71,7 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
    private static final String KEY_IS_INSERT_ACTION = "isInsertAction";
    private static final String KEY_GROUP_URI = "groupUri";
    private static final String KEY_GROUP_METADATA = "groupMetadata";
    private static final String KEY_IS_EDIT_MODE = "editMode";

    private static final String TAG_GROUP_MEMBERS = "groupMembers";
    private static final String TAG_SELECT_ACCOUNT_DIALOG = "selectAccountDialog";
@@ -118,19 +119,21 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
            }
            final long[] rawContactIdsToAdd;
            final long[] rawContactIdsToRemove;
            final String action;
            if (mType == TYPE_ADD) {
                rawContactIdsToAdd = rawContactIds;
                rawContactIdsToRemove = null;
                action = GroupMembersActivity.ACTION_ADD_TO_GROUP;
            } else if (mType == TYPE_REMOVE) {
                rawContactIdsToAdd = null;
                rawContactIdsToRemove = rawContactIds;
                action = GroupMembersActivity.ACTION_REMOVE_FROM_GROUP;
            } else {
                throw new IllegalStateException("Unrecognized type " + mType);
            }
            return ContactSaveService.createGroupUpdateIntent(
                    mContext, mGroupId, /* newLabel */ null, rawContactIdsToAdd,
                    rawContactIdsToRemove, GroupMembersActivity.class,
                    GroupMembersActivity.ACTION_ADD_TO_GROUP);
                    rawContactIdsToRemove, GroupMembersActivity.class, action);
        }

        // TODO(wjang): prune raw contacts that are already in the group; ContactSaveService will
@@ -177,12 +180,13 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements

    private ActionBarAdapter mActionBarAdapter;

    private GroupMetadata mGroupMetadata;

    private GroupMembersFragment mMembersFragment;

    private Uri mGroupUri;
    private boolean mIsInsertAction;
    private boolean mIsEditMode;

    private GroupMetadata mGroupMetadata;

    @Override
    public void onCreate(Bundle savedState) {
@@ -192,6 +196,7 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
        if (savedState != null) {
            mGroupUri = savedState.getParcelable(KEY_GROUP_URI);
            mIsInsertAction = savedState.getBoolean(KEY_IS_INSERT_ACTION);
            mIsEditMode = savedState.getBoolean(KEY_IS_EDIT_MODE);
            mGroupMetadata = savedState.getParcelable(KEY_GROUP_METADATA);
        } else {
            mGroupUri = getIntent().getData();
@@ -265,8 +270,9 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
        if (mActionBarAdapter != null) {
            mActionBarAdapter.onSaveInstanceState(outState);
        }
        outState.putBoolean(KEY_IS_INSERT_ACTION, mIsInsertAction);
        outState.putParcelable(KEY_GROUP_URI, mGroupUri);
        outState.putBoolean(KEY_IS_INSERT_ACTION, mIsInsertAction);
        outState.putBoolean(KEY_IS_EDIT_MODE, mIsEditMode);
        outState.putParcelable(KEY_GROUP_METADATA, mGroupMetadata);
    }

@@ -293,14 +299,13 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
        super.onNewIntent(newIntent);

        if (isDeleteAction(newIntent.getAction())) {
            Toast.makeText(this, R.string.groupDeletedToast, Toast.LENGTH_SHORT).show();
            toast(R.string.groupDeletedToast);
            setResult(RESULT_OK);
            finish();
        } else if (isSaveAction(newIntent.getAction())) {
            final Uri groupUri = newIntent.getData();
            if (groupUri == null) {
                Toast.makeText(this, R.string.groupSavedErrorToast, Toast.LENGTH_SHORT).show();
                setResultCanceledAndFinish();
                setResultCanceledAndFinish(R.string.groupSavedErrorToast);
                return;
            }
            if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "Received group URI " + groupUri);
@@ -308,24 +313,16 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
            mGroupUri = groupUri;
            mIsInsertAction = false;

            Toast.makeText(this, getToastMessageForSaveAction(newIntent.getAction()),
                    Toast.LENGTH_SHORT).show();

            mMembersFragment = GroupMembersFragment.newInstance(groupUri);
            mMembersFragment.setListener(this);

            final FragmentTransaction transaction = getFragmentManager().beginTransaction();
            addGroupsAndFiltersFragments(transaction);
            transaction.replace(R.id.fragment_container_inner, mMembersFragment, TAG_GROUP_MEMBERS)
                    .commitAllowingStateLoss();

            if (mGroupMetadata != null && mGroupMetadata.editable) {
                mMembersFragment.setCheckBoxListListener(this);
            }
            toast(getToastMessageForSaveAction(newIntent.getAction()));

            // If we're editing the group, don't reload the fragment so the user can
            // continue to remove group members one by one
            if (!mIsEditMode && !ACTION_REMOVE_FROM_GROUP.equals(newIntent.getAction())) {
                replaceGroupMembersFragment();
                invalidateOptionsMenu();
            }
        }
    }

    private static boolean isDeleteAction(String action) {
        return ACTION_DELETE_GROUP.equals(action);
@@ -346,6 +343,18 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
        throw new IllegalArgumentException("Unhanded contact save action " + action);
    }

    private void replaceGroupMembersFragment() {
        mMembersFragment = GroupMembersFragment.newInstance(mGroupUri);
        mMembersFragment.setListener(this);
        final FragmentTransaction transaction = getFragmentManager().beginTransaction();
        addGroupsAndFiltersFragments(transaction);
        transaction.replace(R.id.fragment_container_inner, mMembersFragment, TAG_GROUP_MEMBERS)
                .commitAllowingStateLoss();
        if (mGroupMetadata != null && mGroupMetadata.editable) {
            mMembersFragment.setCheckBoxListListener(this);
        }
    }

    @Override
    protected void onGroupMenuItemClicked(long groupId) {
        if (mGroupMetadata.groupId != groupId) {
@@ -392,7 +401,9 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
        setVisible(menu, R.id.menu_add, isGroupEditable && !isSelectionMode);
        setVisible(menu, R.id.menu_rename_group, !isGroupReadOnly && !isSelectionMode);
        setVisible(menu, R.id.menu_delete_group, !isGroupReadOnly && !isSelectionMode);
        setVisible(menu, R.id.menu_remove_from_group, isGroupEditable && isSelectionMode);
        setVisible(menu, R.id.menu_edit_group, isGroupEditable && !mIsEditMode);
        setVisible(menu, R.id.menu_remove_from_group, isGroupEditable && isSelectionMode &&
                !mIsEditMode);

        return true;
    }
@@ -431,6 +442,15 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
                deleteGroup();
                return true;
            }
            case R.id.menu_edit_group: {
                if (mMembersFragment == null) {
                    return false;
                }
                mIsEditMode = true;
                mActionBarAdapter.setSelectionMode(true);
                mMembersFragment.displayDeleteButtons(true);
                return true;
            }
            case R.id.menu_remove_from_group: {
                if (mMembersFragment == null) {
                    return false;
@@ -482,6 +502,12 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
            mDrawer.closeDrawer(GravityCompat.START);
        } else if (mIsInsertAction) {
            finish();
        } else if (mIsEditMode) {
            mIsEditMode = false;
            mActionBarAdapter.setSelectionMode(false);
            if (mMembersFragment != null) {
                mMembersFragment.displayDeleteButtons(false);
            }
        } else if (mActionBarAdapter.isSelectionMode()) {
            mActionBarAdapter.setSelectionMode(false);
            if (mMembersFragment != null) {
@@ -525,14 +551,18 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
        setResultCanceledAndFinish(-1);
    }

    private void setResultCanceledAndFinish(int toastResId) {
        if (toastResId >= 0) {
            Toast.makeText(this, toastResId, Toast.LENGTH_SHORT).show();
        }
    private void setResultCanceledAndFinish(int resId) {
        toast(resId);
        setResult(RESULT_CANCELED);
        finish();
    }

    private void toast(int resId) {
        if (resId >= 0) {
            Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
        }
    }

    // SelectAccountDialogFragment.Listener callbacks

    @Override
@@ -554,16 +584,24 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
        switch (action) {
            case ActionBarAdapter.Listener.Action.START_SELECTION_MODE:
                if (mMembersFragment != null) {
                    if (mIsEditMode) {
                        mMembersFragment.displayDeleteButtons(true);
                    } else {
                        mMembersFragment.displayCheckBoxes(true);
                    }
                }
                invalidateOptionsMenu();
                showFabWithAnimation(/* showFabWithAnimation = */ false);
                break;
            case ActionBarAdapter.Listener.Action.STOP_SEARCH_AND_SELECTION_MODE:
                mActionBarAdapter.setSearchMode(false);
                if (mMembersFragment != null) {
                    if (mIsEditMode) {
                        mMembersFragment.displayDeleteButtons(false);
                    } else {
                        mMembersFragment.displayCheckBoxes(false);
                    }
                }
                invalidateOptionsMenu();
                showFabWithAnimation(/* showFabWithAnimation */ true);
                break;
@@ -661,4 +699,13 @@ public class GroupMembersActivity extends ContactsDrawerActivity implements
        intent.putExtra(QuickContactActivity.EXTRA_PREVIOUS_SCREEN_TYPE, ScreenType.LIST_GROUP);
        startActivity(intent);
    }

    @Override
    public void onGroupMemberListItemDeleted(int position, long contactId) {
        final long[] contactIds = new long[1];
        contactIds[0] = contactId;
        new UpdateGroupMembersAsyncTask(UpdateGroupMembersAsyncTask.TYPE_REMOVE,
                this, contactIds, mGroupMetadata.groupId, mGroupMetadata.accountName,
                mGroupMetadata.accountType).execute();
    }
}
+25 −0
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ public class GroupMembersAdapter extends MultiSelectEntryContactListAdapter {

    private final CharSequence mUnknownNameText;
    private long mGroupId;
    private boolean mDisplayDeleteButtons;

    public GroupMembersAdapter(Context context) {
        super(context, GroupMembersQuery.CONTACT_ID);
@@ -89,6 +90,21 @@ public class GroupMembersAdapter extends MultiSelectEntryContactListAdapter {
        return Contacts.getLookupUri(contactId, lookupKey);
    }

    /** Returns the ID of the contact at the given position in the underlying cursor. */
    public long getContactId(int position) {
        final Cursor cursor = (Cursor) getItem(position);
        return cursor.getLong(GroupMembersQuery.CONTACT_ID);
    }

    public void setDisplayDeleteButtons(boolean displayDeleteButtons) {
        mDisplayDeleteButtons = displayDeleteButtons;
        notifyDataSetChanged();
    }

    public boolean getDisplayDeleteButtons() {
        return mDisplayDeleteButtons;
    }

    @Override
    public void configureLoader(CursorLoader loader, long directoryId) {
        loader.setUri(Data.CONTENT_URI.buildUpon()
@@ -143,6 +159,7 @@ public class GroupMembersAdapter extends MultiSelectEntryContactListAdapter {
        bindSectionHeaderAndDivider(view, position);
        bindName(view, cursor);
        bindPhoto(view, cursor);
        bindDeleteButton(view);
    }

    protected void bindSectionHeaderAndDivider(ContactListItemView view, int position) {
@@ -170,4 +187,12 @@ public class GroupMembersAdapter extends MultiSelectEntryContactListAdapter {
        getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, false, getCircularPhotos(),
                imageRequest);
    }

    private void bindDeleteButton(final ContactListItemView view) {
        if (mDisplayDeleteButtons) {
            view.getDeleteImageButton();
        } else {
            view.hideDeleteImageButton();
        }
    }
}
+14 −2
Original line number Diff line number Diff line
@@ -63,6 +63,9 @@ public class GroupMembersFragment extends MultiSelectContactsListFragment<GroupM

        /** Invoked when a group member in the list is clicked. */
        void onGroupMemberListItemClicked(int position, Uri contactLookupUri);

        /** Invoked when a the delete button for a group member in the list is clicked. */
        void onGroupMemberListItemDeleted(int position, long contactId);
    }

    /** Filters out duplicate contacts. */
@@ -209,6 +212,10 @@ public class GroupMembersFragment extends MultiSelectContactsListFragment<GroupM
        mListener = listener;
    }

    public void displayDeleteButtons(boolean displayDeleteButtons) {
        getAdapter().setDisplayDeleteButtons(displayDeleteButtons);
    }

    public ArrayList<String> getMemberContactIds() {
        return  new ArrayList<>(mGroupMemberContactIds);
    }
@@ -328,8 +335,13 @@ public class GroupMembersFragment extends MultiSelectContactsListFragment<GroupM
            return;
        }
        if (mListener != null) {
            if (getAdapter().getDisplayDeleteButtons()) {
                final long contactId = getAdapter().getContactId(position);
                mListener.onGroupMemberListItemDeleted(position, contactId);
            } else {
                final Uri contactLookupUri = getAdapter().getContactLookupUri(position);
                mListener.onGroupMemberListItemClicked(position, contactLookupUri);
            }
        }
    }
}