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 android.hardware;
18 
19 import android.annotation.SystemService;
20 import android.content.Context;
21 import android.os.RemoteException;
22 import android.os.ServiceManager;
23 import android.os.ServiceManager.ServiceNotFoundException;
24 import android.util.Log;
25 
26 /**
27  * Class that operates consumer infrared on the device.
28  */
29 @SystemService(Context.CONSUMER_IR_SERVICE)
30 public final class ConsumerIrManager {
31     private static final String TAG = "ConsumerIr";
32 
33     private final String mPackageName;
34     private final IConsumerIrService mService;
35 
36     /**
37      * @hide to prevent subclassing from outside of the framework
38      */
ConsumerIrManager(Context context)39     public ConsumerIrManager(Context context) throws ServiceNotFoundException {
40         mPackageName = context.getPackageName();
41         mService = IConsumerIrService.Stub.asInterface(
42                 ServiceManager.getServiceOrThrow(Context.CONSUMER_IR_SERVICE));
43     }
44 
45     /**
46      * Check whether the device has an infrared emitter.
47      *
48      * @return true if the device has an infrared emitter, else false.
49      */
hasIrEmitter()50     public boolean hasIrEmitter() {
51         if (mService == null) {
52             Log.w(TAG, "no consumer ir service.");
53             return false;
54         }
55 
56         try {
57             return mService.hasIrEmitter();
58         } catch (RemoteException e) {
59             throw e.rethrowFromSystemServer();
60         }
61     }
62 
63     /**
64      * Transmit an infrared pattern
65      * <p>
66      * This method is synchronous; when it returns the pattern has
67      * been transmitted. Only patterns shorter than 2 seconds will
68      * be transmitted.
69      * </p>
70      *
71      * @param carrierFrequency The IR carrier frequency in Hertz.
72      * @param pattern The alternating on/off pattern in microseconds to transmit.
73      */
transmit(int carrierFrequency, int[] pattern)74     public void transmit(int carrierFrequency, int[] pattern) {
75         if (mService == null) {
76             Log.w(TAG, "failed to transmit; no consumer ir service.");
77             return;
78         }
79 
80         try {
81             mService.transmit(mPackageName, carrierFrequency, pattern);
82         } catch (RemoteException e) {
83             throw e.rethrowFromSystemServer();
84         }
85     }
86 
87     /**
88      * Represents a range of carrier frequencies (inclusive) on which the
89      * infrared transmitter can transmit
90      */
91     public final class CarrierFrequencyRange {
92         private final int mMinFrequency;
93         private final int mMaxFrequency;
94 
95         /**
96          * Create a segment of a carrier frequency range.
97          *
98          * @param min The minimum transmittable frequency in this range segment.
99          * @param max The maximum transmittable frequency in this range segment.
100          */
CarrierFrequencyRange(int min, int max)101         public CarrierFrequencyRange(int min, int max) {
102             mMinFrequency = min;
103             mMaxFrequency = max;
104         }
105 
106         /**
107          * Get the minimum (inclusive) frequency in this range segment.
108          */
getMinFrequency()109         public int getMinFrequency() {
110             return mMinFrequency;
111         }
112 
113         /**
114          * Get the maximum (inclusive) frequency in this range segment.
115          */
getMaxFrequency()116         public int getMaxFrequency() {
117             return mMaxFrequency;
118         }
119     };
120 
121     /**
122      * Query the infrared transmitter's supported carrier frequencies
123      *
124      * @return an array of
125      * {@link android.hardware.ConsumerIrManager.CarrierFrequencyRange}
126      * objects representing the ranges that the transmitter can support, or
127      * null if there was an error communicating with the Consumer IR Service.
128      */
getCarrierFrequencies()129     public CarrierFrequencyRange[] getCarrierFrequencies() {
130         if (mService == null) {
131             Log.w(TAG, "no consumer ir service.");
132             return null;
133         }
134 
135         try {
136             int[] freqs = mService.getCarrierFrequencies();
137             if (freqs.length % 2 != 0) {
138                 Log.w(TAG, "consumer ir service returned an uneven number of frequencies.");
139                 return null;
140             }
141             CarrierFrequencyRange[] range = new CarrierFrequencyRange[freqs.length / 2];
142 
143             for (int i = 0; i < freqs.length; i += 2) {
144                 range[i / 2] = new CarrierFrequencyRange(freqs[i], freqs[i+1]);
145             }
146             return range;
147         } catch (RemoteException e) {
148             throw e.rethrowFromSystemServer();
149         }
150     }
151 }
152