1 /* 2 * Copyright (C) 2018 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 android.app.ActivityManager; 17 import android.content.ContentResolver; 18 import android.content.Context; 19 import android.database.ContentObserver; 20 import android.hardware.display.ColorDisplayManager; 21 import android.net.Uri; 22 import android.os.Handler; 23 import android.os.Looper; 24 import android.os.UserHandle; 25 import android.provider.Settings.Secure; 26 import android.provider.Settings.System; 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.settingslib.core.lifecycle.LifecycleObserver; 32 import com.android.settingslib.core.lifecycle.events.OnStart; 33 import com.android.settingslib.core.lifecycle.events.OnStop; 34 35 import com.android.settings.core.TogglePreferenceController; 36 37 public class DisplayWhiteBalancePreferenceController extends TogglePreferenceController 38 implements LifecycleObserver, OnStart, OnStop { 39 40 private ColorDisplayManager mColorDisplayManager; 41 @VisibleForTesting 42 ContentObserver mContentObserver; 43 private Preference mPreference; 44 DisplayWhiteBalancePreferenceController(Context context, String key)45 public DisplayWhiteBalancePreferenceController(Context context, String key) { 46 super(context, key); 47 } 48 49 @Override getAvailabilityStatus()50 public int getAvailabilityStatus() { 51 return getColorDisplayManager().isDisplayWhiteBalanceAvailable(mContext) ? 52 AVAILABLE : DISABLED_FOR_USER; 53 } 54 55 @Override isChecked()56 public boolean isChecked() { 57 return getColorDisplayManager().isDisplayWhiteBalanceEnabled(); 58 } 59 60 @Override setChecked(boolean isChecked)61 public boolean setChecked(boolean isChecked) { 62 return getColorDisplayManager().setDisplayWhiteBalanceEnabled(isChecked); 63 } 64 65 @Override onStart()66 public void onStart() { 67 if (!isAvailable()) { 68 return; 69 } 70 71 final ContentResolver cr = mContext.getContentResolver(); 72 mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { 73 @Override 74 public void onChange(boolean selfChange, Uri uri) { 75 super.onChange(selfChange, uri); 76 updateVisibility(); 77 } 78 }; 79 cr.registerContentObserver( 80 Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED), 81 false /* notifyForDescendants */, mContentObserver, 82 ActivityManager.getCurrentUser()); 83 cr.registerContentObserver( 84 Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED), 85 false /* notifyForDescendants */, mContentObserver, 86 ActivityManager.getCurrentUser()); 87 cr.registerContentObserver( 88 System.getUriFor(System.DISPLAY_COLOR_MODE), 89 false /* notifyForDescendants */, mContentObserver, 90 ActivityManager.getCurrentUser()); 91 92 updateVisibility(); 93 } 94 95 @Override onStop()96 public void onStop() { 97 if (mContentObserver != null) { 98 mContext.getContentResolver().unregisterContentObserver(mContentObserver); 99 mContentObserver = null; 100 } 101 } 102 103 @Override displayPreference(PreferenceScreen screen)104 public void displayPreference(PreferenceScreen screen) { 105 super.displayPreference(screen); 106 mPreference = screen.findPreference(getPreferenceKey()); 107 } 108 109 @VisibleForTesting getColorDisplayManager()110 ColorDisplayManager getColorDisplayManager() { 111 if (mColorDisplayManager == null) { 112 mColorDisplayManager = mContext.getSystemService(ColorDisplayManager.class); 113 } 114 return mColorDisplayManager; 115 } 116 117 @VisibleForTesting updateVisibility()118 void updateVisibility() { 119 if (mPreference != null) { 120 ColorDisplayManager cdm = getColorDisplayManager(); 121 122 // Display white balance is only valid in linear light space. COLOR_MODE_SATURATED 123 // implies unmanaged color mode, and hence unknown color processing conditions. 124 // We also disallow display white balance when color accessibility features are enabled. 125 mPreference.setVisible(cdm.getColorMode() != ColorDisplayManager.COLOR_MODE_SATURATED && 126 !cdm.areAccessibilityTransformsEnabled(mContext)); 127 } 128 } 129 } 130