summaryrefslogtreecommitdiffstats
path: root/sdk/src/java/cyanogenmod/hardware/CMHardwareManager.java
blob: cc6a82a791b6a1e7d8dc76bc427dc48c7271b7e7 (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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/*
 * Copyright (C) 2015-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 cyanogenmod.app.CMContextConstants;
import cyanogenmod.hardware.HSIC;

import java.io.UnsupportedEncodingException;
import java.lang.IllegalArgumentException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;

/**
 * Manages access to CyanogenMod hardware extensions
 *
 *  <p>
 *  This manager requires the HARDWARE_ABSTRACTION_ACCESS permission.
 *  <p>
 *  To get the instance of this class, utilize CMHardwareManager#getInstance(Context context)
 */
public final class CMHardwareManager {
    private static final String TAG = "CMHardwareManager";

    private static ICMHardwareService sService;

    private Context mContext;
    /**
     * Adaptive backlight support (this refers to technologies like NVIDIA SmartDimmer,
     * QCOM CABL or Samsung CABC)
     */
    public static final int FEATURE_ADAPTIVE_BACKLIGHT = 0x1;

    /**
     * Color enhancement support
     */
    public static final int FEATURE_COLOR_ENHANCEMENT = 0x2;

    /**
     * Display RGB color calibration
     */
    public static final int FEATURE_DISPLAY_COLOR_CALIBRATION = 0x4;

    /**
     * Display gamma calibration
     */
    public static final int FEATURE_DISPLAY_GAMMA_CALIBRATION = 0x8;

    /**
     * High touch sensitivity for touch panels
     */
    public static final int FEATURE_HIGH_TOUCH_SENSITIVITY = 0x10;

    /**
     * Hardware navigation key disablement
     */
    public static final int FEATURE_KEY_DISABLE = 0x20;

    /**
     * Long term orbits (LTO)
     */
    public static final int FEATURE_LONG_TERM_ORBITS = 0x40;

    /**
     * Serial number other than ro.serialno
     */
    public static final int FEATURE_SERIAL_NUMBER = 0x80;

    /**
     * Increased display readability in bright light
     */
    public static final int FEATURE_SUNLIGHT_ENHANCEMENT = 0x100;

    /**
     * Double-tap the touch panel to wake up the device
     *
     * @deprecated This functionality is replaced by AOSP's implementation as of CM 13.0.
     */
    @Deprecated
    public static final int FEATURE_TAP_TO_WAKE = 0x200;

    /**
     * Variable vibrator intensity
     */
    public static final int FEATURE_VIBRATOR = 0x400;

    /**
     * Touchscreen hovering
     */
    public static final int FEATURE_TOUCH_HOVERING = 0x800;

    /**
     * Auto contrast
     */
    public static final int FEATURE_AUTO_CONTRAST = 0x1000;

    /**
     * Display modes
     */
    public static final int FEATURE_DISPLAY_MODES = 0x2000;

    /**
     * Persistent storage
     */
    public static final int FEATURE_PERSISTENT_STORAGE = 0x4000;

    /**
     * Thermal change monitor
     */
    public static final int FEATURE_THERMAL_MONITOR = 0x8000;

    /**
     * Unique device ID
     */
    public static final int FEATURE_UNIQUE_DEVICE_ID = 0x10000;

    /**
     * Color balance
     */
    public static final int FEATURE_COLOR_BALANCE = 0x20000;

    /**
     * HSIC picture adjustment
     */
    public static final int FEATURE_PICTURE_ADJUSTMENT = 0x40000;


    private static final List<Integer> BOOLEAN_FEATURES = Arrays.asList(
        FEATURE_ADAPTIVE_BACKLIGHT,
        FEATURE_COLOR_ENHANCEMENT,
        FEATURE_HIGH_TOUCH_SENSITIVITY,
        FEATURE_KEY_DISABLE,
        FEATURE_SUNLIGHT_ENHANCEMENT,
        FEATURE_TOUCH_HOVERING,
        FEATURE_AUTO_CONTRAST,
        FEATURE_THERMAL_MONITOR
    );

    private static CMHardwareManager sCMHardwareManagerInstance;

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

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

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

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

    /**
     * @return the supported features bitmask
     */
    public int getSupportedFeatures() {
        try {
            if (checkService()) {
                return sService.getSupportedFeatures();
            }
        } catch (RemoteException e) {
        }
        return 0;
    }

    /**
     * Determine if a CM Hardware feature is supported on this device
     *
     * @param feature The CM Hardware feature to query
     *
     * @return true if the feature is supported, false otherwise.
     */
    public boolean isSupported(int feature) {
        return feature == (getSupportedFeatures() & feature);
    }

    /**
     * Determine if the given feature is enabled or disabled.
     *
     * Only used for features which have simple enable/disable controls.
     *
     * @param feature the CM Hardware feature to query
     *
     * @return true if the feature is enabled, false otherwise.
     */
    public boolean get(int feature) {
        if (!BOOLEAN_FEATURES.contains(feature)) {
            throw new IllegalArgumentException(feature + " is not a boolean");
        }

        try {
            if (checkService()) {
                return sService.get(feature);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * Enable or disable the given feature
     *
     * Only used for features which have simple enable/disable controls.
     *
     * @param feature the CM Hardware feature to set
     * @param enable true to enable, false to disale
     *
     * @return true if the feature is enabled, false otherwise.
     */
    public boolean set(int feature, boolean enable) {
        if (!BOOLEAN_FEATURES.contains(feature)) {
            throw new IllegalArgumentException(feature + " is not a boolean");
        }

        try {
            if (checkService()) {
                return sService.set(feature, enable);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    private int getArrayValue(int[] arr, int idx, int defaultValue) {
        if (arr == null || arr.length <= idx) {
            return defaultValue;
        }

        return arr[idx];
    }

    /**
     * {@hide}
     */
    public static final int VIBRATOR_INTENSITY_INDEX = 0;
    /**
     * {@hide}
     */
    public static final int VIBRATOR_DEFAULT_INDEX = 1;
    /**
     * {@hide}
     */
    public static final int VIBRATOR_MIN_INDEX = 2;
    /**
     * {@hide}
     */
    public static final int VIBRATOR_MAX_INDEX = 3;
    /**
     * {@hide}
     */
    public static final int VIBRATOR_WARNING_INDEX = 4;

    private int[] getVibratorIntensityArray() {
        try {
            if (checkService()) {
                return sService.getVibratorIntensity();
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * @return The current vibrator intensity.
     */
    public int getVibratorIntensity() {
        return getArrayValue(getVibratorIntensityArray(), VIBRATOR_INTENSITY_INDEX, 0);
    }

    /**
     * @return The default vibrator intensity.
     */
    public int getVibratorDefaultIntensity() {
        return getArrayValue(getVibratorIntensityArray(), VIBRATOR_DEFAULT_INDEX, 0);
    }

    /**
     * @return The minimum vibrator intensity.
     */
    public int getVibratorMinIntensity() {
        return getArrayValue(getVibratorIntensityArray(), VIBRATOR_MIN_INDEX, 0);
    }

    /**
     * @return The maximum vibrator intensity.
     */
    public int getVibratorMaxIntensity() {
        return getArrayValue(getVibratorIntensityArray(), VIBRATOR_MAX_INDEX, 0);
    }

    /**
     * @return The warning threshold vibrator intensity.
     */
    public int getVibratorWarningIntensity() {
        return getArrayValue(getVibratorIntensityArray(), VIBRATOR_WARNING_INDEX, 0);
    }

    /**
     * Set the current vibrator intensity
     *
     * @param intensity the intensity to set, between {@link #getVibratorMinIntensity()} and
     * {@link #getVibratorMaxIntensity()} inclusive.
     *
     * @return true on success, false otherwise.
     */
    public boolean setVibratorIntensity(int intensity) {
        try {
            if (checkService()) {
                return sService.setVibratorIntensity(intensity);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * {@hide}
     */
    public static final int COLOR_CALIBRATION_RED_INDEX = 0;
    /**
     * {@hide}
     */
    public static final int COLOR_CALIBRATION_GREEN_INDEX = 1;
    /**
     * {@hide}
     */
    public static final int COLOR_CALIBRATION_BLUE_INDEX = 2;
    /**
     * {@hide}
     */
    public static final int COLOR_CALIBRATION_DEFAULT_INDEX = 3;
    /**
     * {@hide}
     */
    public static final int COLOR_CALIBRATION_MIN_INDEX = 4;
    /**
     * {@hide}
     */
    public static final int COLOR_CALIBRATION_MAX_INDEX = 5;

    private int[] getDisplayColorCalibrationArray() {
        try {
            if (checkService()) {
                return sService.getDisplayColorCalibration();
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * @return the current RGB calibration, where int[0] = R, int[1] = G, int[2] = B.
     */
    public int[] getDisplayColorCalibration() {
        int[] arr = getDisplayColorCalibrationArray();
        if (arr == null || arr.length < 3) {
            return null;
        }
        return Arrays.copyOf(arr, 3);
    }

    /**
     * @return the default value for all colors
     */
    public int getDisplayColorCalibrationDefault() {
        return getArrayValue(getDisplayColorCalibrationArray(), COLOR_CALIBRATION_DEFAULT_INDEX, 0);
    }

    /**
     * @return The minimum value for all colors
     */
    public int getDisplayColorCalibrationMin() {
        return getArrayValue(getDisplayColorCalibrationArray(), COLOR_CALIBRATION_MIN_INDEX, 0);
    }

    /**
     * @return The minimum value for all colors
     */
    public int getDisplayColorCalibrationMax() {
        return getArrayValue(getDisplayColorCalibrationArray(), COLOR_CALIBRATION_MAX_INDEX, 0);
    }

    /**
     * Set the display color calibration to the given rgb triplet
     *
     * @param rgb RGB color calibration.  Each value must be between
     * {@link #getDisplayColorCalibrationMin()} and {@link #getDisplayColorCalibrationMax()},
     * inclusive.
     *
     * @return true on success, false otherwise.
     */
    public boolean setDisplayColorCalibration(int[] rgb) {
        try {
            if (checkService()) {
                return sService.setDisplayColorCalibration(rgb);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * Write a string to persistent storage, which persists thru factory reset
     *
     * @param key String identifier for this item. Must not exceed 64 characters.
     * @param value The UTF-8 encoded string to store of at least 1 character. null deletes the key/value pair.
     * @return true on success
     */
    public boolean writePersistentString(String key, String value) {
        try {
            if (checkService()) {
                return sService.writePersistentBytes(key,
                        value == null ? null : value.getBytes("UTF-8"));
            }
        } catch (RemoteException e) {
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return false;
    }

    /**
     * Write an integer to persistent storage, which persists thru factory reset
     *
     * @param key String identifier for this item. Must not exceed 64 characters.
     * @param value The integer to store
     * @return true on success
     */
    public boolean writePersistentInt(String key, int value) {
        try {
            if (checkService()) {
                return sService.writePersistentBytes(key,
                        ByteBuffer.allocate(4).putInt(value).array());
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * Write a byte array to persistent storage, which persists thru factory reset
     *
     * @param key String identifier for this item. Must not exceed 64 characters.
     * @param value The byte array to store, must be 1-4096 bytes. null deletes the key/value pair.
     * @return true on success
     */
    public boolean writePersistentBytes(String key, byte[] value) {
        try {
            if (checkService()) {
                return sService.writePersistentBytes(key, value);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * Read a string from persistent storage
     *
     * @param key String identifier for this item. Must not exceed 64 characters.
     * @return the stored UTF-8 encoded string, null if not found
     */
    public String readPersistentString(String key) {
        try {
            if (checkService()) {
                byte[] bytes = sService.readPersistentBytes(key);
                if (bytes != null) {
                    return new String(bytes, "UTF-8");
                }
            }
        } catch (RemoteException e) {
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return null;
    }

    /**
     * Read an integer from persistent storage
     *
     * @param key String identifier for this item. Must not exceed 64 characters.
     * @return the stored integer, zero if not found
     */
    public int readPersistentInt(String key) {
        try {
            if (checkService()) {
                byte[] bytes = sService.readPersistentBytes(key);
                if (bytes != null) {
                    return ByteBuffer.wrap(bytes).getInt();
                }
            }
        } catch (RemoteException e) {
        }
        return 0;
    }

    /**
     * Read a byte array from persistent storage
     *
     * @param key String identifier for this item. Must not exceed 64 characters.
     * @return the stored byte array, null if not found
     */
    public byte[] readPersistentBytes(String key) {
        try {
            if (checkService()) {
                return sService.readPersistentBytes(key);
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /** Delete an object from persistent storage
     *
     * @param key String identifier for this item
     * @return true if an item was deleted
     */
    public boolean deletePersistentObject(String key) {
        try {
            if (checkService()) {
                return sService.writePersistentBytes(key, null);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * {@hide}
     */
    public static final int GAMMA_CALIBRATION_RED_INDEX = 0;
    /**
     * {@hide}
     */
    public static final int GAMMA_CALIBRATION_GREEN_INDEX = 1;
    /**
     * {@hide}
     */
    public static final int GAMMA_CALIBRATION_BLUE_INDEX = 2;
    /**
     * {@hide}
     */
    public static final int GAMMA_CALIBRATION_MIN_INDEX = 3;
    /**
     * {@hide}
     */
    public static final int GAMMA_CALIBRATION_MAX_INDEX = 4;

    private int[] getDisplayGammaCalibrationArray(int idx) {
        try {
            if (checkService()) {
                return sService.getDisplayGammaCalibration(idx);
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * @return the number of RGB controls the device supports
     */
    @Deprecated
    public int getNumGammaControls() {
        try {
            if (checkService()) {
                return sService.getNumGammaControls();
            }
        } catch (RemoteException e) {
        }
        return 0;
    }

    /**
     * @param idx the control to query
     *
     * @return the current RGB gamma calibration for the given control
     */
    @Deprecated
    public int[] getDisplayGammaCalibration(int idx) {
        int[] arr = getDisplayGammaCalibrationArray(idx);
        if (arr == null || arr.length < 3) {
            return null;
        }
        return Arrays.copyOf(arr, 3);
    }

    /**
     * @return the minimum value for all colors
     */
    @Deprecated
    public int getDisplayGammaCalibrationMin() {
        return getArrayValue(getDisplayGammaCalibrationArray(0), GAMMA_CALIBRATION_MIN_INDEX, 0);
    }

    /**
     * @return the maximum value for all colors
     */
    @Deprecated
    public int getDisplayGammaCalibrationMax() {
        return getArrayValue(getDisplayGammaCalibrationArray(0), GAMMA_CALIBRATION_MAX_INDEX, 0);
    }

    /**
     * Set the display gamma calibration for a specific control
     *
     * @param idx the control to set
     * @param rgb RGB color calibration.  Each value must be between
     * {@link #getDisplayGammaCalibrationMin()} and {@link #getDisplayGammaCalibrationMax()},
     * inclusive.
     *
     * @return true on success, false otherwise.
     */
    @Deprecated
    public boolean setDisplayGammaCalibration(int idx, int[] rgb) {
        try {
            if (checkService()) {
                return sService.setDisplayGammaCalibration(idx, rgb);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * @return the source location of LTO data, or null on failure
     */
    public String getLtoSource() {
        try {
            if (checkService()) {
                return sService.getLtoSource();
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * @return the destination location of LTO data, or null on failure
     */
    public String getLtoDestination() {
        try {
            if (checkService()) {
                return sService.getLtoDestination();
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * @return the interval, in milliseconds, to trigger LTO data download
     */
    public long getLtoDownloadInterval() {
        try {
            if (checkService()) {
                return sService.getLtoDownloadInterval();
            }
        } catch (RemoteException e) {
        }
        return 0;
    }

    /**
     * @return the serial number to display instead of ro.serialno, or null on failure
     */
    public String getSerialNumber() {
        try {
            if (checkService()) {
                return sService.getSerialNumber();
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * @return an id that's both unique and deterministic for the device
     */
    public String getUniqueDeviceId() {
        try {
            if (checkService()) {
                return sService.getUniqueDeviceId();
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * @return true if adaptive backlight should be enabled when sunlight enhancement
     * is enabled.
     */
    public boolean requireAdaptiveBacklightForSunlightEnhancement() {
        try {
            if (checkService()) {
                return sService.requireAdaptiveBacklightForSunlightEnhancement();
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * @return true if this implementation does it's own lux metering
     */
    public boolean isSunlightEnhancementSelfManaged() {
        try {
            if (checkService()) {
                return sService.isSunlightEnhancementSelfManaged();
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * @return a list of available display modes on the devices
     */
    public DisplayMode[] getDisplayModes() {
        try {
            if (checkService()) {
                return sService.getDisplayModes();
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * @return the currently active display mode
     */
    public DisplayMode getCurrentDisplayMode() {
        try {
            if (checkService()) {
                return sService.getCurrentDisplayMode();
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * @return the default display mode to be set on boot
     */
    public DisplayMode getDefaultDisplayMode() {
        try {
            if (checkService()) {
                return sService.getDefaultDisplayMode();
            }
        } catch (RemoteException e) {
        }
        return null;
    }

    /**
     * @return true if setting the mode was successful
     */
    public boolean setDisplayMode(DisplayMode mode, boolean makeDefault) {
        try {
            if (checkService()) {
                return sService.setDisplayMode(mode, makeDefault);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * @return the available range for color temperature adjustments
     */
    public Range<Integer> getColorBalanceRange() {
        int min = 0;
        int max = 0;
        try {
            if (checkService()) {
                min = sService.getColorBalanceMin();
                max = sService.getColorBalanceMax();
            }
        } catch (RemoteException e) {
        }
        return new Range<Integer>(min, max);
    }

    /**
     * @return the current color balance value
     */
    public int getColorBalance() {
        try {
            if (checkService()) {
                return sService.getColorBalance();
            }
        } catch (RemoteException e) {
        }
        return 0;
    }

    /**
     * Sets the desired color balance. Must fall within the range obtained from
     * getColorBalanceRange()
     *
     * @param value
     * @return true if success
     */
    public boolean setColorBalance(int value) {
        try {
            if (checkService()) {
                return sService.setColorBalance(value);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

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

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

    /**
     * Sets the desired hue/saturation/intensity/contrast
     *
     * @param hsic
     * @return true if success
     */
    public boolean setPictureAdjustment(final HSIC hsic) {
        try {
            if (checkService()) {
                return sService.setPictureAdjustment(hsic);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

    /**
     * Get a list of ranges valid for picture adjustment.
     *
     * @return range list
     */
    public List<Range<Float>> getPictureAdjustmentRanges() {
        try {
            if (checkService()) {
                float[] ranges = sService.getPictureAdjustmentRanges();
                if (ranges.length > 7) {
                    return Arrays.asList(new Range<Float>(ranges[0], ranges[1]),
                            new Range<Float>(ranges[2], ranges[3]),
                            new Range<Float>(ranges[4], ranges[5]),
                            new Range<Float>(ranges[6], ranges[7]),
                            (ranges.length > 9 ?
                                    new Range<Float>(ranges[8], ranges[9]) :
                                    new Range<Float>(0.0f, 0.0f)));
                }
            }
        } catch (RemoteException e) {
        }
        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;
    }

    /**
     * @return current thermal {@link cyanogenmod.hardware.ThermalListenerCallback.State}
     */
    public int getThermalState() {
        try {
            if (checkService()) {
                return sService.getThermalState();
            }
        } catch (RemoteException e) {
        }
        return ThermalListenerCallback.State.STATE_UNKNOWN;
    }

   /**
    * Register a callback to be notified of thermal state changes
    * @return boolean indicating whether register succeeded or failed
    */
    public boolean registerThermalListener(ThermalListenerCallback thermalCallback) {
        try {
            if (checkService()) {
                return sService.registerThermalListener(thermalCallback);
            }
        } catch (RemoteException e) {
        }
        return false;
    }

   /**
    * Unregister a callback previously registered to be notified of thermal state changes
    * @return boolean indicating whether un-registering succeeded or failed
    */
    public boolean unRegisterThermalListener(ThermalListenerCallback thermalCallback) {
        try {
            if (checkService()) {
                return sService.unRegisterThermalListener(thermalCallback);
            }
        } catch (RemoteException e) {
        }
        return false;
    }
}