Skip to content

Commit

Permalink
#286: A progress dialog box for selecting and deleting all lines has …
Browse files Browse the repository at this point in the history
…been added.
  • Loading branch information
Keidan committed Nov 10, 2023
1 parent 22b1fd6 commit d1d414c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
import android.os.Handler;
import android.os.Looper;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;

import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -44,19 +41,16 @@ public abstract class GenericMultiChoiceCallback implements AbsListView.MultiCho
private final ListView mListView;
protected final SearchableListArrayAdapter mAdapter;
protected final MainActivity mActivity;
private final ImageView mRefreshActionViewSelectAll;
private final ClipboardManager mClipboard;
protected final LayoutInflater mLayoutInflater;
private final AlertDialog mProgress;

@SuppressLint("InflateParams")
protected GenericMultiChoiceCallback(MainActivity mainActivity, final ListView listView, final SearchableListArrayAdapter adapter) {
mActivity = mainActivity;
mListView = listView;
mAdapter = adapter;
/* Attach a rotating ImageView to the refresh item as an ActionView */
mLayoutInflater = (LayoutInflater) mainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRefreshActionViewSelectAll = (ImageView) mLayoutInflater.inflate(R.layout.refresh_action_view_select_all, null);
mClipboard = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
mProgress = UIHelper.createCircularProgressDialog(mActivity, null);
}

/**
Expand Down Expand Up @@ -127,6 +121,8 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
@Override
public void onDestroyActionMode(ActionMode mode) {
mAdapter.removeSelection();
if (mProgress.isShowing())
mProgress.dismiss();
}

/**
Expand All @@ -150,7 +146,7 @@ public void onItemCheckedStateChanged(ActionMode mode, int position, long id, bo
* @param item The item that was clicked.
*/
private void actionSelectAll(MenuItem item) {
setActionView(item, mRefreshActionViewSelectAll, () -> {
setActionView(item, () -> {
final int count = mAdapter.getCount();
for (int i = 0; i < count; i++) {
mListView.setItemChecked(i, true);
Expand Down Expand Up @@ -207,21 +203,12 @@ protected void displayError(@StringRes int message) {
/**
* Sets the action view.
*
* @param item MenuItem
* @param refreshActionView ImageView
* @param action Runnable
* @param item MenuItem
* @param action Runnable
*/
protected void setActionView(final MenuItem item, final ImageView refreshActionView, final Runnable action) {
protected void setActionView(final MenuItem item, final Runnable action) {
UIHelper.showCircularProgressDialog(mProgress);
AtomicBoolean checkable = new AtomicBoolean(false);
if (item != null) {
refreshActionView.clearAnimation();
Animation rotation = AnimationUtils.loadAnimation(mActivity, R.anim.clockwise_refresh);
rotation.setRepeatCount(Animation.INFINITE);
checkable.set(item.isCheckable());
item.setCheckable(false);// Do not accept any click events while scanning
refreshActionView.startAnimation(rotation);
item.setActionView(refreshActionView);
}
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(() -> {
action.run();
Expand All @@ -233,7 +220,8 @@ protected void setActionView(final MenuItem item, final ImageView refreshActionV
item.setActionView(null);
}
}
}, 1000);
mProgress.dismiss();
}, 500);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.annotation.SuppressLint;
import android.view.ActionMode;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;

import java.io.ByteArrayOutputStream;
Expand All @@ -30,12 +29,10 @@
* ******************************************************************************
*/
public class HexMultiChoiceCallback extends GenericMultiChoiceCallback {
private final ImageView mRefreshActionViewDelete;

@SuppressLint("InflateParams")
public HexMultiChoiceCallback(MainActivity mainActivity, final ListView listView, final HexTextArrayAdapter adapter) {
super(mainActivity, listView, adapter);
mRefreshActionViewDelete = (ImageView) mLayoutInflater.inflate(R.layout.refresh_action_view_delete, null);
}

/**
Expand Down Expand Up @@ -74,7 +71,7 @@ protected boolean actionCopy(ActionMode mode) {
* @param mode The ActionMode providing the selection mode.
*/
protected void actionClear(MenuItem item, ActionMode mode) {
setActionView(item, mRefreshActionViewDelete, () -> {
setActionView(item, () -> {
final List<Integer> selected = mAdapter.getSelectedIds();
Map<Integer, LineEntry> map = new HashMap<>();
// Captures all selected ids with a loop
Expand Down
38 changes: 38 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 @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Paint;
Expand All @@ -12,7 +13,10 @@
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
Expand Down Expand Up @@ -49,6 +53,40 @@ public class UIHelper {
private UIHelper() {
}

/**
* Show the progress dialog.
*/
public static void showCircularProgressDialog(AlertDialog dialog) {
dialog.show();
Window window = dialog.getWindow();
if (window != null) {
window.setLayout(350, 350);
View v = window.getDecorView();
v.setBackgroundResource(R.drawable.rounded_border);
}
}

/**
* Displays a circular progress dialog.
*
* @param context The Android context.
* @param cancel The cancel event callback (if null the dialog is not cancelable).
* @return AlertDialog
*/
public static AlertDialog createCircularProgressDialog(Context context, DialogInterface.OnCancelListener cancel) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
final ViewGroup nullParent = null;
View view = layoutInflater.inflate(R.layout.circular_progress, nullParent);
AlertDialog progress = new AlertDialog.Builder(context).create();
if (cancel != null) {
progress.setOnCancelListener(cancel);
progress.setCancelable(true);
} else
progress.setCancelable(false);
progress.setView(view);
return progress;
}

/**
* Hides the soft keyboard.
*
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/drawable/circular_progress_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360">

<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="7.0">
<gradient
android:angle="0"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/rounded_border.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorButtonNormal" />
<corners android:radius="20dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
19 changes: 19 additions & 0 deletions app/src/main/res/layout/circular_progress.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">

<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="15dp"
android:backgroundTint="@color/colorAccent"
android:indeterminate="true"
android:max="100"
android:progressDrawable="@drawable/circular_progress_view" />
</RelativeLayout>

0 comments on commit d1d414c

Please sign in to comment.