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.hardware.Camera.Parameters;
21import android.view.LayoutInflater;
22
23import com.android.camera.ui.AbstractSettingPopup;
24import com.android.camera.ui.ListPrefSettingPopup;
25import com.android.camera.ui.MoreSettingPopup;
26import com.android.camera.ui.PieItem;
27import com.android.camera.ui.PieItem.OnClickListener;
28import com.android.camera.ui.PieRenderer;
29import com.android.camera.ui.TimerSettingPopup;
30
31public class PhotoController extends PieController
32        implements MoreSettingPopup.Listener,
33        TimerSettingPopup.Listener,
34        ListPrefSettingPopup.Listener {
35    private static String TAG = "CAM_photocontrol";
36    private static float FLOAT_PI_DIVIDED_BY_TWO = (float) Math.PI / 2;
37    private final String mSettingOff;
38
39    private PhotoModule mModule;
40    private String[] mOtherKeys;
41    // First level popup
42    private MoreSettingPopup mPopup;
43    // Second level popup
44    private AbstractSettingPopup mSecondPopup;
45
46    public PhotoController(CameraActivity activity, PhotoModule module, PieRenderer pie) {
47        super(activity, pie);
48        mModule = module;
49        mSettingOff = activity.getString(R.string.setting_off_value);
50    }
51
52    public void initialize(PreferenceGroup group) {
53        super.initialize(group);
54        mPopup = null;
55        mSecondPopup = null;
56        float sweep = FLOAT_PI_DIVIDED_BY_TWO / 2;
57        addItem(CameraSettings.KEY_FLASH_MODE, FLOAT_PI_DIVIDED_BY_TWO - sweep, sweep);
58        addItem(CameraSettings.KEY_EXPOSURE, 3 * FLOAT_PI_DIVIDED_BY_TWO - sweep, sweep);
59        addItem(CameraSettings.KEY_WHITE_BALANCE, 3 * FLOAT_PI_DIVIDED_BY_TWO + sweep, sweep);
60        if (group.findPreference(CameraSettings.KEY_CAMERA_ID) != null) {
61            PieItem item = makeItem(R.drawable.ic_switch_photo_facing_holo_light);
62            item.setFixedSlice(FLOAT_PI_DIVIDED_BY_TWO + sweep, sweep);
63            item.setOnClickListener(new OnClickListener() {
64                @Override
65                public void onClick(PieItem item) {
66                    // Find the index of next camera.
67                    ListPreference camPref = mPreferenceGroup
68                            .findPreference(CameraSettings.KEY_CAMERA_ID);
69                    if (camPref != null) {
70                        int index = camPref.findIndexOfValue(camPref.getValue());
71                        CharSequence[] values = camPref.getEntryValues();
72                        index = (index + 1) % values.length;
73                        int newCameraId = Integer
74                                .parseInt((String) values[index]);
75                        mListener.onCameraPickerClicked(newCameraId);
76                    }
77                }
78            });
79            mRenderer.addItem(item);
80        }
81        if (group.findPreference(CameraSettings.KEY_CAMERA_HDR) != null) {
82            PieItem hdr = makeItem(R.drawable.ic_hdr);
83            hdr.setFixedSlice(FLOAT_PI_DIVIDED_BY_TWO, sweep);
84            hdr.setOnClickListener(new OnClickListener() {
85                @Override
86                public void onClick(PieItem item) {
87                    // Find the index of next camera.
88                    ListPreference pref = mPreferenceGroup
89                            .findPreference(CameraSettings.KEY_CAMERA_HDR);
90                    if (pref != null) {
91                        // toggle hdr value
92                        int index = (pref.findIndexOfValue(pref.getValue()) + 1) % 2;
93                        pref.setValueIndex(index);
94                        onSettingChanged(pref);
95                    }
96                }
97            });
98            mRenderer.addItem(hdr);
99        }
100        mOtherKeys = new String[] {
101                CameraSettings.KEY_SCENE_MODE,
102                CameraSettings.KEY_RECORD_LOCATION,
103                CameraSettings.KEY_PICTURE_SIZE,
104                CameraSettings.KEY_FOCUS_MODE,
105                CameraSettings.KEY_TIMER,
106                CameraSettings.KEY_TIMER_SOUND_EFFECTS,
107                };
108        PieItem item = makeItem(R.drawable.ic_settings_holo_light);
109        item.setFixedSlice(FLOAT_PI_DIVIDED_BY_TWO * 3, sweep);
110        item.setOnClickListener(new OnClickListener() {
111            @Override
112            public void onClick(PieItem item) {
113                if (mPopup == null) {
114                    initializePopup();
115                }
116                mModule.showPopup(mPopup);
117            }
118        });
119        mRenderer.addItem(item);
120    }
121
122    protected void setCameraId(int cameraId) {
123        ListPreference pref = mPreferenceGroup.findPreference(CameraSettings.KEY_CAMERA_ID);
124        pref.setValue("" + cameraId);
125    }
126
127    @Override
128    public void reloadPreferences() {
129        super.reloadPreferences();
130        if (mPopup != null) {
131            mPopup.reloadPreference();
132        }
133    }
134
135    @Override
136    // Hit when an item in the second-level popup gets selected
137    public void onListPrefChanged(ListPreference pref) {
138        if (mPopup != null && mSecondPopup != null) {
139                mModule.dismissPopup(true);
140                mPopup.reloadPreference();
141        }
142        onSettingChanged(pref);
143    }
144
145    @Override
146    public void overrideSettings(final String ... keyvalues) {
147        super.overrideSettings(keyvalues);
148        if (mPopup == null) initializePopup();
149        mPopup.overrideSettings(keyvalues);
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 (mSecondPopup != null) {
170            mSecondPopup = null;
171            if (topPopupOnly) mModule.showPopup(mPopup);
172        }
173    }
174
175    // Return true if the preference has the specified key but not the value.
176    private static boolean notSame(ListPreference pref, String key, String value) {
177        return (key.equals(pref.getKey()) && !value.equals(pref.getValue()));
178    }
179
180    private void setPreference(String key, String value) {
181        ListPreference pref = mPreferenceGroup.findPreference(key);
182        if (pref != null && !value.equals(pref.getValue())) {
183            pref.setValue(value);
184            reloadPreferences();
185        }
186    }
187
188    @Override
189    public void onSettingChanged(ListPreference pref) {
190        // Reset the scene mode if HDR is set to on. Reset HDR if scene mode is
191        // set to non-auto.
192        if (notSame(pref, CameraSettings.KEY_CAMERA_HDR, mSettingOff)) {
193            setPreference(CameraSettings.KEY_SCENE_MODE, Parameters.SCENE_MODE_AUTO);
194        } else if (notSame(pref, CameraSettings.KEY_SCENE_MODE, Parameters.SCENE_MODE_AUTO)) {
195            setPreference(CameraSettings.KEY_CAMERA_HDR, mSettingOff);
196        }
197        super.onSettingChanged(pref);
198    }
199
200    @Override
201    // Hit when an item in the first-level popup gets selected, then bring up
202    // the second-level popup
203    public void onPreferenceClicked(ListPreference pref) {
204        if (mSecondPopup != null) return;
205
206        LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
207                Context.LAYOUT_INFLATER_SERVICE);
208        if (CameraSettings.KEY_TIMER.equals(pref.getKey())) {
209            TimerSettingPopup timerPopup = (TimerSettingPopup) inflater.inflate(
210                    R.layout.timer_setting_popup, null, false);
211            timerPopup.initialize(pref);
212            timerPopup.setSettingChangedListener(this);
213            mModule.dismissPopup(true);
214            mSecondPopup = timerPopup;
215        } else {
216            ListPrefSettingPopup basic = (ListPrefSettingPopup) inflater.inflate(
217                    R.layout.list_pref_setting_popup, null, false);
218            basic.initialize(pref);
219            basic.setSettingChangedListener(this);
220            mModule.dismissPopup(true);
221            mSecondPopup = basic;
222        }
223        mModule.showPopup(mSecondPopup);
224    }
225}
226