1/*
2 * Copyright (C) 2012 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;
18
19import android.content.Context;
20import android.view.LayoutInflater;
21
22import com.android.camera.ui.AbstractSettingPopup;
23import com.android.camera.ui.ListPrefSettingPopup;
24import com.android.camera.ui.MoreSettingPopup;
25import com.android.camera.ui.PieItem;
26import com.android.camera.ui.PieItem.OnClickListener;
27import com.android.camera.ui.PieRenderer;
28import com.android.camera.ui.TimeIntervalPopup;
29import com.android.gallery3d.R;
30
31public class VideoMenu extends PieController
32        implements MoreSettingPopup.Listener,
33        ListPrefSettingPopup.Listener,
34        TimeIntervalPopup.Listener {
35
36    private static String TAG = "CAM_VideoMenu";
37
38    private VideoUI mUI;
39    private String[] mOtherKeys;
40    private AbstractSettingPopup mPopup;
41
42    private static final int POPUP_NONE = 0;
43    private static final int POPUP_FIRST_LEVEL = 1;
44    private static final int POPUP_SECOND_LEVEL = 2;
45    private int mPopupStatus;
46    private CameraActivity mActivity;
47
48    public VideoMenu(CameraActivity activity, VideoUI ui, PieRenderer pie) {
49        super(activity, pie);
50        mUI = ui;
51        mActivity = activity;
52    }
53
54    public void initialize(PreferenceGroup group) {
55        super.initialize(group);
56        mPopup = null;
57        mPopupStatus = POPUP_NONE;
58        PieItem item = null;
59        // white balance
60        if (group.findPreference(CameraSettings.KEY_WHITE_BALANCE) != null) {
61            item = makeItem(CameraSettings.KEY_WHITE_BALANCE);
62            mRenderer.addItem(item);
63        }
64        // settings popup
65        mOtherKeys = new String[] {
66                CameraSettings.KEY_VIDEO_EFFECT,
67                CameraSettings.KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL,
68                CameraSettings.KEY_VIDEO_QUALITY,
69                CameraSettings.KEY_RECORD_LOCATION
70        };
71        item = makeItem(R.drawable.ic_settings_holo_light);
72        item.setLabel(mActivity.getResources().getString(R.string.camera_menu_settings_label));
73        item.setOnClickListener(new OnClickListener() {
74            @Override
75            public void onClick(PieItem item) {
76                if (mPopup == null || mPopupStatus != POPUP_FIRST_LEVEL) {
77                    initializePopup();
78                    mPopupStatus = POPUP_FIRST_LEVEL;
79                }
80                mUI.showPopup(mPopup);
81            }
82        });
83        mRenderer.addItem(item);
84        // camera switcher
85        if (group.findPreference(CameraSettings.KEY_CAMERA_ID) != null) {
86            item = makeItem(R.drawable.ic_switch_back);
87            IconListPreference lpref = (IconListPreference) group.findPreference(
88                    CameraSettings.KEY_CAMERA_ID);
89            item.setLabel(lpref.getLabel());
90            item.setImageResource(mActivity,
91                    ((IconListPreference) lpref).getIconIds()
92                    [lpref.findIndexOfValue(lpref.getValue())]);
93
94            final PieItem fitem = item;
95            item.setOnClickListener(new OnClickListener() {
96
97                @Override
98                public void onClick(PieItem item) {
99                    // Find the index of next camera.
100                    ListPreference pref =
101                            mPreferenceGroup.findPreference(CameraSettings.KEY_CAMERA_ID);
102                    if (pref != null) {
103                        int index = pref.findIndexOfValue(pref.getValue());
104                        CharSequence[] values = pref.getEntryValues();
105                        index = (index + 1) % values.length;
106                        int newCameraId = Integer.parseInt((String) values[index]);
107                        fitem.setImageResource(mActivity,
108                                ((IconListPreference) pref).getIconIds()[index]);
109                        fitem.setLabel(pref.getLabel());
110                        mListener.onCameraPickerClicked(newCameraId);
111                    }
112                }
113            });
114            mRenderer.addItem(item);
115        }
116        // flash
117        if (group.findPreference(CameraSettings.KEY_VIDEOCAMERA_FLASH_MODE) != null) {
118            item = makeItem(CameraSettings.KEY_VIDEOCAMERA_FLASH_MODE);
119            mRenderer.addItem(item);
120        }
121    }
122
123    @Override
124    public void reloadPreferences() {
125        super.reloadPreferences();
126        if (mPopup != null) {
127            mPopup.reloadPreference();
128        }
129    }
130
131    @Override
132    public void overrideSettings(final String ... keyvalues) {
133        super.overrideSettings(keyvalues);
134        if (mPopup == null || mPopupStatus != POPUP_FIRST_LEVEL) {
135            mPopupStatus = POPUP_FIRST_LEVEL;
136            initializePopup();
137        }
138        ((MoreSettingPopup) mPopup).overrideSettings(keyvalues);
139    }
140
141    @Override
142    // Hit when an item in the second-level popup gets selected
143    public void onListPrefChanged(ListPreference pref) {
144        if (mPopup != null) {
145            if (mPopupStatus == POPUP_SECOND_LEVEL) {
146                mUI.dismissPopup(true);
147            }
148        }
149        super.onSettingChanged(pref);
150    }
151
152    protected void initializePopup() {
153        LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
154                Context.LAYOUT_INFLATER_SERVICE);
155
156        MoreSettingPopup popup = (MoreSettingPopup) inflater.inflate(
157                R.layout.more_setting_popup, null, false);
158        popup.setSettingChangedListener(this);
159        popup.initialize(mPreferenceGroup, mOtherKeys);
160        if (mActivity.isSecureCamera()) {
161            // Prevent location preference from getting changed in secure camera mode
162            popup.setPreferenceEnabled(CameraSettings.KEY_RECORD_LOCATION, false);
163        }
164        mPopup = popup;
165    }
166
167    public void popupDismissed(boolean topPopupOnly) {
168        // if the 2nd level popup gets dismissed
169        if (mPopupStatus == POPUP_SECOND_LEVEL) {
170            initializePopup();
171            mPopupStatus = POPUP_FIRST_LEVEL;
172            if (topPopupOnly) mUI.showPopup(mPopup);
173        }
174    }
175
176    @Override
177    // Hit when an item in the first-level popup gets selected, then bring up
178    // the second-level popup
179    public void onPreferenceClicked(ListPreference pref) {
180        if (mPopupStatus != POPUP_FIRST_LEVEL) return;
181
182        LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
183                Context.LAYOUT_INFLATER_SERVICE);
184
185        if (CameraSettings.KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL.equals(pref.getKey())) {
186            TimeIntervalPopup timeInterval = (TimeIntervalPopup) inflater.inflate(
187                    R.layout.time_interval_popup, null, false);
188            timeInterval.initialize((IconListPreference) pref);
189            timeInterval.setSettingChangedListener(this);
190            mUI.dismissPopup(true);
191            mPopup = timeInterval;
192        } else {
193            ListPrefSettingPopup basic = (ListPrefSettingPopup) inflater.inflate(
194                    R.layout.list_pref_setting_popup, null, false);
195            basic.initialize(pref);
196            basic.setSettingChangedListener(this);
197            mUI.dismissPopup(true);
198            mPopup = basic;
199        }
200        mUI.showPopup(mPopup);
201        mPopupStatus = POPUP_SECOND_LEVEL;
202    }
203
204}
205