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

Make available current card's and deck's details in WebView #6307

Merged
merged 53 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
3a4a301
Add files via upload
krmanik Mar 14, 2020
3e57c4c
Update card.js
krmanik Mar 15, 2020
e7e91bc
Update AbstractFlashcardViewer.java
krmanik Mar 15, 2020
3666101
Update AbstractFlashcardViewer.java
krmanik Mar 15, 2020
7c5ec74
Update card.js
krmanik Mar 15, 2020
f8041a9
Update AbstractFlashcardViewer.java
krmanik Mar 15, 2020
f0f9331
Update AbstractFlashcardViewer.java
krmanik Mar 15, 2020
2f4df32
Merge branch 'master' of https://github.com/infinyte7/Anki-Android
krmanik Mar 15, 2020
7bd146c
Merge pull request #1 from ankidroid/master
krmanik Mar 17, 2020
38ccf48
Added localhost domain-config
krmanik Apr 1, 2020
2b6bcf3
Merge pull request #2 from ankidroid/master
krmanik May 7, 2020
465b0a2
Anki
krmanik May 7, 2020
30863ff
Merge branch 'master' of https://github.com/infinyte7/Anki-Android
krmanik May 7, 2020
7df6d45
Merge pull request #3 from ankidroid/master
krmanik May 7, 2020
e3045d9
Update
krmanik May 7, 2020
955285a
Update Preferences.java
krmanik May 7, 2020
a9ef431
Merge branch 'master' of https://github.com/infinyte7/Anki-Android
krmanik May 7, 2020
9157409
Update preferences_appearance.xml
krmanik May 7, 2020
46d7fc5
update
krmanik May 7, 2020
ae83279
update
krmanik May 8, 2020
25aac57
Update DeckPicker.java
krmanik May 8, 2020
333008c
Background Image
krmanik May 8, 2020
5871b34
Update DeckPicker.java
krmanik May 8, 2020
47a1c6f
Update Preferences.java
krmanik May 8, 2020
829d14f
Update DeckPicker.java
krmanik May 8, 2020
5e7d40d
Update 03-dialogs.xml
krmanik May 8, 2020
f5a04d4
Update Preferences.java
krmanik May 8, 2020
60a4ff0
Update Preferences.java
krmanik May 8, 2020
f1d4602
Update DeckPicker.java
krmanik May 9, 2020
e609443
deckpicker background image
krmanik May 9, 2020
3dc19a5
update Deckpicker.java
krmanik May 9, 2020
e2ceb64
update 03-dialogs.xml
krmanik May 9, 2020
cabb8a3
Update Preferences.java
krmanik May 15, 2020
32c28a1
Update Fork
krmanik May 15, 2020
b0841b8
Merge pull request #6 from ankidroid/master
krmanik May 15, 2020
1d93e93
Update Preferences.java
krmanik May 15, 2020
81bc40b
Merge branch 'master' of https://github.com/infinyte7/Anki-Android
krmanik May 15, 2020
05bec67
Update Preferences.java
krmanik May 15, 2020
a92f1b7
Update 03-dialogs.xml
krmanik May 16, 2020
9ec5c80
Update 03-dialogs.xml
krmanik May 16, 2020
e9d0b1d
update preferences.java, 03-dialog.xml
krmanik May 17, 2020
61e60bd
Merge pull request #8 from ankidroid/master
krmanik May 29, 2020
c738f56
Added functions to card.js
krmanik May 30, 2020
08ccbae
Update AbstractFlashcardViewer.java
krmanik May 30, 2020
1c8b248
Update card.js & AbstractFlashcardViewer.java
krmanik May 30, 2020
49e6567
Merge branch 'master' of https://github.com/infinyte7/Anki-Android
krmanik May 30, 2020
eeb4702
Update card.js & AbstractFlashcardViewer.java
krmanik May 31, 2020
ad189ee
Update card.js
krmanik May 31, 2020
b8d79d2
Update AbstractFlashcardViewer.java
krmanik May 31, 2020
089a20e
Update card.js
krmanik May 31, 2020
d3f035e
Update card.js
krmanik May 31, 2020
2edeb68
Update card.js
krmanik Jun 1, 2020
dddbccb
Update AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer…
mikehardy Jun 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion AnkiDroid/src/main/assets/scripts/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,32 @@ function reloadPage() {
window.location.href = "signal:reload_card_html";
}

// Mark current card
function ankiMarkCard() {
window.location.href = "signal:mark_current_card";
}

