Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings activity app lock #2926

Merged
merged 20 commits into from
Oct 21, 2022

Conversation

RoboMagus
Copy link
Contributor

Summary

This PR fixes issue #2877.
The following changes are included:

  • App-lock related processing is extracted from the WebViewActivity and unified such that the SettingsActivity can use it as well.
  • A blur view was added to the SettingsActivity when app lock is active.
  • Authentication for enabling the app-lock was migrated from the SettingsFragment to SettingsActivity, as it was impossible to have 2 different authentication initiators and callback methods in the same activity.
  • A 200ms grace period was added to the app-lock timeout, to allow switching between the settings and main activities.

Screenshots

Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#

Any other notes

@jpelgrom jpelgrom linked an issue Sep 27, 2022 that may be closed by this pull request
Copy link
Member

@jpelgrom jpelgrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSessionExpireMillis() in the integration repository is no longer used from outside the integration repository so it should switch to a private fun instead.

I see a lot of log statements, are all of those still useful outside development / when the app is released? For example in SettingsActivity.onResume I see it logging if the app lock is enabled, but this will also become obvious immediately after as the app is locked (or not) (and is also logged by the function you're calling).

For logging, try using Kotlin string templates to make the code cleaner, so "someValue: " + variable should be "someValue: $variable" instead (Android Studio offers a quick fix for this).

@jpelgrom
Copy link
Member

jpelgrom commented Oct 2, 2022

Testing on an older device (though not that old, ~3y flagship level) with commit c6dae45 I'm getting an authentication prompt when opening settings. Is that supposed to happen?

Logcat output
2022-10-02 11:55:27.562 26954-27100 WebviewActivity         io....stant.companion.android.debug  D  External bus {"type":"config_screen/show","id":5}
2022-10-02 11:55:27.574 26954-26954 IntegrationRepository   io....stant.companion.android.debug  D  setSessionExpireMillis(): 1664704527774
2022-10-02 11:55:27.574 26954-26954 IntegrationRepository   io....stant.companion.android.debug  D  setAppActive(): false
2022-10-02 11:55:27.576 26954-26954 IntegrationRepository   io....stant.companion.android.debug  D  setSessionExpireMillis(): 1664704527776
2022-10-02 11:55:27.576 26954-26954 IntegrationRepository   io....stant.companion.android.debug  D  setAppActive(): false
2022-10-02 11:55:27.576 26954-27162 FA                      io....stant.companion.android.debug  V  Recording user engagement, ms: 44722
2022-10-02 11:55:27.577 26954-27162 FA                      io....stant.companion.android.debug  E  Invalid google_app_id. Firebase Analytics disabled. See https://goo.gl/NAOOOI. provided id: mobilesdk_app_id
2022-10-02 11:55:27.577 26954-27162 FA                      io....stant.companion.android.debug  V  Connecting to remote service
2022-10-02 11:55:27.584 26954-27162 FA                      io....stant.companion.android.debug  V  Activity paused, time: 2550996
2022-10-02 11:55:27.596 26954-26982 FA                      io....stant.companion.android.debug  V  onActivityCreated
2022-10-02 11:55:27.626 26954-26954 UrlRepository           io....stant.companion.android.debug  D  Using external URL
2022-10-02 11:55:27.626 26954-26954 UrlRepository           io....stant.companion.android.debug  D  Using external URL
2022-10-02 11:55:27.627 26954-26954 chatty                  io....stant.companion.android.debug  I  uid=10385(io.homeassistant.companion.android.debug) identical 1 line
2022-10-02 11:55:27.628 26954-26954 UrlRepository           io....stant.companion.android.debug  D  Using external URL
2022-10-02 11:55:27.664 26954-26954 IntegrationRepository   io....stant.companion.android.debug  D  isAppLocked(). LockEnabled: true, appActive: false, expireMillis: 1664704527776, currentMillis: 1664704527664
2022-10-02 11:55:27.664 26954-27162 FA                      io....stant.companion.android.debug  V  Activity resumed, time: 2551084
2022-10-02 11:55:27.665 26954-27162 FA                      io....stant.companion.android.debug  V  Connection attempt already in progress
2022-10-02 11:55:27.786 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  --> POST https://mobile-apps.home-assistant.io/api/checkRateLimits
2022-10-02 11:55:27.786 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Content-Type: application/json; charset=UTF-8
2022-10-02 11:55:27.786 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Content-Length: 17
2022-10-02 11:55:27.786 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  {"push_token":""}
2022-10-02 11:55:27.786 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  --> END POST (17-byte body)
2022-10-02 11:55:27.854 26954-27112 UrlRepository           io....stant.companion.android.debug  D  localUrl is: false and usesInternalSsid is: false
2022-10-02 11:55:27.857 26954-26994 WM-Processor            io....stant.companion.android.debug  I  Moving WorkSpec (1a0dd5e3-675c-4930-95d9-bdce878bec3c) to the foreground
2022-10-02 11:55:27.863 26954-26954 IntegrationRepository   io....stant.companion.android.debug  D  isAppLocked(). LockEnabled: true, appActive: false, expireMillis: 1664704527776, currentMillis: 1664704527863
2022-10-02 11:55:27.867 26954-27112 SensorWorker            io....stant.companion.android.debug  D  Updating all Sensors in foreground.
2022-10-02 11:55:27.868 26954-27112 LastUpdate              io....stant.companion.android.debug  D  Last update is SensorWorker
2022-10-02 11:55:27.872 26954-27162 FA                      io....stant.companion.android.debug  D  Connected to remote service
2022-10-02 11:55:27.873 26954-27112 UrlRepository           io....stant.companion.android.debug  D  localUrl is: false and usesInternalSsid is: false
2022-10-02 11:55:27.874 26954-27112 UrlRepository           io....stant.companion.android.debug  D  localUrl is: false and usesInternalSsid is: false
2022-10-02 11:55:27.874 26954-27162 FA                      io....stant.companion.android.debug  V  Processing queued up service tasks: 2
2022-10-02 11:55:27.877 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  --> POST /api/webhook/d1ad019a235edc7781f5ef667d61e503189f3f0ccec7a4a00d08a5b9c9c42012
2022-10-02 11:55:27.877 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Content-Type: application/json; charset=UTF-8
2022-10-02 11:55:27.877 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Content-Length: 21
2022-10-02 11:55:27.877 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  {"type":"get_config"}
2022-10-02 11:55:27.877 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  --> END POST (21-byte body)
2022-10-02 11:55:27.892 26954-26954 WM-SystemFgDispatcher   io....stant.companion.android.debug  I  Started foreground service Intent { act=ACTION_START_FOREGROUND cmp=io.homeassistant.companion.android.debug/androidx.work.impl.foreground.SystemForegroundService (has extras) }
2022-10-02 11:55:27.913 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  <-- 200 OK /api/webhook/d1ad019a235edc7781f5ef667d61e503189f3f0ccec7a4a00d08a5b9c9c42012 (35ms)
2022-10-02 11:55:27.913 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Content-Type: application/json; charset=utf-8
2022-10-02 11:55:27.913 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Content-Length: 5658
2022-10-02 11:55:27.913 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Date: Sun, 02 Oct 2022 09:55:27 GMT
2022-10-02 11:55:27.913 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Server: Python/3.10 aiohttp/3.8.1
2022-10-02 11:55:27.914 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  [private data]
2022-10-02 11:55:27.914 26954-27072 okhttp.OkHttpClient     io....stant.companion.android.debug  I  <-- END HTTP (5658-byte body)
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  <-- 403 https://mobile-apps.home-assistant.io/api/checkRateLimits (170ms)
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  content-type: application/json; charset=utf-8
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  etag: W/"2c-gBARTr9vp3TOJjFE+ZvQBukBp+I"
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  function-execution-id: a93s0q6d8xtf
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  server: Google Frontend
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  x-cloud-trace-context: 56c8cb750f85c054082bf1a4e254b41e;o=1
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  accept-ranges: bytes
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  date: Sun, 02 Oct 2022 09:55:27 GMT
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  x-served-by: cache-ams21047-AMS
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  x-cache: MISS
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  x-cache-hits: 0
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  x-timer: S1664704528.798795,VS0,VE137
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  vary: x-fh-requested-host, accept-encoding
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  alt-svc: h3=":443";ma=86400,h3-29=":443";ma=86400,h3-27=":443";ma=86400
2022-10-02 11:55:27.957 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  content-length: 44
2022-10-02 11:55:27.958 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  {"errorMessage":"You did not send a token!"}
2022-10-02 11:55:27.958 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  <-- END HTTP (44-byte body)
2022-10-02 11:55:27.960 26954-27092 IntegrationRepository   io....stant.companion.android.debug  E  Unable to get notification rate limits
                                                                                                    retrofit2.HttpException: HTTP 403 
                                                                                                    	at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.kt:53)
                                                                                                    	at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:161)
                                                                                                    	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
                                                                                                    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
                                                                                                    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
                                                                                                    	at java.lang.Thread.run(Thread.java:923)
