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