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 17 package com.android.contacts.interactions; 18 19 20 import com.android.contacts.ContactSaveService; 21 import com.android.contacts.R; 22 23 import android.app.Activity; 24 import android.app.AlertDialog; 25 import android.app.Dialog; 26 import android.app.DialogFragment; 27 import android.app.FragmentTransaction; 28 import android.content.DialogInterface; 29 import android.content.Intent; 30 import android.os.Bundle; 31 32 import java.util.TreeSet; 33 34 /** 35 * An interaction invoked to join multiple contacts together. 36 */ 37 public 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 { onContactsJoined()43 void onContactsJoined(); 44 } 45 start(Activity activity, TreeSet<Long> contactIds)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 newInstance(TreeSet<Long> contactIds)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 onCreateDialog(Bundle savedInstanceState)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_link_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_link_confirmation) 75 .setNegativeButton(android.R.string.cancel, null) 76 .setPositiveButton(R.string.batch_link_confirmation_positive_button, 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