1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.display;
15 
16 import static com.android.settingslib.display.BrightnessUtils.GAMMA_SPACE_MAX;
17 import static com.android.settingslib.display.BrightnessUtils.convertLinearToGamma;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.database.ContentObserver;
22 import android.net.Uri;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.os.PowerManager;
26 import android.os.RemoteException;
27 import android.os.ServiceManager;
28 import android.provider.Settings;
29 import android.provider.Settings.System;
30 import android.service.vr.IVrManager;
31 import android.util.Log;
32 
33 import androidx.annotation.VisibleForTesting;
34 import androidx.preference.Preference;
35 import androidx.preference.PreferenceScreen;
36 
37 import com.android.settings.core.PreferenceControllerMixin;
38 import com.android.settingslib.core.AbstractPreferenceController;
39 import com.android.settingslib.core.lifecycle.Lifecycle;
40 import com.android.settingslib.core.lifecycle.LifecycleObserver;
41 import com.android.settingslib.core.lifecycle.events.OnStart;
42 import com.android.settingslib.core.lifecycle.events.OnStop;
43 
44 import java.text.NumberFormat;
45 
46 public class BrightnessLevelPreferenceController extends AbstractPreferenceController implements
47         PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop {
48 
49     private static final String TAG = "BrightnessPrefCtrl";
50     private static final String KEY_BRIGHTNESS = "brightness";
51     private static final Uri BRIGHTNESS_URI;
52     private static final Uri BRIGHTNESS_FOR_VR_URI;
53     private static final Uri BRIGHTNESS_ADJ_URI;
54 
55     private final int mMinBrightness;
56     private final int mMaxBrightness;
57     private final int mMinVrBrightness;
58     private final int mMaxVrBrightness;
59     private final ContentResolver mContentResolver;
60 
61     private Preference mPreference;
62 
63     static {
64         BRIGHTNESS_URI = System.getUriFor(System.SCREEN_BRIGHTNESS);
65         BRIGHTNESS_FOR_VR_URI = System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR);
66         BRIGHTNESS_ADJ_URI = System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ);
67     }
68 
69     private ContentObserver mBrightnessObserver =
70             new ContentObserver(new Handler(Looper.getMainLooper())) {
71                 @Override
72                 public void onChange(boolean selfChange) {
73                     updatedSummary(mPreference);
74                 }
75             };
76 
BrightnessLevelPreferenceController(Context context, Lifecycle lifecycle)77     public BrightnessLevelPreferenceController(Context context, Lifecycle lifecycle) {
78         super(context);
79         if (lifecycle != null) {
80             lifecycle.addObserver(this);
81         }
82         PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
83         mMinBrightness = powerManager.getMinimumScreenBrightnessSetting();
84         mMaxBrightness = powerManager.getMaximumScreenBrightnessSetting();
85         mMinVrBrightness = powerManager.getMinimumScreenBrightnessForVrSetting();
86         mMaxVrBrightness = powerManager.getMaximumScreenBrightnessForVrSetting();
87         mContentResolver = mContext.getContentResolver();
88     }
89 
90     @Override
isAvailable()91     public boolean isAvailable() {
92         return true;
93     }
94 
95     @Override
getPreferenceKey()96     public String getPreferenceKey() {
97         return KEY_BRIGHTNESS;
98     }
99 
100     @Override
displayPreference(PreferenceScreen screen)101     public void displayPreference(PreferenceScreen screen) {
102         super.displayPreference(screen);
103         mPreference = screen.findPreference(KEY_BRIGHTNESS);
104     }
105 
106     @Override
updateState(Preference preference)107     public void updateState(Preference preference) {
108         updatedSummary(preference);
109     }
110 
111     @Override
onStart()112     public void onStart() {
113         mContentResolver.registerContentObserver(BRIGHTNESS_URI, false, mBrightnessObserver);
114         mContentResolver.registerContentObserver(BRIGHTNESS_FOR_VR_URI, false, mBrightnessObserver);
115         mContentResolver.registerContentObserver(BRIGHTNESS_ADJ_URI, false, mBrightnessObserver);
116     }
117 
118     @Override
onStop()119     public void onStop() {
120         mContentResolver.unregisterContentObserver(mBrightnessObserver);
121     }
122 
updatedSummary(Preference preference)123     private void updatedSummary(Preference preference) {
124         if (preference != null) {
125             preference.setSummary(NumberFormat.getPercentInstance().format(getCurrentBrightness()));
126         }
127     }
128 
getCurrentBrightness()129     private double getCurrentBrightness() {
130         final int value;
131         if (isInVrMode()) {
132             value = convertLinearToGamma(System.getInt(mContentResolver,
133                     System.SCREEN_BRIGHTNESS_FOR_VR, mMaxBrightness),
134                     mMinVrBrightness, mMaxVrBrightness);
135         } else {
136             value = convertLinearToGamma(Settings.System.getInt(mContentResolver,
137                     System.SCREEN_BRIGHTNESS, mMinBrightness),
138                     mMinBrightness, mMaxBrightness);
139 
140         }
141         return getPercentage(value, 0, GAMMA_SPACE_MAX);
142     }
143 
getPercentage(double value, int min, int max)144     private double getPercentage(double value, int min, int max) {
145         if (value > max) {
146             return 1.0;
147         }
148         if (value < min) {
149             return 0.0;
150         }
151         return (value - min) / (max - min);
152     }
153 
154     @VisibleForTesting
safeGetVrManager()155     IVrManager safeGetVrManager() {
156         return IVrManager.Stub.asInterface(ServiceManager.getService(
157                 Context.VR_SERVICE));
158     }
159 
160     @VisibleForTesting
isInVrMode()161     boolean isInVrMode() {
162         IVrManager vrManager = safeGetVrManager();
163         if (vrManager != null) {
164             try {
165                 return vrManager.getVrModeState();
166             } catch (RemoteException e) {
167                 Log.e(TAG, "Failed to check vr mode!", e);
168             }
169         }
170         return false;
171     }
172 }
173