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 (Arrays.equals(mCecKeycodeAndParams, cecKeycodeAndParams)) { 217 return mAndroidKeycode; 218 } else { 219 return UNSUPPORTED_KEYCODE; 220 } 221 } 222 isRepeatableIfMatched(int androidKeycode)223 private Boolean isRepeatableIfMatched(int androidKeycode) { 224 if (mAndroidKeycode == androidKeycode) { 225 return mIsRepeatable; 226 } else { 227 return null; 228 } 229 } 230 } 231 intToSingleByteArray(int value)232 private static byte[] intToSingleByteArray(int value) { 233 return new byte[] { 234 (byte) (value & 0xFF) }; 235 } 236 237 // Keycode entry container for all mappings. 238 // Note that order of entry is the same as above cec keycode definition. 239 private static final KeycodeEntry[] KEYCODE_ENTRIES = new KeycodeEntry[] { 240 new KeycodeEntry(KeyEvent.KEYCODE_DPAD_CENTER, CEC_KEYCODE_SELECT), 241 new KeycodeEntry(KeyEvent.KEYCODE_DPAD_UP, CEC_KEYCODE_UP), 242 new KeycodeEntry(KeyEvent.KEYCODE_DPAD_DOWN, CEC_KEYCODE_DOWN), 243 new KeycodeEntry(KeyEvent.KEYCODE_DPAD_LEFT, CEC_KEYCODE_LEFT), 244 new KeycodeEntry(KeyEvent.KEYCODE_DPAD_RIGHT, CEC_KEYCODE_RIGHT), 245 // No Android keycode defined for CEC_KEYCODE_RIGHT_UP 246 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_RIGHT_UP), 247 // No Android keycode defined for CEC_KEYCODE_RIGHT_DOWN 248 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_RIGHT_DOWN), 249 // No Android keycode defined for CEC_KEYCODE_LEFT_UP 250 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_LEFT_UP), 251 // No Android keycode defined for CEC_KEYCODE_LEFT_DOWN 252 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_LEFT_DOWN), 253 // Both KEYCODE_HOME and KEYCODE_MENU keys are sent as CEC_KEYCODE_ROOT_MENU 254 // NOTE that the HOME key is not usually forwarded. 255 // When CEC_KEYCODE_ROOT_MENU is received, it is translated to KEYCODE_HOME 256 new KeycodeEntry(KeyEvent.KEYCODE_HOME, CEC_KEYCODE_ROOT_MENU), 257 new KeycodeEntry(KeyEvent.KEYCODE_MENU, CEC_KEYCODE_ROOT_MENU), 258 new KeycodeEntry(KeyEvent.KEYCODE_SETTINGS, CEC_KEYCODE_SETUP_MENU), 259 new KeycodeEntry(KeyEvent.KEYCODE_TV_CONTENTS_MENU, CEC_KEYCODE_CONTENTS_MENU, false), 260 // No Android keycode defined for CEC_KEYCODE_FAVORITE_MENU 261 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_FAVORITE_MENU), 262 // Note that both BACK and ESCAPE are mapped to EXIT of CEC keycode. 263 // This would be problematic when translates CEC keycode to Android keycode. 264 // In current implementation, we pick BACK as mapping of EXIT key. 265 // If you'd like to map CEC EXIT to Android EXIT key, change order of 266 // the following two definition. 267 new KeycodeEntry(KeyEvent.KEYCODE_BACK, CEC_KEYCODE_EXIT), 268 new KeycodeEntry(KeyEvent.KEYCODE_ESCAPE, CEC_KEYCODE_EXIT), 269 // RESERVED 270 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_TOP_MENU, CEC_KEYCODE_MEDIA_TOP_MENU), 271 new KeycodeEntry(KeyEvent.KEYCODE_TV_MEDIA_CONTEXT_MENU, 272 CEC_KEYCODE_MEDIA_CONTEXT_SENSITIVE_MENU), 273 // RESERVED 274 // No Android keycode defined for CEC_KEYCODE_NUMBER_ENTRY_MODE 275 new KeycodeEntry(KeyEvent.KEYCODE_TV_NUMBER_ENTRY, CEC_KEYCODE_NUMBER_ENTRY_MODE), 276 new KeycodeEntry(KeyEvent.KEYCODE_11, CEC_KEYCODE_NUMBER_11), 277 new KeycodeEntry(KeyEvent.KEYCODE_12, CEC_KEYCODE_NUMBER_12), 278 new KeycodeEntry(KeyEvent.KEYCODE_0, CEC_KEYCODE_NUMBER_0_OR_NUMBER_10), 279 new KeycodeEntry(KeyEvent.KEYCODE_1, CEC_KEYCODE_NUMBERS_1), 280 new KeycodeEntry(KeyEvent.KEYCODE_2, CEC_KEYCODE_NUMBERS_2), 281 new KeycodeEntry(KeyEvent.KEYCODE_3, CEC_KEYCODE_NUMBERS_3), 282 new KeycodeEntry(KeyEvent.KEYCODE_4, CEC_KEYCODE_NUMBERS_4), 283 new KeycodeEntry(KeyEvent.KEYCODE_5, CEC_KEYCODE_NUMBERS_5), 284 new KeycodeEntry(KeyEvent.KEYCODE_6, CEC_KEYCODE_NUMBERS_6), 285 new KeycodeEntry(KeyEvent.KEYCODE_7, CEC_KEYCODE_NUMBERS_7), 286 new KeycodeEntry(KeyEvent.KEYCODE_8, CEC_KEYCODE_NUMBERS_8), 287 new KeycodeEntry(KeyEvent.KEYCODE_9, CEC_KEYCODE_NUMBERS_9), 288 new KeycodeEntry(KeyEvent.KEYCODE_PERIOD, CEC_KEYCODE_DOT), 289 new KeycodeEntry(KeyEvent.KEYCODE_NUMPAD_ENTER, CEC_KEYCODE_ENTER), 290 new KeycodeEntry(KeyEvent.KEYCODE_CLEAR, CEC_KEYCODE_CLEAR), 291 // RESERVED 292 // No Android keycode defined for CEC_KEYCODE_NEXT_FAVORITE 293 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_NEXT_FAVORITE), 294 new KeycodeEntry(KeyEvent.KEYCODE_CHANNEL_UP, CEC_KEYCODE_CHANNEL_UP), 295 new KeycodeEntry(KeyEvent.KEYCODE_CHANNEL_DOWN, CEC_KEYCODE_CHANNEL_DOWN), 296 new KeycodeEntry(KeyEvent.KEYCODE_LAST_CHANNEL, CEC_KEYCODE_PREVIOUS_CHANNEL), 297 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK, CEC_KEYCODE_SOUND_SELECT), 298 new KeycodeEntry(KeyEvent.KEYCODE_TV_INPUT, CEC_KEYCODE_INPUT_SELECT), 299 new KeycodeEntry(KeyEvent.KEYCODE_INFO, CEC_KEYCODE_DISPLAY_INFORMATION), 300 // No Android keycode defined for CEC_KEYCODE_HELP 301 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_HELP), 302 new KeycodeEntry(KeyEvent.KEYCODE_PAGE_UP, CEC_KEYCODE_PAGE_UP), 303 new KeycodeEntry(KeyEvent.KEYCODE_PAGE_DOWN, CEC_KEYCODE_PAGE_DOWN), 304 // RESERVED 305 new KeycodeEntry(KeyEvent.KEYCODE_POWER, CEC_KEYCODE_POWER, false), 306 new KeycodeEntry(KeyEvent.KEYCODE_VOLUME_UP, CEC_KEYCODE_VOLUME_UP), 307 new KeycodeEntry(KeyEvent.KEYCODE_VOLUME_DOWN, CEC_KEYCODE_VOLUME_DOWN), 308 new KeycodeEntry(KeyEvent.KEYCODE_VOLUME_MUTE, CEC_KEYCODE_MUTE, false), 309 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_PLAY, CEC_KEYCODE_PLAY), 310 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_STOP, CEC_KEYCODE_STOP), 311 // Note that we map both MEDIA_PAUSE and MEDIA_PLAY_PAUSE to CEC PAUSE key. 312 // When it translates CEC PAUSE key, it picks Android MEDIA_PAUSE key as a mapping of 313 // it. If you'd like to choose MEDIA_PLAY_PAUSE, please change order of the following 314 // two lines. 315 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_PAUSE, CEC_KEYCODE_PAUSE), 316 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, CEC_KEYCODE_PAUSE), 317 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_RECORD, CEC_KEYCODE_RECORD), 318 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_REWIND, CEC_KEYCODE_REWIND), 319 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, CEC_KEYCODE_FAST_FORWARD), 320 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_EJECT, CEC_KEYCODE_EJECT), 321 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_NEXT, CEC_KEYCODE_FORWARD), 322 new KeycodeEntry(KeyEvent.KEYCODE_MEDIA_PREVIOUS, CEC_KEYCODE_BACKWARD), 323 // No Android keycode defined for CEC_KEYCODE_STOP_RECORD 324 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_STOP_RECORD), 325 // No Android keycode defined for CEC_KEYCODE_PAUSE_RECORD 326 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_PAUSE_RECORD), 327 // No Android keycode defined for CEC_KEYCODE_RESERVED 328 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_RESERVED), 329 // No Android keycode defined for CEC_KEYCODE_ANGLE 330 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_ANGLE), 331 new KeycodeEntry(KeyEvent.KEYCODE_CAPTIONS, CEC_KEYCODE_SUB_PICTURE), 332 // No Android keycode defined for CEC_KEYCODE_VIDEO_ON_DEMAND 333 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_VIDEO_ON_DEMAND), 334 new KeycodeEntry(KeyEvent.KEYCODE_GUIDE, CEC_KEYCODE_ELECTRONIC_PROGRAM_GUIDE), 335 new KeycodeEntry(KeyEvent.KEYCODE_TV_TIMER_PROGRAMMING, CEC_KEYCODE_TIMER_PROGRAMMING), 336 // No Android keycode defined for CEC_KEYCODE_INITIAL_CONFIGURATION 337 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_INITIAL_CONFIGURATION), 338 // No Android keycode defined for CEC_KEYCODE_SELECT_BROADCAST_TYPE 339 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_SELECT_BROADCAST_TYPE), 340 new KeycodeEntry(KeyEvent.KEYCODE_TV_TERRESTRIAL_ANALOG, 341 CEC_KEYCODE_SELECT_BROADCAST_TYPE, true, 342 intToSingleByteArray(UI_BROADCAST_ANALOGUE)), 343 new KeycodeEntry(KeyEvent.KEYCODE_TV_TERRESTRIAL_DIGITAL, 344 CEC_KEYCODE_SELECT_BROADCAST_TYPE, true, 345 intToSingleByteArray(UI_BROADCAST_DIGITAL_TERRESTRIAL)), 346 new KeycodeEntry(KeyEvent.KEYCODE_TV_SATELLITE_BS, 347 CEC_KEYCODE_SELECT_BROADCAST_TYPE, true, 348 intToSingleByteArray(UI_BROADCAST_DIGITAL_SATELLITE)), 349 new KeycodeEntry(KeyEvent.KEYCODE_TV_SATELLITE_CS, 350 CEC_KEYCODE_SELECT_BROADCAST_TYPE, true, 351 intToSingleByteArray(UI_BROADCAST_DIGITAL_COMMNICATIONS_SATELLITE)), 352 new KeycodeEntry(KeyEvent.KEYCODE_TV_NETWORK, 353 CEC_KEYCODE_SELECT_BROADCAST_TYPE, true, 354 intToSingleByteArray(UI_BROADCAST_TOGGLE_ANALOGUE_DIGITAL)), 355 // No Android keycode defined for CEC_KEYCODE_SELECT_SOUND_PRESENTATION 356 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_SELECT_SOUND_PRESENTATION), 357 // RESERVED 358 // The following deterministic key definitions do not need key mapping 359 // since they are supposed to be generated programmatically only. 360 // No Android keycode defined for CEC_KEYCODE_PLAY_FUNCTION 361 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_PLAY_FUNCTION, false), 362 // No Android keycode defined for CEC_KEYCODE_PAUSE_PLAY_FUNCTION 363 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_PAUSE_PLAY_FUNCTION, false), 364 // No Android keycode defined for CEC_KEYCODE_RECORD_FUNCTION 365 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_RECORD_FUNCTION, false), 366 // No Android keycode defined for CEC_KEYCODE_PAUSE_RECORD_FUNCTION 367 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_PAUSE_RECORD_FUNCTION, false), 368 // No Android keycode defined for CEC_KEYCODE_STOP_FUNCTION 369 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_STOP_FUNCTION, false), 370 // No Android keycode defined for CEC_KEYCODE_MUTE_FUNCTION 371 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_MUTE_FUNCTION, false), 372 // No Android keycode defined for CEC_KEYCODE_RESTORE_VOLUME_FUNCTION 373 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_RESTORE_VOLUME_FUNCTION, false), 374 // No Android keycode defined for CEC_KEYCODE_TUNE_FUNCTION 375 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_TUNE_FUNCTION, false), 376 // No Android keycode defined for CEC_KEYCODE_SELECT_MEDIA_FUNCTION 377 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_SELECT_MEDIA_FUNCTION, false), 378 // No Android keycode defined for CEC_KEYCODE_SELECT_AV_INPUT_FUNCTION 379 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_SELECT_AV_INPUT_FUNCTION, false), 380 // No Android keycode defined for CEC_KEYCODE_SELECT_AUDIO_INPUT_FUNCTION 381 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_SELECT_AUDIO_INPUT_FUNCTION, false), 382 // No Android keycode defined for CEC_KEYCODE_POWER_TOGGLE_FUNCTION 383 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_POWER_TOGGLE_FUNCTION, false), 384 // No Android keycode defined for CEC_KEYCODE_POWER_OFF_FUNCTION 385 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_POWER_OFF_FUNCTION, false), 386 // No Android keycode defined for CEC_KEYCODE_POWER_ON_FUNCTION 387 new KeycodeEntry(UNSUPPORTED_KEYCODE, CEC_KEYCODE_POWER_ON_FUNCTION, false), 388 // RESERVED 389 new KeycodeEntry(KeyEvent.KEYCODE_PROG_BLUE, CEC_KEYCODE_F1_BLUE), 390 new KeycodeEntry(KeyEvent.KEYCODE_PROG_RED, CEC_KEYCODE_F2_RED), 391 new KeycodeEntry(KeyEvent.KEYCODE_PROG_GREEN, CEC_KEYCODE_F3_GREEN), 392 new KeycodeEntry(KeyEvent.KEYCODE_PROG_YELLOW, CEC_KEYCODE_F4_YELLOW), 393 new KeycodeEntry(KeyEvent.KEYCODE_F5, CEC_KEYCODE_F5), 394 new KeycodeEntry(KeyEvent.KEYCODE_TV_DATA_SERVICE, CEC_KEYCODE_DATA), 395 // RESERVED 396 // Add a new key mapping here if new keycode is introduced. 397 }; 398 399 /** 400 * Translate Android keycode to Hdmi Cec keycode and params. 401 * 402 * @param keycode Android keycode. For details, refer {@link KeyEvent} 403 * @return byte array of CEC keycode and params if matched. Otherwise, return null. 404 */ androidKeyToCecKey(int keycode)405 static byte[] androidKeyToCecKey(int keycode) { 406 for (int i = 0; i < KEYCODE_ENTRIES.length; ++i) { 407 byte[] cecKeycodeAndParams = KEYCODE_ENTRIES[i].toCecKeycodeAndParamIfMatched(keycode); 408 if (cecKeycodeAndParams != null) { 409 return cecKeycodeAndParams; 410 } 411 } 412 return null; 413 } 414 415 /** 416 * Translate Hdmi CEC keycode with params to Android keycode. 417 * 418 * @param cecKeycodeAndParams CEC keycode and params 419 * @return cec keycode corresponding to the given android keycode. If finds no matched keycode, 420 * return {@link #UNSUPPORTED_KEYCODE} 421 */ cecKeycodeAndParamsToAndroidKey(byte[] cecKeycodeAndParams)422 static int cecKeycodeAndParamsToAndroidKey(byte[] cecKeycodeAndParams) { 423 for (int i = 0; i < KEYCODE_ENTRIES.length; ++i) { 424 int androidKey = KEYCODE_ENTRIES[i].toAndroidKeycodeIfMatched(cecKeycodeAndParams); 425 if (androidKey != UNSUPPORTED_KEYCODE) { 426 return androidKey; 427 } 428 } 429 return UNSUPPORTED_KEYCODE; 430 } 431 432 /** 433 * Whether the given {@code androidKeycode} is repeatable key or not. 434 * 435 * @param androidKeycode keycode of android 436 * @return false if the given {@code androidKeycode} is not supported key code 437 */ isRepeatableKey(int androidKeycode)438 static boolean isRepeatableKey(int androidKeycode) { 439 for (int i = 0; i < KEYCODE_ENTRIES.length; ++i) { 440 Boolean isRepeatable = KEYCODE_ENTRIES[i].isRepeatableIfMatched(androidKeycode); 441 if (isRepeatable != null) { 442 return isRepeatable; 443 } 444 } 445 return false; 446 } 447 448 /** 449 * Returns {@code true} if given Android keycode is supported, otherwise {@code false}. 450 */ isSupportedKeycode(int androidKeycode)451 static boolean isSupportedKeycode(int androidKeycode) { 452 return HdmiCecKeycode.androidKeyToCecKey(androidKeycode) != null; 453 } 454 455 /** 456 * Returns {@code true} if given Android keycode is volume control related, 457 * otherwise {@code false}. 458 */ isVolumeKeycode(int androidKeycode)459 static boolean isVolumeKeycode(int androidKeycode) { 460 int cecKeyCode = HdmiCecKeycode.androidKeyToCecKey(androidKeycode)[0]; 461 return isSupportedKeycode(androidKeycode) 462 && (cecKeyCode == CEC_KEYCODE_VOLUME_UP 463 || cecKeyCode == CEC_KEYCODE_VOLUME_DOWN 464 || cecKeyCode == CEC_KEYCODE_MUTE 465 || cecKeyCode == CEC_KEYCODE_MUTE_FUNCTION 466 || cecKeyCode == CEC_KEYCODE_RESTORE_VOLUME_FUNCTION); 467 } 468 469 /** 470 * Returns CEC keycode to control audio mute status. 471 * 472 * @param muting {@code true} if audio is being muted 473 */ getMuteKey(boolean muting)474 public static int getMuteKey(boolean muting) { 475 // CEC_KEYCODE_MUTE_FUNCTION, CEC_KEYCODE_RESTORE_VOLUME_FUNCTION are deterministic 476 // commands that ensures the status changes to what we want, while CEC_KEYCODE_MUTE 477 // simply toggles the status. 478 // The former is a better choice in this regard, but there are compatibility issues 479 // observed - many audio receivers don't recognize the commands. We fall back on 480 // CEC_KEYCODE_MUTE for now. 481 // return muting ? CEC_KEYCODE_MUTE_FUNCTION : CEC_KEYCODE_RESTORE_VOLUME_FUNCTION; 482 return CEC_KEYCODE_MUTE; 483 } 484 } 485