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 17package com.android.server.accessibility; 18 19import android.content.ContentResolver; 20import android.content.Context; 21import android.os.Binder; 22import android.provider.Settings.Secure; 23import android.view.accessibility.AccessibilityManager; 24 25import com.android.server.LocalServices; 26import com.android.server.display.DisplayTransformManager; 27 28/** 29 * Utility methods for performing accessibility display adjustments. 30 */ 31class DisplayAdjustmentUtils { 32 33 /** Default inversion mode for display color correction. */ 34 private static final int DEFAULT_DISPLAY_DALTONIZER = 35 AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY; 36 37 /** Matrix and offset used for converting color to gray-scale. */ 38 private static final float[] MATRIX_GRAYSCALE = new float[] { 39 .2126f, .2126f, .2126f, 0, 40 .7152f, .7152f, .7152f, 0, 41 .0722f, .0722f, .0722f, 0, 42 0, 0, 0, 1 43 }; 44 45 /** 46 * Matrix and offset used for luminance inversion. Represents a transform 47 * from RGB to YIQ color space, rotation around the Y axis by 180 degrees, 48 * transform back to RGB color space, and subtraction from 1. The last row 49 * represents a non-multiplied addition, see surfaceflinger's ProgramCache 50 * for full implementation details. 51 */ 52 private static final float[] MATRIX_INVERT_COLOR = new float[] { 53 0.402f, -0.598f, -0.599f, 0, 54 -1.174f, -0.174f, -1.175f, 0, 55 -0.228f, -0.228f, 0.772f, 0, 56 1, 1, 1, 1 57 }; 58 59 public static void applyDaltonizerSetting(Context context, int userId) { 60 final ContentResolver cr = context.getContentResolver(); 61 final DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class); 62 63 int daltonizerMode = AccessibilityManager.DALTONIZER_DISABLED; 64 long identity = Binder.clearCallingIdentity(); 65 try { 66 if (Secure.getIntForUser(cr, 67 Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0, userId) != 0) { 68 daltonizerMode = Secure.getIntForUser(cr, 69 Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, DEFAULT_DISPLAY_DALTONIZER, userId); 70 } 71 } finally { 72 Binder.restoreCallingIdentity(identity); 73 } 74 75 float[] grayscaleMatrix = null; 76 if (daltonizerMode == AccessibilityManager.DALTONIZER_SIMULATE_MONOCHROMACY) { 77 // Monochromacy isn't supported by the native Daltonizer. 78 grayscaleMatrix = MATRIX_GRAYSCALE; 79 daltonizerMode = AccessibilityManager.DALTONIZER_DISABLED; 80 } 81 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_GRAYSCALE, grayscaleMatrix); 82 dtm.setDaltonizerMode(daltonizerMode); 83 } 84 85 /** 86 * Applies the specified user's display color adjustments. 87 */ 88 public static void applyInversionSetting(Context context, int userId) { 89 final ContentResolver cr = context.getContentResolver(); 90 final DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class); 91 92 long identity = Binder.clearCallingIdentity(); 93 try { 94 final boolean invertColors = Secure.getIntForUser(cr, 95 Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, 0, userId) != 0; 96 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_INVERT_COLOR, 97 invertColors ? MATRIX_INVERT_COLOR : null); 98 } finally { 99 Binder.restoreCallingIdentity(identity); 100 } 101 } 102} 103