Home | History | Annotate | Download | only in editor
      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 use as primary photo. */
     44         public static final int ALLOW_PRIMARY = 1;
     45         /** If set, show choice to remove photo. */
     46         public static final int REMOVE_PHOTO = 2;
     47         /** If set, show choices to take a picture with the camera, or pick one from the gallery. */
     48         public static final int TAKE_OR_PICK_PHOTO = 4;
     49         /**
     50          *  If set, modifies the wording in the choices for TAKE_OR_PICK_PHOTO
     51          *  to emphasize that the existing photo will be replaced.
     52          */
     53         public static final int TAKE_OR_PICK_PHOTO_REPLACE_WORDING = 8;
     54     }
     55 
     56     /**
     57      * Convenient combinations of commonly-used flags (see {@link Flags}).
     58      */
     59     public static final class Modes {
     60         public static final int NO_PHOTO =
     61                 Flags.TAKE_OR_PICK_PHOTO;
     62         public static final int READ_ONLY_ALLOW_PRIMARY =
     63                 Flags.ALLOW_PRIMARY;
     64         public static final int PHOTO_DISALLOW_PRIMARY =
     65                 Flags.REMOVE_PHOTO |
     66                 Flags.TAKE_OR_PICK_PHOTO |
     67                 Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING;
     68         public static final int PHOTO_ALLOW_PRIMARY =
     69                 Flags.ALLOW_PRIMARY |
     70                 Flags.REMOVE_PHOTO |
     71                 Flags.TAKE_OR_PICK_PHOTO |
     72                 Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING;
     73     }
     74 
     75     public static ListPopupWindow createPopupMenu(Context context, View anchorView,
     76             final Listener listener, int mode) {
     77         // Build choices, depending on the current mode. We assume this Dialog is never called
     78         // if there are NO choices (e.g. a read-only picture is already super-primary)
     79         final ArrayList<ChoiceListItem> choices = new ArrayList<ChoiceListItem>(4);
     80         // Use as Primary
     81         if ((mode & Flags.ALLOW_PRIMARY) > 0) {
     82             choices.add(new ChoiceListItem(ChoiceListItem.ID_USE_AS_PRIMARY,
     83                     context.getString(R.string.use_photo_as_primary)));
     84         }
     85         // Remove
     86         if ((mode & Flags.REMOVE_PHOTO) > 0) {
     87             choices.add(new ChoiceListItem(ChoiceListItem.ID_REMOVE,
     88                     context.getString(R.string.removePhoto)));
     89         }
     90         // Take photo or pick one from the gallery.  Wording differs if there is already a photo.
     91         if ((mode & Flags.TAKE_OR_PICK_PHOTO) > 0) {
     92             boolean replace = (mode & Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING) > 0;
     93             final int takePhotoResId = replace ? R.string.take_new_photo : R.string.take_photo;
     94             final String takePhotoString = context.getString(takePhotoResId);
     95             final int pickPhotoResId = replace ? R.string.pick_new_photo : R.string.pick_photo;
     96             final String pickPhotoString = context.getString(pickPhotoResId);
     97             if (PhoneCapabilityTester.isCameraIntentRegistered(context)) {
     98                 choices.add(new ChoiceListItem(ChoiceListItem.ID_TAKE_PHOTO, takePhotoString));
     99             }
    100             choices.add(new ChoiceListItem(ChoiceListItem.ID_PICK_PHOTO, pickPhotoString));
    101         }
    102 
    103         final ListAdapter adapter = new ArrayAdapter<ChoiceListItem>(context,
    104                 R.layout.select_dialog_item, choices);
    105 
    106         final ListPopupWindow listPopupWindow = new ListPopupWindow(context);
    107         final OnItemClickListener clickListener = new OnItemClickListener() {
    108             @Override
    109             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    110                 final ChoiceListItem choice = choices.get(position);
    111                 switch (choice.getId()) {
    112                     case ChoiceListItem.ID_USE_AS_PRIMARY:
    113                         listener.onUseAsPrimaryChosen();
    114                         break;
    115                     case ChoiceListItem.ID_REMOVE:
    116                         listener.onRemovePictureChosen();
    117                         break;
    118                     case ChoiceListItem.ID_TAKE_PHOTO:
    119                         listener.onTakePhotoChosen();
    120                         break;
    121                     case ChoiceListItem.ID_PICK_PHOTO:
    122                         listener.onPickFromGalleryChosen();
    123                         break;
    124                 }
    125 
    126                 UiClosables.closeQuietly(listPopupWindow);
    127             }
    128         };
    129 
    130         listPopupWindow.setAnchorView(anchorView);
    131         listPopupWindow.setAdapter(adapter);
    132         listPopupWindow.setOnItemClickListener(clickListener);
    133         listPopupWindow.setModal(true);
    134         listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
    135         final int minWidth = context.getResources().getDimensionPixelSize(
    136                 R.dimen.photo_action_popup_min_width);
    137         if (anchorView.getWidth() < minWidth) {
    138             listPopupWindow.setWidth(minWidth);
    139         }
    140         return listPopupWindow;
    141     }
    142 
    143     private static final class ChoiceListItem {
    144         private final int mId;
    145         private final String mCaption;
    146 
    147         public static final int ID_USE_AS_PRIMARY = 0;
    148         public static final int ID_TAKE_PHOTO = 1;
    149         public static final int ID_PICK_PHOTO = 2;
    150         public static final int ID_REMOVE = 3;
    151 
    152         public ChoiceListItem(int id, String caption) {
    153             mId = id;
    154             mCaption = caption;
    155         }
    156 
    157         @Override
    158         public String toString() {
    159             return mCaption;
    160         }
    161 
    162         public int getId() {
    163             return mId;
    164         }
    165     }
    166 
    167     public interface Listener {
    168         void onUseAsPrimaryChosen();
    169         void onRemovePictureChosen();
    170         void onTakePhotoChosen();
    171         void onPickFromGalleryChosen();
    172     }
    173 }
    174