diff --git a/app/src/main/java/fr/ralala/hexviewer/ui/activities/MainActivity.java b/app/src/main/java/fr/ralala/hexviewer/ui/activities/MainActivity.java index 48a1bdf5..b1ad88e3 100644 --- a/app/src/main/java/fr/ralala/hexviewer/ui/activities/MainActivity.java +++ b/app/src/main/java/fr/ralala/hexviewer/ui/activities/MainActivity.java @@ -199,7 +199,7 @@ public boolean onCreateOptionsMenu(final Menu menu) { private void updateEditEmptyMenu() { if (mEditEmptyMenu != null) { mEditEmptyMenu.setVisible(!FileData.isEmpty(mFileData) && mPayloadHexHelper.getAdapter().getEntries().getItems().isEmpty()); - if(mEditEmptyMenu.isVisible()) { + if (mEditEmptyMenu.isVisible()) { mPayloadHexHelper.getAdapter().displayTitle(); } } @@ -394,7 +394,7 @@ public void onItemClick(AdapterView parent, View view, int position, long id) if (e == null) return; if (mPayloadPlainSwipe.isVisible()) { - UIHelper.toast(this, getString(R.string.error_not_supported_in_plain_text)); + UIHelper.showErrorDialog(this, R.string.error_title, R.string.error_not_supported_in_plain_text); return; } @@ -536,7 +536,7 @@ private void popupActionOpen(boolean sequential) { */ private void popupActionSave() { if (FileData.isEmpty(mFileData)) { - UIHelper.toast(this, getString(R.string.open_a_file_before)); + UIHelper.showErrorDialog(this, R.string.error_title, getString(R.string.open_a_file_before)); return; } new TaskSave(this, this).execute(new TaskSave.Request(mFileData, @@ -549,7 +549,7 @@ private void popupActionSave() { */ private void popupActionSaveAs() { if (FileData.isEmpty(mFileData)) { - UIHelper.toast(this, getString(R.string.open_a_file_before)); + UIHelper.showErrorDialog(this, R.string.error_title, getString(R.string.open_a_file_before)); return; } mLauncherSave.startActivity(); diff --git a/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherOpen.java b/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherOpen.java index 3ccf3e6e..b0f9bef2 100644 --- a/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherOpen.java +++ b/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherOpen.java @@ -7,6 +7,7 @@ import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; + import fr.ralala.hexviewer.ApplicationCtx; import fr.ralala.hexviewer.R; import fr.ralala.hexviewer.models.FileData; @@ -35,7 +36,7 @@ public class LauncherOpen { public LauncherOpen(MainActivity activity, LinearLayout mainLayout) { mActivity = activity; - mApp = (ApplicationCtx)activity.getApplicationContext(); + mApp = (ApplicationCtx) activity.getApplicationContext(); mMainLayout = mainLayout; register(); } @@ -60,7 +61,7 @@ private void register() { if (FileHelper.takeUriPermissions(mActivity, data.getData(), false)) { processFileOpen(new FileData(mActivity, data.getData(), false, 0L, 0L)); } else - UIHelper.toast(mActivity, String.format(mActivity.getString(R.string.error_file_permission), FileHelper.getFileName(mApp, data.getData()))); + UIHelper.showErrorDialog(mActivity, R.string.error_title, String.format(mActivity.getString(R.string.error_file_permission), FileHelper.getFileName(mApp, data.getData()))); } else { Log.e(getClass().getSimpleName(), "Null data!!!"); mApp.setSequential(false); @@ -96,7 +97,7 @@ public void processFileOpen(final FileData fd, final String oldToString, final b else r.run(); } else { - UIHelper.toast(mActivity, mActivity.getString(R.string.error_filename)); + UIHelper.showErrorDialog(mActivity, R.string.error_title, mActivity.getString(R.string.error_filename)); } } } diff --git a/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherRecentlyOpen.java b/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherRecentlyOpen.java index 21d6b566..3fa33b88 100644 --- a/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherRecentlyOpen.java +++ b/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherRecentlyOpen.java @@ -34,7 +34,7 @@ public class LauncherRecentlyOpen { private ActivityResultLauncher activityResultLauncherRecentlyOpen; public LauncherRecentlyOpen(MainActivity activity) { - mApp = (ApplicationCtx)activity.getApplicationContext(); + mApp = (ApplicationCtx) activity.getApplicationContext(); mActivity = activity; register(); } @@ -52,14 +52,14 @@ private void processIntentData(Intent data) { long endOffset = data.getLongExtra(RecentlyOpenActivity.RESULT_END_OFFSET, 0L); final FileData fd = new FileData(mActivity, uri, false, startOffset, endOffset); final String oldToString; - if(data.hasExtra(RecentlyOpenActivity.RESULT_OLD_TO_STRING)) + if (data.hasExtra(RecentlyOpenActivity.RESULT_OLD_TO_STRING)) oldToString = data.getStringExtra(RecentlyOpenActivity.RESULT_OLD_TO_STRING); else oldToString = null; if (FileHelper.isFileExists(mActivity.getContentResolver(), uri)) { processFile(fd, uri, oldToString); } else { - UIHelper.toast(mActivity, String.format(mActivity.getString(R.string.error_file_not_found), FileHelper.getFileName(mApp, uri))); + UIHelper.showErrorDialog(mActivity, R.string.error_title, String.format(mActivity.getString(R.string.error_file_not_found), FileHelper.getFileName(mApp, uri))); mApp.getRecentlyOpened().remove(fd); FileHelper.releaseUriPermissions(mActivity, uri); } @@ -68,7 +68,7 @@ private void processIntentData(Intent data) { private void processFile(final FileData fd, final Uri uri, final String oldToString) { if (FileHelper.hasUriPermission(mActivity, uri, true)) { final Runnable r = () -> { - if(fd.getEndOffset() > fd.getRealSize()) + if (fd.getEndOffset() > fd.getRealSize()) mApp.setSequential(true); mActivity.getLauncherOpen().processFileOpen(fd, oldToString, true); }; @@ -78,7 +78,7 @@ private void processFile(final FileData fd, final Uri uri, final String oldToStr } else r.run(); } else { - UIHelper.toast(mActivity, String.format(mActivity.getString(R.string.error_file_permission), FileHelper.getFileName(mApp, uri))); + UIHelper.showErrorDialog(mActivity, R.string.error_title, String.format(mActivity.getString(R.string.error_file_permission), FileHelper.getFileName(mApp, uri))); mApp.getRecentlyOpened().remove(fd); } } diff --git a/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherSave.java b/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherSave.java index 219e8fb1..6f1abb6a 100644 --- a/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherSave.java +++ b/app/src/main/java/fr/ralala/hexviewer/ui/launchers/LauncherSave.java @@ -96,7 +96,8 @@ private void processFileSaveWithDialog(final Uri uri) { private void processFileSave(final Uri uri, final String filename) { DocumentFile sourceDir = DocumentFile.fromTreeUri(mActivity, uri); if (sourceDir == null) { - UIHelper.toast(mActivity, mActivity.getString(R.string.uri_exception)); + UIHelper.showErrorDialog(mActivity, R.string.error_title, + mActivity.getString(R.string.uri_exception) + ": '" + uri.toString() + "'"); Log.e(getClass().getSimpleName(), "1 - Uri exception: '" + uri + "'"); return; } @@ -122,8 +123,9 @@ private void processFileSave(final Uri uri, final String filename) { } else { DocumentFile dFile = sourceDir.createFile("application/octet-stream", filename); if (dFile == null) { - UIHelper.toast(mActivity, mActivity.getString(R.string.uri_exception)); - Log.e(getClass().getSimpleName(), "2 - Uri exception: '" + uri + "'"); + UIHelper.showErrorDialog(mActivity, R.string.error_title, + mActivity.getString(R.string.uri_exception) + ": '" + filename + "'"); + Log.e(getClass().getSimpleName(), "2 - Uri exception: '" + uri + "', filename: '" + filename + "'"); } else { FileData fd = new FileData(mActivity, dFile.getUri(), false); mActivity.setFileData(fd); diff --git a/app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskOpen.java b/app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskOpen.java index bf2a3a47..57aef813 100644 --- a/app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskOpen.java +++ b/app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskOpen.java @@ -59,7 +59,7 @@ public TaskOpen(final Activity activity, final HexTextArrayAdapter adapter, final OpenResultListener listener, final String oldToString, final boolean addRecent) { super(activity, true); - mApp = (ApplicationCtx)activity.getApplicationContext(); + mApp = (ApplicationCtx) activity.getApplicationContext(); mMemoryMonitor = new MemoryMonitor(mApp.getMemoryThreshold(), 2000); mContext = activity; mContentResolver = activity.getContentResolver(); @@ -93,11 +93,11 @@ public void onPostExecute(final Result result) { super.onPostExecute(result); mMemoryMonitor.stop(); if (mLowMemory.get()) - UIHelper.toast(mContext, mContext.getString(R.string.not_enough_memory)); + UIHelper.showErrorDialog(mContext, R.string.error_title, mContext.getString(R.string.not_enough_memory)); else if (isCancelled()) UIHelper.toast(mContext, mContext.getString(R.string.operation_canceled)); else if (result.exception != null) - UIHelper.toast(mContext, mContext.getString(R.string.exception) + ": " + result.exception); + UIHelper.showErrorDialog(mContext, R.string.error_title, mContext.getString(R.string.exception) + ": " + result.exception); else { if (result.listHex != null) { mAdapter.setStartOffset(result.startOffset); @@ -130,10 +130,10 @@ public void onCancelled() { } private void processRead(final FileData fd, - final List list, - final Result result, - long totalSequential, - int maxLength) throws IOException { + final List list, + final Result result, + long totalSequential, + int maxLength) throws IOException { boolean first = true; int reads; boolean forceBreak = false; @@ -155,7 +155,7 @@ private void processRead(final FileData fd, result.exception = iae.getMessage(); forceBreak = true; } - if(forceBreak) + if (forceBreak) break; } } @@ -188,8 +188,8 @@ public Result doInBackground(ContentResolver contentResolver, FileData fd) { /* prepare result */ if (result.exception == null) { result.listHex = list; - if(!mCancel.get()) { - if(mOldToString != null) + if (!mCancel.get()) { + if (mOldToString != null) mApp.getRecentlyOpened().remove(mOldToString); if (mAddRecent) mApp.getRecentlyOpened().add(fd); diff --git a/app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskSave.java b/app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskSave.java index 93336bf2..7775784a 100644 --- a/app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskSave.java +++ b/app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskSave.java @@ -94,11 +94,11 @@ public void onPostExecute(final Result result) { Log.e(this.getClass().getSimpleName(), "File delete error"); } } - UIHelper.toast(mContext, mContext.getString(R.string.operation_canceled)); + UIHelper.showErrorDialog(mContext, R.string.error_title, mContext.getString(R.string.operation_canceled)); } else if (result.exception == null) UIHelper.toast(mContext, mContext.getString(R.string.save_success)); else - UIHelper.toast(mContext, mContext.getString(R.string.exception) + ": " + result.exception); + UIHelper.showErrorDialog(mContext, R.string.error_title, mContext.getString(R.string.exception) + ": " + result.exception); if (mListener != null) mListener.onSaveResult(result.fd, result.exception == null && !isCancelled(), result.runnable); } diff --git a/app/src/main/java/fr/ralala/hexviewer/ui/utils/UIHelper.java b/app/src/main/java/fr/ralala/hexviewer/ui/utils/UIHelper.java index 29ba80d3..c101d209 100644 --- a/app/src/main/java/fr/ralala/hexviewer/ui/utils/UIHelper.java +++ b/app/src/main/java/fr/ralala/hexviewer/ui/utils/UIHelper.java @@ -172,6 +172,28 @@ public static void showErrorDialog(final Context context, CharSequence title, St .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> dialog.dismiss()).show(); } + /** + * Displays an error dialog. + * + * @param context The Android context. + * @param title The dialog title. + * @param message The dialog message. + */ + public static void showErrorDialog(final Context context, int title, String message) { + showErrorDialog(context, context.getString(title), message); + } + + /** + * Displays an error dialog. + * + * @param context The Android context. + * @param title The dialog title. + * @param message The dialog message. + */ + public static void showErrorDialog(final Context context, int title, int message) { + showErrorDialog(context, context.getString(title), context.getString(message)); + } + /** * Returns the view associated with a position *