Home | History | Annotate | Download | only in hardware
      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.RequiresFeature;
     20 import android.annotation.SystemService;
     21 import android.content.Context;
     22 import android.content.pm.PackageManager;
     23 import android.os.RemoteException;
     24 import android.os.ServiceManager;
     25 import android.os.ServiceManager.ServiceNotFoundException;
     26 import android.util.Log;
     27 
     28 /**
     29  * Class that operates consumer infrared on the device.
     30  */
     31 @SystemService(Context.CONSUMER_IR_SERVICE)
     32 @RequiresFeature(PackageManager.FEATURE_CONSUMER_IR)
     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      */
     42     public ConsumerIrManager(Context context) throws ServiceNotFoundException {
     43         mPackageName = context.getPackageName();
     44         mService = IConsumerIrService.Stub.asInterface(
     45                 ServiceManager.getServiceOrThrow(Context.CONSUMER_IR_SERVICE));
     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) {
     62             throw e.rethrowFromSystemServer();
     63         }
     64     }
     65 
     66     /**
     67      * Transmit an infrared pattern
     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) {
     86             throw e.rethrowFromSystemServer();
     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      *
    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.
    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) {
    151             throw e.rethrowFromSystemServer();
    152         }
    153     }
    154 }
    155