/* Toggle flag on card from AnkiDroid Webview using JavaScript
Possible values: "none", "red", "orange", "green", "blue"
See AnkiDroid Manual for Usage
*/
function ankiToggleFlag(flag) {
david-allison marked this conversation as resolved.
Show resolved Hide resolved
var flagVal = Number.isInteger(flag);

if (flagVal) {
switch (flag) {
case 0: window.location.href = "signal:flag_none"; break;
case 1: window.location.href = "signal:flag_red"; break;
case 2: window.location.href = "signal:flag_orange"; break;
case 3: window.location.href = "signal:flag_green"; break;
case 4: window.location.href = "signal:flag_blue"; break;
default: console.log('No Flag Found'); break;
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
window.location.href = "signal:flag_" + flag;
}
}

/* Tell the app the text in the input box when it loses focus */
function taBlur(itag) {
//#5944 - percent wasn't encoded, but Mandarin was.
Expand Down Expand Up @@ -168,4 +194,4 @@ var onPageFinished = function() {
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.RenderProcessGoneDetail;
import android.webkit.WebChromeClient;
Expand Down Expand Up @@ -175,7 +176,14 @@ public abstract class AbstractFlashcardViewer extends NavigationDrawerActivity i
private boolean mReplayOnTtsInit = false;

protected static final int MENU_DISABLED = 3;
// TODO: Consider extracting to ViewModel
// Card counts
private SpannableString newCount;
private SpannableString lrnCount;
private SpannableString revCount;

// ETA
private int eta;

/**
* Broadcast that informs us when the sd card is about to be unmounted
Expand Down Expand Up @@ -1481,6 +1489,9 @@ private WebView createWebView() {
webView.setWebViewClient(new CardViewerWebClient());
// Set transparent color to prevent flashing white when night mode enabled
webView.setBackgroundColor(Color.argb(1, 0, 0, 0));

// Javascript interface for calling AnkiDroid functions in webview, see card.js
webView.addJavascriptInterface(new JavaScriptFunction(), "AnkiDroidJS");
return webView;
}

Expand Down Expand Up @@ -1742,14 +1753,14 @@ protected void updateScreenCounts() {
String title = Decks.basename(getCol().getDecks().get(mCurrentCard.getDid()).getString("name"));
actionBar.setTitle(title);
if (mPrefShowETA) {
int eta = mSched.eta(counts, false);
eta = mSched.eta(counts, false);
actionBar.setSubtitle(Utils.remainingTime(AnkiDroidApp.getInstance(), eta * 60));
}
}

SpannableString newCount = new SpannableString(String.valueOf(counts[0]));
SpannableString lrnCount = new SpannableString(String.valueOf(counts[1]));
SpannableString revCount = new SpannableString(String.valueOf(counts[2]));
newCount = new SpannableString(String.valueOf(counts[0]));
lrnCount = new SpannableString(String.valueOf(counts[1]));
revCount = new SpannableString(String.valueOf(counts[2]));
if (mPrefHideDueCount) {
revCount = new SpannableString("???");
}
Expand Down Expand Up @@ -2852,6 +2863,7 @@ private void drawMark() {
if (mCurrentCard == null) {
return;
}

mCardMarker.displayMark(shouldDisplayMark());
}

Expand Down Expand Up @@ -3022,6 +3034,31 @@ private boolean filterUrl(String url) {
redrawCard();
return true;
}
// mark card using javascript
if (url.startsWith("signal:mark_current_card")) {
executeCommand(COMMAND_MARK);
return true;
}
// flag card (blue, green, orange, red) using javascript from AnkiDroid webview
if (url.startsWith("signal:flag_")) {
String mFlag = url.replaceFirst("signal:flag_","");
switch (mFlag) {
case "none": executeCommand(COMMAND_UNSET_FLAG);
return true;
case "red": executeCommand(COMMAND_TOGGLE_FLAG_RED);
return true;
case "orange": executeCommand(COMMAND_TOGGLE_FLAG_ORANGE);
return true;
case "green": executeCommand(COMMAND_TOGGLE_FLAG_GREEN);
return true;
case "blue": executeCommand(COMMAND_TOGGLE_FLAG_BLUE);
return true;
default:
Timber.d("No such Flag found.");
return true;
david-allison marked this conversation as resolved.
Show resolved Hide resolved
}
}

int signalOrdinal = WebViewSignalParserUtils.getSignalFromUrl(url);
switch (signalOrdinal) {
case WebViewSignalParserUtils.SIGNAL_UNHANDLED:
Expand Down Expand Up @@ -3275,4 +3312,41 @@ static void setEditorCard(Card card) {
//I don't see why we don't do this by intent.
sEditorCard = card;
}

/*
Javascript Interface class for calling Java function from AnkiDroid WebView
see card.js for available functions
*/
public class JavaScriptFunction {

@JavascriptInterface
public String ankiGetNewCardCount() {
return newCount.toString();
}

@JavascriptInterface
public String ankiGetLrnCardCount() {
return lrnCount.toString();
}

@JavascriptInterface
public String ankiGetRevCardCount() {
return revCount.toString();
}

@JavascriptInterface
public int ankiGetETA() {
return eta;
}

@JavascriptInterface
public boolean ankiGetCardMark() {
return shouldDisplayMark();
}

@JavascriptInterface
public int ankiGetCardFlag() {
return mCurrentCard.getUserFlag();
}
}
}