summaryrefslogtreecommitdiffstats
path: root/sdk/src/java/cyanogenmod/hardware/LiveDisplayManager.java
blob: cfe904f885f8553987513ff9d89e95e53d25bbf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * Copyright (C) 2016 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cyanogenmod.hardware;

import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import android.util.Range;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import cyanogenmod.app.CMContextConstants;

/**
 * LiveDisplay is an advanced set of features for improving
 * display quality under various ambient conditions.
 *
 * The backend service is constructed with a set of LiveDisplayFeatures
 * which provide capabilities such as outdoor mode, night mode,
 * and calibration. It interacts with CMHardwareService to relay
 * changes down to the lower layers.
 *
 * Multiple adaptive modes are supported, and various hardware
 * features such as CABC, ACO and color enhancement are also
 * managed by LiveDisplay.
 */
public class LiveDisplayManager {

    /**
     * Disable all LiveDisplay adaptive features
     */
    public static final int MODE_OFF = 0;

    /**
     * Change color temperature to night mode
     */
    public static final int MODE_NIGHT = 1;

    /**
     * Enable automatic detection of appropriate mode
     */
    public static final int MODE_AUTO = 2;

    /**
     * Increase brightness/contrast/saturation for sunlight
     */
    public static final int MODE_OUTDOOR = 3;

    /**
     * Change color temperature to day mode, and allow
     * detection of outdoor conditions
     */
    public static final int MODE_DAY = 4;

    /** @hide */
    public static final int MODE_FIRST = MODE_OFF;
    /** @hide */
    public static final int MODE_LAST = MODE_DAY;

    /**
     * Content adaptive backlight control, adjust images to
     * increase brightness in order to reduce backlight level
     */
    public static final int FEATURE_CABC = 10;

    /**
     * Adjust images to increase contrast
     */
    public static final int FEATURE_AUTO_CONTRAST = 11;

    /**
     * Adjust image to improve saturation and color
     */
    public static final int FEATURE_COLOR_ENHANCEMENT = 12;

    /**
     * Capable of adjusting RGB levels
     */
    public static final int FEATURE_COLOR_ADJUSTMENT = 13;

    /**
     * System supports outdoor mode, but environmental sensing
     * is done by an external application.
     */
    public static final int FEATURE_MANAGED_OUTDOOR_MODE = 14;

    /**
     * System supports multiple display calibrations
     * for different viewing intents.
     */
    public static final int FEATURE_DISPLAY_MODES = 15;

    /**
     * System supports direct range-based control of display
     * color balance (temperature). This is preferred over
     * simple RGB adjustment.
     */
    public static final int FEATURE_COLOR_BALANCE = 16;

    /**
     * System supports manual hue/saturation/intensity/contrast
     * adjustment of display.
     */
    public static final int FEATURE_PICTURE_ADJUSTMENT = 17;

    public static final int ADJUSTMENT_HUE = 0;
    public static final int ADJUSTMENT_SATURATION = 1;
    public static final int ADJUSTMENT_INTENSITY = 2;
    public static final int ADJUSTMENT_CONTRAST = 3;

    /** @hide */
    public static final int FEATURE_FIRST = FEATURE_CABC;
    /** @hide */
    public static final int FEATURE_LAST = FEATURE_PICTURE_ADJUSTMENT;

    private static final String TAG = "LiveDisplay";

    private final Context mContext;
    private final LiveDisplayConfig mConfig;

    private static LiveDisplayManager sInstance;
    private static ILiveDisplayService sService;

    /**
     * @hide to prevent subclassing from outside of the framework
     */
    private LiveDisplayManager(Context context) {
        Context appContext = context.getApplicationContext();
        if (appContext != null) {
            mContext = appContext;
        } else {
            mContext = context;
        }
        sService = getService();

        if (!context.getPackageManager().hasSystemFeature(
                CMContextConstants.Features.LIVEDISPLAY) || !checkService()) {
            Log.wtf(TAG, "Unable to get LiveDisplayService. The service either" +
                    " crashed, was not started, or the interface has been called to early in" +
                    " SystemServer init");
        }

        try {
            mConfig = sService.getConfig();
            if (mConfig == null) {
                throw new RuntimeException("Unable to get LiveDisplay configuration!");
            }
        } catch (RemoteException e) {
            throw new RuntimeException("Unable to fetch LiveDisplay configuration!", e);
        }
    }

