refactor: java 8 migration aid - replace with lambda

This commit is contained in:
エリス 2024-12-05 18:30:32 +09:00
parent 7ed491cf4d
commit bb37da02b9
No known key found for this signature in database
GPG Key ID: 2E5FF17FB26AF7F2
6 changed files with 107 additions and 214 deletions

View File

@ -390,24 +390,16 @@ public class CordovaActivity extends AppCompatActivity {
final String errorUrl = preferences.getString("errorUrl", null); final String errorUrl = preferences.getString("errorUrl", null);
if ((errorUrl != null) && (!failingUrl.equals(errorUrl)) && (appView != null)) { if ((errorUrl != null) && (!failingUrl.equals(errorUrl)) && (appView != null)) {
// Load URL on UI thread // Load URL on UI thread
me.runOnUiThread(new Runnable() { me.runOnUiThread(() -> me.appView.showWebPage(errorUrl, false, true, null));
@Override
public void run() {
me.appView.showWebPage(errorUrl, false, true, null);
}
});
} }
// If not, then display error dialog // If not, then display error dialog
else { else {
final boolean exit = !(errorCode == WebViewClient.ERROR_HOST_LOOKUP); final boolean exit = !(errorCode == WebViewClient.ERROR_HOST_LOOKUP);
me.runOnUiThread(new Runnable() { me.runOnUiThread(() -> {
@Override
public void run() {
if (exit) { if (exit) {
me.appView.getView().setVisibility(View.GONE); me.appView.getView().setVisibility(View.GONE);
me.displayError("Application Error", description + " (" + failingUrl + ")", "OK", exit); me.displayError("Application Error", description + " (" + failingUrl + ")", "OK", exit);
} }
}
}); });
} }
} }
@ -417,30 +409,24 @@ public class CordovaActivity extends AppCompatActivity {
*/ */
public void displayError(final String title, final String message, final String button, final boolean exit) { public void displayError(final String title, final String message, final String button, final boolean exit) {
final CordovaActivity me = this; final CordovaActivity me = this;
me.runOnUiThread(new Runnable() { me.runOnUiThread(() -> {
@Override
public void run() {
try { try {
AlertDialog.Builder dlg = new AlertDialog.Builder(me); AlertDialog.Builder dlg = new AlertDialog.Builder(me);
dlg.setMessage(message); dlg.setMessage(message);
dlg.setTitle(title); dlg.setTitle(title);
dlg.setCancelable(false); dlg.setCancelable(false);
dlg.setPositiveButton(button, dlg.setPositiveButton(button,
new AlertDialog.OnClickListener() { (dialog, which) -> {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); dialog.dismiss();
if (exit) { if (exit) {
finish(); finish();
} }
}
}); });
dlg.create(); dlg.create();
dlg.show(); dlg.show();
} catch (Exception e) { } catch (Exception e) {
finish(); finish();
} }
}
}); });
} }

View File

@ -42,23 +42,11 @@ public class CordovaDialogsHelper {
//Don't let alerts break the back button //Don't let alerts break the back button
dlg.setCancelable(true); dlg.setCancelable(true);
dlg.setPositiveButton(android.R.string.ok, dlg.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() { (dialog, which) -> result.gotResult(true, null));
@Override
public void onClick(DialogInterface dialog, int which) {
result.gotResult(true, null);
}
});
dlg.setOnCancelListener( dlg.setOnCancelListener(
new DialogInterface.OnCancelListener() { dialog -> result.gotResult(false, null));
@Override
public void onCancel(DialogInterface dialog) {
result.gotResult(false, null);
}
});
dlg.setOnKeyListener(new DialogInterface.OnKeyListener() {
//DO NOTHING //DO NOTHING
@Override dlg.setOnKeyListener((dialog, keyCode, event) -> {
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) if (keyCode == KeyEvent.KEYCODE_BACK)
{ {
result.gotResult(true, null); result.gotResult(true, null);
@ -66,7 +54,6 @@ public class CordovaDialogsHelper {
} }
else else
return true; return true;
}
}); });
lastHandledDialog = dlg.show(); lastHandledDialog = dlg.show();
} }
@ -77,30 +64,13 @@ public class CordovaDialogsHelper {
dlg.setTitle("Confirm"); dlg.setTitle("Confirm");
dlg.setCancelable(true); dlg.setCancelable(true);
dlg.setPositiveButton(android.R.string.ok, dlg.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() { (dialog, which) -> result.gotResult(true, null));
@Override
public void onClick(DialogInterface dialog, int which) {
result.gotResult(true, null);
}
});
dlg.setNegativeButton(android.R.string.cancel, dlg.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() { (dialog, which) -> result.gotResult(false, null));
@Override
public void onClick(DialogInterface dialog, int which) {
result.gotResult(false, null);
}
});
dlg.setOnCancelListener( dlg.setOnCancelListener(
new DialogInterface.OnCancelListener() { dialog -> result.gotResult(false, null));
@Override
public void onCancel(DialogInterface dialog) {
result.gotResult(false, null);
}
});
dlg.setOnKeyListener(new DialogInterface.OnKeyListener() {
//DO NOTHING //DO NOTHING
@Override dlg.setOnKeyListener((dialog, keyCode, event) -> {
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) if (keyCode == KeyEvent.KEYCODE_BACK)
{ {
result.gotResult(false, null); result.gotResult(false, null);
@ -108,7 +78,6 @@ public class CordovaDialogsHelper {
} }
else else
return true; return true;
}
}); });
lastHandledDialog = dlg.show(); lastHandledDialog = dlg.show();
} }
@ -132,20 +101,12 @@ public class CordovaDialogsHelper {
dlg.setView(input); dlg.setView(input);
dlg.setCancelable(false); dlg.setCancelable(false);
dlg.setPositiveButton(android.R.string.ok, dlg.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() { (dialog, which) -> {
@Override
public void onClick(DialogInterface dialog, int which) {
String userText = input.getText().toString(); String userText = input.getText().toString();
result.gotResult(true, userText); result.gotResult(true, userText);
}
}); });
dlg.setNegativeButton(android.R.string.cancel, dlg.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() { (dialog, which) -> result.gotResult(false, null));
@Override
public void onClick(DialogInterface dialog, int which) {
result.gotResult(false, null);
}
});
lastHandledDialog = dlg.show(); lastHandledDialog = dlg.show();
} }

