2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server.accessibility;
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.os.Binder;
22 import android.provider.Settings.Secure;
23 import android.view.accessibility.AccessibilityManager;
25 import com.android.server.LocalServices;
26 import com.android.server.display.DisplayTransformManager;
29 * Utility methods for performing accessibility display adjustments.
31 class DisplayAdjustmentUtils {
33 /** Default inversion mode for display color correction. */
34 private static final int DEFAULT_DISPLAY_DALTONIZER =
35 AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY;
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,
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.
52 private static final float[] MATRIX_INVERT_COLOR = new float[] {
59 public static void applyDaltonizerSetting(Context context, int userId) {
60 final ContentResolver cr = context.getContentResolver();
61 final DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class);
63 int daltonizerMode = AccessibilityManager.DALTONIZER_DISABLED;
64 long identity = Binder.clearCallingIdentity();
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);
72 Binder.restoreCallingIdentity(identity);
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;
81 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_GRAYSCALE, grayscaleMatrix);
82 dtm.setDaltonizerMode(daltonizerMode);
86 * Applies the specified user's display color adjustments.
88 public static void applyInversionSetting(Context context, int userId) {
89 final ContentResolver cr = context.getContentResolver();
90 final DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class);
92 long identity = Binder.clearCallingIdentity();
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);
99 Binder.restoreCallingIdentity(identity);