diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java index 325e9e9d..28540338 100755 --- a/framework/src/org/apache/cordova/CordovaActivity.java +++ b/framework/src/org/apache/cordova/CordovaActivity.java @@ -390,23 +390,15 @@ public class CordovaActivity extends AppCompatActivity { final String errorUrl = preferences.getString("errorUrl", null); if ((errorUrl != null) && (!failingUrl.equals(errorUrl)) && (appView != null)) { // Load URL on UI thread - me.runOnUiThread(new Runnable() { - @Override - public void run() { - me.appView.showWebPage(errorUrl, false, true, null); - } - }); + me.runOnUiThread(() -> me.appView.showWebPage(errorUrl, false, true, null)); } // If not, then display error dialog else { final boolean exit = !(errorCode == WebViewClient.ERROR_HOST_LOOKUP); - me.runOnUiThread(new Runnable() { - @Override - public void run() { - if (exit) { - me.appView.getView().setVisibility(View.GONE); - me.displayError("Application Error", description + " (" + failingUrl + ")", "OK", exit); - } + me.runOnUiThread(() -> { + if (exit) { + me.appView.getView().setVisibility(View.GONE); + me.displayError("Application Error", description + " (" + failingUrl + ")", "OK", exit); } }); } @@ -417,29 +409,23 @@ public class CordovaActivity extends AppCompatActivity { */ public void displayError(final String title, final String message, final String button, final boolean exit) { final CordovaActivity me = this; - me.runOnUiThread(new Runnable() { - @Override - public void run() { - try { - AlertDialog.Builder dlg = new AlertDialog.Builder(me); - dlg.setMessage(message); - dlg.setTitle(title); - dlg.setCancelable(false); - dlg.setPositiveButton(button, - new AlertDialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - if (exit) { - finish(); - } - } - }); - dlg.create(); - dlg.show(); - } catch (Exception e) { - finish(); - } + me.runOnUiThread(() -> { + try { + AlertDialog.Builder dlg = new AlertDialog.Builder(me); + dlg.setMessage(message); + dlg.setTitle(title); + dlg.setCancelable(false); + dlg.setPositiveButton(button, + (dialog, which) -> { + dialog.dismiss(); + if (exit) { + finish(); + } + }); + dlg.create(); + dlg.show(); + } catch (Exception e) { + finish(); } }); } diff --git a/framework/src/org/apache/cordova/CordovaDialogsHelper.java b/framework/src/org/apache/cordova/CordovaDialogsHelper.java index a4c7ceb7..9009807a 100644 --- a/framework/src/org/apache/cordova/CordovaDialogsHelper.java +++ b/framework/src/org/apache/cordova/CordovaDialogsHelper.java @@ -42,31 +42,18 @@ public class CordovaDialogsHelper { //Don't let alerts break the back button dlg.setCancelable(true); dlg.setPositiveButton(android.R.string.ok, - new AlertDialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.gotResult(true, null); - } - }); + (dialog, which) -> result.gotResult(true, null)); dlg.setOnCancelListener( - new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - result.gotResult(false, null); - } - }); - dlg.setOnKeyListener(new DialogInterface.OnKeyListener() { - //DO NOTHING - @Override - public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK) - { - result.gotResult(true, null); - return false; - } - else - return true; + dialog -> result.gotResult(false, null)); + //DO NOTHING + dlg.setOnKeyListener((dialog, keyCode, event) -> { + if (keyCode == KeyEvent.KEYCODE_BACK) + { + result.gotResult(true, null); + return false; } + else + return true; }); lastHandledDialog = dlg.show(); } @@ -77,38 +64,20 @@ public class CordovaDialogsHelper { dlg.setTitle("Confirm"); dlg.setCancelable(true); dlg.setPositiveButton(android.R.string.ok, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.gotResult(true, null); - } - }); + (dialog, which) -> result.gotResult(true, null)); dlg.setNegativeButton(android.R.string.cancel, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.gotResult(false, null); - } - }); + (dialog, which) -> result.gotResult(false, null)); dlg.setOnCancelListener( - new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - result.gotResult(false, null); - } - }); - dlg.setOnKeyListener(new DialogInterface.OnKeyListener() { - //DO NOTHING - @Override - public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK) - { - result.gotResult(false, null); - return false; - } - else - return true; + dialog -> result.gotResult(false, null)); + //DO NOTHING + dlg.setOnKeyListener((dialog, keyCode, event) -> { + if (keyCode == KeyEvent.KEYCODE_BACK) + { + result.gotResult(false, null); + return false; } + else + return true; }); lastHandledDialog = dlg.show(); } @@ -132,20 +101,12 @@ public class CordovaDialogsHelper { dlg.setView(input); dlg.setCancelable(false); dlg.setPositiveButton(android.R.string.ok, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String userText = input.getText().toString(); - result.gotResult(true, userText); - } + (dialog, which) -> { + String userText = input.getText().toString(); + result.gotResult(true, userText); }); dlg.setNegativeButton(android.R.string.cancel, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.gotResult(false, null); - } - }); + (dialog, which) -> result.gotResult(false, null)); lastHandledDialog = dlg.show(); } diff --git a/framework/src/org/apache/cordova/CordovaWebViewImpl.java b/framework/src/org/apache/cordova/CordovaWebViewImpl.java index d6791332..caa2f093 100644 --- a/framework/src/org/apache/cordova/CordovaWebViewImpl.java +++ b/framework/src/org/apache/cordova/CordovaWebViewImpl.java @@ -148,23 +148,20 @@ public class CordovaWebViewImpl implements CordovaWebView { final int loadUrlTimeoutValue = preferences.getInteger("LoadUrlTimeoutValue", 20000); // Timeout error method - final Runnable loadError = new Runnable() { - @Override - public void run() { - stopLoading(); - LOG.e(TAG, "CordovaWebView: TIMEOUT ERROR!"); + final Runnable loadError = () -> { + stopLoading(); + LOG.e(TAG, "CordovaWebView: TIMEOUT ERROR!"); - // Handle other errors by passing them to the webview in JS - JSONObject data = new JSONObject(); - try { - data.put("errorCode", -6); - data.put("description", "The connection to the server was unsuccessful."); - data.put("url", url); - } catch (JSONException e) { - // Will never happen. - } - pluginManager.postMessage("onReceivedError", data); + // Handle other errors by passing them to the webview in JS + JSONObject data = new JSONObject(); + try { + data.put("errorCode", -6); + data.put("description", "The connection to the server was unsuccessful."); + data.put("url", url); + } catch (JSONException e) { + // Will never happen. } + pluginManager.postMessage("onReceivedError", data); }; // Timeout timer method @@ -190,14 +187,11 @@ public class CordovaWebViewImpl implements CordovaWebView { if (cordova.getActivity() != null) { final boolean _recreatePlugins = recreatePlugins; - cordova.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - if (loadUrlTimeoutValue > 0) { - cordova.getThreadPool().execute(timeoutCheck); - } - engine.loadUrl(url, _recreatePlugins); + cordova.getActivity().runOnUiThread(() -> { + if (loadUrlTimeoutValue > 0) { + cordova.getThreadPool().execute(timeoutCheck); } + engine.loadUrl(url, _recreatePlugins); }); } else { LOG.d(TAG, "Cordova activity does not exist."); @@ -581,23 +575,15 @@ public class CordovaWebViewImpl implements CordovaWebView { // Make app visible after 2 sec in case there was a JS error and Cordova JS never initialized correctly if (engine.getView().getVisibility() != View.VISIBLE) { - Thread t = new Thread(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(2000); - if (cordova.getActivity() != null) { - cordova.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - pluginManager.postMessage("spinner", "stop"); - } - }); - } else { - LOG.d(TAG, "Cordova activity does not exist."); - } - } catch (InterruptedException e) { + Thread t = new Thread(() -> { + try { + Thread.sleep(2000); + if (cordova.getActivity() != null) { + cordova.getActivity().runOnUiThread(() -> pluginManager.postMessage("spinner", "stop")); + } else { + LOG.d(TAG, "Cordova activity does not exist."); } + } catch (InterruptedException e) { } }); t.start(); diff --git a/framework/src/org/apache/cordova/CoreAndroid.java b/framework/src/org/apache/cordova/CoreAndroid.java index 8bdf9340..04249280 100755 --- a/framework/src/org/apache/cordova/CoreAndroid.java +++ b/framework/src/org/apache/cordova/CoreAndroid.java @@ -86,12 +86,7 @@ public class CoreAndroid extends CordovaPlugin { // This gets called from JavaScript onCordovaReady to show the webview. // I recommend we change the name of the Message as spinner/stop is not // indicative of what this actually does (shows the webview). - cordova.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - webView.getPluginManager().postMessage("spinner", "stop"); - } - }); + cordova.getActivity().runOnUiThread(() -> webView.getPluginManager().postMessage("spinner", "stop")); } else if (action.equals("loadUrl")) { this.loadUrl(args.getString(0), args.optJSONObject(1)); @@ -145,12 +140,7 @@ public class CoreAndroid extends CordovaPlugin { * Clear the resource cache. */ public void clearCache() { - cordova.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - webView.clearCache(); - } - }); + cordova.getActivity().runOnUiThread(() -> webView.clearCache()); } /** @@ -217,12 +207,7 @@ public class CoreAndroid extends CordovaPlugin { * Clear page history for the app. */ public void clearHistory() { - cordova.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - webView.clearHistory(); - } - }); + cordova.getActivity().runOnUiThread(() -> webView.clearHistory()); } /** @@ -230,12 +215,7 @@ public class CoreAndroid extends CordovaPlugin { * This is the same as pressing the backbutton on Android device. */ public void backHistory() { - cordova.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - webView.backHistory(); - } - }); + cordova.getActivity().runOnUiThread(() -> webView.backHistory()); } /** diff --git a/framework/src/org/apache/cordova/NativeToJsMessageQueue.java b/framework/src/org/apache/cordova/NativeToJsMessageQueue.java index a140bacb..04b72da2 100755 --- a/framework/src/org/apache/cordova/NativeToJsMessageQueue.java +++ b/framework/src/org/apache/cordova/NativeToJsMessageQueue.java @@ -301,13 +301,10 @@ public class NativeToJsMessageQueue { @Override public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) { - cordova.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - String js = queue.popAndEncodeAsJs(); - if (js != null) { - engine.loadUrl("javascript:" + js, false); - } + cordova.getActivity().runOnUiThread(() -> { + String js = queue.popAndEncodeAsJs(); + if (js != null) { + engine.loadUrl("javascript:" + js, false); } }); } @@ -330,26 +327,20 @@ public class NativeToJsMessageQueue { @Override public void reset() { - delegate.runOnUiThread(new Runnable() { - @Override - public void run() { - online = false; - // If the following call triggers a notifyOfFlush, then ignore it. - ignoreNextFlush = true; - delegate.setNetworkAvailable(true); - } + delegate.runOnUiThread(() -> { + online = false; + // If the following call triggers a notifyOfFlush, then ignore it. + ignoreNextFlush = true; + delegate.setNetworkAvailable(true); }); } @Override public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) { - delegate.runOnUiThread(new Runnable() { - @Override - public void run() { - if (!queue.isEmpty()) { - ignoreNextFlush = false; - delegate.setNetworkAvailable(online); - } + delegate.runOnUiThread(() -> { + if (!queue.isEmpty()) { + ignoreNextFlush = false; + delegate.setNetworkAvailable(online); } }); } @@ -374,13 +365,10 @@ public class NativeToJsMessageQueue { @Override public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) { - cordova.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - String js = queue.popAndEncodeAsJs(); - if (js != null) { - engine.evaluateJavascript(js, null); - } + cordova.getActivity().runOnUiThread(() -> { + String js = queue.popAndEncodeAsJs(); + if (js != null) { + engine.evaluateJavascript(js, null); } }); } diff --git a/framework/src/org/apache/cordova/engine/SystemWebChromeClient.java b/framework/src/org/apache/cordova/engine/SystemWebChromeClient.java index 9c0c01b1..5ea757e2 100755 --- a/framework/src/org/apache/cordova/engine/SystemWebChromeClient.java +++ b/framework/src/org/apache/cordova/engine/SystemWebChromeClient.java @@ -84,13 +84,11 @@ public class SystemWebChromeClient extends WebChromeClient { */ @Override public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { - dialogsHelper.showAlert(message, new CordovaDialogsHelper.Result() { - @Override public void gotResult(boolean success, String value) { - if (success) { - result.confirm(); - } else { - result.cancel(); - } + dialogsHelper.showAlert(message, (success, value) -> { + if (success) { + result.confirm(); + } else { + result.cancel(); } }); return true; @@ -101,14 +99,11 @@ public class SystemWebChromeClient extends WebChromeClient { */ @Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { - dialogsHelper.showConfirm(message, new CordovaDialogsHelper.Result() { - @Override - public void gotResult(boolean success, String value) { - if (success) { - result.confirm(); - } else { - result.cancel(); - } + dialogsHelper.showConfirm(message, (success, value) -> { + if (success) { + result.confirm(); + } else { + result.cancel(); } }); return true; @@ -129,14 +124,11 @@ public class SystemWebChromeClient extends WebChromeClient { if (handledRet != null) { result.confirm(handledRet); } else { - dialogsHelper.showPrompt(message, defaultValue, new CordovaDialogsHelper.Result() { - @Override - public void gotResult(boolean success, String value) { - if (success) { - result.confirm(value); - } else { - result.cancel(); - } + dialogsHelper.showPrompt(message, defaultValue, (success, value) -> { + if (success) { + result.confirm(value); + } else { + result.cancel(); } }); }