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,23 +390,15 @@ 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 if (exit) {
public void run() { me.appView.getView().setVisibility(View.GONE);
if (exit) { me.displayError("Application Error", description + " (" + failingUrl + ")", "OK", 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) { 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 try {
public void run() { AlertDialog.Builder dlg = new AlertDialog.Builder(me);
try { dlg.setMessage(message);
AlertDialog.Builder dlg = new AlertDialog.Builder(me); dlg.setTitle(title);
dlg.setMessage(message); dlg.setCancelable(false);
dlg.setTitle(title); dlg.setPositiveButton(button,
dlg.setCancelable(false); (dialog, which) -> {
dlg.setPositiveButton(button, dialog.dismiss();
new AlertDialog.OnClickListener() { if (exit) {
@Override finish();
public void onClick(DialogInterface dialog, int which) { }
dialog.dismiss(); });
if (exit) { dlg.create();
finish(); dlg.show();
} } catch (Exception e) {
} finish();
});
dlg.create();
dlg.show();
} catch (Exception e) {
finish();
}
} }
}); });
} }

View File

@ -42,31 +42,18 @@ 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 //DO NOTHING
public void onCancel(DialogInterface dialog) { dlg.setOnKeyListener((dialog, keyCode, event) -> {
result.gotResult(false, null); if (keyCode == KeyEvent.KEYCODE_BACK)
} {
}); result.gotResult(true, null);
dlg.setOnKeyListener(new DialogInterface.OnKeyListener() { return false;
//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;
} }
else
return true;
}); });
lastHandledDialog = dlg.show(); lastHandledDialog = dlg.show();
} }
@ -77,38 +64,20 @@ 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 //DO NOTHING
public void onCancel(DialogInterface dialog) { dlg.setOnKeyListener((dialog, keyCode, event) -> {
result.gotResult(false, null); if (keyCode == KeyEvent.KEYCODE_BACK)
} {
}); result.gotResult(false, null);
dlg.setOnKeyListener(new DialogInterface.OnKeyListener() { return false;
//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;
} }
else
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 String userText = input.getText().toString();
public void onClick(DialogInterface dialog, int which) { result.gotResult(true, userText);
String userText = input.getText().toString();
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,23 +148,20 @@ 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 stopLoading();
public void run() { LOG.e(TAG, "CordovaWebView: TIMEOUT ERROR!");
stopLoading();
LOG.e(TAG, "CordovaWebView: TIMEOUT ERROR!");
// Handle other errors by passing them to the webview in JS // Handle other errors by passing them to the webview in JS
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
try { try {
data.put("errorCode", -6); data.put("errorCode", -6);
data.put("description", "The connection to the server was unsuccessful."); data.put("description", "The connection to the server was unsuccessful.");
data.put("url", url); data.put("url", url);
} catch (JSONException e) { } catch (JSONException e) {
// 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 if (loadUrlTimeoutValue > 0) {
public void run() { cordova.getThreadPool().execute(timeoutCheck);
if (loadUrlTimeoutValue > 0) {
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,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 // 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 try {
public void run() { Thread.sleep(2000);
try { if (cordova.getActivity() != null) {
Thread.sleep(2000); cordova.getActivity().runOnUiThread(() -> pluginManager.postMessage("spinner", "stop"));
if (cordova.getActivity() != null) { } else {
cordova.getActivity().runOnUiThread(new Runnable() { LOG.d(TAG, "Cordova activity does not exist.");
@Override
public void run() {
pluginManager.postMessage("spinner", "stop");
}
});
} else {
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,13 +301,10 @@ 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 String js = queue.popAndEncodeAsJs();
public void run() { if (js != null) {
String js = queue.popAndEncodeAsJs(); engine.loadUrl("javascript:" + js, false);
if (js != null) {
engine.loadUrl("javascript:" + js, false);
}
} }
}); });
} }
@ -330,26 +327,20 @@ public class NativeToJsMessageQueue {
@Override @Override
public void reset() { public void reset() {
delegate.runOnUiThread(new Runnable() { delegate.runOnUiThread(() -> {
@Override online = false;
public void run() { // If the following call triggers a notifyOfFlush, then ignore it.
online = false; ignoreNextFlush = true;
// If the following call triggers a notifyOfFlush, then ignore it. delegate.setNetworkAvailable(true);
ignoreNextFlush = 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 if (!queue.isEmpty()) {
public void run() { ignoreNextFlush = false;
if (!queue.isEmpty()) { delegate.setNetworkAvailable(online);
ignoreNextFlush = false;
delegate.setNetworkAvailable(online);
}
} }
}); });
} }
@ -374,13 +365,10 @@ 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 String js = queue.popAndEncodeAsJs();
public void run() { if (js != null) {
String js = queue.popAndEncodeAsJs(); engine.evaluateJavascript(js, null);
if (js != null) {
engine.evaluateJavascript(js, null);
}
} }
}); });
} }

View File

@ -84,13 +84,11 @@ 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,14 +99,11 @@ 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 if (success) {
public void gotResult(boolean success, String value) { result.confirm();
if (success) { } else {
result.confirm(); result.cancel();
} else {
result.cancel();
}
} }
}); });
return true; return true;
@ -129,14 +124,11 @@ 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 if (success) {
public void gotResult(boolean success, String value) { result.confirm(value);
if (success) { } else {
result.confirm(value); result.cancel();
} else {
result.cancel();
}
} }
}); });
} }