Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 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.editor; |
| 18 | |
| 19 | import com.android.contacts.R; |
| 20 | import com.android.contacts.editor.PhotoActionPopup.ChoiceListItem; |
| 21 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 22 | import android.app.Activity; |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 23 | import android.app.AlertDialog; |
| 24 | import android.app.Dialog; |
| 25 | import android.app.DialogFragment; |
| 26 | import android.content.DialogInterface; |
| 27 | import android.content.DialogInterface.OnClickListener; |
| 28 | import android.os.Bundle; |
guanxiongliu | f34bd42 | 2016-07-29 20:24:00 -0700 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.widget.TextView; |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 31 | |
| 32 | import java.util.ArrayList; |
| 33 | |
| 34 | /** |
| 35 | * Displays the options for changing the contact photo. |
| 36 | */ |
| 37 | public class PhotoSourceDialogFragment extends DialogFragment { |
| 38 | |
| 39 | private static final String ARG_PHOTO_MODE = "photoMode"; |
| 40 | |
| 41 | /** |
| 42 | * Callbacks for the host of the {@link PhotoSourceDialogFragment}. |
| 43 | */ |
| 44 | public interface Listener { |
| 45 | void onRemovePictureChosen(); |
| 46 | void onTakePhotoChosen(); |
| 47 | void onPickFromGalleryChosen(); |
| 48 | } |
| 49 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 50 | public static void show(Activity activity, int photoMode) { |
| 51 | if (!(activity instanceof Listener)) { |
| 52 | throw new IllegalArgumentException( |
| 53 | "Activity must implement " + Listener.class.getName()); |
| 54 | } |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 55 | final Bundle args = new Bundle(); |
| 56 | args.putInt(ARG_PHOTO_MODE, photoMode); |
| 57 | |
| 58 | PhotoSourceDialogFragment dialog = new PhotoSourceDialogFragment(); |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 59 | dialog.setArguments(args); |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 60 | dialog.show(activity.getFragmentManager(), "photoSource"); |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 65 | // Get the available options for changing the photo |
| 66 | final int photoMode = getArguments().getInt(ARG_PHOTO_MODE); |
| 67 | final ArrayList<ChoiceListItem> choices = |
| 68 | PhotoActionPopup.getChoices(getActivity(), photoMode); |
| 69 | |
| 70 | // Prepare the AlertDialog items and click listener |
| 71 | final CharSequence[] items = new CharSequence[choices.size()]; |
| 72 | for (int i = 0; i < items.length; i++) { |
| 73 | items[i] = choices.get(i).toString(); |
| 74 | } |
| 75 | final OnClickListener clickListener = new OnClickListener() { |
| 76 | @Override |
| 77 | public void onClick(DialogInterface dialogInterface, int which) { |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 78 | final Listener listener = (Listener) getActivity(); |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 79 | final ChoiceListItem choice = choices.get(which); |
| 80 | switch (choice.getId()) { |
| 81 | case ChoiceListItem.ID_REMOVE: |
| 82 | listener.onRemovePictureChosen(); |
| 83 | break; |
| 84 | case ChoiceListItem.ID_TAKE_PHOTO: |
| 85 | listener.onTakePhotoChosen(); |
| 86 | break; |
| 87 | case ChoiceListItem.ID_PICK_PHOTO: |
| 88 | listener.onPickFromGalleryChosen(); |
| 89 | break; |
| 90 | } |
| 91 | dismiss(); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | // Build the AlertDialog |
guanxiongliu | f34bd42 | 2016-07-29 20:24:00 -0700 | [diff] [blame] | 96 | final TextView title = (TextView) View.inflate(getActivity(), R.layout.dialog_title, null); |
| 97 | title.setText(R.string.menu_change_photo); |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 98 | final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
guanxiongliu | f34bd42 | 2016-07-29 20:24:00 -0700 | [diff] [blame] | 99 | builder.setCustomTitle(title); |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 100 | builder.setItems(items, clickListener); |
| 101 | builder.setNegativeButton(android.R.string.cancel, /* listener =*/ null); |
| 102 | return builder.create(); |
| 103 | } |
| 104 | } |