2022-10-02 11:55:27.960 26954-27092 SettingsPresenter       io....stant.companion.android.debug  D  Unable to get rate limits
2022-10-02 11:55:28.071 26954-27112 UrlRepository           io....stant.companion.android.debug  D  localUrl is: false and usesInternalSsid is: false
2022-10-02 11:55:28.075 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  --> POST /api/webhook/d1ad019a235edc7781f5ef667d61e503189f3f0ccec7a4a00d08a5b9c9c42012
2022-10-02 11:55:28.075 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Content-Type: application/json; charset=UTF-8
2022-10-02 11:55:28.075 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Content-Length: 277
2022-10-02 11:55:28.075 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  {"type":"update_sensor_states","data":[{"unique_id":"battery_temperature","state":27.0,"type":"sensor","icon":"mdi:battery","attributes":{}},{"unique_id":"battery_power","state":1.03,"type":"sensor","icon":"mdi:battery-plus","attributes":{"current":0.235351,"voltage":4.386}}]}
2022-10-02 11:55:28.075 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  --> END POST (277-byte body)
2022-10-02 11:55:28.117 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  <-- 200 OK /api/webhook/d1ad019a235edc7781f5ef667d61e503189f3f0ccec7a4a00d08a5b9c9c42012 (41ms)
2022-10-02 11:55:28.117 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Content-Type: application/json; charset=utf-8
2022-10-02 11:55:28.117 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Content-Length: 78
2022-10-02 11:55:28.117 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Date: Sun, 02 Oct 2022 09:55:28 GMT
2022-10-02 11:55:28.117 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  Server: Python/3.10 aiohttp/3.8.1
2022-10-02 11:55:28.118 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  {"battery_temperature": {"success": true}, "battery_power": {"success": true}}
2022-10-02 11:55:28.118 26954-27078 okhttp.OkHttpClient     io....stant.companion.android.debug  I  <-- END HTTP (78-byte body)
2022-10-02 11:55:28.124 26954-26986 WM-WorkerWrapper        io....stant.companion.android.debug  I  Worker result SUCCESS for Work [ id=1a0dd5e3-675c-4930-95d9-bdce878bec3c, tags={ io.homeassistant.companion.android.sensors.SensorWorker } ]
2022-10-02 11:55:28.126 26954-26954 WM-SystemFgDispatcher   io....stant.companion.android.debug  I  Stopping foreground service

