blob: 36a1340d6b155f49e3c9faf1ad18c96cc2459b0d [file] [log] [blame]
Daniel Lehmann9442ef42010-11-09 17:35:53 -08001/*
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
Dmitri Plotnikov18ffaa22010-12-03 14:28:00 -080017package com.android.contacts.editor;
Daniel Lehmann9442ef42010-11-09 17:35:53 -080018
Daniel Lehmann9442ef42010-11-09 17:35:53 -080019import android.content.Context;
20import android.view.View;
21import android.widget.AdapterView;
22import android.widget.AdapterView.OnItemClickListener;
23import android.widget.ArrayAdapter;
24import android.widget.ListAdapter;
25import android.widget.ListPopupWindow;
26
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070027import com.android.contacts.R;
28
Daniel Lehmann9442ef42010-11-09 17:35:53 -080029import java.util.ArrayList;
30
31/**
Josh Gargusced983d2012-01-09 17:31:37 -080032 * Shows a popup asking the user what to do for a photo. The result is passed back to the Listener
Daniel Lehmann9442ef42010-11-09 17:35:53 -080033 */
34public class PhotoActionPopup {
35 public static final String TAG = "PhotoActionPopup";
36
Josh Gargusced983d2012-01-09 17:31:37 -080037 /**
38 * Bitmask flags to specify which actions should be presented to the user.
39 */
40 public static final class Flags {
41 /** If set, show choice to use as primary photo. */
42 public static final int ALLOW_PRIMARY = 1;
43 /** If set, show choice to remove photo. */
44 public static final int REMOVE_PHOTO = 2;
45 /** If set, show choices to take a picture with the camera, or pick one from the gallery. */
46 public static final int TAKE_OR_PICK_PHOTO = 4;
47 /**
48 * If set, modifies the wording in the choices for TAKE_OR_PICK_PHOTO
49 * to emphasize that the existing photo will be replaced.
50 */
51 public static final int TAKE_OR_PICK_PHOTO_REPLACE_WORDING = 8;
52 }
53
54 /**
55 * Convenient combinations of commonly-used flags (see {@link Flags}).
56 */
57 public static final class Modes {
58 public static final int NO_PHOTO =
59 Flags.TAKE_OR_PICK_PHOTO;
60 public static final int READ_ONLY_ALLOW_PRIMARY =
61 Flags.ALLOW_PRIMARY;
62 public static final int PHOTO_DISALLOW_PRIMARY =
63 Flags.REMOVE_PHOTO |
64 Flags.TAKE_OR_PICK_PHOTO |
65 Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING;
66 public static final int PHOTO_ALLOW_PRIMARY =
67 Flags.ALLOW_PRIMARY |
68 Flags.REMOVE_PHOTO |
69 Flags.TAKE_OR_PICK_PHOTO |
70 Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING;
71 }
Daniel Lehmann9442ef42010-11-09 17:35:53 -080072
73 public static ListPopupWindow createPopupMenu(Context context, View anchorView,
74 final Listener listener, int mode) {
75 // Build choices, depending on the current mode. We assume this Dialog is never called
76 // if there are NO choices (e.g. a read-only picture is already super-primary)
77 final ArrayList<ChoiceListItem> choices = new ArrayList<ChoiceListItem>(4);
78 // Use as Primary
Josh Gargusced983d2012-01-09 17:31:37 -080079 if ((mode & Flags.ALLOW_PRIMARY) > 0) {
Daniel Lehmann9442ef42010-11-09 17:35:53 -080080 choices.add(new ChoiceListItem(ChoiceListItem.ID_USE_AS_PRIMARY,
81 context.getString(R.string.use_photo_as_primary)));
82 }
83 // Remove
Josh Gargusced983d2012-01-09 17:31:37 -080084 if ((mode & Flags.REMOVE_PHOTO) > 0) {
Daniel Lehmann9442ef42010-11-09 17:35:53 -080085 choices.add(new ChoiceListItem(ChoiceListItem.ID_REMOVE,
86 context.getString(R.string.removePhoto)));
87 }
Josh Gargusced983d2012-01-09 17:31:37 -080088 // Take photo or pick one from the gallery. Wording differs if there is already a photo.
89 if ((mode & Flags.TAKE_OR_PICK_PHOTO) > 0) {
90 boolean replace = (mode & Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING) > 0;
91 final int takePhotoResId = replace ? R.string.take_new_photo : R.string.take_photo;
92 final String takePhotoString = context.getString(takePhotoResId);
93 final int pickPhotoResId = replace ? R.string.pick_new_photo : R.string.pick_photo;
94 final String pickPhotoString = context.getString(pickPhotoResId);
95 choices.add(new ChoiceListItem(ChoiceListItem.ID_TAKE_PHOTO, takePhotoString));
96 choices.add(new ChoiceListItem(ChoiceListItem.ID_PICK_PHOTO, pickPhotoString));
Daniel Lehmann9442ef42010-11-09 17:35:53 -080097 }
Josh Gargusced983d2012-01-09 17:31:37 -080098
Daniel Lehmann9442ef42010-11-09 17:35:53 -080099 final ListAdapter adapter = new ArrayAdapter<ChoiceListItem>(context,
Katherine Kuan507d8512011-09-07 17:15:49 -0700100 R.layout.select_dialog_item, choices);
Daniel Lehmann9442ef42010-11-09 17:35:53 -0800101
102 final ListPopupWindow listPopupWindow = new ListPopupWindow(context);
103 final OnItemClickListener clickListener = new OnItemClickListener() {
104 @Override
105 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
106 final ChoiceListItem choice = choices.get(position);
Daniel Lehmann9442ef42010-11-09 17:35:53 -0800107 switch (choice.getId()) {
108 case ChoiceListItem.ID_USE_AS_PRIMARY:
109 listener.onUseAsPrimaryChosen();
110 break;
111 case ChoiceListItem.ID_REMOVE:
Katherine Kuan2293e552011-07-21 20:25:44 -0700112 listener.onRemovePictureChosen();
Daniel Lehmann9442ef42010-11-09 17:35:53 -0800113 break;
114 case ChoiceListItem.ID_TAKE_PHOTO:
115 listener.onTakePhotoChosen();
116 break;
117 case ChoiceListItem.ID_PICK_PHOTO:
118 listener.onPickFromGalleryChosen();
119 break;
120 }
Dave Santoro6fa73842011-09-28 14:37:06 -0700121
122 listPopupWindow.dismiss();
Daniel Lehmann9442ef42010-11-09 17:35:53 -0800123 }
124 };
125
126 listPopupWindow.setAnchorView(anchorView);
127 listPopupWindow.setAdapter(adapter);
128 listPopupWindow.setOnItemClickListener(clickListener);
Daniel Lehmann9442ef42010-11-09 17:35:53 -0800129 listPopupWindow.setModal(true);
Dmitri Plotnikov832dc0e2011-02-14 12:18:00 -0800130 listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
Maurice Chu2d4632c2012-05-09 18:29:54 -0700131 final int minWidth = context.getResources().getDimensionPixelSize(
132 R.dimen.photo_action_popup_min_width);
133 if (anchorView.getWidth() < minWidth) {
134 listPopupWindow.setWidth(minWidth);
135 }
Daniel Lehmann9442ef42010-11-09 17:35:53 -0800136 return listPopupWindow;
137 }
138
139 private static final class ChoiceListItem {
140 private final int mId;
141 private final String mCaption;
142
143 public static final int ID_USE_AS_PRIMARY = 0;
144 public static final int ID_TAKE_PHOTO = 1;
145 public static final int ID_PICK_PHOTO = 2;
146 public static final int ID_REMOVE = 3;
147
148 public ChoiceListItem(int id, String caption) {
149 mId = id;
150 mCaption = caption;
151 }
152
153 @Override
154 public String toString() {
155 return mCaption;
156 }
157
158 public int getId() {
159 return mId;
160 }
161 }
162
163 public interface Listener {
164 void onUseAsPrimaryChosen();
Katherine Kuan2293e552011-07-21 20:25:44 -0700165 void onRemovePictureChosen();
Daniel Lehmann9442ef42010-11-09 17:35:53 -0800166 void onTakePhotoChosen();
167 void onPickFromGalleryChosen();
168 }
169}