deptofdefense / AndroidTacticalAssaultKit-CIV

Other
4 stars 6 forks source link

Do not know how to create a service in a plugin #189

Open rob-baily opened 2 years ago

rob-baily commented 2 years ago

In the plugin SDK HelloWord plugin there is a NotificationService which is supposed to be run when the "Notification with Plugin Icon" button is pressed. It seems that the service is never created an run. I have debugged it to see that a ComponentName is being created from the call to startForegroundService but the NotificationService.onCreate event is never called and so no notification processing occurs.

I've tested this with ATAK versions from 4.2 to 4.4 on a tablet running Android 11 so it is possible that the platform could be the issue but I don't know. I've been unable to run ATAK on any emulators through Android Studio so not able to completely debug the Android code.

This seems somewhat related to #104 but I would like to find out in general how services can be created in ATAK plugins. Are there any examples available elsewhere or is this not possible or should another methodology be used? Thanks for any assistance or advice.

bisgroves commented 2 years ago

Unable to reproduce on Android 12 (S10) running ATAK 4.6

rob-baily commented 2 years ago

Where do you get version 4.6? I only see releases up to 4.4. Can it be reproduced with any released version?

bisgroves commented 2 years ago

Still working back to 4.4 to see if I can reproduce it there. Sorry for the delay.

rob-baily commented 2 years ago

No worries. Let me know if I can provide more info. Also if there is some other version I can try I am happy to try that assuming I don't need to do a full ATAK build and can use an SDK build of some type.

I had been trying something similar as #104 trying to get an MQTT service active and then I found the basic example did not work for me.

rob-baily commented 2 years ago

@bisgroves Checking back to see if you have any updates or if there is anything I can do to facilitate gathering information. Please let me know. Thank you!

bisgroves commented 2 years ago

Please take a look at the helloworld example included in the open source.

rob-baily commented 2 years ago

@bisgroves I just tested this again with ATAK 4.5.0.1 on an Android 10 phone and ran into the same issue as I noted in my initial comment. I debug and I can get to the line in the onClick listener where startForegroundService is called but the onCreate method for NotificationService is never called. I also do not see any log messages related to this service as there should be if it is working correctly.

Above you indicated you were not able to recreate this on version 4.6. Is that the correct version or was it supposed to be 4.5? What version of hardware and Android version are you using? I would like to see if I can get it working on anything else.

As noted above we also have the same issue on an Android 11 tablet and as noted in the mentioned issue people have not been able to have the MQTT service library run successfully under an ATAK plugin. Not sure if this is because of the plugin infrastructure in general or something specific with how ATAK

rob-baily commented 2 years ago

@bisgroves I double checked this and we are seeing the notification come up from the onCreate and I can actually see the log messages if I use "No filters" when in the Logcat tab.

However I do see these message regarding the service and the fact that it killed the service because it did not call the startForground method (since it was kicked off with startForegroundService)

2022-06-20 13:47:23.778 8376-8405/? W/ActivityManager: Bringing down service while still waiting for start foreground: ServiceRecord{df857a1 u0 com.atakmap.android.helloworld.plugin/com.atakmap.android.helloworld.notification.NotificationService} 2022-06-20 13:47:23.940 8376-8405/? E/ActivityManager: ANR in com.atakmap.android.helloworld.plugin PID: 4584 Reason: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{df857a1 u0 com.atakmap.android.helloworld.plugin/com.atakmap.android.helloworld.notification.NotificationService}

I am trying out using startForeground in the NotificationService and it looks like it works if you include this line in the manifest: <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

I will do a little more research on the MQTT service and see if I can determine what is going on there and post updates there.

Do you think it makes sense to update the plugin example to use startForeground and the permission since it was called with startForegroundService?

bisgroves commented 2 years ago

The helloworld plugin has been updated as of 4.6.1. Thank you.

rob-baily commented 2 years ago

@bisgroves I have done a bit more research and experimentation with this and have some information. What I have found is that it seems that a new process is created for the service even when the "android:process" tag is not included in the manifest definition for the service. What I have seen is that in order for the service to run properly the starting service intent must use the setPackage call with the package name that is defined in the Android manifest. This is what the HelloWorld example does and when the service is created it runs in a separate process with this package name. The problem I have been seeing and the one noted in the MQTT issue is that you can't bind to this service using a local binder because you end up with an instance of BinderProxy instead of the local binder class. I have since noticed that the HelloWorld example service actually returns null in its onBind function and there is no attempt to bind and communicate with the service after it is created. It appears that AIDL is required to use any services within the Android plugin architecture. Not sure if there is any good way to document this and possibly provide an example using AIDL. I could possibly provide the example as I have used some other services which are using AIDL. Please let me know if you think this is worthwhile.

One more question before I close this, where is the source code for 4.6.x? You are saying the plugin has been updated but I do not see it in commits in this repo. I would like to be able to check out 4.6 when needed to check for compatibility.

bisgroves commented 2 years ago

4.6 has not been published yet to the open source and I expect it to happen in the coming weeks.  We have a few programs that do use aidl and correctly provide an onBind but I would always accept a consise example to add to helloworld.

rob-baily commented 2 years ago

@bisgroves I have done a local update to modify the NotificationService to use AIDL and binding along with some interaction with the service. In terms of getting that to you I have a couple questions:

bisgroves commented 2 years ago

I would recommend forking now and producing a pull request.

rob-baily commented 2 years ago

@bisgroves Please review the pull request when you have a chance and provide any feedback. Thank you!

bisgroves commented 2 years ago

I ended up coming up with a slightly different version of what you submitted and it will be in the helloworld plugin for 4.6 and 4.7

rob-baily commented 2 years ago

Ok, I will wait for those to be published and then confirm to close out this issue and the pull request.

gk1016 commented 2 years ago

@bisgroves, any estimate on the release for the civilian SDK (4.6 or 4.7)? We are very interested in how AIDL implemented will be implemented post 4.5.

pbolduc123 commented 2 years ago

@gk1016, The ATAK 4.7.0 SDK is currently on tak.gov on the ATAK product page in the developper resources section (https://tak.gov/products/atak-civ).

gk1016 commented 2 years ago

@pbolduc123 I appreciate the help! I’m seeing the APK download but not the civilian SDK. I dug though tak.gov , and for the civilian version, it directs me to this GitHub repository. If they have moved the civilian SDK to tak.gov, I need to figure out why I’m missing that resource in my account.

pbolduc123 commented 2 years ago

@gk1016, If you scroll down on the page I linked in my previous post, you will see a section with 4 tabs, open the developer resources and you should find the SDK.

image

gk1016 commented 2 years ago

@pbolduc123 Thank you! It’s in there. I’m clearly an idiot and need to rethink my career decisions. I blame it on my public school education….once again, thank you for your help!