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