1 /*
2 ** Copyright 2019, 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.google.android.yukawaservice;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.util.Log;
23 import android.view.KeyEvent;
24 
25 import java.util.List;
26 
27 /**
28  * Handles global keys.
29  */
30 public class RemoteSyncReceiver extends BroadcastReceiver {
31     private static final String TAG = "YukawaGlobalKey";
32     private static final String ACTION_CONNECT_INPUT_NORMAL =
33             "com.google.android.intent.action.CONNECT_INPUT";
34     private static final String INTENT_EXTRA_NO_INPUT_MODE = "no_input_mode";
35 
sendPairingIntent(Context context, KeyEvent event)36     private static void sendPairingIntent(Context context, KeyEvent event) {
37         Intent intent = new Intent(ACTION_CONNECT_INPUT_NORMAL)
38                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
39 
40         if (event != null) {
41             intent.putExtra(INTENT_EXTRA_NO_INPUT_MODE, true)
42                     .putExtra(Intent.EXTRA_KEY_EVENT, event);
43         }
44         context.startActivity(intent);
45     }
46 
47     @Override
onReceive(Context context, Intent intent)48     public void onReceive(Context context, Intent intent) {
49         if (Intent.ACTION_GLOBAL_BUTTON.equals(intent.getAction())) {
50             KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
51             int keyCode = event.getKeyCode();
52             int keyAction = event.getAction();
53             switch (keyCode) {
54                 case KeyEvent.KEYCODE_PAIRING:
55                     if (keyAction == KeyEvent.ACTION_UP) {
56                         sendPairingIntent(context, event);
57                     }
58                     break;
59 
60                 default:
61                     Log.e(TAG, "Unhandled KeyEvent: " + keyCode);
62             }
63         }
64     }
65 }
66