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.provider.Settings.Secure; 25 import android.provider.Settings.System; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.settings.R; 32 import com.android.settings.core.TogglePreferenceController; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnStart; 35 import com.android.settingslib.core.lifecycle.events.OnStop; 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 getSliceHighlightMenuRes()66 public int getSliceHighlightMenuRes() { 67 return R.string.menu_key_display; 68 } 69 70 @Override onStart()71 public void onStart() { 72 if (!isAvailable()) { 73 return; 74 } 75 76 final ContentResolver cr = mContext.getContentResolver(); 77 mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { 78 @Override 79 public void onChange(boolean selfChange, Uri uri) { 80 super.onChange(selfChange, uri); 81 updateVisibility(); 82 } 83 }; 84 cr.registerContentObserver( 85 Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED), 86 false /* notifyForDescendants */, mContentObserver, 87 ActivityManager.getCurrentUser()); 88 cr.registerContentObserver( 89 Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED), 90 false /* notifyForDescendants */, mContentObserver, 91 ActivityManager.getCurrentUser()); 92 cr.registerContentObserver( 93 System.getUriFor(System.DISPLAY_COLOR_MODE), 94 false /* notifyForDescendants */, mContentObserver, 95 ActivityManager.getCurrentUser()); 96 97 updateVisibility(); 98 } 99 100 @Override onStop()101 public void onStop() { 102 if (mContentObserver != null) { 103 mContext.getContentResolver().unregisterContentObserver(mContentObserver); 104 mContentObserver = null; 105 } 106 } 107 108 @Override displayPreference(PreferenceScreen screen)109 public void displayPreference(PreferenceScreen screen) { 110 super.displayPreference(screen); 111 mPreference = screen.findPreference(getPreferenceKey()); 112 } 113 114 @VisibleForTesting getColorDisplayManager()115 ColorDisplayManager getColorDisplayManager() { 116 if (mColorDisplayManager == null) { 117 mColorDisplayManager = mContext.getSystemService(ColorDisplayManager.class); 118 } 119 return mColorDisplayManager; 120 } 121 122 @VisibleForTesting updateVisibility()123 void updateVisibility() { 124 if (mPreference != null) { 125 ColorDisplayManager cdm = getColorDisplayManager(); 126 127 // Display white balance is only valid in linear light space. COLOR_MODE_SATURATED 128 // implies unmanaged color mode, and hence unknown color processing conditions. 129 // We also disallow display white balance when color accessibility features are enabled. 130 mPreference.setVisible(cdm.getColorMode() != ColorDisplayManager.COLOR_MODE_SATURATED && 131 !cdm.areAccessibilityTransformsEnabled(mContext)); 132 } 133 } 134 } 135