medic / cht-gateway

Android SMS gateway app for CHT Applications
GNU Affero General Public License v3.0
84 stars 41 forks source link

135 configuration option autoenable wifi #161

Closed latin-panda closed 3 years ago

latin-panda commented 4 years ago

Description

This PR will:

  1. Prevent requesting again to Webapp, if there are more SMS or status updates to send and the previous request failed. It will wait for the WakeService to pick the SMS and status updates and retry sending again.
  2. Add a configuration option to turn on/off the autoenabling of WiFi -> The story: when the request to Webapp fails, Gateway will disable the WiFi and try to resend the request by using Mobile Data, after the job is done it will activate the WiFi again automatically.

This PR is dependant of this one: https://github.com/medic/medic-gateway/pull/166

To AT: My test device at the moment is Android 9. Can you please test this in older Android version? Thanks!

https://github.com/medic/medic-gateway/issues/135

Code review items

License

The software is provided under AGPL-3.0. Contributions to this project are accepted under the same license.

mrjones-plip commented 3 years ago

@latin-panda & @newtewt - I'm stuck as the APK @latin-panda built and published doesn't work.

In the meanwhile, I have my CHT instance up and running, exposed externally (thanks DIY SSH tunnels!) and have the stock version of Medic Gateway installed (v1.7.4) so I can see how things should be setup. I'll run through testing stuff this way and wait for a new APK.

NB - I'm testing on a Pixel 1 running Android 10, but @latin-panda said:

To AT: My test device at the moment is Android 9. Can you please test this in older Android version? Thanks!

I did just confirm my sim works in Android 5.x/Moto X (but oddly NOT my Android 7.x/Samsung SIII w/ Lineage?)

Update 1 : Oh - maybe the APK was built for older android API versions?! :facepalm: I'll check that now. But then again...This is specifically to test the "newer" android feature of "clever" wifi?

Update 2 : the APK doesn't work on android 5.x too, confirmed.

mrjones-plip commented 3 years ago

@latin-panda - no luck I don't think? I paired with @newtewt and we never saw WiFi fully turn off when the "Automatically enable Wi-Fi" checkbox was checked OR unchecked.

Maybe I need more explicit test steps though? I got a little confused with screenshots and doc you wrote. Here's the steps I took:

  1. have a WiFi access point (AP) you can easily remove Ethernet from so WiFi is on, but there's no connectivity. Start with Ethernet connected and working.
  2. connect phone to to this AP
  3. install medic-gateway test APK, leaving "automatically enable wifi" unchecked (see screenshot)
  4. ensure medic-gateway is talking to your dev CHT instance, use ngrok or something similar if need be. This includes make sure medic-gateway is your default SMS app and "Automatically enable Wi-Fi" checked
  5. disconnect ethernet from step 1
  6. send an SMS to the gateway such that it will try and connect to CHT to relay it. This message should fail to send initially
  7. note that wifi should turn off and cell data should be used. messages should succeed in being sent.

after step 5, nothing worked. The phone remained in "WiFi broken" state and no messages were sent. While there were notices in debug log about being turned on or of, WiFi was not actually turned on or off (or if it was, mobile data was never used). I left it this way for 60+ minutes :( You can see below SMS could be sent and received over mobile data connection when WiFi was in broken state.

It took a LONG time (15+ minutes?) for the phone to notice the WiFi was gone. The "WiFi all good" symbol was in the status bar for this time:

wifi good

Before step 5, I confirmed SMS sent to the gateway showed up in the CHT as expected.

Here's screenshots of the queue:

Here's screenshots of the Event log after step 5. Note that WiFi icon finally denotes phone is offline:

The last thing I did was turn off WiFi and zooooom all the queued messages went through.

newtewt commented 3 years ago

I think we are not going to be able to get this to work. As of Android 10 the method we call(setWifiEnabled) to disable wifi is deprecated and always returns false. This is for apps targeting 10 or greater. We are targeting 11.

This method was deprecated in API level 29. Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.

https://developer.android.com/reference/android/net/wifi/WifiManager#setWifiEnabled(boolean)

latin-panda commented 3 years ago

I can see our WifiConnectionManager class is using this inside, so newer wont work.

latin-panda commented 3 years ago

Closing PR because it's not compatible with Android 10+. We'll remove the WiFi logic in another PR.