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