Refactor to Java 1.8

This commit is contained in:
dgis 2019-07-14 23:15:42 +02:00
parent d8fb3a9ec0
commit 2123f5c45c
5 changed files with 184 additions and 261 deletions

View file

@ -616,13 +616,10 @@ public class PanAndScaleView extends View {
boolean osdAllowed = false;
Handler osdTimerHandler = new Handler();
Runnable osdTimerRunnable = new Runnable() {
@Override
public void run() {
Runnable osdTimerRunnable = () -> {
// OSD should stop now!
osdAllowed = false;
invalidate();
}
};
void startOSDTimer() {

View file

@ -17,8 +17,6 @@ package org.emulator.calculator;
import android.graphics.Bitmap;
import android.util.Log;
import java.util.ArrayList;
/**
* Based on the free HP82240B Printer Simulator by Christoph Giesselink
*/
@ -26,7 +24,6 @@ public class PrinterSimulator {
private static final String TAG = "PrinterSimulator";
private boolean debug = false;
private ArrayList<Integer> data = new ArrayList<>();
private StringBuilder m_Text = new StringBuilder();
private StringBuilder textUpdate = new StringBuilder();
@ -188,8 +185,6 @@ public class PrinterSimulator {
textUpdate.setLength(0);
data.add(byData);
do {
// check for begin of ESC sequence
if (byData == ESC && !m_bEsc && m_byGraphLength == 0) {

View file

@ -27,7 +27,6 @@ import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
@ -113,19 +112,12 @@ public class PrinterSimulatorFragment extends AppCompatDialogFragment {
toolbar.setTitle(title);
toolbar.setNavigationIcon(Utils.resId(this, "drawable", "ic_keyboard_backspace_white_24dp"));
toolbar.setNavigationOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
}
v -> dismiss()
);
toolbar.inflateMenu(Utils.resId(this, "menu", "fragment_printer_simulator"));
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
toolbar.setOnMenuItemClickListener(item -> {
if(item.getItemId() == Utils.resId(PrinterSimulatorFragment.this, "id", "menu_printer_simulator_share_text")) {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, Utils.resId(PrinterSimulatorFragment.this, "string", "message_printer_share_text"));
intent.putExtra(Intent.EXTRA_TEXT, printerSimulator.getText());
@ -147,7 +139,7 @@ public class PrinterSimulatorFragment extends AppCompatDialogFragment {
croppedPaperBitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
fileOutputStream.close();
String mimeType = "application/png";
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_SUBJECT, Utils.resId(PrinterSimulatorFragment.this, "string", "message_printer_share_graphic"));
@ -169,7 +161,6 @@ public class PrinterSimulatorFragment extends AppCompatDialogFragment {
}
}
return true;
}
});
setMenuVisibility(true);
@ -235,21 +226,15 @@ public class PrinterSimulatorFragment extends AppCompatDialogFragment {
}
}
public void setPrinterSimulator(final PrinterSimulator printerSimulator) {
public void setPrinterSimulator(PrinterSimulator printerSimulator) {
this.printerSimulator = printerSimulator;
this.printerSimulator.setOnPrinterUpdateListener(new PrinterSimulator.OnPrinterUpdateListener() {
@Override
public void onPrinterUpdate(final String textAppended) {
this.printerSimulator.setOnPrinterUpdateListener(textAppended -> {
Activity activity = getActivity();
if(activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.runOnUiThread(() -> {
if(debug) Log.d(TAG, "onPrinterUpdate(" + textAppended + ")");
updatePaper(textAppended);
}
});
}
});
}

View file

@ -74,7 +74,6 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
@ -134,7 +133,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = findViewById(R.id.toolbar);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setVisibility(View.GONE);
@ -187,19 +186,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
fragmentPrinterSimulator.setPrinterSimulator(printerSimulator);
printerSimulator.setOnPrinterOutOfPaperListener(new PrinterSimulator.OnPrinterOutOfPaperListener() {
@Override
public void onPrinterOutOfPaper(final int currentLine, final int maxLine, final int currentPixelRow, final int maxPixelRow) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, String.format(Locale.US,
getString(R.string.message_printer_out_of_paper),
maxLine, maxPixelRow), Toast.LENGTH_LONG).show();
}
});
}
});
printerSimulator.setOnPrinterOutOfPaperListener((currentLine, maxLine, currentPixelRow, maxPixelRow) -> runOnUiThread(
() -> Toast.makeText(MainActivity.this, String.format(Locale.US, getString(R.string.message_printer_out_of_paper), maxLine, maxPixelRow), Toast.LENGTH_LONG).show()
)
);
//android.os.Debug.waitForDebugger();
@ -387,16 +377,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
String[] mrus = mruLinkedHashMapKeySet.toArray(new String[mruLength]);
int mruClickedIndex = id - MRU_ID_START;
final String url = mrus[mruClickedIndex];
String url = mrus[mruClickedIndex];
mruLinkedHashMap.get(url);
ensureDocumentSaved(new Runnable() {
@Override
public void run() {
ensureDocumentSaved(() -> {
if(onFileOpen(url) != 0) {
saveLastDocument(url);
}
}
});
}
@ -519,9 +506,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
DocumentFile kmlFolderDocumentFile = DocumentFile.fromTreeUri(this, kmlFolderUri);
if(kmlFolderDocumentFile != null) {
for (DocumentFile file : kmlFolderDocumentFile.listFiles()) {
final String url = file.getUri().toString();
final String name = file.getName();
final String mime = file.getType();
String url = file.getUri().toString();
String name = file.getName();
String mime = file.getType();
Log.d(TAG, "url: " + url + ", name: " + name + ", mime: " + mime);
if(kmlMimeType.equals(mime)) {
calculatorsAssetFilenames.add(url);
@ -595,27 +582,21 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
Collections.sort(kmlScripts, new Comparator<KMLScriptItem>() {
@Override
public int compare(KMLScriptItem lhs, KMLScriptItem rhs) {
return lhs.title.compareTo(rhs.title);
}
});
Collections.sort(kmlScripts, (lhs, rhs) -> lhs.title.compareTo(rhs.title));
}
}
private void ensureDocumentSaved(final Runnable continueCallback) {
private void ensureDocumentSaved(Runnable continueCallback) {
ensureDocumentSaved(continueCallback, false);
}
private Runnable fileSaveAsCallback = null;
private void ensureDocumentSaved(final Runnable continueCallback, boolean forceRequest) {
private void ensureDocumentSaved(Runnable continueCallback, boolean forceRequest) {
if(NativeLib.isDocumentAvailable()) {
final String currentFilename = NativeLib.getCurrentFilename();
final boolean hasFilename = (currentFilename != null && currentFilename.length() > 0);
String currentFilename = NativeLib.getCurrentFilename();
boolean hasFilename = (currentFilename != null && currentFilename.length() > 0);
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
DialogInterface.OnClickListener onClickListener = (dialog, which) -> {
if (which == DialogInterface.BUTTON_POSITIVE) {
if (hasFilename) {
if(NativeLib.onFileSave() == 1)
@ -630,7 +611,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
if(continueCallback != null)
continueCallback.run();
}
}
};
if(!forceRequest && hasFilename && sharedPreferences.getBoolean("settings_autosave", true)) {
@ -650,12 +630,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
// By default Port1 is set
setPort1Settings(true, true);
ensureDocumentSaved(new Runnable() {
@Override
public void run() {
showKMLPicker(false);
}
});
ensureDocumentSaved(() -> showKMLPicker(false));
}
private void newFileFromKML(String kmlScriptFilename) {
@ -670,15 +645,12 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
private void OnFileOpen() {
ensureDocumentSaved(new Runnable() {
@Override
public void run() {
ensureDocumentSaved(() -> {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.filename) + "-state.e48");
startActivityForResult(intent, INTENT_GETOPENFILENAME);
}
});
}
private void OnFileSave() {
@ -722,21 +694,14 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
startActivityForResult(intent, INTENT_GETSAVEFILENAME);
}
private void OnFileClose() {
ensureDocumentSaved(new Runnable() {
@Override
public void run() {
ensureDocumentSaved(() -> {
NativeLib.onFileClose();
showCalculatorView(false);
saveLastDocument("");
updateNavigationDrawerItems();
displayFilename("");
if(drawer != null) {
new android.os.Handler().postDelayed(new Runnable() {
public void run() {
drawer.openDrawer(GravityCompat.START);
}
}, 300);
}
new android.os.Handler().postDelayed(() -> drawer.openDrawer(GravityCompat.START), 300);
}
}, true);
}
@ -755,12 +720,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private void OnObjectLoad() {
if(sharedPreferences.getBoolean("settings_objectloadwarning", false)) {
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
DialogInterface.OnClickListener onClickListener = (dialog, which) -> {
if (which == DialogInterface.BUTTON_POSITIVE) {
openDocument();
}
}
};
new AlertDialog.Builder(this)
.setMessage(getString(R.string.message_object_load))
@ -812,12 +775,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
NativeLib.onStackPaste();
}
private void OnViewReset() {
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
DialogInterface.OnClickListener onClickListener = (dialog, which) -> {
if (which == DialogInterface.BUTTON_POSITIVE) {
NativeLib.onViewReset();
}
}
};
new AlertDialog.Builder(this)
.setMessage(getString(R.string.message_press_reset))
@ -850,10 +811,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
fragmentPrinterSimulator.show(getSupportFragmentManager(), "Hello Fragment");
}
private void showKMLPicker(final boolean changeKML) {
private void showKMLPicker(boolean changeKML) {
extractKMLScripts();
final ArrayList<KMLScriptItem> kmlScriptsForCurrentModel;
ArrayList<KMLScriptItem> kmlScriptsForCurrentModel;
if(changeKML) {
kmlScriptsForCurrentModel = new ArrayList<>();
char m = (char) NativeLib.getCurrentModel();
@ -866,8 +827,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
kmlScriptsForCurrentModel = kmlScripts;
boolean hasEmbeddedKMLs = getPackageName().contains("org.emulator.forty.eight");
final int lastIndex = kmlScriptsForCurrentModel.size();
final String[] kmlScriptTitles = new String[lastIndex + (hasEmbeddedKMLs ? 2 : 1)];
int lastIndex = kmlScriptsForCurrentModel.size();
String[] kmlScriptTitles = new String[lastIndex + (hasEmbeddedKMLs ? 2 : 1)];
for (int i = 0; i < kmlScriptsForCurrentModel.size(); i++)
kmlScriptTitles[i] = kmlScriptsForCurrentModel.get(i).title;
kmlScriptTitles[lastIndex] = getResources().getString(R.string.load_custom_kml);
@ -875,9 +836,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
kmlScriptTitles[lastIndex + 1] = getResources().getString(R.string.load_default_kml);
new AlertDialog.Builder(MainActivity.this)
.setTitle(getResources().getString(R.string.pick_calculator))
.setItems(kmlScriptTitles, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
.setItems(kmlScriptTitles, (dialog, which) -> {
if(which == lastIndex) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // < API 21
new AlertDialog.Builder(MainActivity.this)
@ -914,7 +873,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
} else
newFileFromKML(kmlScriptFilename);
}
}
}).show();
}
@ -1172,12 +1130,12 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
private void makeUriPersistable(Intent data, Uri uri) {
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
getContentResolver().takePersistableUriPermission(uri, takeFlags);
}
private void makeUriPersistableReadOnly(Intent data, Uri uri) {
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION);
int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
getContentResolver().takePersistableUriPermission(uri, takeFlags);
}
@ -1288,8 +1246,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
DocumentFile folderDocumentFile = DocumentFile.fromTreeUri(this, folderURI);
if(folderDocumentFile != null) {
for (DocumentFile file : folderDocumentFile.listFiles()) {
final String url = file.getUri().toString();
final String name = file.getName();
String url = file.getUri().toString();
String name = file.getName();
//Log.d(TAG, "url: " + url + ", name: " + name);
if (filename.equals(name)) {
return openFileFromContentResolver(url, writeAccess);

View file

@ -122,9 +122,7 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere
// final ColorPickerPreferenceCompat preferenceBackgroundCustomColor = (ColorPickerPreferenceCompat)findPreference("settings_background_custom_color");
if(preferenceBackgroundFallbackColor != null /*&& preferenceBackgroundCustomColor != null*/) {
final String[] stringArrayBackgroundFallbackColor = getResources().getStringArray(R.array.settings_background_fallback_color_item);
Preference.OnPreferenceChangeListener onPreferenceChangeListenerBackgroundFallbackColor = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
Preference.OnPreferenceChangeListener onPreferenceChangeListenerBackgroundFallbackColor = (preference, value) -> {
if(value != null) {
String stringValue = value.toString();
int backgroundFallbackColor = -1;
@ -136,7 +134,6 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere
// preferenceBackgroundCustomColor.setEnabled(backgroundFallbackColor == 2);
}
return true;
}
};
preferenceBackgroundFallbackColor.setOnPreferenceChangeListener(onPreferenceChangeListenerBackgroundFallbackColor);
onPreferenceChangeListenerBackgroundFallbackColor.onPreferenceChange(preferenceBackgroundFallbackColor,
@ -161,15 +158,12 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere
// Macro
Preference preferenceMacroRealSpeed = findPreference("settings_macro_real_speed");
final Preference preferenceMacroManualSpeed = findPreference("settings_macro_manual_speed");
Preference preferenceMacroManualSpeed = findPreference("settings_macro_manual_speed");
if(preferenceMacroRealSpeed != null && preferenceMacroManualSpeed != null) {
Preference.OnPreferenceChangeListener onPreferenceChangeListenerMacroRealSpeed = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
Preference.OnPreferenceChangeListener onPreferenceChangeListenerMacroRealSpeed = (preference, value) -> {
if(value != null)
preferenceMacroManualSpeed.setEnabled(!(Boolean) value);
return true;
}
};
preferenceMacroRealSpeed.setOnPreferenceChangeListener(onPreferenceChangeListenerMacroRealSpeed);
onPreferenceChangeListenerMacroRealSpeed.onPreferenceChange(preferenceMacroRealSpeed, sharedPreferences.getBoolean(preferenceMacroRealSpeed.getKey(), true));
@ -177,41 +171,35 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere
// Ports 1 & 2 settings
final Preference preferencePort1en = findPreference("settings_port1en");
final Preference preferencePort1wr = findPreference("settings_port1wr");
final Preference preferencePort2en = findPreference("settings_port2en");
final Preference preferencePort2wr = findPreference("settings_port2wr");
Preference preferencePort1en = findPreference("settings_port1en");
Preference preferencePort1wr = findPreference("settings_port1wr");
Preference preferencePort2en = findPreference("settings_port2en");
Preference preferencePort2wr = findPreference("settings_port2wr");
preferencePort2load = findPreference("settings_port2load");
if(preferencePort1en != null && preferencePort1wr != null
&& preferencePort2en != null && preferencePort2wr != null
&& preferencePort2load != null) {
boolean enablePortPreferences = NativeLib.isPortExtensionPossible();
final boolean enablePortPreferences = NativeLib.isPortExtensionPossible();
Preference.OnPreferenceChangeListener onPreferenceChangeListenerPort1en = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
Preference.OnPreferenceChangeListener onPreferenceChangeListenerPort1en = (preference, value) -> {
preferencePort1en.setEnabled(enablePortPreferences);
preferencePort1wr.setEnabled(enablePortPreferences);
return true;
}
};
preferencePort1en.setOnPreferenceChangeListener(onPreferenceChangeListenerPort1en);
onPreferenceChangeListenerPort1en.onPreferenceChange(preferencePort1en, sharedPreferences.getBoolean(preferencePort1en.getKey(), false));
Preference.OnPreferenceChangeListener onPreferenceChangeListenerPort2en = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
Preference.OnPreferenceChangeListener onPreferenceChangeListenerPort2en = (preference, value) -> {
preferencePort2en.setEnabled(enablePortPreferences);
preferencePort2wr.setEnabled(enablePortPreferences);
preferencePort2load.setEnabled(enablePortPreferences);
return true;
}
};
preferencePort2en.setOnPreferenceChangeListener(onPreferenceChangeListenerPort2en);
onPreferenceChangeListenerPort2en.onPreferenceChange(preferencePort2en, sharedPreferences.getBoolean(preferencePort2en.getKey(), false));
updatePort2LoadFilename(sharedPreferences.getString(preferencePort2load.getKey(), ""));
preferencePort2load.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
preferencePort2load.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
@ -220,9 +208,9 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere
if (activity != null)
activity.startActivityForResult(intent, MainActivity.INTENT_PORT2LOAD);
return true;
}
});
}
}
void updatePort2LoadFilename(String port2Filename) {
if(preferencePort2load != null) {
@ -259,7 +247,7 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere
}
private void makeUriPersistable(Intent data, Uri uri) {
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
getContentResolver().takePersistableUriPermission(uri, takeFlags);
}