    /**
     * Get or create an instance of the {@link cyanogenmod.hardware.LiveDisplayManager}
     * @param context
     * @return {@link LiveDisplayManager}
     */
    public synchronized static LiveDisplayManager getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new LiveDisplayManager(context);
        }
        return sInstance;
    }

    /** @hide */
    public static ILiveDisplayService getService() {
        if (sService != null) {
            return sService;
        }
        IBinder b = ServiceManager.getService(CMContextConstants.CM_LIVEDISPLAY_SERVICE);
        if (b != null) {
            sService = ILiveDisplayService.Stub.asInterface(b);
            return sService;
        }
        return null;
    }

    /**
     * @return true if service is valid
     */
    private boolean checkService() {
        if (sService == null) {
            Log.w(TAG, "not connected to CMHardwareManagerService");
            return false;
        }
        return true;
    }

    /**
     * Gets the static configuration and settings.
     *
     * @return the configuration
     */
    public LiveDisplayConfig getConfig() {
        return mConfig;
    }

    /**
     * Returns the current adaptive mode.
     *
     * @return id of the selected mode
     */
    public int getMode() {
        try {
            return checkService() ? sService.getMode() : MODE_OFF;
        } catch (RemoteException e) {
            return MODE_OFF;
        }
    }

    /**
     * Selects a new adaptive mode.
     *
     * @param mode
     * @return true if the mode was selected
     */
    public boolean setMode(int mode) {
        try {
            return checkService() && sService.setMode(mode);
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Checks if the auto contrast optimization feature is enabled.
     *
     * @return true if enabled
     */
    public boolean isAutoContrastEnabled() {
        try {
            return checkService() && sService.isAutoContrastEnabled();
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Sets the state of auto contrast optimization
     *
     * @param enabled
     * @return true if state was changed
     */
    public boolean setAutoContrastEnabled(boolean enabled) {
        try {
            return checkService() && sService.setAutoContrastEnabled(enabled);
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Checks if the CABC feature is enabled
     *
     * @return true if enabled
     */
    public boolean isCABCEnabled() {
        try {
            return checkService() && sService.isCABCEnabled();
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Sets the state of CABC
     *
     * @param enabled
     * @return true if state was changed
     */
    public boolean setCABCEnabled(boolean enabled) {
        try {
            return checkService() && sService.setCABCEnabled(enabled);
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Checks if the color enhancement feature is enabled
     *
     * @return true if enabled
     */
    public boolean isColorEnhancementEnabled() {
        try {
            return checkService() && sService.isColorEnhancementEnabled();
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Sets the state of color enhancement
     *
     * @param enabled
     * @return true if state was changed
     */
    public boolean setColorEnhancementEnabled(boolean enabled) {
        try {
            return checkService() && sService.setColorEnhancementEnabled(enabled);
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Gets the user-specified color temperature to use in the daytime.
     *
     * @return the day color temperature
     */
    public int getDayColorTemperature() {
        try {
            return checkService() ? sService.getDayColorTemperature() : -1;
        } catch (RemoteException e) {
            return -1;
        }
    }

    /**
     * Sets the color temperature to use in the daytime.
     *
     * @param temperature
     * @return true if state was changed
     */
    public boolean setDayColorTemperature(int temperature) {
        try {
            return checkService() && sService.setDayColorTemperature(temperature);
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Gets the user-specified color temperature to use at night.
     *
     * @return the night color temperature
     */
    public int getNightColorTemperature() {
        try {
            return checkService() ? sService.getNightColorTemperature() : -1;
        } catch (RemoteException e) {
            return -1;
        }
    }

    /**
     * Sets the color temperature to use at night.
     *
     * @param temperature
     * @return true if state was changed
     */
    public boolean setNightColorTemperature(int temperature) {
        try {
            return checkService() && sService.setNightColorTemperature(temperature);
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Checks if outdoor mode should be enabled automatically when under extremely high
     * ambient light. This is typically around 12000 lux.
     *
     * @return if outdoor conditions should be detected
     */
    public boolean isAutomaticOutdoorModeEnabled() {
        try {
            return checkService() && sService.isAutomaticOutdoorModeEnabled();
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Enables automatic detection of outdoor conditions. Outdoor mode is triggered
     * when high ambient light is detected and it's not night.
     *
     * @param enabled
     * @return true if state was changed
     */
    public boolean setAutomaticOutdoorModeEnabled(boolean enabled) {
        try {
            return checkService() && sService.setAutomaticOutdoorModeEnabled(enabled);
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Gets the current RGB triplet which is applied as a color adjustment.
     * The values are floats between 0 and 1. A setting of { 1.0, 1.0, 1.0 }
     * means that no adjustment is made.
     *
     * @return array of { R, G, B } offsets
     */
    public float[] getColorAdjustment() {
        try {
            if (checkService()) {
                return sService.getColorAdjustment();
            }
        } catch (RemoteException e) {
        }
        return new float[] { 1.0f, 1.0f, 1.0f };
    }

    /**
     * Sets the color adjustment to use. This can be set by the user to calibrate
     * their screen. This should be sent in the format { R, G, B } as floats from
     * 0 to 1. A setting of { 1.0, 1.0, 1.0 } means that no adjustment is made.
     * The hardware implementation may refuse certain values which make the display
     * unreadable, such as { 0, 0, 0 }. This calibration will be combined with other
     * internal adjustments, such as night mode, if necessary.
     *
     * @param array of { R, G, B } offsets
     * @return true if state was changed
     */
    public boolean setColorAdjustment(float[] adj) {
        try {
            return checkService() && sService.setColorAdjustment(adj);
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Gets the current picture adjustment settings (hue, saturation, intensity, contrast)
     *
     * @return HSIC object with current settings
     */
    public HSIC getPictureAdjustment() {
        try {
            if (checkService()) {
                return sService.getPictureAdjustment();
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * Sets a new picture adjustment
     *
     * @param hsic
     * @return true if success
     */
    public boolean setPictureAdjustment(final HSIC hsic) {
        try {
            return checkService() && sService.setPictureAdjustment(hsic);
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * Gets the default picture adjustment for the current display mode
     *
     * @return HSIC object with default values
     */
    public HSIC getDefaultPictureAdjustment() {
        try {
            if (checkService()) {
                return sService.getDefaultPictureAdjustment();
            }
        } catch (RemoteException e) {
        }
        return null;
    }
}