|
45 | 45 | import android.hardware.usb.UsbManager;
|
46 | 46 | import android.net.wifi.WifiManager;
|
47 | 47 | import android.net.wifi.WifiManager.WifiLock;
|
| 48 | +import android.os.Build; |
48 | 49 | import android.os.IBinder;
|
49 | 50 | import android.os.PowerManager;
|
50 | 51 | import android.os.PowerManager.WakeLock;
|
@@ -82,7 +83,6 @@ public void onReceive(Context context, Intent intent) {
|
82 | 83 | }
|
83 | 84 | };
|
84 | 85 |
|
85 |
| - @SuppressWarnings("deprecation") |
86 | 86 | private void updateNotification() {
|
87 | 87 | Intent intent = new Intent(this, UsbIpConfig.class);
|
88 | 88 | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
@@ -113,21 +113,35 @@ public void onCreate() {
|
113 | 113 | super.onCreate();
|
114 | 114 |
|
115 | 115 | usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
116 |
| - connections = new SparseArray<AttachedDeviceContext>(); |
117 |
| - permission = new SparseArray<Boolean>(); |
118 |
| - socketMap = new HashMap<Socket, AttachedDeviceContext>(); |
119 |
| - |
120 |
| - usbPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); |
| 116 | + connections = new SparseArray<>(); |
| 117 | + permission = new SparseArray<>(); |
| 118 | + socketMap = new HashMap<>(); |
| 119 | + |
| 120 | + int intentFlags = 0; |
| 121 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { |
| 122 | + // This PendingIntent must be mutable to allow the framework to populate EXTRA_DEVICE and EXTRA_PERMISSION_GRANTED. |
| 123 | + intentFlags |= PendingIntent.FLAG_MUTABLE; |
| 124 | + } |
| 125 | + |
| 126 | + Intent i = new Intent(ACTION_USB_PERMISSION); |
| 127 | + i.setPackage(getPackageName()); |
| 128 | + |
| 129 | + usbPermissionIntent = PendingIntent.getBroadcast(this, 0, i, intentFlags); |
121 | 130 | IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
|
122 |
| - registerReceiver(usbReceiver, filter); |
| 131 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 132 | + registerReceiver(usbReceiver, filter, RECEIVER_NOT_EXPORTED); |
| 133 | + } |
| 134 | + else { |
| 135 | + registerReceiver(usbReceiver, filter); |
| 136 | + } |
123 | 137 |
|
124 | 138 | PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
125 |
| - WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE); |
| 139 | + WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); |
126 | 140 |
|
127 |
| - cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "USB/IP Service"); |
| 141 | + cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "USBIPServerForAndroid:Service"); |
128 | 142 | cpuWakeLock.acquire();
|
129 | 143 |
|
130 |
| - wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "USB/IP Service"); |
| 144 | + wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "USBIPServerForAndroid:Service"); |
131 | 145 | wifiLock.acquire();
|
132 | 146 |
|
133 | 147 | server = new UsbIpServer();
|
@@ -327,7 +341,7 @@ private UsbDeviceInfo getInfoForDevice(UsbDevice dev, UsbDeviceConnection devCon
|
327 | 341 |
|
328 | 342 | @Override
|
329 | 343 | public List<UsbDeviceInfo> getDevices() {
|
330 |
| - ArrayList<UsbDeviceInfo> list = new ArrayList<UsbDeviceInfo>(); |
| 344 | + ArrayList<UsbDeviceInfo> list = new ArrayList<>(); |
331 | 345 |
|
332 | 346 | for (UsbDevice dev : usbManager.getDeviceList().values()) {
|
333 | 347 | AttachedDeviceContext context = connections.get(dev.getDeviceId());
|
@@ -699,11 +713,11 @@ public boolean attachToDevice(Socket s, String busId) {
|
699 | 713 |
|
700 | 714 | // Use a thread pool with a thread per endpoint
|
701 | 715 | context.requestPool = new ThreadPoolExecutor(endpointCount, endpointCount,
|
702 |
| - Long.MAX_VALUE, TimeUnit.DAYS, |
703 |
| - new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.DiscardPolicy()); |
| 716 | + Long.MAX_VALUE, TimeUnit.DAYS, |
| 717 | + new LinkedBlockingQueue<>(), new ThreadPoolExecutor.DiscardPolicy()); |
704 | 718 |
|
705 | 719 | // Create the active message set
|
706 |
| - context.activeMessages = new HashSet<UsbIpSubmitUrb>(); |
| 720 | + context.activeMessages = new HashSet<>(); |
707 | 721 |
|
708 | 722 | connections.put(dev.getDeviceId(), context);
|
709 | 723 | socketMap.put(s, context);
|
|
0 commit comments