OSDN Git Service

Work in progress - Put preview in TextureView
[android-x86/packages-apps-Camera2.git] / src / com / android / camera / PhotoMenu.java
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
17 package com.android.camera;
18
19 import android.content.Context;
20 import android.hardware.Camera.Parameters;
21 import android.view.LayoutInflater;
22
23 import com.android.camera.ui.AbstractSettingPopup;
24 import com.android.camera.ui.ListPrefSettingPopup;
25 import com.android.camera.ui.MoreSettingPopup;
26 import com.android.camera.ui.PieItem;
27 import com.android.camera.ui.PieItem.OnClickListener;
28 import com.android.camera.ui.PieRenderer;
29 import com.android.camera.ui.TimerSettingPopup;
30 import com.android.gallery3d.R;
31
32 public class PhotoMenu extends PieController
33         implements MoreSettingPopup.Listener,
34         TimerSettingPopup.Listener,
35         ListPrefSettingPopup.Listener {
36     private static String TAG = "CAM_photomenu";
37
38     private static final int POS_HDR = 0;
39     private static final int POS_EXP = 1;
40     private static final int POS_MORE = 2;
41     private static final int POS_FLASH = 3;
42     private static final int POS_SWITCH = 4;
43     private static final int POS_WB = 1;
44     private static final int POS_SET = 2;
45
46     private final String mSettingOff;
47
48     private PhotoUI mUI;
49     private String[] mOtherKeys;
50     // First level popup
51     private MoreSettingPopup mPopup;
52     // Second level popup
53     private AbstractSettingPopup mSecondPopup;
54     private CameraActivity mActivity;
55
56     public PhotoMenu(CameraActivity activity, PhotoUI ui, PieRenderer pie) {
57         super(activity, pie);
58         mUI = ui;
59         mSettingOff = activity.getString(R.string.setting_off_value);
60         mActivity = activity;
61     }
62
63     public void initialize(PreferenceGroup group) {
64         super.initialize(group);
65         mPopup = null;
66         mSecondPopup = null;
67         PieItem item = null;
68         // flash
69         if (group.findPreference(CameraSettings.KEY_FLASH_MODE) != null) {
70             item = makeItem(CameraSettings.KEY_FLASH_MODE, POS_FLASH, 5);
71             mRenderer.addItem(item);
72         }
73         // exposure compensation
74         if (group.findPreference(CameraSettings.KEY_EXPOSURE) != null) {
75             item = makeItem(CameraSettings.KEY_EXPOSURE, POS_EXP, 5);
76             mRenderer.addItem(item);
77         }
78         // camera switcher
79         if (group.findPreference(CameraSettings.KEY_CAMERA_ID) != null) {
80             item = makeItem(R.drawable.ic_switch_photo_facing_holo_light);
81             item.setPosition(POS_SWITCH, 5);
82             item.setOnClickListener(new OnClickListener() {
83                 @Override
84                 public void onClick(PieItem item) {
85                     // Find the index of next camera.
86                     ListPreference camPref = mPreferenceGroup
87                             .findPreference(CameraSettings.KEY_CAMERA_ID);
88                     if (camPref != null) {
89                         int index = camPref.findIndexOfValue(camPref.getValue());
90                         CharSequence[] values = camPref.getEntryValues();
91                         index = (index + 1) % values.length;
92                         int newCameraId = Integer
93                                 .parseInt((String) values[index]);
94                         mListener.onCameraPickerClicked(newCameraId);
95                     }
96                 }
97             });
98             mRenderer.addItem(item);
99         }
100         // hdr
101         if (group.findPreference(CameraSettings.KEY_CAMERA_HDR) != null) {
102             item = makeItem(R.drawable.ic_hdr);
103             item.setPosition(POS_HDR, 5);
104             item.setOnClickListener(new OnClickListener() {
105                 @Override
106                 public void onClick(PieItem item) {
107                     // Find the index of next camera.
108                     ListPreference pref = mPreferenceGroup
109                             .findPreference(CameraSettings.KEY_CAMERA_HDR);
110                     if (pref != null) {
111                         // toggle hdr value
112                         int index = (pref.findIndexOfValue(pref.getValue()) + 1) % 2;
113                         pref.setValueIndex(index);
114                         onSettingChanged(pref);
115                     }
116                 }
117             });
118             mRenderer.addItem(item);
119         }
120
121         // more settings
122         PieItem more = makeItem(R.drawable.ic_settings_holo_light);
123         more.setPosition(POS_MORE, 5);
124         mRenderer.addItem(more);
125         // white balance
126         if (group.findPreference(CameraSettings.KEY_WHITE_BALANCE) != null) {
127             item = makeItem(CameraSettings.KEY_WHITE_BALANCE, POS_WB, 5);
128             more.addItem(item);
129         }
130         // settings popup
131         mOtherKeys = new String[] {
132                 CameraSettings.KEY_SCENE_MODE,
133                 CameraSettings.KEY_RECORD_LOCATION,
134                 CameraSettings.KEY_PICTURE_SIZE,
135                 CameraSettings.KEY_FOCUS_MODE,
136                 CameraSettings.KEY_TIMER,
137                 CameraSettings.KEY_TIMER_SOUND_EFFECTS,
138                 };
139         item = makeItem(R.drawable.ic_settings_holo_light);
140         item.setPosition(POS_SET, 5);
141         item.setOnClickListener(new OnClickListener() {
142             @Override
143             public void onClick(PieItem item) {
144                 if (mPopup == null) {
145                     initializePopup();
146                 }
147                 mUI.showPopup(mPopup);
148             }
149         });
150         more.addItem(item);
151     }
152
153     @Override
154     public void reloadPreferences() {
155         super.reloadPreferences();
156         if (mPopup != null) {
157             mPopup.reloadPreference();
158         }
159     }
160
161     @Override
162     // Hit when an item in the second-level popup gets selected
163     public void onListPrefChanged(ListPreference pref) {
164         if (mPopup != null && mSecondPopup != null) {
165                 mUI.dismissPopup(true);
166                 mPopup.reloadPreference();
167         }
168         onSettingChanged(pref);
169     }
170
171     @Override
172     public void overrideSettings(final String ... keyvalues) {
173         super.overrideSettings(keyvalues);
174         if (mPopup == null) initializePopup();
175         mPopup.overrideSettings(keyvalues);
176     }
177
178     protected void initializePopup() {
179         LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
180                 Context.LAYOUT_INFLATER_SERVICE);
181
182         MoreSettingPopup popup = (MoreSettingPopup) inflater.inflate(
183                 R.layout.more_setting_popup, null, false);
184         popup.setSettingChangedListener(this);
185         popup.initialize(mPreferenceGroup, mOtherKeys);
186         if (mActivity.isSecureCamera()) {
187             // Prevent location preference from getting changed in secure camera mode
188             popup.setPreferenceEnabled(CameraSettings.KEY_RECORD_LOCATION, false);
189         }
190         mPopup = popup;
191     }
192
193     public void popupDismissed(boolean topPopupOnly) {
194         // if the 2nd level popup gets dismissed
195         if (mSecondPopup != null) {
196             mSecondPopup = null;
197             if (topPopupOnly) mUI.showPopup(mPopup);
198         }
199     }
200
201     // Return true if the preference has the specified key but not the value.
202     private static boolean notSame(ListPreference pref, String key, String value) {
203         return (key.equals(pref.getKey()) && !value.equals(pref.getValue()));
204     }
205
206     private void setPreference(String key, String value) {
207         ListPreference pref = mPreferenceGroup.findPreference(key);
208         if (pref != null && !value.equals(pref.getValue())) {
209             pref.setValue(value);
210             reloadPreferences();
211         }
212     }
213
214     @Override
215     public void onSettingChanged(ListPreference pref) {
216         // Reset the scene mode if HDR is set to on. Reset HDR if scene mode is
217         // set to non-auto.
218         if (notSame(pref, CameraSettings.KEY_CAMERA_HDR, mSettingOff)) {
219             setPreference(CameraSettings.KEY_SCENE_MODE, Parameters.SCENE_MODE_AUTO);
220         } else if (notSame(pref, CameraSettings.KEY_SCENE_MODE, Parameters.SCENE_MODE_AUTO)) {
221             setPreference(CameraSettings.KEY_CAMERA_HDR, mSettingOff);
222         }
223         super.onSettingChanged(pref);
224     }
225
226     @Override
227     // Hit when an item in the first-level popup gets selected, then bring up
228     // the second-level popup
229     public void onPreferenceClicked(ListPreference pref) {
230         if (mSecondPopup != null) return;
231
232         LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
233                 Context.LAYOUT_INFLATER_SERVICE);
234         if (CameraSettings.KEY_TIMER.equals(pref.getKey())) {
235             TimerSettingPopup timerPopup = (TimerSettingPopup) inflater.inflate(
236                     R.layout.timer_setting_popup, null, false);
237             timerPopup.initialize(pref);
238             timerPopup.setSettingChangedListener(this);
239             mUI.dismissPopup(true);
240             mSecondPopup = timerPopup;
241         } else {
242             ListPrefSettingPopup basic = (ListPrefSettingPopup) inflater.inflate(
243                     R.layout.list_pref_setting_popup, null, false);
244             basic.initialize(pref);
245             basic.setSettingChangedListener(this);
246             mUI.dismissPopup(true);
247             mSecondPopup = basic;
248         }
249         mUI.showPopup(mSecondPopup);
250     }
251 }