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