Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Ref #3939: Add support to Apple Screen Time Limits #7639

Closed

Conversation

domcorvasce
Copy link

@domcorvasce domcorvasce commented Jun 25, 2023

Summary of Changes

This pull request fixes #3939. It adds support for:

tracking time spent on individual websites (unless you're in incognito mode)
enforcing usage limits per single web page

The Screen Time API is not well documented. I benefitted from reading mozilla-mobile/firefox-ios#15111.

There is no way to test this feature using the Simulator—I've tried but Screen Time does not work. You need a real device.

Submitter Checklist:

  • Unit Tests are updated to cover new or changed functionality
  • User-facing strings use NSLocalizableString()
  • New or updated UI has been tested across:
    • Light & dark mode
    • Different size classes (iPhone, landscape, iPad)
    • Different dynamic type sizes

Test Plan:

Background

  1. Build the app on your real device
  2. Go to Settings > Screen Time > turn it on if you didn't already
  3. Go to Settings > Screen Time > App limits > Add Limit and add a one minute limit on a website of your choice

Test 1

  1. Given you visit the website in a non-private tab
  2. Given you browse the website for one minute
  3. Then you should see the Screen Time limit over view

Test 2

  1. Given you visit the website in a private tab
  2. Given you browse the website for one minute
  3. Then you won't see the Screen Time limit over view nor the page usage will appear in the Screen Time activity log

Test 3

  1. Given you visit the website in a non-private tab
  2. Given you switch to an empty tab
  3. Given you wait for one minute
  4. Then you won't see a dedicated entry for the internal URL in the Screen Time activity log

Test 4

  1. Given you visit the website in a non-private tab
  2. Given you switch to an empty tab
  3. Given you wait for one minute to show the Screen Time block view
  4. Then the ST block view should go away when you change the current tab URL

Test 5

  1. Given you visit the website in a non-private tab
  2. Given you browse the website for one minute
  3. Given you switch to a private tab and visit the same website
  4. Then you should see the Screen Time limit over view

Screenshots:

Reviewer Checklist:

  • Issues include necessary QA labels:
    • QA/(Yes|No)
    • bug / enhancement
  • Necessary security reviews have taken place.
  • Adequate unit test coverage exists to prevent regressions.
  • Adequate test plan exists for QA to validate (if applicable).
  • Issue and pull request is assigned to a milestone (should happen at merge time).

@domcorvasce domcorvasce requested a review from a team as a code owner June 25, 2023 15:44
@iccub
Copy link
Contributor

iccub commented Jun 26, 2023

Code looks legit, thanks @domcorvasce.
This PR will stay open for a bit, we have to test it well then make a security review for it

@iccub iccub added needs security review blocked If a ticket is blocked for some reason, if not using a sub-block label, please provide info in issue labels Jun 26, 2023
@soner-yuksel soner-yuksel requested a review from iccub June 26, 2023 17:22
Copy link
Contributor

@iccub iccub left a comment

Choose a reason for hiding this comment

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

There's one change required.
Currently this implementation screen limit covers entire screen because it's added to the BrowserViewController.
We have to add it somewhere at the webview constraints level so the user can change to another tab.
It would also have to be one STWebpageController per tab.
I will try to update this code, find a good place for it

@domcorvasce
Copy link
Author

domcorvasce commented Jul 6, 2023

Currently this implementation screen limit covers entire screen because it's added to the BrowserViewController.
We have to add it somewhere at the webview constraints level so the user can change to another tab.

I was going for Safari behaviour but I double-checked and Safari attaches the controller to the specific tab.
So, yeah, we need to align that. I'll give it a try on the weekend ⚡

It would also have to be one STWebpageController per tab.

Yet we only actively track the active tabs. suppressUsageRecording should be enabled once a tab becomes inactive.

@domcorvasce
Copy link
Author

domcorvasce commented Jul 7, 2023

@iccub I moved the logic into Tab and I've checked that tabs switch is possible. It now follows Safari's behaviour (check top PR description for the up-to-date screenshot).

One quick note about Tab.swift:180: even though we suppress usage recording, assuming the user already maxed out the available time we need to set the active URL to null when within a private tab so the ST view is not shown.

PS: Yes, I double-checked that inactive tabs don't count towards ST.

Copy link
Contributor

@iccub iccub left a comment

Choose a reason for hiding this comment

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

The placement is good now, I am able to switch tabs.

I noticed one bug that bypasses the screen limit though.

STR:

  1. Open new tab
  2. Enter url of the website you passed the limit on
  3. It will show screen limit screen correctly
  4. Now press back arrow to take you to NTP, then forward back to the website

Result is the website does not show the limit screen anymore, you can refresh and navigate within that tab without any limits

Don't feel pressured to fix that if you don't want to, we can try to find the cause of the problem too

@iccub
Copy link
Contributor

iccub commented Jul 11, 2023

I also noticed the screen limit is ignored in private mode. Safari does not do that. It is a product decision however, we have to decide on that behavior.
Should be fine since all screen time data is saved locally only

@domcorvasce
Copy link
Author

domcorvasce commented Jul 11, 2023

Result is the website does not show the limit screen anymore, you can refresh and navigate within that tab without any limits

Good catch. Now I’m eager to learn why it’s happening 😅
I see the following error message when switching back to the initial page (and it seems to only happen with that one):

The connection to service with pid XYZ named com.apple.ScreenTime.ScreenTimeWebExtension.viewservice
was interrupted, but the message was sent over an additional proxy
and therefore this proxy has become invalid.

I also noticed the screen limit is ignored in private mode. Safari does not do that. It is a product decision however, we have to decide on that behavior.

I see. They’re attaching the ST controller to block websites that hit the usage limit in non private tabs but they don’t actively track page usage in private tabs (try browsing a website in private mode and it won’t show up in your Screen Time breakdown)

This commit only allows us to show the ST block in private tabs.
Page usage is still not tracked while browsing privately (see `Tab.swift:312`).
@aherrera31
Copy link

Question -- would this update also replicate Safari's behavior of syncing time spent on individual websites with ScreenTime, as opposed to aggregating and simply showing cumulative time spent on "Brave Browser"?

Thanks for building this! Have been waiting for this feature for a loooooong time! You all rock!

@domcorvasce
Copy link
Author

domcorvasce commented Jul 14, 2023

Would this update also replicate Safari's behavior of syncing time spent on individual websites with ScreenTime, as opposed to aggregating and simply showing cumulative time spent on "Brave Browser"?

Yep

@aherrera31
Copy link

Would this update also replicate Safari's behavior of syncing time spent on individual websites with ScreenTime, as opposed to aggregating and simply showing cumulative time spent on "Brave Browser"?

Yep

Grazie mille!

@iccub
Copy link
Contributor

iccub commented Aug 3, 2023

small update on this one. i ran this branch for a while and i've been getting multiple random crash related to screen time. we are investigating the probme

@link1day
Copy link

link1day commented Aug 7, 2023

I have Brave browser freshly downloaded for iOS. Screentime /time limit websites is not working. Is there a solution on this page, can someone explain further? Do I need to just download something from here to add to that browser?

@bgala
Copy link

bgala commented Aug 18, 2023

Can this support extent to private browsing mode?

@iccub
Copy link
Contributor

iccub commented Aug 18, 2023

@bgala the time limits I hope yes.
Recording web usage no.

We will try to match what Safari does

@iccub
Copy link
Contributor

iccub commented Aug 29, 2023

There's been couple bigger problems and I made a new one here #7983

@iccub iccub closed this Aug 29, 2023
@iccub iccub mentioned this pull request Aug 29, 2023
11 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked If a ticket is blocked for some reason, if not using a sub-block label, please provide info in issue needs security review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement screentime
6 participants