Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 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 | |
Dmitri Plotnikov | 18ffaa2 | 2010-12-03 14:28:00 -0800 | [diff] [blame] | 17 | package com.android.contacts.editor; |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 18 | |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.view.View; |
| 21 | import android.widget.AdapterView; |
| 22 | import android.widget.AdapterView.OnItemClickListener; |
| 23 | import android.widget.ArrayAdapter; |
| 24 | import android.widget.ListAdapter; |
| 25 | import android.widget.ListPopupWindow; |
| 26 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 27 | import com.android.contacts.R; |
Yorke Lee | c21751d | 2012-12-05 15:56:40 -0800 | [diff] [blame] | 28 | import com.android.contacts.util.PhoneCapabilityTester; |
Chiao Cheng | 619ac16 | 2012-12-20 14:29:03 -0800 | [diff] [blame] | 29 | import com.android.contacts.util.UiClosables; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 30 | |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 31 | import java.util.ArrayList; |
| 32 | |
| 33 | /** |
Josh Gargus | ced983d | 2012-01-09 17:31:37 -0800 | [diff] [blame] | 34 | * Shows a popup asking the user what to do for a photo. The result is passed back to the Listener |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 35 | */ |
| 36 | public class PhotoActionPopup { |
| 37 | public static final String TAG = "PhotoActionPopup"; |
| 38 | |
Josh Gargus | ced983d | 2012-01-09 17:31:37 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Bitmask flags to specify which actions should be presented to the user. |
| 41 | */ |
| 42 | public static final class Flags { |
Josh Gargus | ced983d | 2012-01-09 17:31:37 -0800 | [diff] [blame] | 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; |
Brian Attwell | 7e67082 | 2014-11-07 16:36:30 -0800 | [diff] [blame] | 60 | public static final int READ_ONLY_PHOTO = 0; |
| 61 | public static final int WRITE_ABLE_PHOTO = |
Josh Gargus | ced983d | 2012-01-09 17:31:37 -0800 | [diff] [blame] | 62 | Flags.REMOVE_PHOTO | |
| 63 | Flags.TAKE_OR_PICK_PHOTO | |
| 64 | Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING; |
Brian Attwell | b0afa20 | 2015-05-15 12:25:23 -0700 | [diff] [blame^] | 65 | // When the popup represents multiple photos, the REMOVE_PHOTO option doesn't make sense. |
| 66 | // The REMOVE_PHOTO option would have to remove all photos. And sometimes some of the |
| 67 | // photos are readonly. |
| 68 | public static final int MULTIPLE_WRITE_ABLE_PHOTOS = |
| 69 | Flags.TAKE_OR_PICK_PHOTO | |
| 70 | Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING; |
Josh Gargus | ced983d | 2012-01-09 17:31:37 -0800 | [diff] [blame] | 71 | } |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 72 | |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 73 | public static ArrayList<ChoiceListItem> getChoices(Context context, int mode) { |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 74 | // Build choices, depending on the current mode. We assume this Dialog is never called |
| 75 | // if there are NO choices (e.g. a read-only picture is already super-primary) |
| 76 | final ArrayList<ChoiceListItem> choices = new ArrayList<ChoiceListItem>(4); |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 77 | // Remove |
Josh Gargus | ced983d | 2012-01-09 17:31:37 -0800 | [diff] [blame] | 78 | if ((mode & Flags.REMOVE_PHOTO) > 0) { |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 79 | choices.add(new ChoiceListItem(ChoiceListItem.ID_REMOVE, |
| 80 | context.getString(R.string.removePhoto))); |
| 81 | } |
Josh Gargus | ced983d | 2012-01-09 17:31:37 -0800 | [diff] [blame] | 82 | // Take photo or pick one from the gallery. Wording differs if there is already a photo. |
| 83 | if ((mode & Flags.TAKE_OR_PICK_PHOTO) > 0) { |
| 84 | boolean replace = (mode & Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING) > 0; |
| 85 | final int takePhotoResId = replace ? R.string.take_new_photo : R.string.take_photo; |
| 86 | final String takePhotoString = context.getString(takePhotoResId); |
| 87 | final int pickPhotoResId = replace ? R.string.pick_new_photo : R.string.pick_photo; |
| 88 | final String pickPhotoString = context.getString(pickPhotoResId); |
Yorke Lee | c21751d | 2012-12-05 15:56:40 -0800 | [diff] [blame] | 89 | if (PhoneCapabilityTester.isCameraIntentRegistered(context)) { |
| 90 | choices.add(new ChoiceListItem(ChoiceListItem.ID_TAKE_PHOTO, takePhotoString)); |
| 91 | } |
Josh Gargus | ced983d | 2012-01-09 17:31:37 -0800 | [diff] [blame] | 92 | choices.add(new ChoiceListItem(ChoiceListItem.ID_PICK_PHOTO, pickPhotoString)); |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 93 | } |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 94 | return choices; |
| 95 | } |
| 96 | |
| 97 | public static ListPopupWindow createPopupMenu(Context context, View anchorView, |
| 98 | final Listener listener, int mode) { |
| 99 | final ArrayList<ChoiceListItem> choices = getChoices(context, mode); |
Josh Gargus | ced983d | 2012-01-09 17:31:37 -0800 | [diff] [blame] | 100 | |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 101 | final ListAdapter adapter = new ArrayAdapter<ChoiceListItem>(context, |
Katherine Kuan | 507d851 | 2011-09-07 17:15:49 -0700 | [diff] [blame] | 102 | R.layout.select_dialog_item, choices); |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 103 | |
| 104 | final ListPopupWindow listPopupWindow = new ListPopupWindow(context); |
| 105 | final OnItemClickListener clickListener = new OnItemClickListener() { |
| 106 | @Override |
| 107 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 108 | final ChoiceListItem choice = choices.get(position); |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 109 | switch (choice.getId()) { |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 110 | case ChoiceListItem.ID_REMOVE: |
Katherine Kuan | 2293e55 | 2011-07-21 20:25:44 -0700 | [diff] [blame] | 111 | listener.onRemovePictureChosen(); |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 112 | break; |
| 113 | case ChoiceListItem.ID_TAKE_PHOTO: |
| 114 | listener.onTakePhotoChosen(); |
| 115 | break; |
| 116 | case ChoiceListItem.ID_PICK_PHOTO: |
| 117 | listener.onPickFromGalleryChosen(); |
| 118 | break; |
| 119 | } |
Dave Santoro | 6fa7384 | 2011-09-28 14:37:06 -0700 | [diff] [blame] | 120 | |
Chiao Cheng | 619ac16 | 2012-12-20 14:29:03 -0800 | [diff] [blame] | 121 | UiClosables.closeQuietly(listPopupWindow); |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 122 | } |
| 123 | }; |
| 124 | |
| 125 | listPopupWindow.setAnchorView(anchorView); |
| 126 | listPopupWindow.setAdapter(adapter); |
| 127 | listPopupWindow.setOnItemClickListener(clickListener); |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 128 | listPopupWindow.setModal(true); |
Dmitri Plotnikov | 832dc0e | 2011-02-14 12:18:00 -0800 | [diff] [blame] | 129 | listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
Maurice Chu | 2d4632c | 2012-05-09 18:29:54 -0700 | [diff] [blame] | 130 | final int minWidth = context.getResources().getDimensionPixelSize( |
| 131 | R.dimen.photo_action_popup_min_width); |
| 132 | if (anchorView.getWidth() < minWidth) { |
| 133 | listPopupWindow.setWidth(minWidth); |
| 134 | } |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 135 | return listPopupWindow; |
| 136 | } |
| 137 | |
Walter Jang | 0e72ce9 | 2015-02-23 12:27:21 -0800 | [diff] [blame] | 138 | public static final class ChoiceListItem { |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 139 | private final int mId; |
| 140 | private final String mCaption; |
| 141 | |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 142 | public static final int ID_TAKE_PHOTO = 1; |
| 143 | public static final int ID_PICK_PHOTO = 2; |
| 144 | public static final int ID_REMOVE = 3; |
| 145 | |
| 146 | public ChoiceListItem(int id, String caption) { |
| 147 | mId = id; |
| 148 | mCaption = caption; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return mCaption; |
| 154 | } |
| 155 | |
| 156 | public int getId() { |
| 157 | return mId; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | public interface Listener { |
Katherine Kuan | 2293e55 | 2011-07-21 20:25:44 -0700 | [diff] [blame] | 162 | void onRemovePictureChosen(); |
Daniel Lehmann | 9442ef4 | 2010-11-09 17:35:53 -0800 | [diff] [blame] | 163 | void onTakePhotoChosen(); |
| 164 | void onPickFromGalleryChosen(); |
| 165 | } |
| 166 | } |