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

Commit 0d9c24b7 authored by Walter Jang's avatar Walter Jang
Browse files

Warn about saving pending changes before unlinking

Also, remove unchanged new raw contacts that were
created to edit read-only contacts before unlinking.

Bug 25314004

Change-Id: Id75083456cd24fc1b06e489380614172bd370ad4
parent e3373dce
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -147,6 +147,9 @@
    <!-- Confirmation dialog for unlinking contacts into multiple instances [CHAR LIMIT=NONE] -->
    <!-- Confirmation dialog for unlinking contacts into multiple instances [CHAR LIMIT=NONE] -->
    <string name="splitConfirmation">This contact will be unlinked into multiple contacts.</string>
    <string name="splitConfirmation">This contact will be unlinked into multiple contacts.</string>


    <!-- Confirmation dialog for unlinking contacts into multiple instances when there are also unsaved changes for the current contact. [CHAR LIMIT=NONE] -->
    <string name="splitConfirmationWithPendingChanges">There are unsaved changes. Do you want to save them before unlinking this contact into multiple contacts?</string>

    <!-- Title of the confirmation dialog for joining contacts when there are unsaved changes. [CHAR LIMIT=40] -->
    <!-- Title of the confirmation dialog for joining contacts when there are unsaved changes. [CHAR LIMIT=40] -->
    <string name="joinConfirmation_title">Link contact?</string>
    <string name="joinConfirmation_title">Link contact?</string>


+14 −2
Original line number Original line Diff line number Diff line
@@ -85,6 +85,7 @@ import android.widget.Toast;


import java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.List;
import java.util.Set;
import java.util.Set;


@@ -860,7 +861,7 @@ abstract public class ContactEditorBaseFragment extends Fragment implements
    }
    }


    @Override
    @Override
    public void onSplitContactConfirmed() {
    public void onSplitContactConfirmed(boolean hasPendingChanges) {
        if (mState.isEmpty()) {
        if (mState.isEmpty()) {
            // This may happen when this Fragment is recreated by the system during users
            // This may happen when this Fragment is recreated by the system during users
            // confirming the split action (and thus this method is called just before onCreate()),
            // confirming the split action (and thus this method is called just before onCreate()),
@@ -870,6 +871,17 @@ abstract public class ContactEditorBaseFragment extends Fragment implements
            return;
            return;
        }
        }


        if (!hasPendingChanges && mHasNewContact) {
            // If the user didn't add anything new, we don't want to split out the newly created
            // raw contact into a name-only contact so remove them.
            final Iterator<RawContactDelta> iterator = mState.iterator();
            while (iterator.hasNext()) {
                final RawContactDelta rawContactDelta = iterator.next();
                if (rawContactDelta.getRawContactId() < 0) {
                    iterator.remove();
                }
            }
        }
        mState.markRawContactsForSplitting();
        mState.markRawContactsForSplitting();
        save(SaveMode.SPLIT);
        save(SaveMode.SPLIT);
    }
    }
@@ -877,7 +889,7 @@ abstract public class ContactEditorBaseFragment extends Fragment implements
    private boolean doSplitContactAction() {
    private boolean doSplitContactAction() {
        if (!hasValidState()) return false;
        if (!hasValidState()) return false;


        SplitContactConfirmationDialogFragment.show(this);
        SplitContactConfirmationDialogFragment.show(this, hasPendingChanges());
        return true;
        return true;
    }
    }


+34 −9
Original line number Original line Diff line number Diff line
@@ -32,34 +32,59 @@ import com.android.contacts.R;
 * Does not split the contact itself.
 * Does not split the contact itself.
 */
 */
public class SplitContactConfirmationDialogFragment extends DialogFragment {
public class SplitContactConfirmationDialogFragment extends DialogFragment {
    public static final String TAG = "SplitContactConfirmationDialog";


    public static void show(ContactEditorBaseFragment fragment) {
    private static final String ARG_HAS_PENDING_CHANGES = "hasPendingChanges";
        SplitContactConfirmationDialogFragment dialog = new

    /**
     * Callbacks for the dialog host.
     */
    public interface Listener {

        /**
         * Invoked after the user has confirmed that they want to proceed with the split.
         *
         * @param hasPendingChanges whether there are unsaved changes in the underlying contact
         *         that should be saved before the split.
         */
        void onSplitContactConfirmed(boolean hasPendingChanges);
    }

    public static void show(ContactEditorBaseFragment fragment, boolean hasPendingChanges) {
        final Bundle args = new Bundle();
        args.putBoolean(ARG_HAS_PENDING_CHANGES, hasPendingChanges);

        final SplitContactConfirmationDialogFragment dialog = new
                SplitContactConfirmationDialogFragment();
                SplitContactConfirmationDialogFragment();
        dialog.setTargetFragment(fragment, 0);
        dialog.setTargetFragment(fragment, 0);
        dialog.setArguments(args);
        dialog.show(fragment.getFragmentManager(), "splitContact");
        dialog.show(fragment.getFragmentManager(), "splitContact");
    }
    }


    private boolean mHasPendingChanges;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHasPendingChanges = getArguments().getBoolean(ARG_HAS_PENDING_CHANGES);
    }

    @Override
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.splitConfirmation_title);
        builder.setTitle(R.string.splitConfirmation_title);
        builder.setIconAttribute(android.R.attr.alertDialogIcon);
        builder.setIconAttribute(android.R.attr.alertDialogIcon);
        builder.setMessage(R.string.splitConfirmation);
        builder.setMessage(mHasPendingChanges
                ? R.string.splitConfirmationWithPendingChanges
                : R.string.splitConfirmation);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            @Override
            public void onClick(DialogInterface dialog, int which) {
            public void onClick(DialogInterface dialog, int which) {
                final Listener targetListener = (Listener) getTargetFragment();
                final Listener targetListener = (Listener) getTargetFragment();
                targetListener.onSplitContactConfirmed();
                targetListener.onSplitContactConfirmed(mHasPendingChanges);
            }
            }
        });
        });
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.setCancelable(false);
        builder.setCancelable(false);
        return builder.create();
        return builder.create();
    }
    }

    public interface Listener {
        void onSplitContactConfirmed();
    }
}
}