Walter Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.contacts.common.vcard; |
| 17 | |
| 18 | import android.app.Activity; |
| 19 | import android.app.AlertDialog; |
| 20 | import android.app.Dialog; |
| 21 | import android.app.DialogFragment; |
| 22 | import android.content.DialogInterface; |
| 23 | import android.net.Uri; |
| 24 | import android.os.Bundle; |
| 25 | |
| 26 | import com.android.contacts.R; |
| 27 | |
| 28 | /** Asks for confirmation before importing contacts from a vcard. */ |
| 29 | public class ImportVCardDialogFragment extends DialogFragment { |
| 30 | |
| 31 | static final String TAG = "importVCardDialog"; |
| 32 | |
| 33 | private static final String ARG_SOURCE_URI = "sourceUri"; |
| 34 | private static final String ARG_SOURCE_DISPLAY_NAME = "sourceDisplayName"; |
| 35 | |
| 36 | /** Callbacks for hosts of the {@link ImportVCardDialogFragment}. */ |
| 37 | public interface Listener { |
| 38 | |
| 39 | /** Invoked after the user has confirmed that contacts should be imported. */ |
| 40 | void onImportVCardConfirmed(Uri sourceUri, String sourceDisplayName); |
| 41 | |
| 42 | /** Invoked after the user has rejected importing contacts. */ |
| 43 | void onImportVCardDenied(); |
| 44 | } |
| 45 | |
| 46 | /** Displays the dialog asking for confirmation before importing contacts. */ |
| 47 | public static void show(Activity activity, Uri sourceUri, |
| 48 | String sourceDisplayName) { |
| 49 | if (!(activity instanceof Listener)) { |
| 50 | throw new IllegalArgumentException( |
| 51 | "Activity must implement " + Listener.class.getName()); |
| 52 | } |
| 53 | |
| 54 | final Bundle args = new Bundle(); |
| 55 | args.putParcelable(ARG_SOURCE_URI, sourceUri); |
| 56 | args.putString(ARG_SOURCE_DISPLAY_NAME, sourceDisplayName); |
| 57 | |
| 58 | final ImportVCardDialogFragment dialog = new ImportVCardDialogFragment(); |
| 59 | dialog.setArguments(args); |
| 60 | dialog.show(activity.getFragmentManager(), TAG); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 65 | final Uri sourceUri = getArguments().getParcelable(ARG_SOURCE_URI); |
| 66 | final String sourceDisplayName = getArguments().getString(ARG_SOURCE_DISPLAY_NAME); |
| 67 | |
| 68 | return new AlertDialog.Builder(getActivity()) |
| 69 | .setIconAttribute(android.R.attr.alertDialogIcon) |
| 70 | .setMessage(R.string.import_from_vcf_file_confirmation_message) |
| 71 | .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { |
| 72 | @Override |
| 73 | public void onClick(DialogInterface dialog, int whichButton) { |
| 74 | final Listener listener = (Listener) getActivity(); |
| 75 | if (listener != null) { |
| 76 | listener.onImportVCardConfirmed(sourceUri, sourceDisplayName); |
| 77 | } |
| 78 | } |
| 79 | }) |
| 80 | .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { |
| 81 | @Override |
| 82 | public void onClick(DialogInterface dialog, int whichButton) { |
| 83 | final Listener listener = (Listener) getActivity(); |
| 84 | if (listener != null) { |
| 85 | listener.onImportVCardDenied(); |
| 86 | } |
| 87 | } |
| 88 | }) |
| 89 | .create(); |
| 90 | } |
| 91 | } |