-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Improve the dialog's features related to BackendSyncException #17017
Draft
peyman79
wants to merge
1
commit into
ankidroid:main
Choose a base branch
from
peyman79:Improve-clock-sync-error-dialog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+38
−2
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ import android.app.Activity | |
import android.app.Dialog | ||
import android.content.Context | ||
import android.content.DialogInterface | ||
import android.content.Intent | ||
import android.provider.Settings | ||
import android.view.WindowManager | ||
import android.view.WindowManager.BadTokenException | ||
import androidx.annotation.StringRes | ||
|
@@ -60,6 +62,9 @@ import net.ankiweb.rsdroid.exceptions.BackendNetworkException | |
import net.ankiweb.rsdroid.exceptions.BackendSyncException | ||
import org.jetbrains.annotations.VisibleForTesting | ||
import timber.log.Timber | ||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
import java.util.Locale | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
import kotlin.coroutines.resume | ||
|
@@ -169,8 +174,12 @@ suspend fun <T> FragmentActivity.runCatching( | |
Timber.w(exc, errorMessage) | ||
exc.localizedMessage?.let { showSnackbar(it) } | ||
} | ||
is BackendNetworkException, is BackendSyncException -> { | ||
// these exceptions do not generate worthwhile crash reports | ||
is BackendSyncException -> { | ||
// this exceptions do not generate worthwhile crash reports | ||
showInvalidClockDialog(this, exc.localizedMessage!!, exc) | ||
} | ||
is BackendNetworkException -> { | ||
// this exceptions do not generate worthwhile crash reports | ||
showError(this, exc.localizedMessage!!, exc, false) | ||
} | ||
is BackendException -> { | ||
|
@@ -278,6 +287,31 @@ fun showError(context: Context, msg: String, exception: Throwable, crashReport: | |
} | ||
} | ||
|
||
fun showInvalidClockDialog(context: Context, msg: String, exception: Throwable) { | ||
if (throwOnShowError) throw IllegalStateException("throwOnShowError: $msg", exception) | ||
try { | ||
val currentTime = LocalDateTime.now() | ||
val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a", Locale.getDefault()) | ||
val formattedTime = currentTime.format(dateFormatter) | ||
val invalidClockMessage = context.getString(R.string.InvalidClockDialog) | ||
|
||
val fullMessage = "$msg\n\n$invalidClockMessage\n$formattedTime" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 normally I would be wary of string concatenation as it may break RTL languages, but it is on a new line so it should be okay |
||
AlertDialog.Builder(context).show { | ||
title(R.string.vague_error) | ||
message(text = fullMessage) | ||
positiveButton(R.string.dialog_go_to_setting) { | ||
// Navigate to Date and Time settings | ||
val intent = Intent(Settings.ACTION_DATE_SETTINGS) | ||
context.startActivity(intent) | ||
} | ||
negativeButton(R.string.dialog_cancel) | ||
} | ||
} catch (ex: BadTokenException) { | ||
// issue 12718: activity provided by `context` was not running | ||
Timber.w(ex, "unable to display error dialog") | ||
} | ||
} | ||
|
||
/** In most cases, you'll want [AnkiActivity.withProgress] | ||
* instead. This lower-level routine can be used to integrate your own | ||
* progress UI. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you certain that the only BackendSyncException is from invalid device time?
Even if you are certain of that now are we certain that will remain true forever?
Is there some way to constrain this new dialog to present only when we are certain that it was from an invalid device time?
Perhaps a string match on exception message "your clock is not set to correct time"? But I believe this message comes through localized and may be translated to whatever we set the backend language too (which is ideally the user's selected language) which means a string search is not valid
So, a specific question for @dae then: is BackendError.Kind.SYNC_OTHER_ERROR a set of errors of different sub-types - sort of a grab bag - or is it always device time related? If it is multiple different types is there any way to differentiate with certainty whether a specific SYNC_OTHER_ERROR is device time related ?
I trolled through and I couldn't see anything specific about BackendError.Kind.SYNC_OTHER_ERROR being constrained only to device time issues:
https://github.com/ankitects/anki/blob/73c97de5d022abcec0ab1fb70921c6bf813cfa28/proto/anki/backend.proto#L33
For this PR, if this is always a device time error, great. If it is not always device time but we can differentiate with certainty, then we need to only show this specific dialog when we are certain, otherwise use old code path. If there is no way to differentiate then the Dialog needs to be made much more ambiguous and say something like "if the error is device time related, you may wish to open time settings and update your time, current device time is xxxx" or similar
...that's the only reference I have really, as the ankiweb sync server code does not seem to be searchable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, lost track of this one. If AD wants to be able to identify the clock message separately, it should send through a PR that moves the error out of OTHER and into a separate specific error, like we currently use to detect sync auth failures. Other is basically a catch-all for errors that we don't (currently) need to special-case in the frontend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 pretty far out of my wheelhouse but after some spelunking, is that roundabouts here?
https://github.com/ankitects/anki/blob/1b7390f22d56cb566ee3aba2faff3615275c5aa4/rslib/src/backend/error.rs#L65-L72
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that maps our internal error to the limited protobuf errors we export. With a new case in the .proto file, ClockIncorrect could be mapped to it.
I had AnkiDroid notifications turned off which is why I missed this (again). I should see future pings.