View File

@ -148,9 +148,7 @@ public class CordovaWebViewImpl implements CordovaWebView {
final int loadUrlTimeoutValue = preferences.getInteger("LoadUrlTimeoutValue", 20000); final int loadUrlTimeoutValue = preferences.getInteger("LoadUrlTimeoutValue", 20000);
// Timeout error method // Timeout error method
final Runnable loadError = new Runnable() { final Runnable loadError = () -> {
@Override
public void run() {
stopLoading(); stopLoading();
LOG.e(TAG, "CordovaWebView: TIMEOUT ERROR!"); LOG.e(TAG, "CordovaWebView: TIMEOUT ERROR!");
@ -164,7 +162,6 @@ public class CordovaWebViewImpl implements CordovaWebView {
// Will never happen. // Will never happen.
} }
pluginManager.postMessage("onReceivedError", data); pluginManager.postMessage("onReceivedError", data);
}
}; };
// Timeout timer method // Timeout timer method
@ -190,14 +187,11 @@ public class CordovaWebViewImpl implements CordovaWebView {
if (cordova.getActivity() != null) { if (cordova.getActivity() != null) {
final boolean _recreatePlugins = recreatePlugins; final boolean _recreatePlugins = recreatePlugins;
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(() -> {
@Override
public void run() {
if (loadUrlTimeoutValue > 0) { if (loadUrlTimeoutValue > 0) {
cordova.getThreadPool().execute(timeoutCheck); cordova.getThreadPool().execute(timeoutCheck);
} }
engine.loadUrl(url, _recreatePlugins); engine.loadUrl(url, _recreatePlugins);
}
}); });
} else { } else {
LOG.d(TAG, "Cordova activity does not exist."); LOG.d(TAG, "Cordova activity does not exist.");
@ -581,24 +575,16 @@ public class CordovaWebViewImpl implements CordovaWebView {
// Make app visible after 2 sec in case there was a JS error and Cordova JS never initialized correctly // 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) { if (engine.getView().getVisibility() != View.VISIBLE) {
Thread t = new Thread(new Runnable() { Thread t = new Thread(() -> {
@Override
public void run() {
try { try {
Thread.sleep(2000); Thread.sleep(2000);
if (cordova.getActivity() != null) { if (cordova.getActivity() != null) {
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(() -> pluginManager.postMessage("spinner", "stop"));
@Override
public void run() {
pluginManager.postMessage("spinner", "stop");
}
});
} else { } else {
LOG.d(TAG, "Cordova activity does not exist."); LOG.d(TAG, "Cordova activity does not exist.");
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
}
}); });
t.start(); t.start();
} }

View File

@ -86,12 +86,7 @@ public class CoreAndroid extends CordovaPlugin {
// This gets called from JavaScript onCordovaReady to show the webview. // This gets called from JavaScript onCordovaReady to show the webview.
// I recommend we change the name of the Message as spinner/stop is not // I recommend we change the name of the Message as spinner/stop is not
// indicative of what this actually does (shows the webview). // indicative of what this actually does (shows the webview).
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(() -> webView.getPluginManager().postMessage("spinner", "stop"));
@Override
public void run() {
webView.getPluginManager().postMessage("spinner", "stop");
}
});
} }
else if (action.equals("loadUrl")) { else if (action.equals("loadUrl")) {
this.loadUrl(args.getString(0), args.optJSONObject(1)); this.loadUrl(args.getString(0), args.optJSONObject(1));
@ -145,12 +140,7 @@ public class CoreAndroid extends CordovaPlugin {
* Clear the resource cache. * Clear the resource cache.
*/ */
public void clearCache() { public void clearCache() {
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(() -> webView.clearCache());
@Override
public void run() {
webView.clearCache();
}
});
} }
/** /**
@ -217,12 +207,7 @@ public class CoreAndroid extends CordovaPlugin {
* Clear page history for the app. * Clear page history for the app.
*/ */
public void clearHistory() { public void clearHistory() {
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(() -> webView.clearHistory());
@Override
public void run() {
webView.clearHistory();
}
});
} }
/** /**
@ -230,12 +215,7 @@ public class CoreAndroid extends CordovaPlugin {
* This is the same as pressing the backbutton on Android device. * This is the same as pressing the backbutton on Android device.
*/ */
public void backHistory() { public void backHistory() {
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(() -> webView.backHistory());
@Override
public void run() {
webView.backHistory();
}
});
} }
/** /**

View File

@ -301,14 +301,11 @@ public class NativeToJsMessageQueue {
@Override @Override
public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) { public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) {
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(() -> {
@Override
public void run() {
String js = queue.popAndEncodeAsJs(); String js = queue.popAndEncodeAsJs();
if (js != null) { if (js != null) {
engine.loadUrl("javascript:" + js, false); engine.loadUrl("javascript:" + js, false);
} }
}
}); });
} }
} }
@ -330,27 +327,21 @@ public class NativeToJsMessageQueue {
@Override @Override
public void reset() { public void reset() {
delegate.runOnUiThread(new Runnable() { delegate.runOnUiThread(() -> {
@Override
public void run() {
online = false; online = false;
// If the following call triggers a notifyOfFlush, then ignore it. // If the following call triggers a notifyOfFlush, then ignore it.
ignoreNextFlush = true; ignoreNextFlush = true;
delegate.setNetworkAvailable(true); delegate.setNetworkAvailable(true);
}
}); });
} }
@Override @Override
public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) { public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) {
delegate.runOnUiThread(new Runnable() { delegate.runOnUiThread(() -> {
@Override
public void run() {
if (!queue.isEmpty()) { if (!queue.isEmpty()) {
ignoreNextFlush = false; ignoreNextFlush = false;
delegate.setNetworkAvailable(online); delegate.setNetworkAvailable(online);
} }
}
}); });
} }
// Track when online/offline events are fired so that we don't fire excess events. // Track when online/offline events are fired so that we don't fire excess events.
@ -374,14 +365,11 @@ public class NativeToJsMessageQueue {
@Override @Override
public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) { public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) {
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(() -> {
@Override
public void run() {
String js = queue.popAndEncodeAsJs(); String js = queue.popAndEncodeAsJs();
if (js != null) { if (js != null) {
engine.evaluateJavascript(js, null); engine.evaluateJavascript(js, null);
} }
}
}); });
} }
} }

View File

@ -84,14 +84,12 @@ public class SystemWebChromeClient extends WebChromeClient {
*/ */
@Override @Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
dialogsHelper.showAlert(message, new CordovaDialogsHelper.Result() { dialogsHelper.showAlert(message, (success, value) -> {
@Override public void gotResult(boolean success, String value) {
if (success) { if (success) {
result.confirm(); result.confirm();
} else { } else {
result.cancel(); result.cancel();
} }
}
}); });
return true; return true;
} }
@ -101,15 +99,12 @@ public class SystemWebChromeClient extends WebChromeClient {
*/ */
@Override @Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
dialogsHelper.showConfirm(message, new CordovaDialogsHelper.Result() { dialogsHelper.showConfirm(message, (success, value) -> {
@Override
public void gotResult(boolean success, String value) {
if (success) { if (success) {
result.confirm(); result.confirm();
} else { } else {
result.cancel(); result.cancel();
} }
}
}); });
return true; return true;
} }
@ -129,15 +124,12 @@ public class SystemWebChromeClient extends WebChromeClient {
if (handledRet != null) { if (handledRet != null) {
result.confirm(handledRet); result.confirm(handledRet);
} else { } else {
dialogsHelper.showPrompt(message, defaultValue, new CordovaDialogsHelper.Result() { dialogsHelper.showPrompt(message, defaultValue, (success, value) -> {
@Override
public void gotResult(boolean success, String value) {
if (success) { if (success) {
result.confirm(value); result.confirm(value);
} else { } else {
result.cancel(); result.cancel();
} }
}
}); });
} }
return true; return true;