1 /*
2  * Copyright (C) 20013The 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 com.example.android.apis.hardware;
18 
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 
22 import android.app.Activity;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.hardware.ConsumerIrManager;
26 import android.view.View;
27 import android.widget.TextView;
28 import android.util.Log;
29 
30 import com.example.android.apis.R;
31 
32 /**
33  * App that transmit an IR code
34  *
35  * <p>This demonstrates the {@link android.hardware.ConsumerIrManager android.hardware.ConsumerIrManager} class.
36  *
37  * <h4>Demo</h4>
38  * Hardware / Consumer IR
39  *
40  * <h4>Source files</h4>
41  * <table class="LinkTable">
42  *         <tr>
43  *             <td>src/com.example.android.apis/hardware/ConsumerIr.java</td>
44  *             <td>Consumer IR demo</td>
45  *         </tr>
46  *         <tr>
47  *             <td>res/any/layout/consumer_ir.xml</td>
48  *             <td>Defines contents of the screen</td>
49  *         </tr>
50  * </table>
51  */
52 public class ConsumerIr extends Activity {
53     private static final String TAG = "ConsumerIrTest";
54     TextView mFreqsText;
55     ConsumerIrManager mCIR;
56 
57     /**
58      * Initialization of the Activity after it is first created.  Must at least
59      * call {@link android.app.Activity#setContentView setContentView()} to
60      * describe what is to be displayed in the screen.
61      */
62     @Override
onCreate(Bundle savedInstanceState)63     protected void onCreate(Bundle savedInstanceState) {
64         // Be sure to call the super class.
65         super.onCreate(savedInstanceState);
66 
67         // Get a reference to the ConsumerIrManager
68         mCIR = (ConsumerIrManager)getSystemService(Context.CONSUMER_IR_SERVICE);
69 
70         // See assets/res/any/layout/consumer_ir.xml for this
71         // view layout definition, which is being set here as
72         // the content of our screen.
73         setContentView(R.layout.consumer_ir);
74 
75         // Set the OnClickListener for the button so we see when it's pressed.
76         findViewById(R.id.send_button).setOnClickListener(mSendClickListener);
77         findViewById(R.id.get_freqs_button).setOnClickListener(mGetFreqsClickListener);
78         mFreqsText = (TextView) findViewById(R.id.freqs_text);
79     }
80 
81     View.OnClickListener mSendClickListener = new View.OnClickListener() {
82         public void onClick(View v) {
83             if (!mCIR.hasIrEmitter()) {
84                 Log.e(TAG, "No IR Emitter found\n");
85                 return;
86             }
87 
88             // A pattern of alternating series of carrier on and off periods measured in
89             // microseconds.
90             int[] pattern = {1901, 4453, 625, 1614, 625, 1588, 625, 1614, 625, 442, 625, 442, 625,
91                 468, 625, 442, 625, 494, 572, 1614, 625, 1588, 625, 1614, 625, 494, 572, 442, 651,
92                 442, 625, 442, 625, 442, 625, 1614, 625, 1588, 651, 1588, 625, 442, 625, 494, 598,
93                 442, 625, 442, 625, 520, 572, 442, 625, 442, 625, 442, 651, 1588, 625, 1614, 625,
94                 1588, 625, 1614, 625, 1588, 625, 48958};
95 
96             // transmit the pattern at 38.4KHz
97             mCIR.transmit(38400, pattern);
98         }
99     };
100 
101     View.OnClickListener mGetFreqsClickListener = new View.OnClickListener() {
102         public void onClick(View v) {
103             StringBuilder b = new StringBuilder();
104 
105             if (!mCIR.hasIrEmitter()) {
106                 mFreqsText.setText("No IR Emitter found!");
107                 Log.e(TAG, "No IR Emitter found!\n");
108                 return;
109             }
110 
111             // Get the available carrier frequency ranges
112             ConsumerIrManager.CarrierFrequencyRange[] freqs = mCIR.getCarrierFrequencies();
113             b.append("IR Carrier Frequencies:\n");
114             for (ConsumerIrManager.CarrierFrequencyRange range : freqs) {
115                 b.append(String.format("    %d - %d\n", range.getMinFrequency(),
116                             range.getMaxFrequency()));
117             }
118             mFreqsText.setText(b.toString());
119         }
120     };
121 }
122