1 /*
2  * Copyright (C) 2014 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.settings;
18 
19 import android.app.Activity;
20 import android.app.Application;
21 import android.content.Context;
22 
23 import android.preference.SwitchPreference;
24 import android.util.AttributeSet;
25 
26 import com.android.camera.app.CameraApp;
27 
28 /**
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.
34  */
35 public class ManagedSwitchPreference extends SwitchPreference {
ManagedSwitchPreference(Context context)36     public ManagedSwitchPreference(Context context) {
37         super(context);
38     }
39 
ManagedSwitchPreference(Context context, AttributeSet attrs)40     public ManagedSwitchPreference(Context context, AttributeSet attrs) {
41         super(context, attrs);
42     }
43 
ManagedSwitchPreference(Context context, AttributeSet attrs, int defStyle)44     public ManagedSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
45         super(context, attrs, defStyle);
46     }
47 
48     @Override
getPersistedBoolean(boolean defaultReturnValue)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;
55         }
56         SettingsManager settingsManager = cameraApp.getSettingsManager();
57         if (settingsManager != null) {
58             return settingsManager.getBoolean(SettingsManager.SCOPE_GLOBAL, getKey());
59         } else {
60             // If the SettingsManager is for some reason not initialized,
61             // perhaps triggered by a monkey, return default value.
62             return defaultReturnValue;
63         }
64     }
65 
66     @Override
persistBoolean(boolean value)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
72             // be persisted.
73             return false;
74         }
75         SettingsManager settingsManager = cameraApp.getSettingsManager();
76         if (settingsManager != null) {
77             settingsManager.set(SettingsManager.SCOPE_GLOBAL, getKey(), value);
78             return true;
79         } else {
80             // If the SettingsManager is for some reason not initialized,
81             // perhaps triggered by a monkey, return false to note the value
82             // was not persisted.
83             return false;
84         }
85     }
86 
getCameraApp()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;
93             }
94         }
95         return null;
96     }
97 }
98