2727 * or implied.
2828 */
2929
30- package ioio .lib .android . accessory ;
30+ package ioio .lib .android ;
3131
32- import ioio .lib .android .accessory .Adapter .UsbAccessoryInterface ;
33- import ioio .lib .api .IOIOConnection ;
34- import ioio .lib .api .exception .ConnectionLostException ;
35- import ioio .lib .impl .FixedReadBufferedInputStream ;
36- import ioio .lib .spi .IOIOConnectionBootstrap ;
37- import ioio .lib .spi .IOIOConnectionFactory ;
38- import ioio .lib .spi .NoRuntimeSupportException ;
32+ import android .annotation .TargetApi ;
33+ import android .app .PendingIntent ;
34+ import android .content .BroadcastReceiver ;
35+ import android .content .Context ;
36+ import android .content .Intent ;
37+ import android .content .IntentFilter ;
38+ import android .os .Build ;
39+ import android .os .ParcelFileDescriptor ;
40+ import android .util .Log ;
41+
42+ import net .sourceforge .smallbasic .ioio .IOIOLoader ;
3943
4044import java .io .BufferedOutputStream ;
4145import java .io .FileDescriptor ;
4650import java .io .OutputStream ;
4751import java .util .Collection ;
4852
49- import android .app .PendingIntent ;
50- import android .content .BroadcastReceiver ;
51- import android .content .Context ;
52- import android .content .ContextWrapper ;
53- import android .content .Intent ;
54- import android .content .IntentFilter ;
55- import android .os .ParcelFileDescriptor ;
56- import android .util .Log ;
53+ import ioio .lib .android .UsbManagerAdapter .UsbAccessoryInterface ;
54+ import ioio .lib .api .IOIOConnection ;
55+ import ioio .lib .api .exception .ConnectionLostException ;
56+ import ioio .lib .impl .FixedReadBufferedInputStream ;
57+ import ioio .lib .spi .IOIOConnectionBootstrap ;
58+ import ioio .lib .spi .IOIOConnectionFactory ;
59+ import ioio .lib .spi .NoRuntimeSupportException ;
5760
5861public class AccessoryConnectionBootstrap extends BroadcastReceiver implements IOIOConnectionBootstrap , IOIOConnectionFactory {
5962 private static final String TAG = AccessoryConnectionBootstrap .class .getSimpleName ();
6063 private static final String ACTION_USB_PERMISSION = "ioio.lib.accessory.action.USB_PERMISSION" ;
6164
62- private ContextWrapper activity ;
63- private final Adapter adapter ;
64- private Adapter .AbstractUsbManager usbManager ;
65+ private final Context activity ;
66+ private final UsbManagerAdapter .AbstractUsbManager usbManager ;
6567 private boolean shouldTryOpen = false ;
6668 private PendingIntent pendingIntent ;
6769 private ParcelFileDescriptor fileDescriptor ;
6870 private InputStream inputStream ;
6971 private OutputStream outputStream ;
7072
7173 public AccessoryConnectionBootstrap () throws NoRuntimeSupportException {
72- adapter = new Adapter ();
73- }
74-
75- //@Override
76- public void onCreate (ContextWrapper wrapper ) {
77- activity = wrapper ;
78- usbManager = adapter .getManager (wrapper );
74+ Log .d (TAG , "creating AccessoryConnectionBootstrap" );
75+ UsbManagerAdapter usbManagerAdapter = new UsbManagerAdapter ();
76+ activity = IOIOLoader .getContext ();
77+ usbManager = usbManagerAdapter .getManager (activity );
7978 registerReceiver ();
8079 }
8180
8281 //@Override
8382 public void onDestroy () {
84- unregisterReceiver ();
83+ activity . unregisterReceiver (this );
8584 }
8685
8786 @ Override
@@ -107,12 +106,9 @@ public synchronized void reopen() {
107106 notifyAll ();
108107 }
109108
110- //@Override
111- public synchronized void close () {
112- }
113-
114109 private synchronized void disconnect () {
115110 // This should abort any current open attempt.
111+ Log .d (TAG , "private disconnect" );
116112 shouldTryOpen = false ;
117113 notifyAll ();
118114
@@ -130,6 +126,7 @@ private synchronized void disconnect() {
130126 pendingIntent .cancel ();
131127 pendingIntent = null ;
132128 }
129+ Log .d (TAG , "leaving private disconnect" );
133130 }
134131
135132 @ Override
@@ -257,38 +254,38 @@ private boolean tryOpen() {
257254 }
258255 }
259256
257+ @ TargetApi (Build .VERSION_CODES .TIRAMISU )
260258 private void registerReceiver () {
261259 IntentFilter filter = new IntentFilter (ACTION_USB_PERMISSION );
262- activity .registerReceiver (this , filter );
263- }
264-
265- private void unregisterReceiver () {
266- activity .unregisterReceiver (this );
260+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
261+ activity .registerReceiver (this , filter , Context .RECEIVER_NOT_EXPORTED );
262+ }
267263 }
268264
269265 private void trySleep (long time ) {
270266 synchronized (AccessoryConnectionBootstrap .this ) {
271267 try {
272268 AccessoryConnectionBootstrap .this .wait (time );
273269 } catch (InterruptedException e ) {
270+ Log .e (TAG , e .toString ());
274271 }
275272 }
276273 }
277274
278- private static enum InstanceState {
275+ private enum InstanceState {
279276 INIT , CONNECTED , DEAD
280- };
277+ }
281278
282279 private class Connection implements IOIOConnection {
283280 private InstanceState instanceState_ = InstanceState .INIT ;
284281
285282 @ Override
286- public InputStream getInputStream () throws ConnectionLostException {
283+ public InputStream getInputStream () {
287284 return inputStream ;
288285 }
289286
290287 @ Override
291- public OutputStream getOutputStream () throws ConnectionLostException {
288+ public OutputStream getOutputStream () {
292289 return outputStream ;
293290 }
294291
@@ -316,6 +313,7 @@ public void waitForConnect() throws ConnectionLostException {
316313
317314 @ Override
318315 public void disconnect () {
316+ Log .d (TAG , "disconnect" );
319317 synchronized (AccessoryConnectionBootstrap .this ) {
320318 if (instanceState_ != InstanceState .DEAD ) {
321319 AccessoryConnectionBootstrap .this .disconnect ();
0 commit comments