package eu.ttbox.androgister.ui.admin.product.photo;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListPopupWindow;
import eu.ttbox.androgister.R;
public class PhotoActionPopup {
public static final String TAG = "PhotoActionPopup";
/**
* Bitmask flags to specify which actions should be presented to the user.
*/
public static final class Flags {
/** If set, show choice to use as primary photo. */
public static final int ALLOW_PRIMARY = 1;
/** If set, show choice to remove photo. */
public static final int REMOVE_PHOTO = 2;
/** If set, show choices to take a picture with the camera, or pick one from the gallery. */
public static final int TAKE_OR_PICK_PHOTO = 4;
/**
* If set, modifies the wording in the choices for TAKE_OR_PICK_PHOTO
* to emphasize that the existing photo will be replaced.
*/
public static final int TAKE_OR_PICK_PHOTO_REPLACE_WORDING = 8;
}
/**
* Convenient combinations of commonly-used flags (see {@link Flags}).
*/
public static final class Modes {
public static final int NO_PHOTO =
Flags.TAKE_OR_PICK_PHOTO;
public static final int READ_ONLY_ALLOW_PRIMARY =
Flags.ALLOW_PRIMARY;
public static final int PHOTO_DISALLOW_PRIMARY =
Flags.REMOVE_PHOTO |
Flags.TAKE_OR_PICK_PHOTO |
Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING;
public static final int PHOTO_ALLOW_PRIMARY =
Flags.ALLOW_PRIMARY |
Flags.REMOVE_PHOTO |
Flags.TAKE_OR_PICK_PHOTO |
Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING;
}
public static ListPopupWindow createPopupMenu(Context context, View anchorView,
final Listener listener, int mode) {
// Build choices, depending on the current mode. We assume this Dialog is never called
// if there are NO choices (e.g. a read-only picture is already super-primary)
Log.d(TAG, "Create Photo Popup Menu ChoiceListItem : Mode " + mode);
final ArrayList<ChoiceListItem> choices = new ArrayList<ChoiceListItem>(4);
// Use as Primary
if ((mode & Flags.ALLOW_PRIMARY) > 0) {
choices.add(new ChoiceListItem(ChoiceListItem.ID_USE_AS_PRIMARY,
context.getString(R.string.use_photo_as_primary)));
Log.d(TAG, "Create Photo Popup Menu ChoiceListItem : ALLOW_PRIMARY");
}
// Remove
if ((mode & Flags.REMOVE_PHOTO) > 0) {
choices.add(new ChoiceListItem(ChoiceListItem.ID_REMOVE,
context.getString(R.string.removePhoto)));
Log.d(TAG, "Create Photo Popup Menu ChoiceListItem : REMOVE_PHOTO");
}
// Take photo or pick one from the gallery. Wording differs if there is already a photo.
if ((mode & Flags.TAKE_OR_PICK_PHOTO) > 0) {
boolean replace = (mode & Flags.TAKE_OR_PICK_PHOTO_REPLACE_WORDING) > 0;
final int takePhotoResId = replace ? R.string.take_new_photo : R.string.take_photo;
final String takePhotoString = context.getString(takePhotoResId);
final int pickPhotoResId = replace ? R.string.pick_new_photo : R.string.pick_photo;
final String pickPhotoString = context.getString(pickPhotoResId);
choices.add(new ChoiceListItem(ChoiceListItem.ID_TAKE_PHOTO, takePhotoString));
choices.add(new ChoiceListItem(ChoiceListItem.ID_PICK_PHOTO, pickPhotoString));
Log.d(TAG, "Create Photo Popup Menu ChoiceListItem : ID_TAKE_PHOTO");
Log.d(TAG, "Create Photo Popup Menu ChoiceListItem : ID_PICK_PHOTO");
}
final ListAdapter adapter = new ArrayAdapter<ChoiceListItem>(context,
R.layout.select_dialog_item, choices);
final ListPopupWindow listPopupWindow = new ListPopupWindow(context);
final OnItemClickListener clickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final ChoiceListItem choice = choices.get(position);
switch (choice.getId()) {
case ChoiceListItem.ID_USE_AS_PRIMARY:
listener.onUseAsPrimaryChosen();
break;
case ChoiceListItem.ID_REMOVE:
listener.onRemovePictureChosen();
break;
case ChoiceListItem.ID_TAKE_PHOTO:
listener.onTakePhotoChosen();
break;
case ChoiceListItem.ID_PICK_PHOTO:
listener.onPickFromGalleryChosen();
break;
}
listPopupWindow.dismiss();
}
};
listPopupWindow.setAnchorView(anchorView);
listPopupWindow.setAdapter(adapter);
listPopupWindow.setOnItemClickListener(clickListener);
listPopupWindow.setModal(true);
listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
final int minWidth = context.getResources().getDimensionPixelSize(
R.dimen.photo_action_popup_min_width);
if (anchorView.getWidth() < minWidth) {
listPopupWindow.setWidth(minWidth);
}
Log.d(TAG, "Return listPopupWindow");
return listPopupWindow;
}
private static final class ChoiceListItem {
private final int mId;
private final String mCaption;
public static final int ID_USE_AS_PRIMARY = 0;
public static final int ID_TAKE_PHOTO = 1;
public static final int ID_PICK_PHOTO = 2;
public static final int ID_REMOVE = 3;
public ChoiceListItem(int id, String caption) {
mId = id;
mCaption = caption;
}
@Override
public String toString() {
return mCaption;
}
public int getId() {
return mId;
}
}
public interface Listener {
void onUseAsPrimaryChosen();
void onRemovePictureChosen();
void onTakePhotoChosen();
void onPickFromGalleryChosen();
}
}