2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera.settings;
19 import android.app.Activity;
20 import android.app.Application;
21 import android.content.Context;
22 import android.preference.SwitchPreference;
23 import android.util.AttributeSet;
25 import com.android.camera.app.CameraApp;
26 import com.android.camera.app.CameraServicesImpl;
29 * This class allows Settings UIs to display and set boolean values controlled
30 * via the {@link SettingManager}. The Default {@link SwitchPreference} uses
31 * {@link android.content.SharedPreferences} as a backing store; since the
32 * {@link SettingManager} stores all settings as Strings we need to ensure we
33 * get and set boolean settings through the manager.
35 public class ManagedSwitchPreference extends SwitchPreference {
36 public ManagedSwitchPreference(Context context) {
40 public ManagedSwitchPreference(Context context, AttributeSet attrs) {
41 super(context, attrs);
44 public ManagedSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
45 super(context, attrs, defStyle);
49 public boolean getPersistedBoolean(boolean defaultReturnValue) {
50 CameraApp cameraApp = getCameraApp();
51 if (cameraApp == null) {
52 // The context and app may not be initialized upon initial inflation of the
53 // preference from XML. In that case return the default value.
54 return defaultReturnValue;
56 SettingsManager settingsManager = CameraServicesImpl.instance().getSettingsManager();
57 if (settingsManager != null) {
58 return settingsManager.getBoolean(SettingsManager.SCOPE_GLOBAL, getKey());
60 // If the SettingsManager is for some reason not initialized,
61 // perhaps triggered by a monkey, return default value.
62 return defaultReturnValue;
67 public boolean persistBoolean(boolean value) {
68 CameraApp cameraApp = getCameraApp();
69 if (cameraApp == null) {
70 // The context may not be initialized upon initial inflation of the
71 // preference from XML. In that case return false to note the value won't
75 SettingsManager settingsManager = CameraServicesImpl.instance().getSettingsManager();
76 if (settingsManager != null) {
77 settingsManager.set(SettingsManager.SCOPE_GLOBAL, getKey(), value);
80 // If the SettingsManager is for some reason not initialized,
81 // perhaps triggered by a monkey, return false to note the value
87 private CameraApp getCameraApp() {
88 Context context = getContext();
89 if (context instanceof Activity) {
90 Application application = ((Activity) context).getApplication();
91 if (application instanceof CameraApp) {
92 return (CameraApp) application;