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