Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.systemui.settings; |
| 18 | |
Fan Zhang | 106b1c0 | 2018-04-24 11:14:06 -0700 | [diff] [blame] | 19 | import static com.android.settingslib.display.BrightnessUtils.GAMMA_SPACE_MAX; |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 20 | import static com.android.settingslib.display.BrightnessUtils.convertGammaToLinearFloat; |
| 21 | import static com.android.settingslib.display.BrightnessUtils.convertLinearToGammaFloat; |
Fan Zhang | 106b1c0 | 2018-04-24 11:14:06 -0700 | [diff] [blame] | 22 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 23 | import android.animation.ValueAnimator; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 24 | import android.content.ContentResolver; |
| 25 | import android.content.Context; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 26 | import android.database.ContentObserver; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 27 | import android.hardware.display.DisplayManager; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 28 | import android.net.Uri; |
| 29 | import android.os.AsyncTask; |
| 30 | import android.os.Handler; |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 31 | import android.os.Looper; |
| 32 | import android.os.Message; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 33 | import android.os.PowerManager; |
| 34 | import android.os.RemoteException; |
| 35 | import android.os.ServiceManager; |
| 36 | import android.os.UserHandle; |
yuemingw | eb2bf7d18 | 2018-01-23 16:05:08 +0000 | [diff] [blame] | 37 | import android.os.UserManager; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 38 | import android.provider.Settings; |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 39 | import android.service.vr.IVrManager; |
| 40 | import android.service.vr.IVrStateCallbacks; |
| 41 | import android.util.Log; |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 42 | import android.util.MathUtils; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 43 | |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 44 | import com.android.internal.BrightnessSynchronizer; |
Jason Monk | 97b87a9 | 2015-06-23 15:04:44 -0400 | [diff] [blame] | 45 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 46 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Philip P. Moltmann | 4e615e6 | 2018-08-28 14:57:49 -0700 | [diff] [blame] | 47 | import com.android.settingslib.RestrictedLockUtilsInternal; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 48 | import com.android.systemui.Dependency; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 49 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Jason Monk | 97b87a9 | 2015-06-23 15:04:44 -0400 | [diff] [blame] | 50 | |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 51 | import java.util.ArrayList; |
| 52 | |
| 53 | public class BrightnessController implements ToggleSlider.Listener { |
| 54 | private static final String TAG = "StatusBar.BrightnessController"; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 55 | private static final int SLIDER_ANIMATION_DURATION = 3000; |
Adrian Roos | daf7d41 | 2014-05-13 14:55:09 +0200 | [diff] [blame] | 56 | |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 57 | private static final int MSG_UPDATE_SLIDER = 1; |
| 58 | private static final int MSG_SET_CHECKED = 2; |
| 59 | private static final int MSG_ATTACH_LISTENER = 3; |
| 60 | private static final int MSG_DETACH_LISTENER = 4; |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 61 | private static final int MSG_VR_MODE_CHANGED = 5; |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 62 | |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 63 | private static final Uri BRIGHTNESS_MODE_URI = |
| 64 | Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE); |
| 65 | private static final Uri BRIGHTNESS_URI = |
| 66 | Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS); |
| 67 | private static final Uri BRIGHTNESS_FLOAT_URI = |
| 68 | Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_FLOAT); |
| 69 | private static final Uri BRIGHTNESS_FOR_VR_FLOAT_URI = |
| 70 | Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT); |
| 71 | |
| 72 | private final float mMinimumBacklight; |
| 73 | private final float mMaximumBacklight; |
| 74 | private final float mDefaultBacklight; |
| 75 | private final float mMinimumBacklightForVr; |
| 76 | private final float mMaximumBacklightForVr; |
| 77 | private final float mDefaultBacklightForVr; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 78 | |
| 79 | private final Context mContext; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 80 | private final ToggleSlider mControl; |
| 81 | private final boolean mAutomaticAvailable; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 82 | private final DisplayManager mDisplayManager; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 83 | private final CurrentUserTracker mUserTracker; |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 84 | private final IVrManager mVrManager; |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 85 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 86 | private final Handler mBackgroundHandler; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 87 | private final BrightnessObserver mBrightnessObserver; |
| 88 | |
| 89 | private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks = |
| 90 | new ArrayList<BrightnessStateChangeCallback>(); |
| 91 | |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 92 | private volatile boolean mAutomatic; // Brightness adjusted automatically using ambient light. |
| 93 | private volatile boolean mIsVrModeEnabled; |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 94 | private boolean mListening; |
Adrian Roos | b7ebbde | 2014-08-15 20:11:38 +0200 | [diff] [blame] | 95 | private boolean mExternalChange; |
Michael Wright | a72faf9 | 2018-01-29 15:17:20 +0000 | [diff] [blame] | 96 | private boolean mControlValueInitialized; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 97 | |
| 98 | private ValueAnimator mSliderAnimator; |
Adrian Roos | daf7d41 | 2014-05-13 14:55:09 +0200 | [diff] [blame] | 99 | |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 100 | public interface BrightnessStateChangeCallback { |
| 101 | public void onBrightnessLevelChanged(); |
| 102 | } |
| 103 | |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 104 | /** ContentObserver to watch brightness */ |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 105 | private class BrightnessObserver extends ContentObserver { |
| 106 | |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 107 | public BrightnessObserver(Handler handler) { |
| 108 | super(handler); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void onChange(boolean selfChange) { |
| 113 | onChange(selfChange, null); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void onChange(boolean selfChange, Uri uri) { |
| 118 | if (selfChange) return; |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 119 | |
| 120 | if (BRIGHTNESS_MODE_URI.equals(uri)) { |
| 121 | mBackgroundHandler.post(mUpdateModeRunnable); |
| 122 | mBackgroundHandler.post(mUpdateSliderRunnable); |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 123 | } else if (BRIGHTNESS_FLOAT_URI.equals(uri)) { |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 124 | mBackgroundHandler.post(mUpdateSliderRunnable); |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 125 | } else if (BRIGHTNESS_FOR_VR_FLOAT_URI.equals(uri)) { |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 126 | mBackgroundHandler.post(mUpdateSliderRunnable); |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 127 | } else { |
| 128 | mBackgroundHandler.post(mUpdateModeRunnable); |
| 129 | mBackgroundHandler.post(mUpdateSliderRunnable); |
| 130 | } |
| 131 | for (BrightnessStateChangeCallback cb : mChangeCallbacks) { |
| 132 | cb.onBrightnessLevelChanged(); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | public void startObserving() { |
| 137 | final ContentResolver cr = mContext.getContentResolver(); |
| 138 | cr.unregisterContentObserver(this); |
| 139 | cr.registerContentObserver( |
| 140 | BRIGHTNESS_MODE_URI, |
| 141 | false, this, UserHandle.USER_ALL); |
| 142 | cr.registerContentObserver( |
| 143 | BRIGHTNESS_URI, |
| 144 | false, this, UserHandle.USER_ALL); |
Adrian Roos | daf7d41 | 2014-05-13 14:55:09 +0200 | [diff] [blame] | 145 | cr.registerContentObserver( |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 146 | BRIGHTNESS_FLOAT_URI, |
| 147 | false, this, UserHandle.USER_ALL); |
| 148 | cr.registerContentObserver( |
| 149 | BRIGHTNESS_FOR_VR_FLOAT_URI, |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 150 | false, this, UserHandle.USER_ALL); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | public void stopObserving() { |
| 154 | final ContentResolver cr = mContext.getContentResolver(); |
| 155 | cr.unregisterContentObserver(this); |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 160 | private final Runnable mStartListeningRunnable = new Runnable() { |
| 161 | @Override |
| 162 | public void run() { |
Lucas Dupin | b803a55 | 2019-10-03 15:00:03 -0700 | [diff] [blame] | 163 | if (mListening) { |
| 164 | return; |
| 165 | } |
| 166 | mListening = true; |
| 167 | |
| 168 | if (mVrManager != null) { |
| 169 | try { |
| 170 | mVrManager.registerListener(mVrStateCallbacks); |
| 171 | mIsVrModeEnabled = mVrManager.getVrModeState(); |
| 172 | } catch (RemoteException e) { |
| 173 | Log.e(TAG, "Failed to register VR mode state listener: ", e); |
| 174 | } |
| 175 | } |
| 176 | |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 177 | mBrightnessObserver.startObserving(); |
| 178 | mUserTracker.startTracking(); |
| 179 | |
| 180 | // Update the slider and mode before attaching the listener so we don't |
| 181 | // receive the onChanged notifications for the initial values. |
| 182 | mUpdateModeRunnable.run(); |
| 183 | mUpdateSliderRunnable.run(); |
| 184 | |
| 185 | mHandler.sendEmptyMessage(MSG_ATTACH_LISTENER); |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | private final Runnable mStopListeningRunnable = new Runnable() { |
| 190 | @Override |
| 191 | public void run() { |
Lucas Dupin | b803a55 | 2019-10-03 15:00:03 -0700 | [diff] [blame] | 192 | if (!mListening) { |
| 193 | return; |
| 194 | } |
| 195 | mListening = false; |
| 196 | |
| 197 | if (mVrManager != null) { |
| 198 | try { |
| 199 | mVrManager.unregisterListener(mVrStateCallbacks); |
| 200 | } catch (RemoteException e) { |
| 201 | Log.e(TAG, "Failed to unregister VR mode state listener: ", e); |
| 202 | } |
| 203 | } |
| 204 | |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 205 | mBrightnessObserver.stopObserving(); |
| 206 | mUserTracker.stopTracking(); |
| 207 | |
| 208 | mHandler.sendEmptyMessage(MSG_DETACH_LISTENER); |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | /** |
| 213 | * Fetch the brightness mode from the system settings and update the icon. Should be called from |
| 214 | * background thread. |
| 215 | */ |
| 216 | private final Runnable mUpdateModeRunnable = new Runnable() { |
| 217 | @Override |
| 218 | public void run() { |
| 219 | if (mAutomaticAvailable) { |
| 220 | int automatic; |
| 221 | automatic = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 222 | Settings.System.SCREEN_BRIGHTNESS_MODE, |
| 223 | Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, |
| 224 | UserHandle.USER_CURRENT); |
| 225 | mAutomatic = automatic != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 226 | } else { |
| 227 | mHandler.obtainMessage(MSG_SET_CHECKED, 0).sendToTarget(); |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | /** |
| 233 | * Fetch the brightness from the system settings and update the slider. Should be called from |
| 234 | * background thread. |
| 235 | */ |
| 236 | private final Runnable mUpdateSliderRunnable = new Runnable() { |
| 237 | @Override |
| 238 | public void run() { |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 239 | final float valFloat; |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 240 | final boolean inVrMode = mIsVrModeEnabled; |
| 241 | if (inVrMode) { |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 242 | valFloat = Settings.System.getFloatForUser(mContext.getContentResolver(), |
| 243 | Settings.System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT, mDefaultBacklightForVr, |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 244 | UserHandle.USER_CURRENT); |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 245 | } else { |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 246 | valFloat = Settings.System.getFloatForUser(mContext.getContentResolver(), |
| 247 | Settings.System.SCREEN_BRIGHTNESS_FLOAT, mDefaultBacklight, |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 248 | UserHandle.USER_CURRENT); |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 249 | } |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 250 | // Value is passed as intbits, since this is what the message takes. |
| 251 | final int valueAsIntBits = Float.floatToIntBits(valFloat); |
| 252 | mHandler.obtainMessage(MSG_UPDATE_SLIDER, valueAsIntBits, |
| 253 | inVrMode ? 1 : 0).sendToTarget(); |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 254 | } |
| 255 | }; |
| 256 | |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 257 | private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() { |
| 258 | @Override |
| 259 | public void onVrStateChanged(boolean enabled) { |
| 260 | mHandler.obtainMessage(MSG_VR_MODE_CHANGED, enabled ? 1 : 0, 0) |
| 261 | .sendToTarget(); |
| 262 | } |
| 263 | }; |
| 264 | |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 265 | private final Handler mHandler = new Handler() { |
| 266 | @Override |
| 267 | public void handleMessage(Message msg) { |
| 268 | mExternalChange = true; |
| 269 | try { |
| 270 | switch (msg.what) { |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 271 | case MSG_UPDATE_SLIDER: |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 272 | updateSlider(Float.intBitsToFloat(msg.arg1), msg.arg2 != 0); |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 273 | break; |
| 274 | case MSG_SET_CHECKED: |
| 275 | mControl.setChecked(msg.arg1 != 0); |
| 276 | break; |
| 277 | case MSG_ATTACH_LISTENER: |
| 278 | mControl.setOnChangedListener(BrightnessController.this); |
| 279 | break; |
| 280 | case MSG_DETACH_LISTENER: |
| 281 | mControl.setOnChangedListener(null); |
Jorim Jaggi | 4adee26 | 2016-09-21 14:49:46 -0700 | [diff] [blame] | 282 | break; |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 283 | case MSG_VR_MODE_CHANGED: |
| 284 | updateVrMode(msg.arg1 != 0); |
| 285 | break; |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 286 | default: |
| 287 | super.handleMessage(msg); |
| 288 | } |
| 289 | } finally { |
| 290 | mExternalChange = false; |
| 291 | } |
| 292 | } |
| 293 | }; |
| 294 | |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 295 | public BrightnessController(Context context, ToggleSlider control, |
| 296 | BroadcastDispatcher broadcastDispatcher) { |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 297 | mContext = context; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 298 | mControl = control; |
Fan Zhang | 106b1c0 | 2018-04-24 11:14:06 -0700 | [diff] [blame] | 299 | mControl.setMax(GAMMA_SPACE_MAX); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 300 | mBackgroundHandler = new Handler((Looper) Dependency.get(Dependency.BG_LOOPER)); |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 301 | mUserTracker = new CurrentUserTracker(broadcastDispatcher) { |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 302 | @Override |
| 303 | public void onUserSwitched(int newUserId) { |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 304 | mBackgroundHandler.post(mUpdateModeRunnable); |
| 305 | mBackgroundHandler.post(mUpdateSliderRunnable); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 306 | } |
| 307 | }; |
| 308 | mBrightnessObserver = new BrightnessObserver(mHandler); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 309 | |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 310 | PowerManager pm = context.getSystemService(PowerManager.class); |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 311 | mMinimumBacklight = pm.getBrightnessConstraint( |
| 312 | PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM); |
| 313 | mMaximumBacklight = pm.getBrightnessConstraint( |
| 314 | PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM); |
| 315 | mDefaultBacklight = pm.getBrightnessConstraint( |
| 316 | PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DEFAULT); |
| 317 | mMinimumBacklightForVr = pm.getBrightnessConstraint( |
| 318 | PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM_VR); |
| 319 | mMaximumBacklightForVr = pm.getBrightnessConstraint( |
| 320 | PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM_VR); |
| 321 | mDefaultBacklightForVr = pm.getBrightnessConstraint( |
| 322 | PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DEFAULT_VR); |
| 323 | |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 324 | |
| 325 | mAutomaticAvailable = context.getResources().getBoolean( |
| 326 | com.android.internal.R.bool.config_automatic_brightness_available); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 327 | mDisplayManager = context.getSystemService(DisplayManager.class); |
Craig Donner | 8deb67c | 2017-02-07 18:10:32 -0800 | [diff] [blame] | 328 | mVrManager = IVrManager.Stub.asInterface(ServiceManager.getService( |
| 329 | Context.VR_SERVICE)); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | public void addStateChangedCallback(BrightnessStateChangeCallback cb) { |
| 333 | mChangeCallbacks.add(cb); |
| 334 | } |
| 335 | |
| 336 | public boolean removeStateChangedCallback(BrightnessStateChangeCallback cb) { |
| 337 | return mChangeCallbacks.remove(cb); |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public void onInit(ToggleSlider control) { |
| 342 | // Do nothing |
| 343 | } |
| 344 | |
Jorim Jaggi | f056e35 | 2014-05-11 23:09:22 +0200 | [diff] [blame] | 345 | public void registerCallbacks() { |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 346 | mBackgroundHandler.post(mStartListeningRunnable); |
Jorim Jaggi | f056e35 | 2014-05-11 23:09:22 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 349 | /** Unregister all call backs, both to and from the controller */ |
| 350 | public void unregisterCallbacks() { |
Jorim Jaggi | c6da82f | 2016-08-02 16:11:25 +0200 | [diff] [blame] | 351 | mBackgroundHandler.post(mStopListeningRunnable); |
Michael Wright | a72faf9 | 2018-01-29 15:17:20 +0000 | [diff] [blame] | 352 | mControlValueInitialized = false; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Adrian Roos | 5fd872e | 2014-08-12 17:28:58 +0200 | [diff] [blame] | 355 | @Override |
Muyuan Li | 3b0f892 | 2016-04-18 19:27:29 -0700 | [diff] [blame] | 356 | public void onChanged(ToggleSlider toggleSlider, boolean tracking, boolean automatic, |
| 357 | int value, boolean stopTracking) { |
Adrian Roos | b7ebbde | 2014-08-15 20:11:38 +0200 | [diff] [blame] | 358 | if (mExternalChange) return; |
| 359 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 360 | if (mSliderAnimator != null) { |
| 361 | mSliderAnimator.cancel(); |
| 362 | } |
| 363 | |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 364 | final float minBacklight; |
| 365 | final float maxBacklight; |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 366 | final int metric; |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 367 | final String settingToChange; |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 368 | |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 369 | if (mIsVrModeEnabled) { |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 370 | metric = MetricsEvent.ACTION_BRIGHTNESS_FOR_VR; |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 371 | minBacklight = mMinimumBacklightForVr; |
| 372 | maxBacklight = mMaximumBacklightForVr; |
| 373 | settingToChange = Settings.System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 374 | } else { |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 375 | metric = mAutomatic |
| 376 | ? MetricsEvent.ACTION_BRIGHTNESS_AUTO |
| 377 | : MetricsEvent.ACTION_BRIGHTNESS; |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 378 | minBacklight = mMinimumBacklight; |
| 379 | maxBacklight = mMaximumBacklight; |
| 380 | settingToChange = Settings.System.SCREEN_BRIGHTNESS_FLOAT; |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 381 | } |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 382 | final float valFloat = MathUtils.min(convertGammaToLinearFloat(value, |
| 383 | minBacklight, maxBacklight), |
| 384 | 1.0f); |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 385 | if (stopTracking) { |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 386 | // TODO(brightnessfloat): change to use float value instead. |
| 387 | MetricsLogger.action(mContext, metric, |
| 388 | BrightnessSynchronizer.brightnessFloatToInt(mContext, valFloat)); |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 389 | |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 390 | } |
| 391 | setBrightness(valFloat); |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 392 | if (!tracking) { |
| 393 | AsyncTask.execute(new Runnable() { |
| 394 | public void run() { |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 395 | Settings.System.putFloatForUser(mContext.getContentResolver(), |
| 396 | settingToChange, valFloat, UserHandle.USER_CURRENT); |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 397 | } |
| 398 | }); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | for (BrightnessStateChangeCallback cb : mChangeCallbacks) { |
| 402 | cb.onBrightnessLevelChanged(); |
| 403 | } |
| 404 | } |
| 405 | |
yuemingw | eb2bf7d18 | 2018-01-23 16:05:08 +0000 | [diff] [blame] | 406 | public void checkRestrictionAndSetEnabled() { |
| 407 | mBackgroundHandler.post(new Runnable() { |
| 408 | @Override |
| 409 | public void run() { |
| 410 | ((ToggleSliderView)mControl).setEnforcedAdmin( |
Philip P. Moltmann | 4e615e6 | 2018-08-28 14:57:49 -0700 | [diff] [blame] | 411 | RestrictedLockUtilsInternal.checkIfRestrictionEnforced(mContext, |
yuemingw | eb2bf7d18 | 2018-01-23 16:05:08 +0000 | [diff] [blame] | 412 | UserManager.DISALLOW_CONFIG_BRIGHTNESS, |
| 413 | mUserTracker.getCurrentUserId())); |
| 414 | } |
| 415 | }); |
| 416 | } |
| 417 | |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 418 | private void setMode(int mode) { |
| 419 | Settings.System.putIntForUser(mContext.getContentResolver(), |
| 420 | Settings.System.SCREEN_BRIGHTNESS_MODE, mode, |
| 421 | mUserTracker.getCurrentUserId()); |
| 422 | } |
| 423 | |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 424 | private void setBrightness(float brightness) { |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 425 | mDisplayManager.setTemporaryBrightness(brightness); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 428 | private void updateVrMode(boolean isEnabled) { |
| 429 | if (mIsVrModeEnabled != isEnabled) { |
| 430 | mIsVrModeEnabled = isEnabled; |
| 431 | mBackgroundHandler.post(mUpdateSliderRunnable); |
| 432 | } |
| 433 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 434 | |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 435 | private void updateSlider(float brightnessValue, boolean inVrMode) { |
| 436 | final float min; |
| 437 | final float max; |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 438 | if (inVrMode) { |
| 439 | min = mMinimumBacklightForVr; |
| 440 | max = mMaximumBacklightForVr; |
| 441 | } else { |
| 442 | min = mMinimumBacklight; |
| 443 | max = mMaximumBacklight; |
| 444 | } |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 445 | // convertGammaToLinearFloat returns 0-1 |
Fiona Campbell | 3dfe344 | 2020-05-26 13:08:53 +0100 | [diff] [blame] | 446 | if (BrightnessSynchronizer.floatEquals(brightnessValue, |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 447 | convertGammaToLinearFloat(mControl.getValue(), min, max))) { |
Fiona Campbell | 3dfe344 | 2020-05-26 13:08:53 +0100 | [diff] [blame] | 448 | // If the value in the slider is equal to the value on the current brightness |
| 449 | // then the slider does not need to animate, since the brightness will not change. |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 450 | return; |
| 451 | } |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 452 | // Returns GAMMA_SPACE_MIN - GAMMA_SPACE_MAX |
| 453 | final int sliderVal = convertLinearToGammaFloat(brightnessValue, min, max); |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 454 | animateSliderTo(sliderVal); |
| 455 | } |
| 456 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 457 | private void animateSliderTo(int target) { |
Michael Wright | a72faf9 | 2018-01-29 15:17:20 +0000 | [diff] [blame] | 458 | if (!mControlValueInitialized) { |
Fabian Kozynski | b9dc0c7 | 2018-09-20 13:00:17 -0400 | [diff] [blame] | 459 | // Don't animate the first value since its default state isn't meaningful to users. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 460 | mControl.setValue(target); |
Michael Wright | a72faf9 | 2018-01-29 15:17:20 +0000 | [diff] [blame] | 461 | mControlValueInitialized = true; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 462 | } |
| 463 | if (mSliderAnimator != null && mSliderAnimator.isStarted()) { |
| 464 | mSliderAnimator.cancel(); |
| 465 | } |
| 466 | mSliderAnimator = ValueAnimator.ofInt(mControl.getValue(), target); |
| 467 | mSliderAnimator.addUpdateListener((ValueAnimator animation) -> { |
| 468 | mExternalChange = true; |
Fabian Kozynski | b9dc0c7 | 2018-09-20 13:00:17 -0400 | [diff] [blame] | 469 | mControl.setValue((int) animation.getAnimatedValue()); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 470 | mExternalChange = false; |
| 471 | }); |
Fabian Kozynski | b9dc0c7 | 2018-09-20 13:00:17 -0400 | [diff] [blame] | 472 | final long animationDuration = SLIDER_ANIMATION_DURATION * Math.abs( |
| 473 | mControl.getValue() - target) / GAMMA_SPACE_MAX; |
| 474 | mSliderAnimator.setDuration(animationDuration); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 475 | mSliderAnimator.start(); |
| 476 | } |
Michael Wright | 585ff98 | 2018-04-13 18:15:27 +0100 | [diff] [blame] | 477 | |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 478 | } |