huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.DeviceTest;
 
import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.RemoteException;
//import android.view.IWindowManager;
//import android.view.IWindowManager.Stub;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnKeyListener;
import android.widget.TextView;
import java.util.HashMap;
 
import com.DeviceTest.helper.ConfigUtil;
import com.DeviceTest.helper.ControlButtonUtil;
 
import android.util.Log;
 
public class KeyboardTestActivity extends Activity {
   static final int Key_Status_Down = 1;
   static final int Key_Status_Null = 0;
   static final int Key_Status_Up = 2;
 
   static final String TAG = "KeyboardTestActivity";
 
   private int[] mButtonIds;
   private HashMap<Integer, Integer> mButtonMaps = new HashMap();
   private HashMap<Integer, Integer> mButtonStatus = new HashMap();
   private int[] mKeyCodes;
   private View v = null;
   private WindowManager wm = null;
   KeyguardLock kl = null;
   public void onCreate(Bundle paramBundle) {
       super.onCreate(paramBundle);
       setContentView(R.layout.keyboadtest);
 
       setTitle(getTitle() + "----("
               + getIntent().getStringExtra(DeviceTest.EXTRA_TEST_PROGRESS)
               + ")");
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
       //(FLAG_FULLSCREEN | FLAG_KEEP_SCREEN_ON);
       ControlButtonUtil.initControlButtonView(this);
       initButtonsMaps();
 
       v = new View(KeyboardTestActivity.this);
       wm = (WindowManager)KeyboardTestActivity.this.getSystemService(WINDOW_SERVICE);
        KeyguardManager km= (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        if (Build.VERSION.SDK_INT < ConfigUtil.ANDROID_SDK_VERSION_P) {
            kl = km.newKeyguardLock("unLock");
        }
//        findViewById(R.id.btn_Pass).setVisibility(View.INVISIBLE);
   }
 
   public boolean onKeyDown(int keyCode, KeyEvent event) {
       Log.d(TAG, "=============onKeyDown==========");
       return super.onKeyDown(keyCode, event);
   }
 
   protected void onResume() {
       super.onResume();
 
        if (Build.VERSION.SDK_INT < ConfigUtil.ANDROID_SDK_VERSION_P) {
            kl.disableKeyguard();
        }
       //addWindow();
   }
   @Override
   protected void onPause() {
       super.onPause();
       //removeWindow();
        if (Build.VERSION.SDK_INT < ConfigUtil.ANDROID_SDK_VERSION_P) {
            kl.reenableKeyguard();
        }
   }
   private void initButtonsMaps() {
       Log.d(TAG, "===========initButtonsMaps======");
       int[] resId = { R.id.bt_sounddown, R.id.bt_soundup, R.id.bt_home,
               R.id.bt_menu, R.id.bt_back };
       mButtonIds = resId;
       int[] keycode = { 25, 24, 3, 82, 4 };
       mKeyCodes = keycode;
 
       int i = 0;
       int j = mButtonIds.length;
 
       Log.d(TAG, "I=" + i + " j = " + j);
 
       for (i = 0; i < j; i++) {
           Integer key = Integer.valueOf(mKeyCodes[i]);
           Integer value = Integer.valueOf(mButtonIds[i]);
           mButtonMaps.put(key, value);
       }
       resetButtonBackground();
   }
 
   private boolean isTestKey(int keycode) {
       int j = mKeyCodes.length;
       int i = 0;
       while (i < j) {
           if (keycode == mKeyCodes[i]) {
               return true;
           } else {
               i++;
           }
       }
       return false;
   }
 
   private void resetButtonBackground() {
       Log.d(TAG, "resetButtonBackground()... ...");
       int i = mButtonIds.length;
       int j = 0;
       while (true) {
           if (j >= i)
               return;
           int k = mButtonIds[j];
           findViewById(k).setBackgroundColor(Color.rgb(255, 255, 255));
           ((TextView)findViewById(k)).setTextColor(Color.BLACK);
           j += 1;
       }
   }
 
   private void setButtonBackgroundDown(int resId) {
       Log.d(TAG, "=====613========setButtonBackgroundDown");
       findViewById(resId).setBackgroundColor(Color.BLUE);
   }
 
   private void setButtonBackgroundUp(int resId) {
       Log.d(TAG, "======setButtonBackgroundUp");
       findViewById(resId).setBackgroundColor(Color.GREEN);
   }
 
   public boolean dispatchKeyEvent(KeyEvent event) {
       int keyCode = event.getKeyCode();
       int actionCode = event.getAction();
       Log.d(TAG, "KeyCode = " + keyCode);
       Log.d(TAG, "actionCode = " + actionCode);
       if (!isTestKey(keyCode))
           return super.dispatchKeyEvent(event);
       int value = 0;
       Integer key = Integer.valueOf(keyCode);
       value = mButtonMaps.get(key).intValue();
       Log.d(TAG, "==================   value = " + value);
       switch (actionCode) {
           
       case 0:
           setButtonBackgroundDown(value);
           mButtonStatus.put(key, Integer.valueOf(1));
           break;
 
       case 1:
           setButtonBackgroundUp(value);
           mButtonStatus.put(key, Integer.valueOf(1));
           if(event.getKeyCode()== KeyEvent.KEYCODE_HOME){
                Log.d(TAG,getWindow().getAttributes().type+ " _____________---- onKeyEEEE(),   " + event.getKeyCode());
                return true;
               }else if(event.getKeyCode()== KeyEvent.KEYCODE_BACK){
                   Log.d(TAG,getWindow().getAttributes().type+ " _____________---- onKeyEEEE(),   " + event.getKeyCode());
                   return true;
               }
           break;
 
       default:
           break;
       }
 
//        if (mButtonStatus.size() == mButtonIds.length) {
//            findViewById(R.id.btn_Pass).performClick();
//        }
       return true;
   }
   
   private void addWindow(){
       WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
//         params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
//        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
//                | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
//         params.flags = WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
//                 |  WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
       params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
       params.width = 1;//WindowManager.LayoutParams.FILL_PARENT;
       params.height = 1;//WindowManager.LayoutParams.FILL_PARENT;
       params.format=PixelFormat.TRANSLUCENT;                
       params.gravity=Gravity.LEFT|Gravity.TOP;
      //����Ļ���Ͻ�Ϊԭ�㣬����x��y��ʼֵ
       params.x = 0;
       params.y = 0;
       wm.addView(v, params);
       v.requestFocus();
       v.setOnKeyListener(new OnKeyListener(){
           public boolean onKey(View v, int keyCodee, KeyEvent event) {
               Log.d(TAG, " _____________---- onKey(),   " + event.getKeyCode());
               int keyCode = event.getKeyCode();
               int actionCode = event.getAction();
               Log.d(TAG, "KeyCode = " + keyCode);
               Log.d(TAG, "actionCode = " + actionCode);
               if (!isTestKey(keyCode))
                   return false;
               int value = 0;
               Integer key = Integer.valueOf(keyCode);
               value = mButtonMaps.get(key).intValue();
               Log.d(TAG, "==================   value = " + value);
               switch (actionCode) {
               case 0:
                   setButtonBackgroundDown(value);
                   mButtonStatus.put(key, Integer.valueOf(1));
                   break;
 
               case 1:
                   setButtonBackgroundUp(value);
                   mButtonStatus.put(key, Integer.valueOf(1));
                   break;
               default:
                   break;
               }
               return true;
           
           }
       });        
   }    
   private void removeWindow(){
       wm.removeView(v);
   }
}