OSDN Git Service

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