OSDN Git Service

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