1212import android .os .Handler ;
1313import android .os .Looper ;
1414
15- import java .util .concurrent .CountDownLatch ;
16- import java .util .concurrent .TimeUnit ;
17- import java .util .concurrent .atomic .AtomicBoolean ;
18-
1915import ioio .lib .spi .Log ;
2016import ioio .smallbasic .IOIOException ;
2117
2218public class AccessoryPermissionCheck extends BroadcastReceiver {
2319 private static final String TAG = AccessoryPermissionCheck .class .getSimpleName ();
24- private static final String ACTION_USB_PERMISSION = "android.hardware.usb.action.USB_PERMISSION" ;
25- private static final String PERMISSION_ERROR = "Not permitted to use usb accessory." ;
26- private static final long TIMEOUT_SECS = 30 ;
27- private final CountDownLatch latch ;
28- private final AtomicBoolean permitted ;
20+ private static final String ACTION_USB_PERMISSION = "ioio.smallbasic.android.USB_PERMISSION" ;
21+ private static final String PERMISSION_ERROR = "Not permitted" ;
2922
3023 @ TargetApi (Build .VERSION_CODES .TIRAMISU )
3124 public AccessoryPermissionCheck () {
3225 Log .d (TAG , "AccessoryPermissionCheck entered" );
33- this .permitted = new AtomicBoolean (false );
34- this .latch = new CountDownLatch (1 );
35-
3626 UsbManager usbManager = (UsbManager ) IOIOLoader .getContext ().getSystemService (Context .USB_SERVICE );
3727 UsbAccessory [] accessories = usbManager .getAccessoryList ();
3828 UsbAccessory accessory = (accessories == null ? null : accessories [0 ]);
@@ -43,43 +33,27 @@ public AccessoryPermissionCheck() {
4333 new Handler (Looper .getMainLooper ()).post (() -> {
4434 Context context = IOIOLoader .getContext ();
4535 IntentFilter filter = new IntentFilter (ACTION_USB_PERMISSION );
46- context .registerReceiver (this , filter , Context .RECEIVER_EXPORTED );
36+ filter .setPriority (IntentFilter .SYSTEM_HIGH_PRIORITY - 1 );
37+ context .registerReceiver (this , filter , Context .RECEIVER_NOT_EXPORTED );
4738 int flags = PendingIntent .FLAG_IMMUTABLE ;
4839 Intent intent = new Intent (ACTION_USB_PERMISSION );
4940 PendingIntent pendingIntent = PendingIntent .getBroadcast (context , 0 , intent , flags );
5041 usbManager .requestPermission (accessory , pendingIntent );
5142 });
52- Log .d (TAG , "waiting for permission" );
53- try {
54- if (!latch .await (TIMEOUT_SECS , TimeUnit .SECONDS )) {
55- permissionError ();
56- } else {
57- IOIOLoader .getContext ().unregisterReceiver (this );
58- }
59- }
60- catch (InterruptedException e ) {
61- permissionError ();
62- throw new IOIOException (PERMISSION_ERROR );
63- }
64- if (!permitted .get ()) {
65- permissionError ();
66- }
43+ // for some reason using a latch caused an ANR here
44+ Log .d (TAG , "requesting permission" );
45+ throw new IOIOException (PERMISSION_ERROR );
6746 }
6847 }
6948
7049 @ Override
71- public void onReceive (Context context , Intent intent ) {
72- Log .d (TAG , intent . getAction () );
73- synchronized ( this ) {
74- if ( ACTION_USB_PERMISSION . equals ( intent . getAction ())) {
75- permitted . set ( intent . getBooleanExtra ( UsbManager . EXTRA_PERMISSION_GRANTED , false ));
76- latch . countDown ( );
77- }
50+ public synchronized void onReceive (final Context context , Intent intent ) {
51+ Log .d (TAG , "onReceive entered" );
52+ if ( ACTION_USB_PERMISSION . equals ( intent . getAction ()) ) {
53+ final BroadcastReceiver receiver = this ;
54+ new Handler ( Looper . getMainLooper ()). post (() -> {
55+ context . unregisterReceiver ( receiver );
56+ });
7857 }
7958 }
80-
81- private void permissionError () {
82- IOIOLoader .getContext ().unregisterReceiver (this );
83- throw new IOIOException (PERMISSION_ERROR );
84- }
8559}
0 commit comments