EffectSettingPopup.java revision 892781804513066b68b4a2fa6f846e862dac252a
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 17package com.android.camera.ui; 18 19import android.content.Context; 20import android.util.AttributeSet; 21import android.util.Log; 22import android.view.View; 23import android.widget.AdapterView; 24import android.widget.GridView; 25import android.widget.SimpleAdapter; 26 27import com.android.camera.IconListPreference; 28import com.android.camera.R; 29 30import java.util.ArrayList; 31import java.util.HashMap; 32 33// A popup window that shows video effect setting. It has two grid view. 34// One shows the goofy face effects. The other shows the background replacer 35// effects. 36public class EffectSettingPopup extends AbstractSettingPopup implements 37 AdapterView.OnItemClickListener, View.OnClickListener { 38 private static final String TAG = "EffectSettingPopup"; 39 private String mNoEffect; 40 private IconListPreference mPreference; 41 private Listener mListener; 42 private View mClearEffects; 43 private GridView mSillyFacesGrid; 44 private GridView mBackgroundGrid; 45 46 // Data for silly face items. (text, image, and preference value) 47 ArrayList<HashMap<String, Object>> mSillyFacesItem = 48 new ArrayList<HashMap<String, Object>>(); 49 50 // Data for background replacer items. (text, image, and preference value) 51 ArrayList<HashMap<String, Object>> mBackgroundItem = 52 new ArrayList<HashMap<String, Object>>(); 53 54 55 static public interface Listener { 56 public void onSettingChanged(); 57 } 58 59 public EffectSettingPopup(Context context, AttributeSet attrs) { 60 super(context, attrs); 61 mNoEffect = context.getString(R.string.pref_video_effect_default); 62 } 63 64 @Override 65 protected void onFinishInflate() { 66 super.onFinishInflate(); 67 mClearEffects = findViewById(R.id.clear_effects); 68 mClearEffects.setOnClickListener(this); 69 mSillyFacesGrid = (GridView) findViewById(R.id.effect_silly_faces); 70 mBackgroundGrid = (GridView) findViewById(R.id.effect_background); 71 } 72 73 public void initialize(IconListPreference preference) { 74 mPreference = preference; 75 Context context = getContext(); 76 CharSequence[] entries = mPreference.getEntries(); 77 CharSequence[] entryValues = mPreference.getEntryValues(); 78 int[] iconIds = mPreference.getImageIds(); 79 if (iconIds == null) { 80 iconIds = mPreference.getLargeIconIds(); 81 } 82 83 // Set title. 84 mTitle.setText(mPreference.getTitle()); 85 86 for(int i = 0; i < entries.length; ++i) { 87 String value = entryValues[i].toString(); 88 if (value.equals(mNoEffect)) continue; // no effect, skip it. 89 HashMap<String, Object> map = new HashMap<String, Object>(); 90 map.put("value", value); 91 map.put("text", entries[i].toString()); 92 if (iconIds != null) map.put("image", iconIds[i]); 93 if (value.startsWith("goofy_face")) { 94 mSillyFacesItem.add(map); 95 } else if (value.startsWith("backdropper")) { 96 mBackgroundItem.add(map); 97 } 98 } 99 100 boolean hasSillyFaces = mSillyFacesItem.size() > 0; 101 boolean hasBackground = mBackgroundItem.size() > 0; 102 103 // Initialize goofy face if it is supported. 104 if (hasSillyFaces) { 105 findViewById(R.id.effect_silly_faces_title).setVisibility(View.VISIBLE); 106 findViewById(R.id.effect_silly_faces_title_separator).setVisibility(View.VISIBLE); 107 mSillyFacesGrid.setVisibility(View.VISIBLE); 108 SimpleAdapter sillyFacesItemAdapter = new SimpleAdapter(context, 109 mSillyFacesItem, R.layout.effect_setting_item, 110 new String[] {"text", "image"}, 111 new int[] {R.id.text, R.id.image}); 112 mSillyFacesGrid.setAdapter(sillyFacesItemAdapter); 113 mSillyFacesGrid.setOnItemClickListener(this); 114 } 115 116 if (hasSillyFaces && hasBackground) { 117 findViewById(R.id.effect_background_separator).setVisibility(View.VISIBLE); 118 } 119 120 // Initialize background replacer if it is supported. 121 if (hasBackground) { 122 findViewById(R.id.effect_background_title).setVisibility(View.VISIBLE); 123 findViewById(R.id.effect_background_title_separator).setVisibility(View.VISIBLE); 124 mBackgroundGrid.setVisibility(View.VISIBLE); 125 SimpleAdapter backgroundItemAdapter = new SimpleAdapter(context, 126 mBackgroundItem, R.layout.effect_setting_item, 127 new String[] {"text", "image"}, 128 new int[] {R.id.text, R.id.image}); 129 mBackgroundGrid.setAdapter(backgroundItemAdapter); 130 mBackgroundGrid.setOnItemClickListener(this); 131 } 132 133 reloadPreference(); 134 } 135 136 @Override 137 public void setVisibility(int visibility) { 138 if (visibility == View.VISIBLE) { 139 if (getVisibility() != View.VISIBLE) { 140 // Do not show or hide "Clear effects" button when the popup 141 // is already visible. Otherwise it looks strange. 142 boolean noEffect = mPreference.getValue().equals(mNoEffect); 143 mClearEffects.setVisibility(noEffect ? View.GONE : View.VISIBLE); 144 } 145 reloadPreference(); 146 } 147 super.setVisibility(visibility); 148 } 149 150 // The value of the preference may have changed. Update the UI. 151 @Override 152 public void reloadPreference() { 153 mBackgroundGrid.setItemChecked(mBackgroundGrid.getCheckedItemPosition(), false); 154 mSillyFacesGrid.setItemChecked(mSillyFacesGrid.getCheckedItemPosition(), false); 155 156 String value = mPreference.getValue(); 157 if (value.equals(mNoEffect)) return; 158 159 for (int i = 0; i < mSillyFacesItem.size(); i++) { 160 if (value.equals(mSillyFacesItem.get(i).get("value"))) { 161 mSillyFacesGrid.setItemChecked(i, true); 162 return; 163 } 164 } 165 166 for (int i = 0; i < mBackgroundItem.size(); i++) { 167 if (value.equals(mBackgroundItem.get(i).get("value"))) { 168 mBackgroundGrid.setItemChecked(i, true); 169 return; 170 } 171 } 172 173 Log.e(TAG, "Invalid preference value: " + value); 174 mPreference.print(); 175 } 176 177 public void setSettingChangedListener(Listener listener) { 178 mListener = listener; 179 } 180 181 @Override 182 public void onItemClick(AdapterView<?> parent, View view, 183 int index, long id) { 184 String value; 185 if (parent == mSillyFacesGrid) { 186 value = (String) mSillyFacesItem.get(index).get("value"); 187 } else if (parent == mBackgroundGrid) { 188 value = (String) mBackgroundItem.get(index).get("value"); 189 } else { 190 return; 191 } 192 193 // Tapping the selected effect will deselect it (clear effects). 194 if (value.equals(mPreference.getValue())) { 195 mPreference.setValue(mNoEffect); 196 } else { 197 mPreference.setValue(value); 198 } 199 reloadPreference(); 200 if (mListener != null) mListener.onSettingChanged(); 201 } 202 203 @Override 204 public void onClick(View v) { 205 // Clear the effect. 206 mPreference.setValue(mNoEffect); 207 reloadPreference(); 208 if (mListener != null) mListener.onSettingChanged(); 209 } 210} 211