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);
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() {
me.runOnUiThread(() -> {
if (exit) {
me.appView.getView().setVisibility(View.GONE);
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) {
final CordovaActivity me = this;
me.runOnUiThread(new Runnable() {
@Override
public void run() {
me.runOnUiThread(() -> {
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, which) -> {
dialog.dismiss();
if (exit) {
finish();
}
}
});
dlg.create();
dlg.show();
} catch (Exception e) {
finish();
}
}
});
}

View File

@ -42,23 +42,11 @@ 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() {
dialog -> result.gotResult(false, null));
//DO NOTHING
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
dlg.setOnKeyListener((dialog, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
result.gotResult(true, null);
@ -66,7 +54,6 @@ public class CordovaDialogsHelper {
}
else
return true;
}
});
lastHandledDialog = dlg.show();
}
@ -77,30 +64,13 @@ 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() {
dialog -> result.gotResult(false, null));
//DO NOTHING
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
dlg.setOnKeyListener((dialog, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
result.gotResult(false, null);
@ -108,7 +78,6 @@ public class CordovaDialogsHelper {
}
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) {
(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();
}

View File

@ -148,9 +148,7 @@ public class CordovaWebViewImpl implements CordovaWebView {
final int loadUrlTimeoutValue = preferences.getInteger("LoadUrlTimeoutValue", 20000);
// Timeout error method
final Runnable loadError = new Runnable() {
@Override
public void run() {
final Runnable loadError = () -> {
stopLoading();
LOG.e(TAG, "CordovaWebView: TIMEOUT ERROR!");
@ -164,7 +162,6 @@ public class CordovaWebViewImpl implements CordovaWebView {
// 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() {
cordova.getActivity().runOnUiThread(() -> {
if (loadUrlTimeoutValue > 0) {
cordova.getThreadPool().execute(timeoutCheck);
}
engine.loadUrl(url, _recreatePlugins);
}
});
} else {
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
if (engine.getView().getVisibility() != View.VISIBLE) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Thread t = new Thread(() -> {
try {
Thread.sleep(2000);
if (cordova.getActivity() != null) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
pluginManager.postMessage("spinner", "stop");
}
});
cordova.getActivity().runOnUiThread(() -> pluginManager.postMessage("spinner", "stop"));
} else {
LOG.d(TAG, "Cordova activity does not exist.");
}
} catch (InterruptedException e) {
}
}
});
t.start();
}

View File

@ -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());
}
/**

View File

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

View File

@ -84,14 +84,12 @@ 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) {
dialogsHelper.showAlert(message, (success, value) -> {
if (success) {
result.confirm();
} else {
result.cancel();
}
}
});
return true;
}
@ -101,15 +99,12 @@ 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) {
dialogsHelper.showConfirm(message, (success, value) -> {
if (success) {
result.confirm();
} else {
result.cancel();
}
}
});
return true;
}
@ -129,15 +124,12 @@ 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) {
dialogsHelper.showPrompt(message, defaultValue, (success, value) -> {
if (success) {
result.confirm(value);
} else {
result.cancel();
}
}
});
}
return true;