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