title: Android 10 stacked USB permission when using intent-filter to start activity when USB attached
body: <p><strong>My setup</strong></p>
  
  <ul>
  <li>Pixel 3/Android 10 (but same problem on Samsung Galaxy S9/Android 9)</li>
  <li>App targets API 29 (Android 10)</li>
  <li>A USB device</li>
  </ul>
  
  <p>On Android 10 there has been a change to the permission model, which means reading serial number for a USB device requires an explicit user permission, see: <a href="https://developer.android.com/preview/privacy/data-identifiers#usb-serial-user-permission" rel="nofollow noreferrer">https://developer.android.com/preview/privacy/data-identifiers#usb-serial-user-permission</a>. </p>
  
  <p>So when receiving an intent that a USB device is attached I automatically request a permission to read the serial number (results in a permission dialog: "Allow X to access Virtual COM Port ?").</p>
  
  <p><a href="https://i.stack.imgur.com/S4gZj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S4gZj.png" alt="Permission dialog 1"></a></p>
  
  <p>To also be able to launch the application when an appropriate USB device was attached it was previously possible to also do this (AndroidManifest.xml):</p>
  
  <pre><code>
          &lt;activity
                  android:name="MainActivity"
                  android:screenOrientation="portrait"
                  &gt;
  
              &lt;intent-filter&gt;
  
                  &lt;action android:name="android.intent.action.MAIN"/&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER"/&gt;
  
              &lt;/intent-filter&gt;
  
              &lt;intent-filter&gt;
                  &lt;action
                          android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                          /&gt;
              &lt;/intent-filter&gt;
  
              &lt;meta-data
                      android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                      android:resource="@xml/my_device_filter"
                      /&gt;
  
          &lt;/activity&gt;
  </code></pre>
  
  <p>... which results in a permission request dialog also when a new USB device is attached ("Open X to handle Com Port?").</p>
  
  <p><a href="https://i.stack.imgur.com/MQP1Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MQP1Y.png" alt="Permission dialog #2"></a></p>
  
  <p>But, as previously mentioned, the application must also request permission for reading the serial number which results in a second permission dialog. </p>
  
  <p><strong>These two dialogs are now stacked and if agreeing to the first dialog ("Open X to handle Virtual COM Port ?"), the second one ("Allow X to access COM Port ?") is dismissed automatically leading to broken functionality.</strong></p>
  
  <p><strong>Any suggestions on how to handle these two permissions?</strong></p>