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.support.annotation.VisibleForTesting; 32 import android.support.v7.preference.Preference; 33 import android.support.v7.preference.PreferenceScreen; 34 import android.util.Log; 35 36 import com.android.settings.core.PreferenceControllerMixin; 37 import com.android.settingslib.core.AbstractPreferenceController; 38 import com.android.settingslib.core.lifecycle.Lifecycle; 39 import com.android.settingslib.core.lifecycle.LifecycleObserver; 40 import com.android.settingslib.core.lifecycle.events.OnStart; 41 import com.android.settingslib.core.lifecycle.events.OnStop; 42 43 import java.text.NumberFormat; 44 45 public class BrightnessLevelPreferenceController extends AbstractPreferenceController implements 46 PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop { 47 48 private static final String TAG = "BrightnessPrefCtrl"; 49 private static final String KEY_BRIGHTNESS = "brightness"; 50 private static final Uri BRIGHTNESS_URI; 51 private static final Uri BRIGHTNESS_FOR_VR_URI; 52 private static final Uri BRIGHTNESS_ADJ_URI; 53 54 private final int mMinBrightness; 55 private final int mMaxBrightness; 56 private final int mMinVrBrightness; 57 private final int mMaxVrBrightness; 58 private final ContentResolver mContentResolver; 59 60 private Preference mPreference; 61 62 static { 63 BRIGHTNESS_URI = System.getUriFor(System.SCREEN_BRIGHTNESS); 64 BRIGHTNESS_FOR_VR_URI = System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR); 65 BRIGHTNESS_ADJ_URI = System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ); 66 } 67 68 private ContentObserver mBrightnessObserver = 69 new ContentObserver(new Handler(Looper.getMainLooper())) { 70 @Override 71 public void onChange(boolean selfChange) { 72 updatedSummary(mPreference); 73 } 74 }; 75 76 public BrightnessLevelPreferenceController(Context context, Lifecycle lifecycle) { 77 super(context); 78 if (lifecycle != null) { 79 lifecycle.addObserver(this); 80 } 81 PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 82 mMinBrightness = powerManager.getMinimumScreenBrightnessSetting(); 83 mMaxBrightness = powerManager.getMaximumScreenBrightnessSetting(); 84 mMinVrBrightness = powerManager.getMinimumScreenBrightnessForVrSetting(); 85 mMaxVrBrightness = powerManager.getMaximumScreenBrightnessForVrSetting(); 86 mContentResolver = mContext.getContentResolver(); 87 } 88 89 @Override 90 public boolean isAvailable() { 91 return true; 92 } 93 94 @Override 95 public String getPreferenceKey() { 96 return KEY_BRIGHTNESS; 97 } 98 99 @Override 100 public void displayPreference(PreferenceScreen screen) { 101 super.displayPreference(screen); 102 mPreference = screen.findPreference(KEY_BRIGHTNESS); 103 } 104 105 @Override 106 public void updateState(Preference preference) { 107 updatedSummary(preference); 108 } 109 110 @Override 111 public void onStart() { 112 mContentResolver.registerContentObserver(BRIGHTNESS_URI, false, mBrightnessObserver); 113 mContentResolver.registerContentObserver(BRIGHTNESS_FOR_VR_URI, false, mBrightnessObserver); 114 mContentResolver.registerContentObserver(BRIGHTNESS_ADJ_URI, false, mBrightnessObserver); 115 } 116 117 @Override 118 public void onStop() { 119 mContentResolver.unregisterContentObserver(mBrightnessObserver); 120 } 121 122 private void updatedSummary(Preference preference) { 123 if (preference != null) { 124 preference.setSummary(NumberFormat.getPercentInstance().format(getCurrentBrightness())); 125 } 126 } 127 128 private double getCurrentBrightness() { 129 final int value; 130 if (isInVrMode()) { 131 value = convertLinearToGamma(System.getInt(mContentResolver, 132 System.SCREEN_BRIGHTNESS_FOR_VR, mMaxBrightness), 133 mMinVrBrightness, mMaxVrBrightness); 134 } else { 135 value = convertLinearToGamma(Settings.System.getInt(mContentResolver, 136 System.SCREEN_BRIGHTNESS, mMinBrightness), 137 mMinBrightness, mMaxBrightness); 138 139 } 140 return getPercentage(value, 0, GAMMA_SPACE_MAX); 141 } 142 143 private double getPercentage(double value, int min, int max) { 144 if (value > max) { 145 return 1.0; 146 } 147 if (value < min) { 148 return 0.0; 149 } 150 return (value - min) / (max - min); 151 } 152 153 @VisibleForTesting 154 boolean isInVrMode() { 155 try { 156 return IVrManager.Stub.asInterface(ServiceManager.getService(Context.VR_SERVICE)) 157 .getVrModeState(); 158 } catch (RemoteException e) { 159 Log.e(TAG, "Failed to check vr mode!", e); 160 } 161 return false; 162 } 163 } 164