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.media.tv.TvContract;
23 import android.util.Log;
24 import android.view.KeyEvent;
25 
26 import com.android.tv.TvApplication;
27 
28 /**
29  * Handles global keys.
30  */
31 public class GlobalKeyReceiver extends BroadcastReceiver {
32     private static final boolean DEBUG = false;
33     private static final String TAG = "GlobalKeyReceiver";
34     private static final String ACTION_GLOBAL_BUTTON = "android.intent.action.GLOBAL_BUTTON";
35 
36     @Override
onReceive(Context context, Intent intent)37     public void onReceive(Context context, Intent intent) {
38         if (ACTION_GLOBAL_BUTTON.equals(intent.getAction())) {
39             KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
40             if (DEBUG) Log.d(TAG, "onReceive: " + event);
41             int keyCode = event.getKeyCode();
42             int action = event.getAction();
43             if (action == KeyEvent.ACTION_UP) {
44                 switch (keyCode) {
45                     case KeyEvent.KEYCODE_GUIDE:
46                         context.startActivity(
47                                 new Intent(Intent.ACTION_VIEW, TvContract.Programs.CONTENT_URI));
48                         break;
49                     case KeyEvent.KEYCODE_TV:
50                         ((TvApplication) context.getApplicationContext()).handleTvKey();
51                         break;
52                     case KeyEvent.KEYCODE_TV_INPUT:
53                         ((TvApplication) context.getApplicationContext()).handleTvInputKey();
54                         break;
55                     default:
56                         // Do nothing
57                         break;
58                 }
59             }
60         }
61     }
62 }
63