@RoboMagus
Copy link
Contributor Author

Testing on an older device (though not that old, ~3y flagship level) with commit c6dae45 I'm getting an authentication prompt when opening settings. Is that supposed to happen?

Based on the logcat output I cannot say the exact scenario in which you got the authentication prompt.

The device I use and run my tests on is a Galaxy S10 running Android 12 (also not that new anymore). Based on the testing done on this device the following behavior was validated:
When the app is unlocked (webview) and you'd navigate to the SettingsActivity, you should not be presented with an authorization prompt. However when you let the app go to standby in the SettingsActivity and the app is re-opened after the session timeout is expired, the prompt should show. Then when navigating back to the webview after unlocking the app, the app would remain unlocked and no secondary authentication is required.

But I would not rule out that the 200ms grace period could be a bit to short for older devices?..

Copy link
Member

@jpelgrom jpelgrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the latest version and now it works correctly with the increased grace period. Hopefully the 1000ms works even on old and slow devices ☺️

@RoboMagus RoboMagus force-pushed the settings_activity_app_lock branch from c829495 to 312a535 Compare October 5, 2022 07:26
@RoboMagus RoboMagus force-pushed the settings_activity_app_lock branch from 7c9b2b0 to 58dc1c4 Compare October 10, 2022 05:35
@RoboMagus RoboMagus force-pushed the settings_activity_app_lock branch from 58dc1c4 to c1505af Compare October 13, 2022 09:18
@RoboMagus
Copy link
Contributor Author

As this PR is approved, can it be merged for the next beta?

@dshokouhi
Copy link
Member

As this PR is approved, can it be merged for the next beta?

right now we are holding off on merges as we are stuck in review with the play store for 2022.10.1 release

@RoboMagus
Copy link
Contributor Author

Got it, thanks for clarifying.

That also explains why I could only update to 2022.10.0 after I saw the 2022.10.1 release on github.

@JBassett
Copy link
Collaborator

@RoboMagus Sorry but there is now a merge conflict. Once resolved we can merge!

@JBassett JBassett merged commit 1f226a7 into home-assistant:master Oct 21, 2022
@RoboMagus RoboMagus deleted the settings_activity_app_lock branch October 23, 2022 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

App lock timeout not honored when app is left open on settings screen
5 participants