Skip to content

Commit ec50459

Browse files
committed
MakeCode - fix click handlers for back, download, save
1 parent c04e1c9 commit ec50459

File tree

1 file changed

+98
-39
lines changed

1 file changed

+98
-39
lines changed

app/src/main/java/com/samsung/microbit/ui/activity/MakeCodeWebView.java

Lines changed: 98 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class MakeCodeWebView extends Activity implements View.OnClickListener {
4242
public static String makecodeUrl = "https://makecode.microbit.org/?androidapp=" + BuildConfig.VERSION_CODE;
4343
public static Activity activityHandle = null;
4444

45+
boolean projectDownload = false;
4546
Uri hexToFlash;
4647

4748
private static final int REQUEST_CODE_SAVEDATA = 1;
@@ -83,15 +84,9 @@ protected void onCreate(Bundle savedInstanceState) {
8384
webSettings.setBuiltInZoomControls(true);
8485
webSettings.setDisplayZoomControls(false);
8586
webSettings.setDomStorageEnabled(true);
86-
webView.setWebContentsDebuggingEnabled(true);
87+
WebView.setWebContentsDebuggingEnabled(false);
8788

8889
webView.addJavascriptInterface(new JavaScriptInterface(this), "AndroidFunction");
89-
webView.evaluateJavascript("javascript:(function f() { document.getElementsByClassName(\"brand\")[0].addEventListener(\"click\", function(e) { AndroidFunction.returnToHome(); e.preventDefault(); return false; }) })()", new ValueCallback<String>() {
90-
@Override
91-
public void onReceiveValue(String s) {
92-
Log.d(TAG, s);
93-
}
94-
});
9590

9691
webView.setWebViewClient(new WebViewClient() {
9792
@Override
@@ -106,6 +101,13 @@ public void onLoadResource(WebView view, String url) {
106101
super.onLoadResource(view, url);
107102
Log.v(TAG, "onLoadResource(" + url + ");");
108103
}
104+
105+
@Override
106+
public void onPageFinished (WebView view, String url) {
107+
super.onPageFinished(view, url);
108+
Log.v(TAG, "onPageFinished(" + url + ");");
109+
onPageFinishedJS( view, url);
110+
}
109111
}); //setWebViewClient
110112

111113
webView.setWebChromeClient(new WebChromeClient() {
@@ -323,7 +325,7 @@ private void importInitialise() {
323325
//TODO - does MakeCode signal when ready?
324326
Looper looper = Looper.getMainLooper();
325327
importHandler = new Handler(looper);
326-
importHandler.postDelayed(importCallback, 1000);
328+
importHandler.postDelayed(importCallback, 2000);
327329
}
328330
}
329331
private final Runnable importCallback = new Runnable() {
@@ -340,6 +342,8 @@ public void run() {
340342
};
341343

342344
public void importPostMessage() {
345+
Log.d(TAG, "importPostMessage");
346+
343347
if ( importHex == null) {
344348
return;
345349
}
@@ -355,57 +359,112 @@ public void importPostMessage() {
355359

356360
importPosting = true;
357361

362+
StringBuilder sb = new StringBuilder();
358363
String nl = "\n";
359-
String script = "javascript:(";
360-
script += nl + "function f() { ";
361-
script += nl + "var ret = 'OK'";
362-
script += nl + "try {";
363-
script += nl + "var loading = document.getElementById('loading')";
364-
script += nl + "if ( loading && loading.parentElement) {";
365-
script += nl + "ret = 'loading'";
366-
script += nl + "} else {";
367-
script += nl + "var name = \"" + importName + "\"";
368-
script += nl + "var hex = \"" + importHex + "\"";
369-
script += nl + "var msg = {";
370-
script += nl + "type: 'importfile',";
371-
script += nl + "filename: name,";
372-
script += nl + "parts: [ hex ]";
373-
script += nl + "}";
374-
script += nl + "window.postMessage( msg, '*')";
375-
script += nl + "}";
376-
script += nl + "} catch( err) {";
377-
script += nl + "ret = err.message";
378-
script += nl + "}";
379-
script += nl + "return ret;";
380-
script += nl + "}";
381-
script += nl + ")()";
382-
webView.evaluateJavascript( script, new ValueCallback<String>() {
364+
sb.append( "javascript:(");
365+
sb.append(nl).append("function f() {");
366+
sb.append(nl).append( "var ret = 'OK'");
367+
sb.append(nl).append( "try {");
368+
sb.append(nl).append( "var loading = document.getElementById('loading')");
369+
sb.append(nl).append( "if ( loading && loading.parentElement) {");
370+
sb.append(nl).append( "ret = 'loading'");
371+
sb.append(nl).append( "} else {");
372+
sb.append(nl).append( "var name = '").append(importName).append("'");
373+
sb.append(nl).append( "var hex = '").append(importHex).append("'");
374+
sb.append(nl).append( "var msg = {");
375+
sb.append(nl).append( "type: 'importfile',");
376+
sb.append(nl).append( "filename: name,");
377+
sb.append(nl).append( "parts: [ hex ]");
378+
sb.append(nl).append( "}");
379+
sb.append(nl).append( "window.postMessage( msg, '*')");
380+
sb.append(nl).append( "}");
381+
sb.append(nl).append( "} catch( err) {");
382+
sb.append(nl).append( "ret = err.message");
383+
sb.append(nl).append( "}");
384+
sb.append(nl).append( "return ret");
385+
sb.append(nl).append("}");
386+
sb.append(nl).append(")()");
387+
388+
webView.evaluateJavascript( sb.toString(), new ValueCallback<String>() {
383389
@Override
384390
public void onReceiveValue(String s) {
385-
Log.d(TAG, s);
386-
if ( s != "loading") {
391+
Log.i(TAG, "importPostMessage: " + s);
392+
String loading = "\"loading\"";
393+
String ok = "\"OK\"";
394+
if ( s.equals( ok)) {
387395
importHex = null;
396+
} else if ( !s.equals( loading)) {
388397
}
389398
}
390399
});
391400
importPosting = false;
392-
}
393-
}
401+
}
402+
403+
public void onPageFinishedJS( WebView view, String url) {
404+
Log.v(TAG, "addListeners(" + url + ");");
405+
406+
StringBuilder sb = new StringBuilder();
407+
String nl = "\n";
408+
sb.append( "javascript:(");
409+
sb.append(nl).append("function f() {");
410+
sb.append(nl).append( "var ret = 'OK'");
411+
sb.append(nl).append( "try {");
412+
sb.append(nl).append( "var brands = document.getElementsByClassName(\"brand\")");
413+
sb.append(nl).append( "for (let i = 0; brands != null && i < brands.length; i++) {");
414+
sb.append(nl).append( "brands[i].addEventListener(\"click\",");
415+
sb.append(nl).append( "function(e) {");
416+
sb.append(nl).append( "AndroidFunction.clickBrand();");
417+
sb.append(nl).append( "e.preventDefault();");
418+
sb.append(nl).append( "return false;");
419+
sb.append(nl).append( "})");
420+
sb.append(nl).append( "}");
421+
sb.append(nl).append( "var downs = document.getElementsByClassName(\"download-button\")");
422+
sb.append(nl).append( "for (let i = 0; downs != null && i < downs.length; i++) {");
423+
sb.append(nl).append( "downs[i].addEventListener(\"click\",");
424+
sb.append(nl).append( "function(e) {");
425+
sb.append(nl).append( "AndroidFunction.clickDownload();");
426+
sb.append(nl).append( "e.preventDefault();");
427+
sb.append(nl).append( "return false;");
428+
sb.append(nl).append( "})");
429+
sb.append(nl).append( "}");
430+
sb.append(nl).append( "} catch( err) {");
431+
sb.append(nl).append( "ret = err.message");
432+
sb.append(nl).append( "}");
433+
sb.append(nl).append( "return ret");
434+
sb.append(nl).append("}");
435+
sb.append(nl).append(")()");
436+
437+
webView.evaluateJavascript( sb.toString(), new ValueCallback<String>() {
438+
@Override
439+
public void onReceiveValue(String s) {
440+
Log.d(TAG, s);
441+
}
442+
});
443+
}}
394444

395445
/* Javascript Interface */
396446
class JavaScriptInterface {
397-
Context mContext;
447+
MakeCodeWebView mContext;
398448

399-
JavaScriptInterface(Context c) {
449+
JavaScriptInterface( MakeCodeWebView c) {
400450
mContext = c;
401451
}
402452

403453
@JavascriptInterface
404-
public void returnToHome() {
454+
public void clickBrand() {
405455
try {
406456
MakeCodeWebView.activityHandle.finish();
407457
} catch(Exception e) {
408458
Log.v(TAG, e.toString());
409459
}
410460
}
461+
462+
@JavascriptInterface
463+
public void clickDownload() {
464+
try {
465+
mContext.projectDownload = true;
466+
} catch(Exception e) {
467+
Log.v(TAG, e.toString());
468+
}
469+
}
411470
}

0 commit comments

Comments
 (0)