Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | 98af2e4 | 2018-02-16 10:14:57 -0700 | [diff] [blame] | 19 | import android.annotation.RequiresFeature; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 20 | import android.annotation.SystemService; |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Jeff Sharkey | 98af2e4 | 2018-02-16 10:14:57 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager; |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 23 | import android.os.RemoteException; |
| 24 | import android.os.ServiceManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 25 | import android.os.ServiceManager.ServiceNotFoundException; |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | |
| 28 | /** |
| 29 | * Class that operates consumer infrared on the device. |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 30 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 31 | @SystemService(Context.CONSUMER_IR_SERVICE) |
Jeff Sharkey | 98af2e4 | 2018-02-16 10:14:57 -0700 | [diff] [blame] | 32 | @RequiresFeature(PackageManager.FEATURE_CONSUMER_IR) |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 33 | public 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 Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 42 | public ConsumerIrManager(Context context) throws ServiceNotFoundException { |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 43 | mPackageName = context.getPackageName(); |
| 44 | mService = IConsumerIrService.Stub.asInterface( |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 45 | ServiceManager.getServiceOrThrow(Context.CONSUMER_IR_SERVICE)); |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 46 | } |
| 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 Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 62 | throw e.rethrowFromSystemServer(); |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 63 | } |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
Martin Storsjo | 991c100 | 2014-01-24 11:57:59 +0200 | [diff] [blame] | 67 | * Transmit an infrared pattern |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 68 | * <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 Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 86 | throw e.rethrowFromSystemServer(); |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 87 | } |
| 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 Ray | 1e67944 | 2013-09-11 14:34:25 -0700 | [diff] [blame] | 127 | * @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 Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 131 | */ |
| 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 Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 151 | throw e.rethrowFromSystemServer(); |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 152 | } |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 153 | } |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 154 | } |