1 /* 2 * Copyright (C) 2022 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.tv.settings; 18 19 import static com.android.tv.settings.accessories.AddAccessoryActivity.ACTION_CONNECT_INPUT; 20 import static com.android.tv.settings.accessories.AddAccessoryActivity.INTENT_EXTRA_NO_INPUT_MODE; 21 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.util.Log; 26 import android.view.KeyEvent; 27 28 /** 29 * Handles global keys. 30 */ 31 public class GlobalKeyReceiver extends BroadcastReceiver { 32 private static final String TAG = "TvGlobalKeyReceiver"; 33 private static final boolean DEBUG = false; 34 35 @Override onReceive(Context context, Intent intent)36 public void onReceive(Context context, Intent intent) { 37 if (Intent.ACTION_GLOBAL_BUTTON.equals(intent.getAction())) { 38 KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT, KeyEvent.class); 39 if (DEBUG) { 40 Log.d(TAG, "Received key event " + event.toString()); 41 } 42 switch (event.getKeyCode()) { 43 case KeyEvent.KEYCODE_PAIRING: 44 sendPairingIntent(context, event); 45 return; 46 default: 47 if (DEBUG) { 48 Log.d(TAG, "Unhandled key " + event.getKeyCode()); 49 } 50 break; 51 } 52 } 53 } 54 sendPairingIntent(Context context, KeyEvent event)55 private static void sendPairingIntent(Context context, KeyEvent event) { 56 Intent intent = 57 new Intent(ACTION_CONNECT_INPUT).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setPackage( 58 context.getPackageName()); 59 if (event != null) { 60 intent.putExtra(INTENT_EXTRA_NO_INPUT_MODE, true) 61 .putExtra(Intent.EXTRA_KEY_EVENT, event); 62 } 63 context.startActivity(intent); 64 } 65 } 66