diff --git a/app/src/main/java/org/emulator/calculator/PanAndScaleView.java b/app/src/main/java/org/emulator/calculator/PanAndScaleView.java index cd86808..bd4cd3c 100644 --- a/app/src/main/java/org/emulator/calculator/PanAndScaleView.java +++ b/app/src/main/java/org/emulator/calculator/PanAndScaleView.java @@ -616,13 +616,10 @@ public class PanAndScaleView extends View { boolean osdAllowed = false; Handler osdTimerHandler = new Handler(); - Runnable osdTimerRunnable = new Runnable() { - @Override - public void run() { - // OSD should stop now! - osdAllowed = false; - invalidate(); - } + Runnable osdTimerRunnable = () -> { + // OSD should stop now! + osdAllowed = false; + invalidate(); }; void startOSDTimer() { diff --git a/app/src/main/java/org/emulator/calculator/PrinterSimulator.java b/app/src/main/java/org/emulator/calculator/PrinterSimulator.java index bbbbf89..a12f304 100644 --- a/app/src/main/java/org/emulator/calculator/PrinterSimulator.java +++ b/app/src/main/java/org/emulator/calculator/PrinterSimulator.java @@ -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 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) { diff --git a/app/src/main/java/org/emulator/calculator/PrinterSimulatorFragment.java b/app/src/main/java/org/emulator/calculator/PrinterSimulatorFragment.java index 0e19206..1067d77 100644 --- a/app/src/main/java/org/emulator/calculator/PrinterSimulatorFragment.java +++ b/app/src/main/java/org/emulator/calculator/PrinterSimulatorFragment.java @@ -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,63 +112,55 @@ 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) { - if(item.getItemId() == Utils.resId(PrinterSimulatorFragment.this, "id", "menu_printer_simulator_share_text")) { - Intent intent = new Intent(android.content.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()); - startActivity(Intent.createChooser(intent, getString(Utils.resId(PrinterSimulatorFragment.this, "string", "message_printer_share_text")))); - } else if(item.getItemId() == Utils.resId(PrinterSimulatorFragment.this, "id", "menu_printer_simulator_share_graphic")) { - String imageFilename = "HPPrinter-" + new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US).format(new Date()); - try { - Bitmap paperBitmap = printerSimulator.getImage(); - Bitmap croppedPaperBitmap = Bitmap.createBitmap(paperBitmap.getWidth(), printerSimulator.getPaperHeight(), Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(croppedPaperBitmap); - Paint paint = new Paint(); - canvas.drawBitmap(paperBitmap, 0, 0, paint); + toolbar.setOnMenuItemClickListener(item -> { + if(item.getItemId() == Utils.resId(PrinterSimulatorFragment.this, "id", "menu_printer_simulator_share_text")) { + 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()); + startActivity(Intent.createChooser(intent, getString(Utils.resId(PrinterSimulatorFragment.this, "string", "message_printer_share_text")))); + } else if(item.getItemId() == Utils.resId(PrinterSimulatorFragment.this, "id", "menu_printer_simulator_share_graphic")) { + String imageFilename = "HPPrinter-" + new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US).format(new Date()); + try { + Bitmap paperBitmap = printerSimulator.getImage(); + Bitmap croppedPaperBitmap = Bitmap.createBitmap(paperBitmap.getWidth(), printerSimulator.getPaperHeight(), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(croppedPaperBitmap); + Paint paint = new Paint(); + canvas.drawBitmap(paperBitmap, 0, 0, paint); - Activity activity = getActivity(); - if(activity != null) { - File storagePath = new File(activity.getExternalCacheDir(), ""); - File imageFile = File.createTempFile(imageFilename, ".png", storagePath); - FileOutputStream fileOutputStream = new FileOutputStream(imageFile); - croppedPaperBitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream); - fileOutputStream.close(); - String mimeType = "application/png"; - Intent intent = new Intent(android.content.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")); - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - intent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".provider", imageFile)); - startActivity(Intent.createChooser(intent, getString(Utils.resId(PrinterSimulatorFragment.this, "string", "message_printer_share_graphic")))); - } - } catch (Exception e) { - e.printStackTrace(); - Utils.showAlert(getActivity(), e.getMessage()); - } - } else if(item.getItemId() == Utils.resId(PrinterSimulatorFragment.this, "id", "menu_printer_simulator_change_paper")) { - printerSimulator.changePaper(); - printerGraphView.updatePaperLayout(); - textViewPrinterText.setText(""); - if(printerGraphView != null) { - printerGraphView.updatePaperLayout(); - printerGraphView.invalidate(); + Activity activity = getActivity(); + if(activity != null) { + File storagePath = new File(activity.getExternalCacheDir(), ""); + File imageFile = File.createTempFile(imageFilename, ".png", storagePath); + FileOutputStream fileOutputStream = new FileOutputStream(imageFile); + croppedPaperBitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream); + fileOutputStream.close(); + String mimeType = "application/png"; + 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")); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + intent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".provider", imageFile)); + startActivity(Intent.createChooser(intent, getString(Utils.resId(PrinterSimulatorFragment.this, "string", "message_printer_share_graphic")))); } + } catch (Exception e) { + e.printStackTrace(); + Utils.showAlert(getActivity(), e.getMessage()); + } + } else if(item.getItemId() == Utils.resId(PrinterSimulatorFragment.this, "id", "menu_printer_simulator_change_paper")) { + printerSimulator.changePaper(); + printerGraphView.updatePaperLayout(); + textViewPrinterText.setText(""); + if(printerGraphView != null) { + printerGraphView.updatePaperLayout(); + printerGraphView.invalidate(); } - return true; } + 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) { - Activity activity = getActivity(); - if(activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - if(debug) Log.d(TAG, "onPrinterUpdate(" + textAppended + ")"); - updatePaper(textAppended); - } - }); - } + this.printerSimulator.setOnPrinterUpdateListener(textAppended -> { + Activity activity = getActivity(); + if(activity != null) + activity.runOnUiThread(() -> { + if(debug) Log.d(TAG, "onPrinterUpdate(" + textAppended + ")"); + updatePaper(textAppended); + }); }); } diff --git a/app/src/main/java/org/emulator/forty/eight/MainActivity.java b/app/src/main/java/org/emulator/forty/eight/MainActivity.java index 6b5040d..57aaa06 100644 --- a/app/src/main/java/org/emulator/forty/eight/MainActivity.java +++ b/app/src/main/java/org/emulator/forty/eight/MainActivity.java @@ -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,15 +377,12 @@ 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() { - if(onFileOpen(url) != 0) { - saveLastDocument(url); - } + 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,41 +582,34 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On } - Collections.sort(kmlScripts, new Comparator() { - @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) { - if (which == DialogInterface.BUTTON_POSITIVE) { - if (hasFilename) { - if(NativeLib.onFileSave() == 1) - showAlert(getString(R.string.message_state_saved)); - if (continueCallback != null) - continueCallback.run(); - } else { - fileSaveAsCallback = continueCallback; - OnFileSaveAs(); - } - } else if (which == DialogInterface.BUTTON_NEGATIVE) { - if(continueCallback != null) + DialogInterface.OnClickListener onClickListener = (dialog, which) -> { + if (which == DialogInterface.BUTTON_POSITIVE) { + if (hasFilename) { + if(NativeLib.onFileSave() == 1) + showAlert(getString(R.string.message_state_saved)); + if (continueCallback != null) continueCallback.run(); + } else { + fileSaveAsCallback = continueCallback; + OnFileSaveAs(); } + } else if (which == DialogInterface.BUTTON_NEGATIVE) { + if(continueCallback != null) + continueCallback.run(); } }; @@ -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() { - 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); - } + 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() { - 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); - } + ensureDocumentSaved(() -> { + NativeLib.onFileClose(); + showCalculatorView(false); + saveLastDocument(""); + updateNavigationDrawerItems(); + displayFilename(""); + if(drawer != null) { + new android.os.Handler().postDelayed(() -> drawer.openDrawer(GravityCompat.START), 300); } }, true); } @@ -755,11 +720,9 @@ 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) { - if (which == DialogInterface.BUTTON_POSITIVE) { - openDocument(); - } + DialogInterface.OnClickListener onClickListener = (dialog, which) -> { + if (which == DialogInterface.BUTTON_POSITIVE) { + openDocument(); } }; new AlertDialog.Builder(this) @@ -812,11 +775,9 @@ 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) { - if (which == DialogInterface.BUTTON_POSITIVE) { - NativeLib.onViewReset(); - } + DialogInterface.OnClickListener onClickListener = (dialog, which) -> { + if (which == DialogInterface.BUTTON_POSITIVE) { + NativeLib.onViewReset(); } }; new AlertDialog.Builder(this) @@ -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 kmlScriptsForCurrentModel; + ArrayList 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,45 +836,42 @@ 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) { - if(which == lastIndex) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // < API 21 - new AlertDialog.Builder(MainActivity.this) - .setTitle(getString(R.string.message_kml_folder_selection_need_api_lollipop)) - .setMessage(getString(R.string.message_kml_folder_selection_need_api_lollipop_description)) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - } - }).show(); - } else { - Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); - startActivityForResult(intent, changeKML ? INTENT_PICK_KML_FOLDER_FOR_CHANGING : INTENT_PICK_KML_FOLDER_FOR_NEW_FILE); - } - } else if(which == lastIndex + 1) { - // Reset to default KML folder - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putBoolean("settings_kml_default", true); - editor.apply(); - updateFromPreferences("settings_kml", true); - if(changeKML) - OnViewScript(); - else - OnFileNew(); + .setItems(kmlScriptTitles, (dialog, which) -> { + if(which == lastIndex) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // < API 21 + new AlertDialog.Builder(MainActivity.this) + .setTitle(getString(R.string.message_kml_folder_selection_need_api_lollipop)) + .setMessage(getString(R.string.message_kml_folder_selection_need_api_lollipop_description)) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + } + }).show(); } else { - String kmlScriptFilename = kmlScriptsForCurrentModel.get(which).filename; - if(changeKML) { - int result = NativeLib.onViewScript(kmlScriptFilename); - if(result > 0) { - displayKMLTitle(); - showKMLLog(); - } else - showKMLLogForce(); - updateNavigationDrawerItems(); - } else - newFileFromKML(kmlScriptFilename); + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); + startActivityForResult(intent, changeKML ? INTENT_PICK_KML_FOLDER_FOR_CHANGING : INTENT_PICK_KML_FOLDER_FOR_NEW_FILE); } + } else if(which == lastIndex + 1) { + // Reset to default KML folder + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putBoolean("settings_kml_default", true); + editor.apply(); + updateFromPreferences("settings_kml", true); + if(changeKML) + OnViewScript(); + else + OnFileNew(); + } else { + String kmlScriptFilename = kmlScriptsForCurrentModel.get(which).filename; + if(changeKML) { + int result = NativeLib.onViewScript(kmlScriptFilename); + if(result > 0) { + displayKMLTitle(); + showKMLLog(); + } else + showKMLLogForce(); + updateNavigationDrawerItems(); + } 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); diff --git a/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java b/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java index e488dac..753e63c 100644 --- a/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java +++ b/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java @@ -122,21 +122,18 @@ 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) { - if(value != null) { - String stringValue = value.toString(); - int backgroundFallbackColor = -1; - try { - backgroundFallbackColor = Integer.parseInt(stringValue); - } catch (NumberFormatException ignored) {} - if(backgroundFallbackColor >= 0 && backgroundFallbackColor < stringArrayBackgroundFallbackColor.length) - preference.setSummary(stringArrayBackgroundFallbackColor[backgroundFallbackColor]); + Preference.OnPreferenceChangeListener onPreferenceChangeListenerBackgroundFallbackColor = (preference, value) -> { + if(value != null) { + String stringValue = value.toString(); + int backgroundFallbackColor = -1; + try { + backgroundFallbackColor = Integer.parseInt(stringValue); + } catch (NumberFormatException ignored) {} + if(backgroundFallbackColor >= 0 && backgroundFallbackColor < stringArrayBackgroundFallbackColor.length) + preference.setSummary(stringArrayBackgroundFallbackColor[backgroundFallbackColor]); // preferenceBackgroundCustomColor.setEnabled(backgroundFallbackColor == 2); - } - return true; } + 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) { - if(value != null) - preferenceMacroManualSpeed.setEnabled(!(Boolean) value); - return true; - } + 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,51 +171,45 @@ 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)); + }; + 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)); + }; + 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) { + updatePort2LoadFilename(sharedPreferences.getString(preferencePort2load.getKey(), "")); + preferencePort2load.setOnPreferenceClickListener(preference -> { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); intent.putExtra(Intent.EXTRA_TITLE, "shared.bin"); Activity activity = getActivity(); - if(activity != null) + if (activity != null) activity.startActivityForResult(intent, MainActivity.INTENT_PORT2LOAD); return true; - } - }); + }); + } } void updatePort2LoadFilename(String port2Filename) { @@ -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); }