JoinContactsDialogFragment.java revision d3946cae17273ed1c2fceb507990882e3f828ba9
1/*
2 * Copyright (C) 2015 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
17package com.android.contacts.interactions;
18
19
20import com.android.contacts.ContactSaveService;
21import com.android.contacts.R;
22
23import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
26import android.app.DialogFragment;
27import android.app.FragmentTransaction;
28import android.content.DialogInterface;
29import android.content.Intent;
30import android.os.Bundle;
31
32import java.util.TreeSet;
33
34/**
35 * An interaction invoked to join multiple contacts together.
36 */
37public class JoinContactsDialogFragment extends DialogFragment {
38
39    private static final String FRAGMENT_TAG = "joinDialog";
40    private static final String KEY_CONTACT_IDS = "contactIds";
41
42    public interface JoinContactsListener {
43        void onContactsJoined();
44    }
45
46    public static void start(Activity activity, TreeSet<Long> contactIds) {
47        final FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
48        final JoinContactsDialogFragment newFragment
49                = JoinContactsDialogFragment.newInstance(contactIds);
50        newFragment.show(ft, FRAGMENT_TAG);
51    }
52
53    private static JoinContactsDialogFragment newInstance(TreeSet<Long> contactIds) {
54        final JoinContactsDialogFragment fragment = new JoinContactsDialogFragment();
55        Bundle arguments = new Bundle();
56        arguments.putSerializable(KEY_CONTACT_IDS, contactIds);
57        fragment.setArguments(arguments);
58        return fragment;
59    }
60
61    @Override
62    public Dialog onCreateDialog(Bundle savedInstanceState) {
63        final TreeSet<Long> contactIds =
64                (TreeSet<Long>) getArguments().getSerializable(KEY_CONTACT_IDS);
65        if (contactIds.size() <= 1) {
66            return new AlertDialog.Builder(getActivity())
67                    .setIconAttribute(android.R.attr.alertDialogIcon)
68                    .setMessage(R.string.batch_merge_single_contact_warning)
69                    .setPositiveButton(android.R.string.ok, null)
70                    .create();
71        }
72        return new AlertDialog.Builder(getActivity())
73                .setIconAttribute(android.R.attr.alertDialogIcon)
74                .setMessage(R.string.batch_merge_confirmation)
75                .setNegativeButton(android.R.string.cancel, null)
76                .setPositiveButton(android.R.string.ok,
77                        new DialogInterface.OnClickListener() {
78                            @Override
79                            public void onClick(DialogInterface dialog, int whichButton) {
80                                joinContacts(contactIds);
81                            }
82                        }
83                )
84                .create();
85    }
86
87    private void joinContacts(TreeSet<Long> contactIds) {
88        final Long[] contactIdsArray = contactIds.toArray(new Long[contactIds.size()]);
89        final long[] contactIdsArray2 = new long[contactIdsArray.length];
90        for (int i = 0; i < contactIds.size(); i++) {
91            contactIdsArray2[i] = contactIdsArray[i];
92        }
93
94        final Intent intent = ContactSaveService.createJoinSeveralContactsIntent(getActivity(),
95                contactIdsArray2);
96        getActivity().startService(intent);
97
98        notifyListener();
99    }
100
101    private void notifyListener() {
102        if (getActivity() instanceof JoinContactsListener) {
103            ((JoinContactsListener) getActivity()).onContactsJoined();
104        }
105    }
106
107}
108