1 /* 2 * Copyright (C) 2013 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.gallery3d.filtershow.editors; 18 19 import android.content.Context; 20 import android.util.Log; 21 import android.util.SparseArray; 22 import android.view.MenuItem; 23 import android.view.View; 24 import android.view.View.OnClickListener; 25 import android.widget.Button; 26 import android.widget.FrameLayout; 27 import android.widget.LinearLayout; 28 import android.widget.PopupMenu; 29 30 import com.android.gallery3d.R; 31 import com.android.gallery3d.filtershow.FilterShowActivity; 32 import com.android.gallery3d.filtershow.filters.FilterCropRepresentation; 33 import com.android.gallery3d.filtershow.filters.FilterRepresentation; 34 import com.android.gallery3d.filtershow.imageshow.ImageCrop; 35 import com.android.gallery3d.filtershow.imageshow.MasterImage; 36 37 public class EditorCrop extends Editor implements EditorInfo { 38 public static final String TAG = EditorCrop.class.getSimpleName(); 39 public static final int ID = R.id.editorCrop; 40 41 // Holder for an aspect ratio it's string id 42 protected static final class AspectInfo { 43 int mAspectX; 44 int mAspectY; 45 int mStringId; 46 AspectInfo(int stringID, int x, int y) { 47 mStringId = stringID; 48 mAspectX = x; 49 mAspectY = y; 50 } 51 }; 52 53 // Mapping from menu id to aspect ratio 54 protected static final SparseArray<AspectInfo> sAspects; 55 static { 56 sAspects = new SparseArray<AspectInfo>(); 57 sAspects.put(R.id.crop_menu_1to1, new AspectInfo(R.string.aspect1to1_effect, 1, 1)); 58 sAspects.put(R.id.crop_menu_4to3, new AspectInfo(R.string.aspect4to3_effect, 4, 3)); 59 sAspects.put(R.id.crop_menu_3to4, new AspectInfo(R.string.aspect3to4_effect, 3, 4)); 60 sAspects.put(R.id.crop_menu_5to7, new AspectInfo(R.string.aspect5to7_effect, 5, 7)); 61 sAspects.put(R.id.crop_menu_7to5, new AspectInfo(R.string.aspect7to5_effect, 7, 5)); 62 sAspects.put(R.id.crop_menu_none, new AspectInfo(R.string.aspectNone_effect, 0, 0)); 63 sAspects.put(R.id.crop_menu_original, new AspectInfo(R.string.aspectOriginal_effect, 0, 0)); 64 } 65 66 protected ImageCrop mImageCrop; 67 private String mAspectString = ""; 68 69 public EditorCrop() { 70 super(ID); 71 mChangesGeometry = true; 72 } 73 74 @Override 75 public void createEditor(Context context, FrameLayout frameLayout) { 76 super.createEditor(context, frameLayout); 77 if (mImageCrop == null) { 78 mImageCrop = new ImageCrop(context); 79 } 80 mView = mImageShow = mImageCrop; 81 mImageCrop.setEditor(this); 82 } 83 84 @Override 85 public void reflectCurrentFilter() { 86 MasterImage master = MasterImage.getImage(); 87 master.setCurrentFilterRepresentation(master.getPreset() 88 .getFilterWithSerializationName(FilterCropRepresentation.SERIALIZATION_NAME)); 89 super.reflectCurrentFilter(); 90 FilterRepresentation rep = getLocalRepresentation(); 91 if (rep == null || rep instanceof FilterCropRepresentation) { 92 mImageCrop.setFilterCropRepresentation((FilterCropRepresentation) rep); 93 } else { 94 Log.w(TAG, "Could not reflect current filter, not of type: " 95 + FilterCropRepresentation.class.getSimpleName()); 96 } 97 mImageCrop.invalidate(); 98 } 99 100 @Override 101 public void finalApplyCalled() { 102 commitLocalRepresentation(mImageCrop.getFinalRepresentation()); 103 } 104 105 @Override 106 public void openUtilityPanel(final LinearLayout accessoryViewList) { 107 Button view = (Button) accessoryViewList.findViewById(R.id.applyEffect); 108 view.setText(mContext.getString(R.string.crop)); 109 view.setOnClickListener(new OnClickListener() { 110 @Override 111 public void onClick(View arg0) { 112 showPopupMenu(accessoryViewList); 113 } 114 }); 115 } 116 117 private void changeCropAspect(int itemId) { 118 AspectInfo info = sAspects.get(itemId); 119 if (info == null) { 120 throw new IllegalArgumentException("Invalid resource ID: " + itemId); 121 } 122 if (itemId == R.id.crop_menu_original) { 123 mImageCrop.applyOriginalAspect(); 124 } else if (itemId == R.id.crop_menu_none) { 125 mImageCrop.applyFreeAspect(); 126 } else { 127 mImageCrop.applyAspect(info.mAspectX, info.mAspectY); 128 } 129 setAspectString(mContext.getString(info.mStringId)); 130 } 131 132 private void showPopupMenu(LinearLayout accessoryViewList) { 133 final Button button = (Button) accessoryViewList.findViewById(R.id.applyEffect); 134 final PopupMenu popupMenu = new PopupMenu(mImageShow.getActivity(), button); 135 popupMenu.getMenuInflater().inflate(R.menu.filtershow_menu_crop, popupMenu.getMenu()); 136 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 137 @Override 138 public boolean onMenuItemClick(MenuItem item) { 139 changeCropAspect(item.getItemId()); 140 return true; 141 } 142 }); 143 popupMenu.show(); 144 ((FilterShowActivity)mContext).onShowMenu(popupMenu); 145 } 146 147 @Override 148 public void setUtilityPanelUI(View actionButton, View editControl) { 149 super.setUtilityPanelUI(actionButton,editControl); 150 setMenuIcon(true); 151 } 152 153 @Override 154 public boolean showsSeekBar() { 155 return false; 156 } 157 158 @Override 159 public int getTextId() { 160 return R.string.crop; 161 } 162 163 @Override 164 public int getOverlayId() { 165 return R.drawable.filtershow_button_geometry_crop; 166 } 167 168 @Override 169 public boolean getOverlayOnly() { 170 return true; 171 } 172 173 private void setAspectString(String s) { 174 mAspectString = s; 175 } 176 } 177