1 /* 2 * Copyright (C) 2015 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.receiver; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.AsyncTask; 23 import android.provider.Settings; 24 import android.util.Log; 25 import android.view.KeyEvent; 26 27 import com.android.tv.TvApplication; 28 29 /** 30 * Handles global keys. 31 */ 32 public class GlobalKeyReceiver extends BroadcastReceiver { 33 private static final boolean DEBUG = false; 34 private static final String TAG = "GlobalKeyReceiver"; 35 36 private static final String ACTION_GLOBAL_BUTTON = "android.intent.action.GLOBAL_BUTTON"; 37 // Settings.Secure.USER_SETUP_COMPLETE is hidden. 38 private static final String SETTINGS_USER_SETUP_COMPLETE = "user_setup_complete"; 39 40 private static long sLastEventTime; 41 private static boolean sUserSetupComplete; 42 43 @Override onReceive(Context context, Intent intent)44 public void onReceive(Context context, Intent intent) { 45 if (!TvApplication.getSingletons(context).getTvInputManagerHelper().hasTvInputManager()) { 46 Log.wtf(TAG, "Stopping because device does not have a TvInputManager"); 47 return; 48 } 49 TvApplication.setCurrentRunningProcess(context, true); 50 Context appContext = context.getApplicationContext(); 51 if (DEBUG) Log.d(TAG, "onReceive: " + intent); 52 if (sUserSetupComplete) { 53 handleIntent(appContext, intent); 54 } else { 55 new AsyncTask<Void, Void, Boolean>() { 56 @Override 57 protected Boolean doInBackground(Void... params) { 58 return Settings.Secure.getInt(appContext.getContentResolver(), 59 SETTINGS_USER_SETUP_COMPLETE, 0) != 0; 60 } 61 62 @Override 63 protected void onPostExecute(Boolean setupComplete) { 64 if (DEBUG) Log.d(TAG, "Is setup complete: " + setupComplete); 65 sUserSetupComplete = setupComplete; 66 if (sUserSetupComplete) { 67 handleIntent(appContext, intent); 68 } 69 } 70 }.execute(); 71 } 72 } 73 handleIntent(Context appContext, Intent intent)74 private void handleIntent(Context appContext, Intent intent) { 75 if (ACTION_GLOBAL_BUTTON.equals(intent.getAction())) { 76 KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); 77 if (DEBUG) Log.d(TAG, "handleIntent: " + event); 78 int keyCode = event.getKeyCode(); 79 int action = event.getAction(); 80 long eventTime = event.getEventTime(); 81 if (action == KeyEvent.ACTION_UP && sLastEventTime != eventTime) { 82 // Workaround for b/23947504, the same key event may be sent twice, filter it. 83 sLastEventTime = eventTime; 84 switch (keyCode) { 85 case KeyEvent.KEYCODE_GUIDE: 86 ((TvApplication) appContext).handleGuideKey(); 87 break; 88 case KeyEvent.KEYCODE_TV: 89 ((TvApplication) appContext).handleTvKey(); 90 break; 91 case KeyEvent.KEYCODE_TV_INPUT: 92 ((TvApplication) appContext).handleTvInputKey(); 93 break; 94 default: 95 // Do nothing 96 break; 97 } 98 } 99 } 100 } 101 } 102