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

[GSoC] Add preference to allow/warn if using metered connections #12121

Merged
merged 5 commits into from
Aug 27, 2022

Conversation

BrayanDSO
Copy link
Member

Pull Request template

Purpose / Description

Internet may be expensive 💵💰💲💸

Fixes

Fixes #10706

Approach

  • Clean things a bit
  • Create a "Advanced" category under "Sync", so things get a little more organized with the increasing number of preferences
  • Add a preference to allow sync on metered connections (off by default)
    • If not allowed, auto sync under metered connections won't occur + the user will be warned if they trigger a manual sync under a metered connection
    • I'm quite open to string changes

How Has This Been Tested?

Screen_Recording_20220820-200646_AnkiDroid.mp4

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

@BrayanDSO BrayanDSO added Review High Priority Request for high priority review Strings labels Aug 20, 2022
@BrayanDSO BrayanDSO changed the title [GSoC] Add preference to [GSoC] Add preference to allow/warn if using metered connections Aug 20, 2022
Copy link
Member

@lukstbit lukstbit left a comment

Choose a reason for hiding this comment

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

Looks good with two minor issues.

AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt Outdated Show resolved Hide resolved
AnkiDroid/src/main/res/values/03-dialogs.xml Show resolved Hide resolved
@lukstbit lukstbit added the Needs Author Reply Waiting for a reply from the original author label Aug 21, 2022
@lukstbit lukstbit added Needs Second Approval Has one approval, one more approval to merge and removed Needs Author Reply Waiting for a reply from the original author labels Aug 21, 2022
AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidJsAPI.kt Outdated Show resolved Hide resolved
AnkiDroid/src/main/java/com/ichi2/utils/NetworkUtils.kt Outdated Show resolved Hide resolved
AnkiDroid/src/main/java/com/ichi2/utils/NetworkUtils.kt Outdated Show resolved Hide resolved
AnkiDroid/src/main/java/com/ichi2/utils/NetworkUtils.kt Outdated Show resolved Hide resolved
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
cm.isActiveNetworkMetered
} catch (e: Exception) {
Timber.w(e, "Exception obtaining metered connection - assuming metered connection")
Copy link
Member

Choose a reason for hiding this comment

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

I am very much against it.
I prefer that we use an enum with three values, yes, no, unknown.

I'll admit that in any case, we will never get real valid information, and that's a shame. So we should never be assertive one way or another.
This is requiring the user to grasp a concept that is not necessarily straighforward. I prefer that we are able to tell them honestly when we can't know instead of mixing it with the case where we are sure.
I agree it makes sense to behave in the same way in both case, and ask for confirmation. However, I hope we can have different warning messages

I'm not sure for JS how to transform it. I'd consider using two boolean function, except that I would not want that each card do the request twice. Is there an easy way to send an enum to JS? Otherwise, I'd want the function to be
isConnectionNotMetered with 0 meaning false, 1 true, and 2 "unknown". So that if a code just do if(!isConnectionNotMetered) {useNetwork} it uses network only if it is certain it's okay to do so.

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually just copied and pasted the method, a 100% straight extraction. It wasn't my goal to refactor it on this PR.

But I agree if your points. Looking on the source code, I only expect it to return an error in the case there isn't a specific permission on the manifest. AnkiDroid's manifest has it, so I don't think that the try catch is necessary.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I didn't realize on first read that you were extracting a method. Then I guess fine.

I still believes my remarks make sense, but it can wait then.

AnkiDroid/src/main/java/com/ichi2/utils/NetworkUtils.kt Outdated Show resolved Hide resolved
AnkiDroid/src/main/java/com/ichi2/utils/NetworkUtils.kt Outdated Show resolved Hide resolved
AnkiDroid/src/main/java/com/ichi2/utils/NetworkUtils.kt Outdated Show resolved Hide resolved
)
)
)
val shouldFetchMedia = preferences.getBoolean("syncFetchesMedia", true)
Copy link
Member

Choose a reason for hiding this comment

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

I'd appreciate for a next time that you do this in multiple commit.

  • Extracting variables in a commit,
  • extracting doSync
  • then taking into account whether it's metered

We can put it in a separate PR and squash it, but it would really help to review and save me time

Copy link
Member Author

Choose a reason for hiding this comment

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

doSync is mostly only useful inside sync(), so to reduce boilerplate, I've kept it as a nested function. Added a javadoc to make it more clear. Let me know if that is fine

@mikehardy
Copy link
Member

Took a conflict just now, and I'm not sure what the state on it is? Is it ready for re-review from previous reviewers?
Only asking because I'm scouring for things to merge before doing a new build and this caught my eye 👀

