EffectSettingPopup.java revision 15402eff4f819bd0984ca8f2f6ea76ea0b92b326
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 com.android.camera.EffectsRecorder;
20import com.android.camera.IconListPreference;
21import com.android.camera.R;
22
23import android.content.Context;
24import android.util.AttributeSet;
25import android.util.Log;
26import android.view.View;
27import android.widget.AdapterView;
28import android.widget.GridView;
29import android.widget.SimpleAdapter;
30
31import java.util.ArrayList;
32import 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.
37public 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 {
57        public void onSettingChanged();
58    }
59
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
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
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
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
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
176    public void setSettingChangedListener(Listener listener) {
177        mListener = listener;
178    }
179
180    @Override
181    public void onItemClick(AdapterView<?> parent, View view,
182            int index, long id) {
183        String value;
184        if (parent == mSillyFacesGrid) {
185            value = (String) mSillyFacesItem.get(index).get("value");
186        } else if (parent == mBackgroundGrid) {
187            value = (String) mBackgroundItem.get(index).get("value");
188        } else {
189            return;
190        }
191
192        // Tapping the selected effect will deselect it (clear effects).
193        if (value.equals(mPreference.getValue())) {
194            mPreference.setValue(mNoEffect);
195        } else {
196            mPreference.setValue(value);
197        }
198        reloadPreference();
199        if (mListener != null) mListener.onSettingChanged();
200    }
201
202    @Override
203    public void onClick(View v) {
204        // Clear the effect.
205        mPreference.setValue(mNoEffect);
206        reloadPreference();
207        if (mListener != null) mListener.onSettingChanged();
208    }
209}
210