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