@BrayanDSO
Copy link
Member Author

Yes, it is ready for review. Just rebased to solve conflicts. Only the last 3 commits are new.

Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

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

This looks good to me, my limited data friends (and myself on occasion - if I'm syncing a big deck) will appreciate this.

cm.isActiveNetworkMetered
} catch (e: Exception) {
Timber.w(e, "Exception obtaining metered connection - assuming metered connection")
true
Copy link
Member

Choose a reason for hiding this comment

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

@Arthur-Milchior in your review you wanted an enum, with this refactor here to a simple util method, I think this is clean and correct and just binary. The assertion it is correct is that from the perspective of consuming people's money (using metered data) defaulting to a cautious stance of assuming it is metered when we catch an (almost entirely impossible) exception is the right one.

So I think this may answer your review comments well

Copy link
Member

Choose a reason for hiding this comment

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

As far as I can tell, this commit does not change the slightest the actual code used. It just ensures that we remove the object from the file.

If most people agree that as soon as we get an exception, we warn in the same way, fine by me I guess.
Anyway, this PR is already a great improvement, and if I can convince peolpe, the change can be done later easily.

One regret I have, but that always can be dealt with later too is that we should send a crash report. I'd love to know if it's something that actually occurs, hether it's related to specific version of the API or specific manufacturers. Also maybe what exceptions we actually get. So that maybe later we could get a better catching and warning

(By the way, this did not seems to require an extra commit. You could just have directly create the file without any enclosing "object NetworkUtils" when you created the file

@lukstbit lukstbit dismissed their stale review August 25, 2022 08:40

Issue resolved

Copy link
Member

@Arthur-Milchior Arthur-Milchior left a comment

Choose a reason for hiding this comment

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

Generally approve.
I believe we could save two commits.
If we squash, feel free to squash.
If we don't, then feel free to merge as soon as the correction is done

.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
cm.isActiveNetworkMetered
} catch (e: Exception) {
Timber.w(e, "Exception obtaining metered connection - assuming metered connection")
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I didn't realize on first read that you were extracting a method. Then I guess fine.

I still believes my remarks make sense, but it can wait then.

// Check whether the option is selected, the user is signed in and last sync was AUTOMATIC_SYNC_TIME ago
// (currently 10 minutes)
// Check whether the option is selected, the user is signed in, last sync was AUTOMATIC_SYNC_TIME ago
// (currently 10 minutes), and is not under a metered connection (if not allowed by preference)
Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with the commint "Add preference to check if sync is on metered connection" except for his title
You're adding a permission request, that should be in the commit message.
Also, you are not adding adding a preference but taking it into account. I suggest it may be nice to indicates both fact

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the heads up. Changed the commit message to:

Add "Allow sync on metered connections" preference
If disabled, auto-sync isn't triggered on metered connections and the user is asked if they want to continue if they trigger a manual sync
If enabled, sync occurs normally

cm.isActiveNetworkMetered
} catch (e: Exception) {
Timber.w(e, "Exception obtaining metered connection - assuming metered connection")
true
Copy link
Member

Choose a reason for hiding this comment

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

As far as I can tell, this commit does not change the slightest the actual code used. It just ensures that we remove the object from the file.

If most people agree that as soon as we get an exception, we warn in the same way, fine by me I guess.
Anyway, this PR is already a great improvement, and if I can convince peolpe, the change can be done later easily.

One regret I have, but that always can be dealt with later too is that we should send a crash report. I'd love to know if it's something that actually occurs, hether it's related to specific version of the API or specific manufacturers. Also maybe what exceptions we actually get. So that maybe later we could get a better catching and warning

(By the way, this did not seems to require an extra commit. You could just have directly create the file without any enclosing "object NetworkUtils" when you created the file

@BrayanDSO BrayanDSO force-pushed the meteredSync branch 2 times, most recently from 3cc6fb9 to 844dcbf Compare August 26, 2022 16:09
If disabled, auto-sync isn't triggered on metered connections and the user is asked if they want to continue if they trigger a manual sync

If enabled, sync occurs normally
@mikehardy
Copy link
Member

I think we are good to go with the standard rebase merge here? I'll do a strings run now

@mikehardy mikehardy merged commit f67bb49 into ankidroid:main Aug 27, 2022
@github-actions github-actions bot removed the Review High Priority Request for high priority review label Aug 27, 2022
@github-actions github-actions bot removed the Needs Second Approval Has one approval, one more approval to merge label Aug 27, 2022
@github-actions github-actions bot added this to the 2.16 release milestone Aug 27, 2022
@BrayanDSO BrayanDSO deleted the meteredSync branch August 27, 2022 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature request] Option to disable auto sync with metered connections
5 participants