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