use of com.ichi2.anki.AbstractFlashcardViewer.JavaScriptFunction in project AnkiChinaAndroid by ankichinateam.
the class ReviewerTest method jsTime4ShouldBeBlankIfButtonUnavailable.
@Test
public void jsTime4ShouldBeBlankIfButtonUnavailable() {
// #6623 - easy should be blank when displaying a card with 3 buttons (after displaying a review)
Note firstNote = addNoteUsingBasicModel("Hello", "World");
moveToReviewQueue(firstNote.firstCard());
addNoteUsingBasicModel("Hello", "World2");
Reviewer reviewer = startReviewer();
JavaScriptFunction javaScriptFunction = reviewer.javaScriptFunction();
// The answer needs to be displayed to be able to get the time.
displayAnswer(reviewer);
assertThat("4 buttons should be displayed", reviewer.getAnswerButtonCount(), is(4));
String nextTime = javaScriptFunction.ankiGetNextTime4();
assertThat(nextTime, not(isEmptyString()));
// Display the next answer
reviewer.answerCard(EASE_4);
displayAnswer(reviewer);
if (schedVersion == 1) {
assertThat("The 4th button should not be visible", reviewer.getAnswerButtonCount(), is(3));
String learnTime = javaScriptFunction.ankiGetNextTime4();
assertThat("If the 4th button is not visible, there should be no time4 in JS", learnTime, isEmptyString());
}
}
use of com.ichi2.anki.AbstractFlashcardViewer.JavaScriptFunction in project AnkiChinaAndroid by ankichinateam.
the class ReviewerTest method assertCounts.
private void assertCounts(Reviewer r, int newCount, int stepCount, int revCount) {
List<String> countList = new ArrayList<>();
JavaScriptFunction jsApi = r.javaScriptFunction();
countList.add(jsApi.ankiGetNewCardCount());
countList.add(jsApi.ankiGetLrnCardCount());
countList.add(jsApi.ankiGetRevCardCount());
List<Integer> expexted = new ArrayList<>();
expexted.add(newCount);
expexted.add(stepCount);
expexted.add(revCount);
// We use toString as hamcrest does not print the whole array and stops at [0].
assertThat(countList.toString(), is(expexted.toString()));
}
use of com.ichi2.anki.AbstractFlashcardViewer.JavaScriptFunction in project Anki-Android by ankidroid.
the class ReviewerTest method jsAnkiGetDeckName.
@Test
public void jsAnkiGetDeckName() {
Collection col = getCol();
ModelManager models = col.getModels();
DeckManager decks = col.getDecks();
Long didAb = addDeck("A::B");
Model basic = models.byName(AnkiDroidApp.getAppResources().getString(R.string.basic_model_name));
basic.put("did", didAb);
addNoteUsingBasicModel("foo", "bar");
Long didA = addDeck("A");
decks.select(didA);
Reviewer reviewer = startReviewer();
AnkiDroidJsAPI javaScriptFunction = reviewer.javaScriptFunction();
waitForAsyncTasksToComplete();
assertThat(javaScriptFunction.ankiGetDeckName(), is("B"));
}
use of com.ichi2.anki.AbstractFlashcardViewer.JavaScriptFunction in project Anki-Android by ankidroid.
the class ReviewerTest method jsTime4ShouldBeBlankIfButtonUnavailable.
@Test
public void jsTime4ShouldBeBlankIfButtonUnavailable() {
// #6623 - easy should be blank when displaying a card with 3 buttons (after displaying a review)
Note firstNote = addNoteUsingBasicModel("Hello", "World");
moveToReviewQueue(firstNote.firstCard());
addNoteUsingBasicModel("Hello", "World2");
Reviewer reviewer = startReviewer();
AnkiDroidJsAPI javaScriptFunction = reviewer.javaScriptFunction();
// The answer needs to be displayed to be able to get the time.
displayAnswer(reviewer);
assertThat("4 buttons should be displayed", reviewer.getAnswerButtonCount(), is(4));
String nextTime = javaScriptFunction.ankiGetNextTime4();
assertThat(nextTime, not(emptyString()));
// Display the next answer
reviewer.answerCard(Consts.BUTTON_FOUR);
displayAnswer(reviewer);
if (schedVersion == 1) {
assertThat("The 4th button should not be visible", reviewer.getAnswerButtonCount(), is(3));
String learnTime = javaScriptFunction.ankiGetNextTime4();
assertThat("If the 4th button is not visible, there should be no time4 in JS", learnTime, emptyString());
}
}
use of com.ichi2.anki.AbstractFlashcardViewer.JavaScriptFunction in project AnkiChinaAndroid by ankichinateam.
the class WebViewActivity method onCreate.
@SuppressLint({ "AddJavascriptInterface", "SetJavaScriptEnabled" })
@Override
@SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
Themes.setThemeLegacy(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
Toolbar toolbar = findViewById(R.id.toolbar);
if (toolbar != null) {
// toolbar.inflateMenu(R.menu.web_view);
setSupportActionBar(toolbar);
}
getSupportActionBar().setTitle(getIntent().getStringExtra("url"));
// Add a home button to the actionbar
// getSupportActionBar().setHomeButtonEnabled(true);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
webView = findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
// webView.getSettings().setAppCacheMaxSize(1024*1024*8);
webView.getSettings().setUserAgentString("User-Agent:Android");
// 可以读取文件缓存
webView.getSettings().setAllowFileAccess(true);
// 开启H5(APPCache)缓存功能
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
webView.getSettings().setAppCachePath(appCachePath);
// webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
webView.setDownloadListener((url, userAgent, contentDisposition, mimeType, contentLength) -> {
String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);
Request request = new Request.Builder().url(url).build();
// 构建我们的进度监听器
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), fileName);
final ProgressResponseBody.ProgressListener listener = (bytesRead, contentLength1, done) -> {
// 计算百分比并更新ProgressBar
if (contentLength1 != 0) {
final int percent = (int) (100 * bytesRead / contentLength1);
mProgressDialog.setProgress(percent);
}
if (done) {
runOnUiThread(() -> {
mProgressDialog.dismiss();
// fixme 下载的如果不是卡牌,需要执行通用程序
AsyncDialogFragment newFragment = ImportDialog.newInstance(DIALOG_IMPORT_ADD_CONFIRM, file.getAbsolutePath(), WebViewActivity.this);
showAsyncDialogFragment(newFragment);
});
}
};
OkHttpClient client = new OkHttpClient.Builder().addNetworkInterceptor(chain -> {
Response response = chain.proceed(chain.request());
return response.newBuilder().body(new ProgressResponseBody(response.body(), listener)).build();
}).build();
// 发送响应
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) {
Timber.i("onResponse:" + response.isSuccessful());
writeFile(file, response);
}
});
mProgressDialog = new MaterialDialog.Builder(WebViewActivity.this).title("正在下载").content("请不要做任何操作,保持屏幕常亮,切换页面或APP会导致下载中断!").progress(false, 100, false).cancelable(false).negativeText("取消下载").onNegative((dialog, which) -> {
call.cancel();
dialog.dismiss();
}).show();
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
writeData(getIntent().getStringExtra("token"));
WebViewActivity.this.getSupportActionBar().setTitle(title);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
writeData(getIntent().getStringExtra("token"));
}
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Timber.i("shouldOverrideUrlLoading:%s", url);
if (url.startsWith("openexternalbrowser://url=")) {
// startThirdpartyApp("https://www.baidu.com");
openUrl(Uri.parse(url.replace("openexternalbrowser://url=", "")));
} else if (urlCanLoad(url.toLowerCase())) {
// 加载正常网页
view.loadUrl(url, map);
} else {
// 打开第三方应用或者下载apk等
startThirdpartyApp(url);
}
return true;
}
});
webView.setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// 按返回键操作并且能回退网页
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
// 后退
webView.goBack();
return true;
}
}
// showShareDialog();
return false;
});
webView.addJavascriptInterface(new JavaScriptFunction(), "AnkiDroidJS");
map.put("Referer", "https://file.ankichinas.cn");
webView.loadUrl(getIntent().getStringExtra("url"), map);
}
Aggregations