Skip to content

Commit

Permalink
#251: Use of an "AlertDialog" component instead of a "Toast"
Browse files Browse the repository at this point in the history
  • Loading branch information
Keidan committed Feb 24, 2023
1 parent 1dcf507 commit 973476d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand Down Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class LauncherRecentlyOpen {
private ActivityResultLauncher<Intent> activityResultLauncherRecentlyOpen;

public LauncherRecentlyOpen(MainActivity activity) {
mApp = (ApplicationCtx)activity.getApplicationContext();
mApp = (ApplicationCtx) activity.getApplicationContext();
mActivity = activity;
register();
}
Expand All @@ -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);
}
Expand All @@ -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);
};
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskOpen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -130,10 +130,10 @@ public void onCancelled() {
}

private void processRead(final FileData fd,
final List<LineEntry> list,
final Result result,
long totalSequential,
int maxLength) throws IOException {
final List<LineEntry> list,
final Result result,
long totalSequential,
int maxLength) throws IOException {
boolean first = true;
int reads;
boolean forceBreak = false;
Expand All @@ -155,7 +155,7 @@ private void processRead(final FileData fd,
result.exception = iae.getMessage();
forceBreak = true;
}
if(forceBreak)
if (forceBreak)
break;
}
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskSave.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/fr/ralala/hexviewer/ui/utils/UIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 973476d

Please sign in to comment.