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 17 package com.android.contacts.editor; 18 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 27 import com.android.contacts.R; 28 import com.android.contacts.util.PhoneCapabilityTester; 29 import com.android.contacts.util.UiClosables; 30 31 import java.util.ArrayList; 32 33 /** 34 * Shows a popup asking the user what to do for a photo. The result is passed back to the Listener 35 */ 36 public class PhotoActionPopup { 37 public static final String TAG = "PhotoActionPopup"; 38 39 /** 40 * Bitmask flags to specify which actions should be presented to the user. 41 */ 42 public static final class Flags { 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_PHOTO = 0; 61 public static final int WRITE_ABLE_PHOTO = 62 Flags.REMOVE_PHOTO | 63 Flags.TAKE_OR_PICK_PHOTO | 64 Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING; 65 } 66 67 public static ListPopupWindow createPopupMenu(Context context, View anchorView, 68 final Listener listener, int mode) { 69 // Build choices, depending on the current mode. We assume this Dialog is never called 70 // if there are NO choices (e.g. a read-only picture is already super-primary) 71 final ArrayList<ChoiceListItem> choices = new ArrayList<ChoiceListItem>(4); 72 // Remove 73 if ((mode & Flags.REMOVE_PHOTO) > 0) { 74 choices.add(new ChoiceListItem(ChoiceListItem.ID_REMOVE, 75 context.getString(R.string.removePhoto))); 76 } 77 // Take photo or pick one from the gallery. Wording differs if there is already a photo. 78 if ((mode & Flags.TAKE_OR_PICK_PHOTO) > 0) { 79 boolean replace = (mode & Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING) > 0; 80 final int takePhotoResId = replace ? R.string.take_new_photo : R.string.take_photo; 81 final String takePhotoString = context.getString(takePhotoResId); 82 final int pickPhotoResId = replace ? R.string.pick_new_photo : R.string.pick_photo; 83 final String pickPhotoString = context.getString(pickPhotoResId); 84 if (PhoneCapabilityTester.isCameraIntentRegistered(context)) { 85 choices.add(new ChoiceListItem(ChoiceListItem.ID_TAKE_PHOTO, takePhotoString)); 86 } 87 choices.add(new ChoiceListItem(ChoiceListItem.ID_PICK_PHOTO, pickPhotoString)); 88 } 89 90 final ListAdapter adapter = new ArrayAdapter<ChoiceListItem>(context, 91 R.layout.select_dialog_item, choices); 92 93 final ListPopupWindow listPopupWindow = new ListPopupWindow(context); 94 final OnItemClickListener clickListener = new OnItemClickListener() { 95 @Override 96 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 97 final ChoiceListItem choice = choices.get(position); 98 switch (choice.getId()) { 99 case ChoiceListItem.ID_REMOVE: 100 listener.onRemovePictureChosen(); 101 break; 102 case ChoiceListItem.ID_TAKE_PHOTO: 103 listener.onTakePhotoChosen(); 104 break; 105 case ChoiceListItem.ID_PICK_PHOTO: 106 listener.onPickFromGalleryChosen(); 107 break; 108 } 109 110 UiClosables.closeQuietly(listPopupWindow); 111 } 112 }; 113 114 listPopupWindow.setAnchorView(anchorView); 115 listPopupWindow.setAdapter(adapter); 116 listPopupWindow.setOnItemClickListener(clickListener); 117 listPopupWindow.setModal(true); 118 listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); 119 final int minWidth = context.getResources().getDimensionPixelSize( 120 R.dimen.photo_action_popup_min_width); 121 if (anchorView.getWidth() < minWidth) { 122 listPopupWindow.setWidth(minWidth); 123 } 124 return listPopupWindow; 125 } 126 127 private static final class ChoiceListItem { 128 private final int mId; 129 private final String mCaption; 130 131 public static final int ID_TAKE_PHOTO = 1; 132 public static final int ID_PICK_PHOTO = 2; 133 public static final int ID_REMOVE = 3; 134 135 public ChoiceListItem(int id, String caption) { 136 mId = id; 137 mCaption = caption; 138 } 139 140 @Override 141 public String toString() { 142 return mCaption; 143 } 144 145 public int getId() { 146 return mId; 147 } 148 } 149 150 public interface Listener { 151 void onRemovePictureChosen(); 152 void onTakePhotoChosen(); 153 void onPickFromGalleryChosen(); 154 } 155 } 156