• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 android.hdmicec.app;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.view.Gravity;
23 import android.view.KeyEvent;
24 import android.widget.LinearLayout;
25 import android.widget.ScrollView;
26 import android.widget.TableRow;
27 import android.widget.TextView;
28 
29 import java.lang.Override;
30 
31 /**
32  * A simple app that captures the key press events and logs them.
33  */
34 public class HdmiCecKeyEventCapture extends Activity {
35 
36     private static final String TAG = HdmiCecKeyEventCapture.class.getSimpleName();
37     private TextView text;
38     private boolean longPressed = false;
39 
40     static final String LONG_PRESS_PREFIX = "Long press ";
41     static final String SHORT_PRESS_PREFIX = "Short press ";
42 
43     @Override
onCreate(Bundle icicle)44     public void onCreate(Bundle icicle) {
45         super.onCreate(icicle);
46         ScrollView sv = new ScrollView(this);
47         LinearLayout layout = new LinearLayout(this);
48         TableRow.LayoutParams layoutParams =
49                 new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
50                         TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
51         text = new TextView(this);
52         text.setGravity(Gravity.CENTER);
53         text.setText("No key pressed!");
54         text.setLayoutParams(layoutParams);
55         layout.addView(text);
56         this.setContentView(layout);
57     }
58 
59     @Override
onKeyDown(int keyCode, KeyEvent event)60     public boolean onKeyDown(int keyCode, KeyEvent event) {
61         event.startTracking();
62         return true;
63     }
64 
65     @Override
onKeyUp(int keyCode, KeyEvent event)66     public boolean onKeyUp(int keyCode, KeyEvent event) {
67 
68         text.setText((longPressed ? "Long press " : "Short press ") +
69                 event.keyCodeToString(keyCode));
70 
71         Log.d(TAG, event.toString());
72         Log.i(TAG, (longPressed ? "Long press " : "Short press ") + event.keyCodeToString(keyCode));
73 
74         longPressed = false;
75         return true;
76     }
77 
78     @Override
onKeyLongPress(int keyCode, KeyEvent event)79     public boolean onKeyLongPress(int keyCode, KeyEvent event) {
80         longPressed = true;
81         return true;
82     }
83 
84     /*
85     Override onUserLeaveHint() to simulate KEYCODE_HOME pressing for now.
86     If the activity will be used to test other complicated scenarios
87     which will let the activity go into the background,
88     the onUserLeaveHint() might be called without KEYCODE_HOME pressing.
89     */
90     @Override
onUserLeaveHint()91     protected void onUserLeaveHint() {
92         text.setText((longPressed ? "Long press " : "Short press ")
93                 + "KEYCODE_HOME");
94         Log.d(TAG, "onUserLeaveHint: KEYCODE_HOME Press");
95         Log.i(TAG, (longPressed ? "Long press " : "Short press ") + "KEYCODE_HOME");
96 
97         longPressed = false;
98         super.onUserLeaveHint();
99     }
100 }
101