blob: 6f589cd9190ba3de560bfb9e06cec0e707ba713a [file] [log] [blame]
Erik Gilling51e95df2013-06-26 11:06:51 -07001/*
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
Jeff Sharkey98af2e42018-02-16 10:14:57 -070019import android.annotation.RequiresFeature;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060020import android.annotation.SystemService;
Erik Gilling51e95df2013-06-26 11:06:51 -070021import android.content.Context;
Jeff Sharkey98af2e42018-02-16 10:14:57 -070022import android.content.pm.PackageManager;
Erik Gilling51e95df2013-06-26 11:06:51 -070023import android.os.RemoteException;
24import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060025import android.os.ServiceManager.ServiceNotFoundException;
Erik Gilling51e95df2013-06-26 11:06:51 -070026import android.util.Log;
27
28/**
29 * Class that operates consumer infrared on the device.
Erik Gilling51e95df2013-06-26 11:06:51 -070030 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060031@SystemService(Context.CONSUMER_IR_SERVICE)
Jeff Sharkey98af2e42018-02-16 10:14:57 -070032@RequiresFeature(PackageManager.FEATURE_CONSUMER_IR)
Erik Gilling51e95df2013-06-26 11:06:51 -070033public final class ConsumerIrManager {
34 private static final String TAG = "ConsumerIr";
35
36 private final String mPackageName;
37 private final IConsumerIrService mService;
38
39 /**
40 * @hide to prevent subclassing from outside of the framework
41 */
Jeff Sharkey49ca5292016-05-10 12:54:45 -060042 public ConsumerIrManager(Context context) throws ServiceNotFoundException {
Erik Gilling51e95df2013-06-26 11:06:51 -070043 mPackageName = context.getPackageName();
44 mService = IConsumerIrService.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -060045 ServiceManager.getServiceOrThrow(Context.CONSUMER_IR_SERVICE));
Erik Gilling51e95df2013-06-26 11:06:51 -070046 }
47
48 /**
49 * Check whether the device has an infrared emitter.
50 *
51 * @return true if the device has an infrared emitter, else false.
52 */
53 public boolean hasIrEmitter() {
54 if (mService == null) {
55 Log.w(TAG, "no consumer ir service.");
56 return false;
57 }
58
59 try {
60 return mService.hasIrEmitter();
61 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -070062 throw e.rethrowFromSystemServer();
Erik Gilling51e95df2013-06-26 11:06:51 -070063 }
Erik Gilling51e95df2013-06-26 11:06:51 -070064 }
65
66 /**
Martin Storsjo991c1002014-01-24 11:57:59 +020067 * Transmit an infrared pattern
Erik Gilling51e95df2013-06-26 11:06:51 -070068 * <p>
69 * This method is synchronous; when it returns the pattern has
70 * been transmitted. Only patterns shorter than 2 seconds will
71 * be transmitted.
72 * </p>
73 *
74 * @param carrierFrequency The IR carrier frequency in Hertz.
75 * @param pattern The alternating on/off pattern in microseconds to transmit.
76 */
77 public void transmit(int carrierFrequency, int[] pattern) {
78 if (mService == null) {
79 Log.w(TAG, "failed to transmit; no consumer ir service.");
80 return;
81 }
82
83 try {
84 mService.transmit(mPackageName, carrierFrequency, pattern);
85 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -070086 throw e.rethrowFromSystemServer();
Erik Gilling51e95df2013-06-26 11:06:51 -070087 }
88 }
89
90 /**
91 * Represents a range of carrier frequencies (inclusive) on which the
92 * infrared transmitter can transmit
93 */
94 public final class CarrierFrequencyRange {
95 private final int mMinFrequency;
96 private final int mMaxFrequency;
97
98 /**
99 * Create a segment of a carrier frequency range.
100 *
101 * @param min The minimum transmittable frequency in this range segment.
102 * @param max The maximum transmittable frequency in this range segment.
103 */
104 public CarrierFrequencyRange(int min, int max) {
105 mMinFrequency = min;
106 mMaxFrequency = max;
107 }
108
109 /**
110 * Get the minimum (inclusive) frequency in this range segment.
111 */
112 public int getMinFrequency() {
113 return mMinFrequency;
114 }
115
116 /**
117 * Get the maximum (inclusive) frequency in this range segment.
118 */
119 public int getMaxFrequency() {
120 return mMaxFrequency;
121 }
122 };
123
124 /**
125 * Query the infrared transmitter's supported carrier frequencies
126 *
Alex Ray1e679442013-09-11 14:34:25 -0700127 * @return an array of
128 * {@link android.hardware.ConsumerIrManager.CarrierFrequencyRange}
129 * objects representing the ranges that the transmitter can support, or
130 * null if there was an error communicating with the Consumer IR Service.
Erik Gilling51e95df2013-06-26 11:06:51 -0700131 */
132 public CarrierFrequencyRange[] getCarrierFrequencies() {
133 if (mService == null) {
134 Log.w(TAG, "no consumer ir service.");
135 return null;
136 }
137
138 try {
139 int[] freqs = mService.getCarrierFrequencies();
140 if (freqs.length % 2 != 0) {
141 Log.w(TAG, "consumer ir service returned an uneven number of frequencies.");
142 return null;
143 }
144 CarrierFrequencyRange[] range = new CarrierFrequencyRange[freqs.length / 2];
145
146 for (int i = 0; i < freqs.length; i += 2) {
147 range[i / 2] = new CarrierFrequencyRange(freqs[i], freqs[i+1]);
148 }
149 return range;
150 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700151 throw e.rethrowFromSystemServer();
Erik Gilling51e95df2013-06-26 11:06:51 -0700152 }
Erik Gilling51e95df2013-06-26 11:06:51 -0700153 }
Erik Gilling51e95df2013-06-26 11:06:51 -0700154}