19442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann/*
29442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann * Copyright (C) 2010 The Android Open Source Project
39442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann *
49442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann * Licensed under the Apache License, Version 2.0 (the "License");
59442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann * you may not use this file except in compliance with the License.
69442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann * You may obtain a copy of the License at
79442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann *
89442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann *      http://www.apache.org/licenses/LICENSE-2.0
99442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann *
109442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann * Unless required by applicable law or agreed to in writing, software
119442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann * distributed under the License is distributed on an "AS IS" BASIS,
129442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann * See the License for the specific language governing permissions and
149442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann * limitations under the License
159442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann */
169442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
1718ffaa2561cc7dd2e3ef81737e6537931c0a9a11Dmitri Plotnikovpackage com.android.contacts.editor;
189442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
199442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmannimport android.content.Context;
209442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmannimport android.view.View;
219442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmannimport android.widget.AdapterView;
229442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmannimport android.widget.AdapterView.OnItemClickListener;
239442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmannimport android.widget.ArrayAdapter;
249442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmannimport android.widget.ListAdapter;
259442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmannimport android.widget.ListPopupWindow;
269442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
27e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
28c21751ddc1a91453a37273ace9e24907bb54cc99Yorke Leeimport com.android.contacts.util.PhoneCapabilityTester;
29619ac169e45bf547a2cbcd8a9775bb234f45c00fChiao Chengimport com.android.contacts.util.UiClosables;
30e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
319442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmannimport java.util.ArrayList;
329442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
339442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann/**
34ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus * Shows a popup asking the user what to do for a photo. The result is passed back to the Listener
359442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann */
369442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmannpublic class PhotoActionPopup {
379442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann    public static final String TAG = "PhotoActionPopup";
389442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
39ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus    /**
40ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus     * Bitmask flags to specify which actions should be presented to the user.
41ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus     */
42ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus    public static final class Flags {
43ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        /** If set, show choice to use as primary photo. */
44ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        public static final int ALLOW_PRIMARY = 1;
45ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        /** If set, show choice to remove photo. */
46ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        public static final int REMOVE_PHOTO = 2;
47ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        /** If set, show choices to take a picture with the camera, or pick one from the gallery. */
48ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        public static final int TAKE_OR_PICK_PHOTO = 4;
49ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        /**
50ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus         *  If set, modifies the wording in the choices for TAKE_OR_PICK_PHOTO
51ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus         *  to emphasize that the existing photo will be replaced.
52ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus         */
53ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        public static final int TAKE_OR_PICK_PHOTO_REPLACE_WORDING = 8;
54ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus    }
55ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus
56ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus    /**
57ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus     * Convenient combinations of commonly-used flags (see {@link Flags}).
58ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus     */
59ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus    public static final class Modes {
60ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        public static final int NO_PHOTO =
61ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus                Flags.TAKE_OR_PICK_PHOTO;
62ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        public static final int READ_ONLY_ALLOW_PRIMARY =
63ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus                Flags.ALLOW_PRIMARY;
64ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        public static final int PHOTO_DISALLOW_PRIMARY =
65ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus                Flags.REMOVE_PHOTO |
66ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus                Flags.TAKE_OR_PICK_PHOTO |
67ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus                Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING;
68ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        public static final int PHOTO_ALLOW_PRIMARY =
69ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus                Flags.ALLOW_PRIMARY |
70ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus                Flags.REMOVE_PHOTO |
71ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus                Flags.TAKE_OR_PICK_PHOTO |
72ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus                Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING;
73ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus    }
749442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
759442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann    public static ListPopupWindow createPopupMenu(Context context, View anchorView,
769442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann            final Listener listener, int mode) {
779442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        // Build choices, depending on the current mode. We assume this Dialog is never called
789442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        // if there are NO choices (e.g. a read-only picture is already super-primary)
799442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        final ArrayList<ChoiceListItem> choices = new ArrayList<ChoiceListItem>(4);
809442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        // Use as Primary
81ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        if ((mode & Flags.ALLOW_PRIMARY) > 0) {
829442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann            choices.add(new ChoiceListItem(ChoiceListItem.ID_USE_AS_PRIMARY,
839442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                    context.getString(R.string.use_photo_as_primary)));
849442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        }
859442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        // Remove
86ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        if ((mode & Flags.REMOVE_PHOTO) > 0) {
879442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann            choices.add(new ChoiceListItem(ChoiceListItem.ID_REMOVE,
889442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                    context.getString(R.string.removePhoto)));
899442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        }
90ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        // Take photo or pick one from the gallery.  Wording differs if there is already a photo.
91ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus        if ((mode & Flags.TAKE_OR_PICK_PHOTO) > 0) {
92ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus            boolean replace = (mode & Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING) > 0;
93ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus            final int takePhotoResId = replace ? R.string.take_new_photo : R.string.take_photo;
94ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus            final String takePhotoString = context.getString(takePhotoResId);
95ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus            final int pickPhotoResId = replace ? R.string.pick_new_photo : R.string.pick_photo;
96ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus            final String pickPhotoString = context.getString(pickPhotoResId);
97c21751ddc1a91453a37273ace9e24907bb54cc99Yorke Lee            if (PhoneCapabilityTester.isCameraIntentRegistered(context)) {
98c21751ddc1a91453a37273ace9e24907bb54cc99Yorke Lee                choices.add(new ChoiceListItem(ChoiceListItem.ID_TAKE_PHOTO, takePhotoString));
99c21751ddc1a91453a37273ace9e24907bb54cc99Yorke Lee            }
100ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus            choices.add(new ChoiceListItem(ChoiceListItem.ID_PICK_PHOTO, pickPhotoString));
1019442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        }
102ced983d7a816256d93fdea1f81e63e4598c18875Josh Gargus
1039442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        final ListAdapter adapter = new ArrayAdapter<ChoiceListItem>(context,
104507d85177886bece6ac38870d0e4b6c4655abc55Katherine Kuan                R.layout.select_dialog_item, choices);
1059442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
1069442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        final ListPopupWindow listPopupWindow = new ListPopupWindow(context);
1079442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        final OnItemClickListener clickListener = new OnItemClickListener() {
1089442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann            @Override
1099442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1109442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                final ChoiceListItem choice = choices.get(position);
1119442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                switch (choice.getId()) {
1129442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                    case ChoiceListItem.ID_USE_AS_PRIMARY:
1139442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                        listener.onUseAsPrimaryChosen();
1149442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                        break;
1159442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                    case ChoiceListItem.ID_REMOVE:
1162293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan                        listener.onRemovePictureChosen();
1179442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                        break;
1189442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                    case ChoiceListItem.ID_TAKE_PHOTO:
1199442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                        listener.onTakePhotoChosen();
1209442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                        break;
1219442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                    case ChoiceListItem.ID_PICK_PHOTO:
1229442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                        listener.onPickFromGalleryChosen();
1239442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                        break;
1249442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann                }
1256fa7384a1a5576ace18159de61af91c8b7c34dc6Dave Santoro
126619ac169e45bf547a2cbcd8a9775bb234f45c00fChiao Cheng                UiClosables.closeQuietly(listPopupWindow);
1279442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann            }
1289442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        };
1299442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
1309442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        listPopupWindow.setAnchorView(anchorView);
1319442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        listPopupWindow.setAdapter(adapter);
1329442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        listPopupWindow.setOnItemClickListener(clickListener);
1339442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        listPopupWindow.setModal(true);
134832dc0e2d0f91a28166ea51a068ebaf339e09e0dDmitri Plotnikov        listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
1352d4632c2eb7d2dc3bb94bf64c14c471aafa67b0fMaurice Chu        final int minWidth = context.getResources().getDimensionPixelSize(
1362d4632c2eb7d2dc3bb94bf64c14c471aafa67b0fMaurice Chu                R.dimen.photo_action_popup_min_width);
1372d4632c2eb7d2dc3bb94bf64c14c471aafa67b0fMaurice Chu        if (anchorView.getWidth() < minWidth) {
1382d4632c2eb7d2dc3bb94bf64c14c471aafa67b0fMaurice Chu            listPopupWindow.setWidth(minWidth);
1392d4632c2eb7d2dc3bb94bf64c14c471aafa67b0fMaurice Chu        }
1409442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        return listPopupWindow;
1419442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann    }
1429442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
1439442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann    private static final class ChoiceListItem {
1449442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        private final int mId;
1459442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        private final String mCaption;
1469442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
1479442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        public static final int ID_USE_AS_PRIMARY = 0;
1489442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        public static final int ID_TAKE_PHOTO = 1;
1499442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        public static final int ID_PICK_PHOTO = 2;
1509442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        public static final int ID_REMOVE = 3;
1519442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
1529442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        public ChoiceListItem(int id, String caption) {
1539442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann            mId = id;
1549442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann            mCaption = caption;
1559442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        }
1569442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
1579442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        @Override
1589442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        public String toString() {
1599442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann            return mCaption;
1609442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        }
1619442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
1629442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        public int getId() {
1639442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann            return mId;
1649442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        }
1659442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann    }
1669442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann
1679442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann    public interface Listener {
1689442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        void onUseAsPrimaryChosen();
1692293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan        void onRemovePictureChosen();
1709442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        void onTakePhotoChosen();
1719442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann        void onPickFromGalleryChosen();
1729442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann    }
1739442ef4d2667eab16f65535b63757a7ffe4ccf69Daniel Lehmann}
174