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             findViewById(R.id.effect_silly_faces_title_separator).setVisibility(View.VISIBLE);
108             mSillyFacesGrid.setVisibility(View.VISIBLE);
109             SimpleAdapter sillyFacesItemAdapter = new SimpleAdapter(context,
110                     mSillyFacesItem, R.layout.effect_setting_item,
111                     new String[] {"text", "image"},
112                     new int[] {R.id.text, R.id.image});
113             mSillyFacesGrid.setAdapter(sillyFacesItemAdapter);
114             mSillyFacesGrid.setOnItemClickListener(this);
115         }
116 
117         if (hasSillyFaces && hasBackground) {
118             findViewById(R.id.effect_background_separator).setVisibility(View.VISIBLE);
119         }
120 
121         // Initialize background replacer if it is supported.
122         if (hasBackground) {
123             findViewById(R.id.effect_background_title).setVisibility(View.VISIBLE);
124             findViewById(R.id.effect_background_title_separator).setVisibility(View.VISIBLE);
125             mBackgroundGrid.setVisibility(View.VISIBLE);
126             SimpleAdapter backgroundItemAdapter = new SimpleAdapter(context,
127                     mBackgroundItem, R.layout.effect_setting_item,
128                     new String[] {"text", "image"},
129                     new int[] {R.id.text, R.id.image});
130             mBackgroundGrid.setAdapter(backgroundItemAdapter);
131             mBackgroundGrid.setOnItemClickListener(this);
132         }
133 
134         reloadPreference();
135     }
136 
137     @Override
setVisibility(int visibility)138     public void setVisibility(int visibility) {
139         if (visibility == View.VISIBLE) {
140             if (getVisibility() != View.VISIBLE) {
141                 // Do not show or hide "Clear effects" button when the popup
142                 // is already visible. Otherwise it looks strange.
143                 boolean noEffect = mPreference.getValue().equals(mNoEffect);
144                 mClearEffects.setVisibility(noEffect ? View.GONE : View.VISIBLE);
145             }
146             reloadPreference();
147         }
148         super.setVisibility(visibility);
149     }
150 
151     // The value of the preference may have changed. Update the UI.
152     @Override
reloadPreference()153     public void reloadPreference() {
154         mBackgroundGrid.setItemChecked(mBackgroundGrid.getCheckedItemPosition(), false);
155         mSillyFacesGrid.setItemChecked(mSillyFacesGrid.getCheckedItemPosition(), false);
156 
157         String value = mPreference.getValue();
158         if (value.equals(mNoEffect)) return;
159 
160         for (int i = 0; i < mSillyFacesItem.size(); i++) {
161             if (value.equals(mSillyFacesItem.get(i).get("value"))) {
162                 mSillyFacesGrid.setItemChecked(i, true);
163                 return;
164             }
165         }
166 
167         for (int i = 0; i < mBackgroundItem.size(); i++) {
168             if (value.equals(mBackgroundItem.get(i).get("value"))) {
169                 mBackgroundGrid.setItemChecked(i, true);
170                 return;
171             }
172         }
173 
174         Log.e(TAG, "Invalid preference value: " + value);
175         mPreference.print();
176     }
177 
setSettingChangedListener(Listener listener)178     public void setSettingChangedListener(Listener listener) {
179         mListener = listener;
180     }
181 
182     @Override
onItemClick(AdapterView<?> parent, View view, int index, long id)183     public void onItemClick(AdapterView<?> parent, View view,
184             int index, long id) {
185         String value;
186         if (parent == mSillyFacesGrid) {
187             value = (String) mSillyFacesItem.get(index).get("value");
188         } else if (parent == mBackgroundGrid) {
189             value = (String) mBackgroundItem.get(index).get("value");
190         } else {
191             return;
192         }
193 
194         // Tapping the selected effect will deselect it (clear effects).
195         if (value.equals(mPreference.getValue())) {
196             mPreference.setValue(mNoEffect);
197         } else {
198             mPreference.setValue(value);
199         }
200         reloadPreference();
201         if (mListener != null) mListener.onSettingChanged();
202     }
203 
204     @Override
onClick(View v)205     public void onClick(View v) {
206         // Clear the effect.
207         mPreference.setValue(mNoEffect);
208         reloadPreference();
209         if (mListener != null) mListener.onSettingChanged();
210     }
211 }
212