USB accessory permissions and eventually missing connection intent.

1,320 views
Skip to first unread message

José Freitas

unread,
Oct 21, 2015, 10:17:18 AM10/21/15
to android-platform
Hello,

I have a project over kitkat that needs to stay connected to an usb accessory.
It's not an app for user-end devices, so we don't want the system.ui popup asking for the permission to connect to it. Also, sometimes after a restart or app update, a new intent connection never reaches the app, so I eventually lose the connection to the accessory.

My goal is to hard-set the permission in order to always receive the intent and get to be always connected.

In order to do that, I'm investigating the android source code, but I guess that the scope is to big, so I was wondering if someone could give me some leads.

1) How does Android knows that an app have being allowed to connect to an accessory?
I know that there's an xml file that stores this permission on android system, but even if I replace this xml file with a new one, it seems that the android system sort of ignores the file. So I am guessing it has a different way to store this. Also, it would be of great help if there's anyone that could point me the project and classes that handle this so I can investigate it more closely.

2) Would be there another reasons (besides permission) that would end up on the system not sending the usb_accessory intents to the app?

kind regards,
José


 

Dave Smith

unread,
Oct 25, 2015, 12:31:34 AM10/25/15
to android-platform
If you are able to move your project to Lollipop or above, there is an config value you can set to disable the user dialogs (assuming you don't require CTS compliance): config_disableUsbPermissionDialogs

The permissions dialogs themselves are found in the SystemUI package:

The UsbService is responsible for handling the requests received from these dialogs to grant permissions (look for grantAccessoryPermission(), setAccessoryPackage(), etc.):

The persistence layer is wrapped by the UsbSettingsManager:

You can see from the code that the defaults settings are stored in the per-user usb_device_manager.xml file (e.g. /data/system/users/0/), but they are also cached in memory. So if you are modifying the file by hand, the device would have to pick up those changes on a reboot. Only the default package data is stored here. The granted permissions themselves are only tracked in memory as they only live as long as the accessory is attached.

Cheers,
--
Dave Smith, PE
@devunwired

José Freitas

unread,
Nov 12, 2015, 10:06:09 AM11/12/15
to android-platform
Hi Dave,

sorry, I haven't received a notification of your answer. But thank you very much for taking your time to answer it!

I've eventually found out how it works and thought that maybe a solution for my problem would be to actually replace the system UsbService with my own implementation. Do you know if that's possible on a rooted device?
I just wrote a different post about this on the forum, and I'd gladly link the post here, but the topic-moderation, although reasonable, it's a showstopper. 

Thanks,
José

p.s.: Now I reminded myself to check the "email updates to me" option ;)

Dave Smith

unread,
Nov 12, 2015, 10:49:53 AM11/12/15
to android-...@googlegroups.com, José Freitas
Technically, service manager will allow a service to be registered multiple times, such that the new service with the same name will replace the old. However, the SELinux policy on service manager forbids any application process except system_server to modify that particular service name (“usb”):
https://android.googlesource.com/platform/external/sepolicy/+/android-5.1.1_r1/service_contexts (the contexts might change slightly for your Android version).

So, presuming you can modify system_server, you can modify the Java class registered as the USB service. This is possible from a custom system image, but likely not so much from just a simple root.

Cheers,
-- 
Dave Smith, PE
@devunwired

--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-platfo...@googlegroups.com.
To post to this group, send email to android-...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-platform.
For more options, visit https://groups.google.com/d/optout.

yo...@queuehop.com

unread,
May 7, 2017, 10:45:21 PM5/7/17
to android-platform, joserodol...@gmail.com
Hi Dave, 

I know this thread is pretty old but I want to implement the same function. I'm building an android embedded system and I don't want the user to see the permission dialog. Is it possible to programmatically get permission granted? 

Thanks
York
Reply all
Reply to author
Forward
0 new messages