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