Add Consumer IR ApiDemo

Change-Id: I4eb27b2ba6a46c27962eaa5e09d3e65939b64c0a
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index bbb01b4..898de27 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -32,6 +32,7 @@
     <uses-permission android:name="android.permission.SEND_SMS" />
     <uses-permission android:name="android.permission.RECEIVE_SMS" />
     <uses-permission android:name="android.permission.NFC" />
+    <uses-permission android:name="android.permission.TRANSMIT_IR" />
 
     <!-- For android.media.audiofx.Visualizer -->
     <uses-permission android:name="android.permission.RECORD_AUDIO" />
@@ -1200,6 +1201,17 @@
                   android:enabled="@bool/atLeastHoneycombMR2" />
 
         <!-- ************************************* -->
+        <!--     HARDWARE PACKAGE SAMPLES          -->
+        <!-- ************************************* -->
+
+        <activity android:name=".hardware.ConsumerIr" android:label="Hardware/Consumer IR">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
+        <!-- ************************************* -->
         <!--     OS PACKAGE SAMPLES                -->
         <!-- ************************************* -->
 
diff --git a/samples/ApiDemos/res/layout/consumer_ir.xml b/samples/ApiDemos/res/layout/consumer_ir.xml
new file mode 100644
index 0000000..bb60b53
--- /dev/null
+++ b/samples/ApiDemos/res/layout/consumer_ir.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2008 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- This activity exercises search invocation options -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <Button
+        android:id="@+id/send_button"
+        android:text="@string/ir_send"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"/>
+    <Button
+        android:id="@+id/get_freqs_button"
+        android:text="@string/ir_get_freqs"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"/>
+
+    <ScrollView
+        android:id="@+id/freqs_text_scroll"
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1" >
+
+        <TextView
+            android:id="@+id/freqs_text"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:paddingLeft="3dp"
+            android:paddingRight="3dp" />
+
+    </ScrollView>
+
+</LinearLayout>
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index d81f769..47445dd 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -460,6 +460,9 @@
 
     <string name="activity_install_apk">Content/Packages/Install Apk</string>
 
+    <string name="ir_send">Send IR</string>
+    <string name="ir_get_freqs">Get Carrier Frequencies</string>
+
     <!-- ============================== -->
     <!--  app/intents examples strings     -->
     <!-- ============================== -->
diff --git a/samples/ApiDemos/src/com/example/android/apis/hardware/ConsumerIr.java b/samples/ApiDemos/src/com/example/android/apis/hardware/ConsumerIr.java
new file mode 100644
index 0000000..c0ae960
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/hardware/ConsumerIr.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 20013The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.hardware;
+
+// Need the following import to get access to the app resources, since this
+// class is in a sub-package.
+
+import android.app.Activity;
+import android.content.Context;
+import android.os.Bundle;
+import android.hardware.ConsumerIrManager;
+import android.view.View;
+import android.widget.TextView;
+import android.util.Log;
+
+import com.example.android.apis.R;
+
+/**
+ * App that transmit an IR code
+ *
+ * <p>This demonstrates the {@link android.hardware.ConsumerIrManager android.hardware.ConsumerIrManager} class.
+ *
+ * <h4>Demo</h4>
+ * Hardware / Consumer IR
+ *
+ * <h4>Source files</h4>
+ * <table class="LinkTable">
+ *         <tr>
+ *             <td>src/com.example.android.apis/hardware/ConsumerIr.java</td>
+ *             <td>Consumer IR demo</td>
+ *         </tr>
+ *         <tr>
+ *             <td>res/any/layout/consumer_ir.xml</td>
+ *             <td>Defines contents of the screen</td>
+ *         </tr>
+ * </table>
+ */
+public class ConsumerIr extends Activity {
+    private static final String TAG = "ConsumerIrTest";
+    TextView mFreqsText;
+    ConsumerIrManager mCIR;
+
+    /**
+     * Initialization of the Activity after it is first created.  Must at least
+     * call {@link android.app.Activity#setContentView setContentView()} to
+     * describe what is to be displayed in the screen.
+     */
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        // Be sure to call the super class.
+        super.onCreate(savedInstanceState);
+
+        // Get a reference to the ConsumerIrManager
+        mCIR = (ConsumerIrManager)getSystemService(Context.CONSUMER_IR_SERVICE);
+
+        // See assets/res/any/layout/consumer_ir.xml for this
+        // view layout definition, which is being set here as
+        // the content of our screen.
+        setContentView(R.layout.consumer_ir);
+
+        // Set the OnClickListener for the button so we see when it's pressed.
+        findViewById(R.id.send_button).setOnClickListener(mSendClickListener);
+        findViewById(R.id.get_freqs_button).setOnClickListener(mGetFreqsClickListener);
+        mFreqsText = (TextView) findViewById(R.id.freqs_text);
+    }
+
+    View.OnClickListener mSendClickListener = new View.OnClickListener() {
+        public void onClick(View v) {
+            if (!mCIR.hasIrEmitter()) {
+                Log.e(TAG, "No IR Emitter found\n");
+                return;
+            }
+
+            // A pattern of alternating series of carrier on and off periods measured in
+            // microseconds.
+            int[] pattern = {1901, 4453, 625, 1614, 625, 1588, 625, 1614, 625, 442, 625, 442, 625,
+                468, 625, 442, 625, 494, 572, 1614, 625, 1588, 625, 1614, 625, 494, 572, 442, 651,
+                442, 625, 442, 625, 442, 625, 1614, 625, 1588, 651, 1588, 625, 442, 625, 494, 598,
+                442, 625, 442, 625, 520, 572, 442, 625, 442, 625, 442, 651, 1588, 625, 1614, 625,
+                1588, 625, 1614, 625, 1588, 625, 48958};
+
+            // transmit the pattern at 38.4KHz
+            mCIR.transmit(38400, pattern);
+        }
+    };
+
+    View.OnClickListener mGetFreqsClickListener = new View.OnClickListener() {
+        public void onClick(View v) {
+            StringBuilder b = new StringBuilder();
+
+            if (!mCIR.hasIrEmitter()) {
+                mFreqsText.setText("No IR Emitter found!");
+                Log.e(TAG, "No IR Emitter found!\n");
+                return;
+            }
+
+            // Get the available carrier frequency ranges
+            ConsumerIrManager.CarrierFrequencyRange[] freqs = mCIR.getCarrierFrequencies();
+            b.append("IR Carrier Frequencies:\n");
+            for (ConsumerIrManager.CarrierFrequencyRange range : freqs) {
+                b.append(String.format("    %d - %d\n", range.getMinFrequency(),
+                            range.getMaxFrequency()));
+            }
+            mFreqsText.setText(b.toString());
+        }
+    };
+}