1/*
2 * Copyright (C) 2010 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;
20
21import android.content.Context;
22import android.view.View;
23import android.widget.AdapterView;
24import android.widget.AdapterView.OnItemClickListener;
25import android.widget.ArrayAdapter;
26import android.widget.ListAdapter;
27import android.widget.ListPopupWindow;
28
29import java.util.ArrayList;
30
31/**
32 * Shows a popup asking the user what to do for a photo. The result is pased back to the Listener
33 */
34public class PhotoActionPopup {
35    public static final String TAG = "PhotoActionPopup";
36
37    public static final int MODE_NO_PHOTO = 0;
38    public static final int MODE_READ_ONLY_ALLOW_PRIMARY = 1;
39    public static final int MODE_PHOTO_DISALLOW_PRIMARY = 2;
40    public static final int MODE_PHOTO_ALLOW_PRIMARY = 3;
41
42    public static ListPopupWindow createPopupMenu(Context context, View anchorView,
43            final Listener listener, int mode) {
44        // Build choices, depending on the current mode. We assume this Dialog is never called
45        // if there are NO choices (e.g. a read-only picture is already super-primary)
46        final ArrayList<ChoiceListItem> choices = new ArrayList<ChoiceListItem>(4);
47        // Use as Primary
48        if (mode == MODE_PHOTO_ALLOW_PRIMARY || mode == MODE_READ_ONLY_ALLOW_PRIMARY) {
49            choices.add(new ChoiceListItem(ChoiceListItem.ID_USE_AS_PRIMARY,
50                    context.getString(R.string.use_photo_as_primary)));
51        }
52        // Remove
53        if (mode == MODE_PHOTO_DISALLOW_PRIMARY || mode == MODE_PHOTO_ALLOW_PRIMARY) {
54            choices.add(new ChoiceListItem(ChoiceListItem.ID_REMOVE,
55                    context.getString(R.string.removePhoto)));
56        }
57        // Take photo (if there is already a photo, it says "Take new photo")
58        if (mode == MODE_NO_PHOTO || mode == MODE_PHOTO_ALLOW_PRIMARY
59                || mode == MODE_PHOTO_DISALLOW_PRIMARY) {
60            final int resId = mode == MODE_NO_PHOTO ? R.string.take_photo :R.string.take_new_photo;
61            choices.add(new ChoiceListItem(ChoiceListItem.ID_TAKE_PHOTO,
62                    context.getString(resId)));
63        }
64        // Select from Gallery (or "Select new from Gallery")
65        if (mode == MODE_NO_PHOTO || mode == MODE_PHOTO_ALLOW_PRIMARY
66                || mode == MODE_PHOTO_DISALLOW_PRIMARY) {
67            final int resId = mode == MODE_NO_PHOTO ? R.string.pick_photo :R.string.pick_new_photo;
68            choices.add(new ChoiceListItem(ChoiceListItem.ID_PICK_PHOTO,
69                    context.getString(resId)));
70        }
71        final ListAdapter adapter = new ArrayAdapter<ChoiceListItem>(context,
72                R.layout.select_dialog_item, choices);
73
74        final ListPopupWindow listPopupWindow = new ListPopupWindow(context);
75        final OnItemClickListener clickListener = new OnItemClickListener() {
76            @Override
77            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
78                final ChoiceListItem choice = choices.get(position);
79                listPopupWindow.dismiss();
80
81                switch (choice.getId()) {
82                    case ChoiceListItem.ID_USE_AS_PRIMARY:
83                        listener.onUseAsPrimaryChosen();
84                        break;
85                    case ChoiceListItem.ID_REMOVE:
86                        listener.onRemovePictureChosen();
87                        break;
88                    case ChoiceListItem.ID_TAKE_PHOTO:
89                        listener.onTakePhotoChosen();
90                        break;
91                    case ChoiceListItem.ID_PICK_PHOTO:
92                        listener.onPickFromGalleryChosen();
93                        break;
94                }
95            }
96        };
97
98        listPopupWindow.setAnchorView(anchorView);
99        listPopupWindow.setAdapter(adapter);
100        listPopupWindow.setOnItemClickListener(clickListener);
101        listPopupWindow.setWidth(context.getResources().getDimensionPixelSize(
102                R.dimen.photo_action_popup_width));
103        listPopupWindow.setModal(true);
104        listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
105        return listPopupWindow;
106    }
107
108    private static final class ChoiceListItem {
109        private final int mId;
110        private final String mCaption;
111
112        public static final int ID_USE_AS_PRIMARY = 0;
113        public static final int ID_TAKE_PHOTO = 1;
114        public static final int ID_PICK_PHOTO = 2;
115        public static final int ID_REMOVE = 3;
116
117        public ChoiceListItem(int id, String caption) {
118            mId = id;
119            mCaption = caption;
120        }
121
122        @Override
123        public String toString() {
124            return mCaption;
125        }
126
127        public int getId() {
128            return mId;
129        }
130    }
131
132    public interface Listener {
133        void onUseAsPrimaryChosen();
134        void onRemovePictureChosen();
135        void onTakePhotoChosen();
136        void onPickFromGalleryChosen();
137    }
138}
139