huangcm
2025-04-26 552bdb764ece01f57e1f174432a69f0af3b2c141
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
package com.DeviceTest;
import java.io.File;
 
import com.DeviceTest.helper.ControlButtonUtil;
import com.DeviceTest.helper.SystemInfoUtil;
 
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.StatFs;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.os.SystemProperties;
import android.os.storage.StorageEventListener;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.view.KeyEvent;
import android.view.View;
import android.hardware.input.InputManager;
import android.view.MotionEvent;
import android.view.InputDevice;
import java.io.File;
import android.content.ComponentName;
import com.DeviceTest.helper.ControlButtonUtil;
import com.DeviceTest.helper.SystemInfoUtil;
import android.os.Build;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.StatFs;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.os.SystemProperties;
import android.os.storage.StorageEventListener;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import java.util.Iterator;
import java.util.HashMap;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.Toast;
 
public class USBDeviceTestActivity extends Activity {
   private final static String TAG = "USBDeviceTestActivity";
   private final static int MSG_OK=0;
   
   // 创建定时器循环
   public Handler USBhandler;
   
   public void onCreate(Bundle paramBundle) {
       super.onCreate(paramBundle);
       setContentView(R.layout.usbdevicetest);
 
       System.out.println("rpdzkj usb test");
 
       // 创建定时器循环
       USBhandler=new Handler();
       USBhandler.postDelayed(runnable, 500);
 
 
       ControlButtonUtil.initControlButtonView(this);
       // 自动通过测试
       //handler.sendEmptyMessageDelayed(MSG_OK, 2500);
   }
 
   private Handler handler=new Handler(){
       @Override
       public void handleMessage(Message msg) {
           switch(msg.what){
           case MSG_OK:
               ((Button) findViewById(R.id.btn_Pass)).performClick();
           }
       }
   };
 
   // 定时器循环代码
    Runnable runnable=new Runnable(){
       public void run() {
       TextView usbHostView = (TextView) findViewById(R.id.USBNum);
       usbHostView.setText("USB设备数量 : " + USBhostDeviceCheck());
       USBhandler.postDelayed(this, 1000);// 每1000毫秒循环一次
       } 
    };
 
 
   // 获取当前USB设备数量
    public int USBhostDeviceCheck(){
        int usbNum = 0;
        UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
       HashMap<String, UsbDevice> devs;
        devs = usbManager.getDeviceList();
        StringBuilder sb = new StringBuilder();
        if(!devs.isEmpty()){
            Iterator<UsbDevice> usbs = devs.values().iterator();
            while(usbs.hasNext()){
                UsbDevice dev = usbs.next();
                int vid = dev.getVendorId();
                int pid = dev.getProductId();
               if (vid == 1507 && pid == 1544) {
                   // skip usb hub gl850g
               } else if (vid == 3034 && pid == 46880) {
                   // skip usb wifi rtl     
               } else if (vid == 4292 && pid == 60000) {
                   // skip usb wifi rtl 
               } else if (vid == 11388 && pid == 293) {
                   // skip EC20
               } else if (vid == 11388 && pid == 24578) {
                   // skip EC200s
               } else if (vid == 0x2c7c && pid == 0x0801) {
                   // RM520N
               } else if (vid == 0x2c7c && pid == 0x0800) {
                   // RM500Q
               } else if (vid == 0x2c7c && pid == 0x0900) {
                   // RM500U
               } else {
                   usbNum++;
               }
                String name = dev.getDeviceName();
                sb.append("-----------------------------------"+"\n");
                sb.append("vid = "+Integer.toHexString(vid)+ "  ,pid = "+ Integer.toHexString(pid) + "\n");
                sb.append("name = "+name+"\n");
               android.util.Log.d("RRRRRRRR", sb.toString());
            }
        }
 
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) return usbNum;
 
       InputManager inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
       int[] deviceIds = inputManager.getInputDeviceIds();
       for (int deviceId : deviceIds) {
              InputDevice device = inputManager.getInputDevice(deviceId);
            if (device != null) {
               int srcid = device.getSources();
                if ((srcid & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE) {
                   usbNum ++;
                }
            }
       }
 
        // for k5c
        usbNum = usbNum - 2;
 
       return usbNum;
    }
 
   public void onGoOtgModeSetBtnClick(View view) {
       goOTGModeSetting();
   }
 
    public void goOTGModeSetting() {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$UsbHostDeviceSettingsActivity"));
        if (intent.resolveActivityInfo(getPackageManager(), PackageManager.MATCH_DEFAULT_ONLY) != null) {
            startActivity(intent);
        }
    }
 
   @Override
   protected void onResume() {
       super.onResume();
   }
   
   public void onCancel(DialogInterface dialog) {
            finish();
   }
 
   public boolean dispatchKeyEvent(KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
           return false;
       }
       return super.dispatchKeyEvent(event);
   }
  
}