1 /*
2  * Copyright (C) 2014 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 com.android.server.hdmi;
18 
19 import android.view.KeyEvent;
20 
21 import libcore.util.EmptyArray;
22 
23 import java.util.Arrays;
24 
25 /**
26  * Helper class to translate android keycode to hdmi cec keycode and vice versa.
27  */
28 final class HdmiCecKeycode {
29     public static final int UNSUPPORTED_KEYCODE = -1;
30     public static final int NO_PARAM = -1;
31 
32     // =========================================================================
33     // Hdmi CEC keycodes
34     public static final int CEC_KEYCODE_SELECT = 0x00;
35     public static final int CEC_KEYCODE_UP = 0x01;
36     public static final int CEC_KEYCODE_DOWN = 0x02;
37     public static final int CEC_KEYCODE_LEFT = 0x03;
38     public static final int CEC_KEYCODE_RIGHT = 0x04;
39     public static final int CEC_KEYCODE_RIGHT_UP = 0x05;
40     public static final int CEC_KEYCODE_RIGHT_DOWN = 0x06;
41     public static final int CEC_KEYCODE_LEFT_UP = 0x07;
42     public static final int CEC_KEYCODE_LEFT_DOWN = 0x08;
43     public static final int CEC_KEYCODE_ROOT_MENU = 0x09;
44     public static final int CEC_KEYCODE_SETUP_MENU = 0x0A;
45     public static final int CEC_KEYCODE_CONTENTS_MENU = 0x0B;
46     public static final int CEC_KEYCODE_FAVORITE_MENU = 0x0C;
47     public static final int CEC_KEYCODE_EXIT = 0x0D;
48     // RESERVED = 0x0E - 0x0F
49     public static final int CEC_KEYCODE_MEDIA_TOP_MENU = 0x10;
50     public static final int CEC_KEYCODE_MEDIA_CONTEXT_SENSITIVE_MENU = 0x11;
51     // RESERVED = 0x12 – 0x1C
52     public static final int CEC_KEYCODE_NUMBER_ENTRY_MODE = 0x1D;
53     public static final int CEC_KEYCODE_NUMBER_11 = 0x1E;
54     public static final int CEC_KEYCODE_NUMBER_12 = 0x1F;
55     public static final int CEC_KEYCODE_NUMBER_0_OR_NUMBER_10 = 0x20;
56     public static final int CEC_KEYCODE_NUMBERS_1 = 0x21;
57     public static final int CEC_KEYCODE_NUMBERS_2 = 0x22;
58     public static final int CEC_KEYCODE_NUMBERS_3 = 0x23;
59     public static final int CEC_KEYCODE_NUMBERS_4 = 0x24;
60     public static final int CEC_KEYCODE_NUMBERS_5 = 0x25;
61     public static final int CEC_KEYCODE_NUMBERS_6 = 0x26;
62     public static final int CEC_KEYCODE_NUMBERS_7 = 0x27;
63     public static final int CEC_KEYCODE_NUMBERS_8 = 0x28;
64     public static final int CEC_KEYCODE_NUMBERS_9 = 0x29;
65     public static final int CEC_KEYCODE_DOT = 0x2A;
66     public static final int CEC_KEYCODE_ENTER = 0x2B;
67     public static final int CEC_KEYCODE_CLEAR = 0x2C;
68     // RESERVED = 0x2D - 0x2E
69     public static final int CEC_KEYCODE_NEXT_FAVORITE = 0x2F;
70     public static final int CEC_KEYCODE_CHANNEL_UP = 0x30;
71     public static final int CEC_KEYCODE_CHANNEL_DOWN = 0x31;
72     public static final int CEC_KEYCODE_PREVIOUS_CHANNEL = 0x32;
73     public static final int CEC_KEYCODE_SOUND_SELECT = 0x33;
74     public static final int CEC_KEYCODE_INPUT_SELECT = 0x34;
75     public static final int CEC_KEYCODE_DISPLAY_INFORMATION = 0x35;
76     public static final int CEC_KEYCODE_HELP = 0x36;
77     public static final int CEC_KEYCODE_PAGE_UP = 0x37;
78     public static final int CEC_KEYCODE_PAGE_DOWN = 0x38;
79     // RESERVED = 0x39 - 0x3F
80     public static final int CEC_KEYCODE_POWER = 0x40;
81     public static final int CEC_KEYCODE_VOLUME_UP = 0x41;
82     public static final int CEC_KEYCODE_VOLUME_DOWN = 0x42;
83     public static final int CEC_KEYCODE_MUTE = 0x43;
84     public static final int CEC_KEYCODE_PLAY = 0x44;
85     public static final int CEC_KEYCODE_STOP = 0x45;
86     public static final int CEC_KEYCODE_PAUSE = 0x46;
87     public static final int CEC_KEYCODE_RECORD = 0x47;
88     public static final int CEC_KEYCODE_REWIND = 0x48;
89     public static final int CEC_KEYCODE_FAST_FORWARD = 0x49;
90     public static final int CEC_KEYCODE_EJECT = 0x4A;
91     public static final int CEC_KEYCODE_FORWARD = 0x4B;
92     public static final int CEC_KEYCODE_BACKWARD = 0x4C;
93     public static final int CEC_KEYCODE_STOP_RECORD = 0x4D;
94     public static final int CEC_KEYCODE_PAUSE_RECORD = 0x4E;
95     public static final int CEC_KEYCODE_RESERVED = 0x4F;
96     public static final int CEC_KEYCODE_ANGLE = 0x50;
97     public static final int CEC_KEYCODE_SUB_PICTURE = 0x51;
98     public static final int CEC_KEYCODE_VIDEO_ON_DEMAND = 0x52;
99     public static final int CEC_KEYCODE_ELECTRONIC_PROGRAM_GUIDE = 0x53;
100     public static final int CEC_KEYCODE_TIMER_PROGRAMMING = 0x54;
101     public static final int CEC_KEYCODE_INITIAL_CONFIGURATION = 0x55;
102     public static final int CEC_KEYCODE_SELECT_BROADCAST_TYPE = 0x56;
103     public static final int CEC_KEYCODE_SELECT_SOUND_PRESENTATION = 0x57;
104     // RESERVED = 0x58-0x5F
105     public static final int CEC_KEYCODE_PLAY_FUNCTION = 0x60;
106     public static final int CEC_KEYCODE_PAUSE_PLAY_FUNCTION = 0x61;
107     public static final int CEC_KEYCODE_RECORD_FUNCTION = 0x62;
108     public static final int CEC_KEYCODE_PAUSE_RECORD_FUNCTION = 0x63;
109     public static final int CEC_KEYCODE_STOP_FUNCTION = 0x64;
110     public static final int CEC_KEYCODE_MUTE_FUNCTION = 0x65;
111     public static final int CEC_KEYCODE_RESTORE_VOLUME_FUNCTION = 0x66;
112     public static final int CEC_KEYCODE_TUNE_FUNCTION = 0x67;
113     public static final int CEC_KEYCODE_SELECT_MEDIA_FUNCTION = 0x68;
114     public static final int CEC_KEYCODE_SELECT_AV_INPUT_FUNCTION = 0x69;
115     public static final int CEC_KEYCODE_SELECT_AUDIO_INPUT_FUNCTION = 0x6A;
116     public static final int CEC_KEYCODE_POWER_TOGGLE_FUNCTION = 0x6B;
117     public static final int CEC_KEYCODE_POWER_OFF_FUNCTION = 0x6C;
118     public static final int CEC_KEYCODE_POWER_ON_FUNCTION = 0x6D;
119     // RESERVED = 0x6E-0x70
120     public static final int CEC_KEYCODE_F1_BLUE = 0x71;
121     public static final int CEC_KEYCODE_F2_RED = 0x72;
122     public static final int CEC_KEYCODE_F3_GREEN = 0x73;
123     public static final int CEC_KEYCODE_F4_YELLOW = 0x74;
124     public static final int CEC_KEYCODE_F5 = 0x75;
125     public static final int CEC_KEYCODE_DATA = 0x76;
126     // RESERVED = 0x77-0xFF
127 
128     // =========================================================================
129     // UI Broadcast Type
130     public static final int UI_BROADCAST_TOGGLE_ALL = 0x00;
131     public static final int UI_BROADCAST_TOGGLE_ANALOGUE_DIGITAL = 0x01;
132     public static final int UI_BROADCAST_ANALOGUE = 0x10;
133     public static final int UI_BROADCAST_ANALOGUE_TERRESTRIAL = 0x20;
134     public static final int UI_BROADCAST_ANALOGUE_CABLE = 0x30;
135     public static final int UI_BROADCAST_ANALOGUE_SATELLITE = 0x40;
136     public static final int UI_BROADCAST_DIGITAL = 0x50;
137     public static final int UI_BROADCAST_DIGITAL_TERRESTRIAL = 0x60;
138     public static final int UI_BROADCAST_DIGITAL_CABLE = 0x70;
139     public static final int UI_BROADCAST_DIGITAL_SATELLITE = 0x80;
140     public static final int UI_BROADCAST_DIGITAL_COMMNICATIONS_SATELLITE = 0x90;
141     public static final int UI_BROADCAST_DIGITAL_COMMNICATIONS_SATELLITE_2 = 0x91;
142     public static final int UI_BROADCAST_IP = 0xA0;
143 
144     // =========================================================================
145     // UI Sound Presentation Control
146     public static final int UI_SOUND_PRESENTATION_SOUND_MIX_DUAL_MONO = 0x20;
147     public static final int UI_SOUND_PRESENTATION_SOUND_MIX_KARAOKE = 0x30;
148     public static final int UI_SOUND_PRESENTATION_SELECT_AUDIO_DOWN_MIX = 0x80;
149     public static final int UI_SOUND_PRESENTATION_SELECT_AUDIO_AUTO_REVERBERATION = 0x90;
150     public static final int UI_SOUND_PRESENTATION_SELECT_AUDIO_AUTO_EQUALIZER = 0xA0;
151     public static final int UI_SOUND_PRESENTATION_BASS_STEP_PLUS = 0xB1;
152     public static final int UI_SOUND_PRESENTATION_BASS_NEUTRAL = 0xB2;
153     public static final int UI_SOUND_PRESENTATION_BASS_STEP_MINUS = 0xB3;
154     public static final int UI_SOUND_PRESENTATION_TREBLE_STEP_PLUS = 0xC1;
155     public static final int UI_SOUND_PRESENTATION_TREBLE_NEUTRAL = 0xC2;
156     public static final int UI_SOUND_PRESENTATION_TREBLE_STEP_MINUS = 0xC3;
157 
HdmiCecKeycode()158     private HdmiCecKeycode() {
159     }
160 
161     /**
162      * A mapping between Android and CEC keycode.
163      * <p>
164      * Normal implementation of this looks like
165      *
166      * <pre>
167      * new KeycodeEntry(KeyEvent.KEYCODE_DPAD_CENTER, CEC_KEYCODE_SELECT);
168      * </pre>
169      * <p>
170      * However, some keys in CEC requires additional parameter. In order to use parameterized cec
171      * key, add unique android keycode (existing or custom) corresponding to a pair of cec keycode
172      * and and its param.
173      *
174      * <pre>
175      * new KeycodeEntry(CUSTOME_ANDORID_KEY_1, CEC_KEYCODE_SELECT_BROADCAST_TYPE,
176      *         UI_BROADCAST_TOGGLE_ALL);
177      * new KeycodeEntry(CUSTOME_ANDORID_KEY_2, CEC_KEYCODE_SELECT_BROADCAST_TYPE,
178      *         UI_BROADCAST_ANALOGUE);
179      * </pre>
180      */
181     private static class KeycodeEntry {
182         private final int mAndroidKeycode;
183         private final boolean mIsRepeatable;
184         private final byte[] mCecKeycodeAndParams;
185 
KeycodeEntry(int androidKeycode, int cecKeycode, boolean isRepeatable, byte[] cecParams)186         private KeycodeEntry(int androidKeycode, int cecKeycode, boolean isRepeatable,
187                 byte[] cecParams) {
188             mAndroidKeycode = androidKeycode;
189             mIsRepeatable = isRepeatable;
190             mCecKeycodeAndParams = new byte[cecParams.length + 1];
191             System.arraycopy(cecParams, 0, mCecKeycodeAndParams, 1, cecParams.length);
192             mCecKeycodeAndParams[0] = (byte) (cecKeycode & 0xFF);
193         }
194 
KeycodeEntry(int androidKeycode, int cecKeycode, boolean isRepeatable)195         private KeycodeEntry(int androidKeycode, int cecKeycode, boolean isRepeatable) {
196             this(androidKeycode, cecKeycode, isRepeatable, EmptyArray.BYTE);
197         }
198 
KeycodeEntry(int androidKeycode, int cecKeycode, byte[] cecParams)199         private KeycodeEntry(int androidKeycode, int cecKeycode, byte[] cecParams) {
200             this(androidKeycode, cecKeycode, true, cecParams);
201         }
202 
KeycodeEntry(int androidKeycode, int cecKeycode)203         private KeycodeEntry(int androidKeycode, int cecKeycode) {
204             this(androidKeycode, cecKeycode, true, EmptyArray.BYTE);
205         }
206 
toCecKeycodeAndParamIfMatched(int androidKeycode)207         private byte[] toCecKeycodeAndParamIfMatched(int androidKeycode) {
208             if (mAndroidKeycode == androidKeycode) {
209                 return mCecKeycodeAndParams;
210             } else {
211                 return null;
212             }
213         }
214 
toAndroidKeycodeIfMatched(byte[] cecKeycodeAndParams)215         private int toAndroidKeycodeIfMatched(byte[] cecKeycodeAndParams) {
216             if (cecKeycodeAndParams.length < mCecKeycodeAndParams.length) {
217                 return UNSUPPORTED_KEYCODE;
218             }
219             byte[] trimmedCecKeycodeAndParams = new byte[mCecKeycodeAndParams.length];
220             System.arraycopy(cecKeycodeAndParams, 0, trimmedCecKeycodeAndParams, 0,
221                     mCecKeycodeAndParams.length);
222             if (Arrays.equals(mCecKeycodeAndParams, trimmedCecKeycodeAndParams)) {
223                 return mAndroidKeycode;
224             } else {
225                 return UNSUPPORTED_KEYCODE;
226             }
227         }
228 
isRepeatableIfMatched(int androidKeycode)229         private Boolean isRepeatableIfMatched(int androidKeycode) {
230             if (mAndroidKeycode == androidKeycode) {
231                 return mIsRepeatable;
232             } else {
233                 return null;
234             }
235         }
236     }
237 
intToSingleByteArray(int value)238     private static byte[] intToSingleByteArray(int value) {
239         return new byte[] {
240                 (byte) (value & 0xFF) };
241     }
242 
243     // Keycode entry container for all mappings.
244     // Note that order of entry is the same as above cec keycode definition.
245     private static final KeycodeEntry[] KEYCODE_ENTRIES = new KeycodeEntry[] {
246             new KeycodeEntry(KeyEvent.KEYCODE_DPAD_CENTER, CEC_KEYCODE_SELECT),
247             new KeycodeEntry(KeyEvent.KEYCODE_DPAD_UP, CEC_KEYCODE_UP),
248             new KeycodeEntry(KeyEvent.KEYCODE_DPAD_DOWN, CEC_KEYCODE_DOWN),
249             new KeycodeEntry(KeyEvent.KEYCODE_DPAD_LEFT, CEC_KEYCODE_LEFT),
250             new KeycodeEntry(KeyEvent.KEYCODE_DPAD_RIGHT, CEC_KEYCODE_RIGHT),
251             // No Android keycode defined for CEC_KEYCODE_RIGHT_UP
252             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_RIGHT_UP),
253             // No Android keycode defined for CEC_KEYCODE_RIGHT_DOWN
254             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_RIGHT_DOWN),
255             // No Android keycode defined for CEC_KEYCODE_LEFT_UP
256             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_LEFT_UP),
257             // No Android keycode defined for CEC_KEYCODE_LEFT_DOWN
258             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_LEFT_DOWN),
259             // Both KEYCODE_HOME and KEYCODE_MENU keys are sent as CEC_KEYCODE_ROOT_MENU
260             // NOTE that the HOME key is not usually forwarded.
261             // When CEC_KEYCODE_ROOT_MENU is received, it is translated to KEYCODE_HOME
262             new KeycodeEntry(KeyEvent.KEYCODE_HOME, CEC_KEYCODE_ROOT_MENU),
263             new KeycodeEntry(KeyEvent.KEYCODE_MENU, CEC_KEYCODE_ROOT_MENU),
264             new KeycodeEntry(KeyEvent.KEYCODE_SETTINGS, CEC_KEYCODE_SETUP_MENU),
265             new KeycodeEntry(KeyEvent.KEYCODE_TV_CONTENTS_MENU, CEC_KEYCODE_CONTENTS_MENU, false),
266             // No Android keycode defined for CEC_KEYCODE_FAVORITE_MENU
267             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_FAVORITE_MENU),
268             // Note that both BACK and ESCAPE are mapped to EXIT of CEC keycode.
269             // This would be problematic when translates CEC keycode to Android keycode.
270             // In current implementation, we pick BACK as mapping of EXIT key.
271             // If you'd like to map CEC EXIT to Android EXIT key, change order of
272             // the following two definition.
273             new KeycodeEntry(KeyEvent.KEYCODE_BACK, CEC_KEYCODE_EXIT),
274             new KeycodeEntry(KeyEvent.KEYCODE_ESCAPE, CEC_KEYCODE_EXIT),
275             // RESERVED
276             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_TOP_MENU, CEC_KEYCODE_MEDIA_TOP_MENU),
277             new KeycodeEntry(KeyEvent.KEYCODE_TV_MEDIA_CONTEXT_MENU,
278                     CEC_KEYCODE_MEDIA_CONTEXT_SENSITIVE_MENU),
279             // RESERVED
280             // No Android keycode defined for CEC_KEYCODE_NUMBER_ENTRY_MODE
281             new KeycodeEntry(KeyEvent.KEYCODE_TV_NUMBER_ENTRY, CEC_KEYCODE_NUMBER_ENTRY_MODE),
282             new KeycodeEntry(KeyEvent.KEYCODE_11, CEC_KEYCODE_NUMBER_11),
283             new KeycodeEntry(KeyEvent.KEYCODE_12, CEC_KEYCODE_NUMBER_12),
284             new KeycodeEntry(KeyEvent.KEYCODE_0, CEC_KEYCODE_NUMBER_0_OR_NUMBER_10),
285             new KeycodeEntry(KeyEvent.KEYCODE_1, CEC_KEYCODE_NUMBERS_1),
286             new KeycodeEntry(KeyEvent.KEYCODE_2, CEC_KEYCODE_NUMBERS_2),
287             new KeycodeEntry(KeyEvent.KEYCODE_3, CEC_KEYCODE_NUMBERS_3),
288             new KeycodeEntry(KeyEvent.KEYCODE_4, CEC_KEYCODE_NUMBERS_4),
289             new KeycodeEntry(KeyEvent.KEYCODE_5, CEC_KEYCODE_NUMBERS_5),
290             new KeycodeEntry(KeyEvent.KEYCODE_6, CEC_KEYCODE_NUMBERS_6),
291             new KeycodeEntry(KeyEvent.KEYCODE_7, CEC_KEYCODE_NUMBERS_7),
292             new KeycodeEntry(KeyEvent.KEYCODE_8, CEC_KEYCODE_NUMBERS_8),
293             new KeycodeEntry(KeyEvent.KEYCODE_9, CEC_KEYCODE_NUMBERS_9),
294             new KeycodeEntry(KeyEvent.KEYCODE_PERIOD, CEC_KEYCODE_DOT),
295             new KeycodeEntry(KeyEvent.KEYCODE_NUMPAD_ENTER, CEC_KEYCODE_ENTER),
296             new KeycodeEntry(KeyEvent.KEYCODE_CLEAR, CEC_KEYCODE_CLEAR),
297             // RESERVED
298             // No Android keycode defined for CEC_KEYCODE_NEXT_FAVORITE
299             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_NEXT_FAVORITE),
300             new KeycodeEntry(KeyEvent.KEYCODE_CHANNEL_UP, CEC_KEYCODE_CHANNEL_UP),
301             new KeycodeEntry(KeyEvent.KEYCODE_CHANNEL_DOWN, CEC_KEYCODE_CHANNEL_DOWN),
302             new KeycodeEntry(KeyEvent.KEYCODE_LAST_CHANNEL, CEC_KEYCODE_PREVIOUS_CHANNEL),
303             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK, CEC_KEYCODE_SOUND_SELECT),
304             new KeycodeEntry(KeyEvent.KEYCODE_TV_INPUT, CEC_KEYCODE_INPUT_SELECT),
305             new KeycodeEntry(KeyEvent.KEYCODE_INFO, CEC_KEYCODE_DISPLAY_INFORMATION),
306             // No Android keycode defined for CEC_KEYCODE_HELP
307             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_HELP),
308             new KeycodeEntry(KeyEvent.KEYCODE_PAGE_UP, CEC_KEYCODE_PAGE_UP),
309             new KeycodeEntry(KeyEvent.KEYCODE_PAGE_DOWN, CEC_KEYCODE_PAGE_DOWN),
310             // RESERVED
311             new KeycodeEntry(KeyEvent.KEYCODE_POWER, CEC_KEYCODE_POWER, false),
312             new KeycodeEntry(KeyEvent.KEYCODE_VOLUME_UP, CEC_KEYCODE_VOLUME_UP),
313             new KeycodeEntry(KeyEvent.KEYCODE_VOLUME_DOWN, CEC_KEYCODE_VOLUME_DOWN),
314             new KeycodeEntry(KeyEvent.KEYCODE_VOLUME_MUTE, CEC_KEYCODE_MUTE, false),
315             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_PLAY, CEC_KEYCODE_PLAY),
316             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_STOP, CEC_KEYCODE_STOP),
317             // Note that we map both MEDIA_PAUSE and MEDIA_PLAY_PAUSE to CEC PAUSE key.
318             // When it translates CEC PAUSE key, it picks Android MEDIA_PAUSE key as a mapping of
319             // it. If you'd like to choose MEDIA_PLAY_PAUSE, please change order of the following
320             // two lines.
321             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_PAUSE, CEC_KEYCODE_PAUSE),
322             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, CEC_KEYCODE_PAUSE),
323             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_RECORD, CEC_KEYCODE_RECORD),
324             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_REWIND, CEC_KEYCODE_REWIND),
325             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, CEC_KEYCODE_FAST_FORWARD),
326             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_EJECT, CEC_KEYCODE_EJECT),
327             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_NEXT, CEC_KEYCODE_FORWARD),
328             new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_PREVIOUS, CEC_KEYCODE_BACKWARD),
329             // No Android keycode defined for CEC_KEYCODE_STOP_RECORD
330             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_STOP_RECORD),
331             // No Android keycode defined for CEC_KEYCODE_PAUSE_RECORD
332             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_PAUSE_RECORD),
333             // No Android keycode defined for CEC_KEYCODE_RESERVED
334             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_RESERVED),
335             // No Android keycode defined for CEC_KEYCODE_ANGLE
336             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_ANGLE),
337             new KeycodeEntry(KeyEvent.KEYCODE_CAPTIONS, CEC_KEYCODE_SUB_PICTURE),
338             // No Android keycode defined for CEC_KEYCODE_VIDEO_ON_DEMAND
339             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_VIDEO_ON_DEMAND),
340             new KeycodeEntry(KeyEvent.KEYCODE_GUIDE, CEC_KEYCODE_ELECTRONIC_PROGRAM_GUIDE),
341             new KeycodeEntry(KeyEvent.KEYCODE_TV_TIMER_PROGRAMMING, CEC_KEYCODE_TIMER_PROGRAMMING),
342             // No Android keycode defined for CEC_KEYCODE_INITIAL_CONFIGURATION
343             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_INITIAL_CONFIGURATION),
344             // No Android keycode defined for CEC_KEYCODE_SELECT_BROADCAST_TYPE
345             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_SELECT_BROADCAST_TYPE),
346             new KeycodeEntry(KeyEvent.KEYCODE_TV_TERRESTRIAL_ANALOG,
347                     CEC_KEYCODE_SELECT_BROADCAST_TYPE, true,
348                     intToSingleByteArray(UI_BROADCAST_ANALOGUE)),
349             new KeycodeEntry(KeyEvent.KEYCODE_TV_TERRESTRIAL_DIGITAL,
350                     CEC_KEYCODE_SELECT_BROADCAST_TYPE, true,
351                     intToSingleByteArray(UI_BROADCAST_DIGITAL_TERRESTRIAL)),
352             new KeycodeEntry(KeyEvent.KEYCODE_TV_SATELLITE_BS,
353                     CEC_KEYCODE_SELECT_BROADCAST_TYPE, true,
354                     intToSingleByteArray(UI_BROADCAST_DIGITAL_SATELLITE)),
355             new KeycodeEntry(KeyEvent.KEYCODE_TV_SATELLITE_CS,
356                     CEC_KEYCODE_SELECT_BROADCAST_TYPE, true,
357                     intToSingleByteArray(UI_BROADCAST_DIGITAL_COMMNICATIONS_SATELLITE)),
358             new KeycodeEntry(KeyEvent.KEYCODE_TV_NETWORK,
359                     CEC_KEYCODE_SELECT_BROADCAST_TYPE, true,
360                     intToSingleByteArray(UI_BROADCAST_TOGGLE_ANALOGUE_DIGITAL)),
361             // No Android keycode defined for CEC_KEYCODE_SELECT_SOUND_PRESENTATION
362             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_SELECT_SOUND_PRESENTATION),
363             // RESERVED
364             // The following deterministic key definitions do not need key mapping
365             // since they are supposed to be generated programmatically only.
366             // No Android keycode defined for CEC_KEYCODE_PLAY_FUNCTION
367             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_PLAY_FUNCTION, false),
368             // No Android keycode defined for CEC_KEYCODE_PAUSE_PLAY_FUNCTION
369             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_PAUSE_PLAY_FUNCTION, false),
370             // No Android keycode defined for CEC_KEYCODE_RECORD_FUNCTION
371             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_RECORD_FUNCTION, false),
372             // No Android keycode defined for CEC_KEYCODE_PAUSE_RECORD_FUNCTION
373             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_PAUSE_RECORD_FUNCTION, false),
374             // No Android keycode defined for CEC_KEYCODE_STOP_FUNCTION
375             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_STOP_FUNCTION, false),
376             // No Android keycode defined for CEC_KEYCODE_MUTE_FUNCTION
377             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_MUTE_FUNCTION, false),
378             // No Android keycode defined for CEC_KEYCODE_RESTORE_VOLUME_FUNCTION
379             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_RESTORE_VOLUME_FUNCTION, false),
380             // No Android keycode defined for CEC_KEYCODE_TUNE_FUNCTION
381             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_TUNE_FUNCTION, false),
382             // No Android keycode defined for CEC_KEYCODE_SELECT_MEDIA_FUNCTION
383             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_SELECT_MEDIA_FUNCTION, false),
384             // No Android keycode defined for CEC_KEYCODE_SELECT_AV_INPUT_FUNCTION
385             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_SELECT_AV_INPUT_FUNCTION, false),
386             // No Android keycode defined for CEC_KEYCODE_SELECT_AUDIO_INPUT_FUNCTION
387             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_SELECT_AUDIO_INPUT_FUNCTION, false),
388             // No Android keycode defined for CEC_KEYCODE_POWER_TOGGLE_FUNCTION
389             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_POWER_TOGGLE_FUNCTION, false),
390             // No Android keycode defined for CEC_KEYCODE_POWER_OFF_FUNCTION
391             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_POWER_OFF_FUNCTION, false),
392             // No Android keycode defined for CEC_KEYCODE_POWER_ON_FUNCTION
393             new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_POWER_ON_FUNCTION, false),
394             // RESERVED
395             new KeycodeEntry(KeyEvent.KEYCODE_PROG_BLUE, CEC_KEYCODE_F1_BLUE),
396             new KeycodeEntry(KeyEvent.KEYCODE_PROG_RED, CEC_KEYCODE_F2_RED),
397             new KeycodeEntry(KeyEvent.KEYCODE_PROG_GREEN, CEC_KEYCODE_F3_GREEN),
398             new KeycodeEntry(KeyEvent.KEYCODE_PROG_YELLOW, CEC_KEYCODE_F4_YELLOW),
399             new KeycodeEntry(KeyEvent.KEYCODE_F5, CEC_KEYCODE_F5),
400             new KeycodeEntry(KeyEvent.KEYCODE_TV_DATA_SERVICE, CEC_KEYCODE_DATA),
401             // RESERVED
402             // Add a new key mapping here if new keycode is introduced.
403     };
404 
405     /**
406      * Translate Android keycode to Hdmi Cec keycode and params.
407      *
408      * @param keycode Android keycode. For details, refer {@link KeyEvent}
409      * @return byte array of CEC keycode and params if matched. Otherwise, return null.
410      */
androidKeyToCecKey(int keycode)411     static byte[] androidKeyToCecKey(int keycode) {
412         for (int i = 0; i < KEYCODE_ENTRIES.length; ++i) {
413             byte[] cecKeycodeAndParams = KEYCODE_ENTRIES[i].toCecKeycodeAndParamIfMatched(keycode);
414             if (cecKeycodeAndParams != null) {
415                 return cecKeycodeAndParams;
416             }
417         }
418         return null;
419     }
420 
421     /**
422      * Translate Hdmi CEC keycode with params to Android keycode.
423      *
424      * @param cecKeycodeAndParams CEC keycode and params
425      * @return cec keycode corresponding to the given android keycode. If finds no matched keycode,
426      *         return {@link #UNSUPPORTED_KEYCODE}
427      */
cecKeycodeAndParamsToAndroidKey(byte[] cecKeycodeAndParams)428     static int cecKeycodeAndParamsToAndroidKey(byte[] cecKeycodeAndParams) {
429         for (int i = 0; i < KEYCODE_ENTRIES.length; ++i) {
430             int androidKey = KEYCODE_ENTRIES[i].toAndroidKeycodeIfMatched(cecKeycodeAndParams);
431             if (androidKey != UNSUPPORTED_KEYCODE) {
432                 return androidKey;
433             }
434         }
435         return UNSUPPORTED_KEYCODE;
436     }
437 
438     /**
439      * Whether the given {@code androidKeycode} is repeatable key or not.
440      *
441      * @param androidKeycode keycode of android
442      * @return false if the given {@code androidKeycode} is not supported key code
443      */
isRepeatableKey(int androidKeycode)444     static boolean isRepeatableKey(int androidKeycode) {
445         for (int i = 0; i < KEYCODE_ENTRIES.length; ++i) {
446             Boolean isRepeatable = KEYCODE_ENTRIES[i].isRepeatableIfMatched(androidKeycode);
447             if (isRepeatable != null) {
448                 return isRepeatable;
449             }
450         }
451         return false;
452     }
453 
454     /**
455      * Returns {@code true} if given Android keycode is supported, otherwise {@code false}.
456      */
isSupportedKeycode(int androidKeycode)457     static boolean isSupportedKeycode(int androidKeycode) {
458         return HdmiCecKeycode.androidKeyToCecKey(androidKeycode) != null;
459     }
460 
461     /**
462      * Returns {@code true} if given Android keycode is volume control related,
463      * otherwise {@code false}.
464      */
isVolumeKeycode(int androidKeycode)465     static boolean isVolumeKeycode(int androidKeycode) {
466         int cecKeyCode = HdmiCecKeycode.androidKeyToCecKey(androidKeycode)[0];
467         return isSupportedKeycode(androidKeycode)
468             && (cecKeyCode == CEC_KEYCODE_VOLUME_UP
469                 || cecKeyCode == CEC_KEYCODE_VOLUME_DOWN
470                 || cecKeyCode == CEC_KEYCODE_MUTE
471                 || cecKeyCode == CEC_KEYCODE_MUTE_FUNCTION
472                 || cecKeyCode == CEC_KEYCODE_RESTORE_VOLUME_FUNCTION);
473     }
474 
475     /**
476      * Returns CEC keycode to control audio mute status.
477      *
478      * @param muting {@code true} if audio is being muted
479      */
getMuteKey(boolean muting)480     public static int getMuteKey(boolean muting) {
481         // CEC_KEYCODE_MUTE_FUNCTION, CEC_KEYCODE_RESTORE_VOLUME_FUNCTION are deterministic
482         // commands that ensures the status changes to what we want, while CEC_KEYCODE_MUTE
483         // simply toggles the status.
484         // The former is a better choice in this regard, but there are compatibility issues
485         // observed - many audio receivers don't recognize the commands. We fall back on
486         // CEC_KEYCODE_MUTE for now.
487         // return muting ? CEC_KEYCODE_MUTE_FUNCTION : CEC_KEYCODE_RESTORE_VOLUME_FUNCTION;
488         return CEC_KEYCODE_MUTE;
489     }
490 
getKeycodeType(byte keycode)491     public static String getKeycodeType(byte keycode) {
492         switch (keycode) {
493             case CEC_KEYCODE_UP:
494             case CEC_KEYCODE_DOWN:
495             case CEC_KEYCODE_LEFT:
496             case CEC_KEYCODE_RIGHT:
497             case CEC_KEYCODE_RIGHT_UP:
498             case CEC_KEYCODE_RIGHT_DOWN:
499             case CEC_KEYCODE_LEFT_UP:
500             case CEC_KEYCODE_LEFT_DOWN:
501             case CEC_KEYCODE_PAGE_UP:
502             case CEC_KEYCODE_PAGE_DOWN:
503             case CEC_KEYCODE_EXIT:
504                 return "Navigation";
505             case CEC_KEYCODE_ROOT_MENU:
506             case CEC_KEYCODE_SETUP_MENU:
507             case CEC_KEYCODE_CONTENTS_MENU:
508             case CEC_KEYCODE_FAVORITE_MENU:
509             case CEC_KEYCODE_MEDIA_TOP_MENU:
510             case CEC_KEYCODE_MEDIA_CONTEXT_SENSITIVE_MENU:
511                 return "Menu";
512             case CEC_KEYCODE_VOLUME_UP:
513                 return "Volume up";
514             case CEC_KEYCODE_VOLUME_DOWN:
515                 return "Volume down";
516             case CEC_KEYCODE_MUTE:
517                 return "Volume mute";
518             case CEC_KEYCODE_POWER:
519                 return "Power";
520             case CEC_KEYCODE_POWER_TOGGLE_FUNCTION:
521                 return "Power toggle";
522             case CEC_KEYCODE_POWER_OFF_FUNCTION:
523                 return "Power off";
524             case CEC_KEYCODE_POWER_ON_FUNCTION:
525                 return "Power on";
526             case CEC_KEYCODE_F1_BLUE:
527             case CEC_KEYCODE_F2_RED:
528             case CEC_KEYCODE_F3_GREEN:
529             case CEC_KEYCODE_F4_YELLOW:
530             case CEC_KEYCODE_F5:
531                 return "Function key";
532             case CEC_KEYCODE_NEXT_FAVORITE:
533             case CEC_KEYCODE_CHANNEL_UP:
534             case CEC_KEYCODE_CHANNEL_DOWN:
535             case CEC_KEYCODE_PREVIOUS_CHANNEL:
536                 return "Channel";
537             case CEC_KEYCODE_NUMBER_11:
538             case CEC_KEYCODE_NUMBER_12:
539             case CEC_KEYCODE_NUMBER_0_OR_NUMBER_10:
540             case CEC_KEYCODE_NUMBERS_1:
541             case CEC_KEYCODE_NUMBERS_2:
542             case CEC_KEYCODE_NUMBERS_3:
543             case CEC_KEYCODE_NUMBERS_4:
544             case CEC_KEYCODE_NUMBERS_5:
545             case CEC_KEYCODE_NUMBERS_6:
546             case CEC_KEYCODE_NUMBERS_7:
547             case CEC_KEYCODE_NUMBERS_8:
548             case CEC_KEYCODE_NUMBERS_9:
549                 return "Number";
550             case CEC_KEYCODE_PLAY:
551             case CEC_KEYCODE_STOP:
552             case CEC_KEYCODE_PAUSE:
553             case CEC_KEYCODE_RECORD:
554             case CEC_KEYCODE_REWIND:
555             case CEC_KEYCODE_FAST_FORWARD:
556             case CEC_KEYCODE_EJECT:
557             case CEC_KEYCODE_FORWARD:
558             case CEC_KEYCODE_BACKWARD:
559             case CEC_KEYCODE_STOP_RECORD:
560             case CEC_KEYCODE_PAUSE_RECORD:
561             case CEC_KEYCODE_ANGLE:
562             case CEC_KEYCODE_SUB_PICTURE:
563             case CEC_KEYCODE_VIDEO_ON_DEMAND:
564                 return "Media";
565             case CEC_KEYCODE_PLAY_FUNCTION:
566             case CEC_KEYCODE_PAUSE_PLAY_FUNCTION:
567             case CEC_KEYCODE_RECORD_FUNCTION:
568             case CEC_KEYCODE_PAUSE_RECORD_FUNCTION:
569             case CEC_KEYCODE_STOP_FUNCTION:
570             case CEC_KEYCODE_MUTE_FUNCTION:
571             case CEC_KEYCODE_RESTORE_VOLUME_FUNCTION:
572             case CEC_KEYCODE_TUNE_FUNCTION:
573             case CEC_KEYCODE_SELECT_MEDIA_FUNCTION:
574             case CEC_KEYCODE_SELECT_AV_INPUT_FUNCTION:
575             case CEC_KEYCODE_SELECT_AUDIO_INPUT_FUNCTION:
576                 return "Functional";
577             case CEC_KEYCODE_ELECTRONIC_PROGRAM_GUIDE:
578             case CEC_KEYCODE_TIMER_PROGRAMMING:
579                 return "Timer";
580             case CEC_KEYCODE_SOUND_SELECT:
581             case CEC_KEYCODE_SELECT_SOUND_PRESENTATION:
582             case CEC_KEYCODE_SELECT_BROADCAST_TYPE:
583             case CEC_KEYCODE_INPUT_SELECT:
584             case CEC_KEYCODE_SELECT:
585                 return "Select";
586             case CEC_KEYCODE_NUMBER_ENTRY_MODE:
587             case CEC_KEYCODE_DOT:
588             case CEC_KEYCODE_CLEAR:
589             case CEC_KEYCODE_ENTER:
590             case CEC_KEYCODE_DISPLAY_INFORMATION:
591             case CEC_KEYCODE_HELP:
592             case CEC_KEYCODE_DATA:
593             case CEC_KEYCODE_INITIAL_CONFIGURATION:
594                 return "General";
595             default:
596                 return "Unknown";
597         }
598     }
599 }
600