summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Gardiner <paulg.artifex@glidos.net>2013-02-07 14:52:09 +0000
committerRobin Watts <robin.watts@artifex.com>2013-02-11 15:47:32 +0000
commitd970d0b4af03d4e03c6ac33d38c98031e59e90f0 (patch)
tree20126d1040a0768873c7c42489b3281f728c7334
parent7a10750ae17ddbcc89a50584093d13a7effbe284 (diff)
downloadmupdf-d970d0b4af03d4e03c6ac33d38c98031e59e90f0.tar.xz
Android: factor out search from main activity
-rw-r--r--android/src/com/artifex/mupdfdemo/MuPDFActivity.java149
-rw-r--r--android/src/com/artifex/mupdfdemo/SearchTask.java128
-rw-r--r--android/src/com/artifex/mupdfdemo/SearchTaskResult.java25
3 files changed, 171 insertions, 131 deletions
diff --git a/android/src/com/artifex/mupdfdemo/MuPDFActivity.java b/android/src/com/artifex/mupdfdemo/MuPDFActivity.java
index 3617f9f2..03beff4b 100644
--- a/android/src/com/artifex/mupdfdemo/MuPDFActivity.java
+++ b/android/src/com/artifex/mupdfdemo/MuPDFActivity.java
@@ -48,50 +48,10 @@ class ThreadPerTaskExecutor implements Executor {
}
}
-class SearchTaskResult {
- public final String txt;
- public final int pageNumber;
- public final RectF searchBoxes[];
- static private SearchTaskResult singleton;
-
- SearchTaskResult(String _txt, int _pageNumber, RectF _searchBoxes[]) {
- txt = _txt;
- pageNumber = _pageNumber;
- searchBoxes = _searchBoxes;
- }
-
- static public SearchTaskResult get() {
- return singleton;
- }
-
- static public void set(SearchTaskResult r) {
- singleton = r;
- }
-}
-
-class ProgressDialogX extends ProgressDialog {
- public ProgressDialogX(Context context) {
- super(context);
- }
-
- private boolean mCancelled = false;
-
- public boolean isCancelled() {
- return mCancelled;
- }
-
- @Override
- public void cancel() {
- mCancelled = true;
- super.cancel();
- }
-}
-
public class MuPDFActivity extends Activity
{
/* The core rendering instance */
private static int tapPageMargin;
- private static final int SEARCH_PROGRESS_DELAY = 200;
private MuPDFCore core;
private String mFileName;
private ReaderView mDocView;
@@ -117,8 +77,7 @@ public class MuPDFActivity extends Activity
private ImageButton mSearchBack;
private ImageButton mSearchFwd;
private EditText mSearchText;
- private AsyncTask<Void,Integer,SearchTaskResult> mSearchTask;
- //private SearchTaskResult mSearchTaskResult;
+ private SearchTask mSearchTask;
private AlertDialog.Builder mAlertBuilder;
private boolean mLinkHighlight = false;
private boolean mSelecting = false;
@@ -569,6 +528,18 @@ public class MuPDFActivity extends Activity
};
mDocView.setAdapter(new MuPDFPageAdapter(this, core));
+ mSearchTask = new SearchTask(this, core) {
+ @Override
+ protected void onTextFound(SearchTaskResult result) {
+ SearchTaskResult.set(result);
+ // Ask the ReaderView to move to the resulting page
+ mDocView.setDisplayedViewIndex(result.pageNumber);
+ // Make the ReaderView act on the change to SearchTaskResult
+ // via overridden onChildSetup method.
+ mDocView.resetupChildren();
+ }
+ };
+
// Make the buttons overlay, and store all its
// controls in variables
makeButtonsView();
@@ -847,7 +818,7 @@ public class MuPDFActivity extends Activity
protected void onPause() {
super.onPause();
- killSearch();
+ mSearchTask.stop();
if (mFileName != null && mDocView != null) {
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
@@ -1006,96 +977,12 @@ public class MuPDFActivity extends Activity
imm.hideSoftInputFromWindow(mSearchText.getWindowToken(), 0);
}
- void killSearch() {
- if (mSearchTask != null) {
- mSearchTask.cancel(true);
- mSearchTask = null;
- }
- }
-
void search(int direction) {
hideKeyboard();
- if (core == null)
- return;
- killSearch();
-
- final int increment = direction;
- final int startIndex = SearchTaskResult.get() == null ? mDocView.getDisplayedViewIndex() : SearchTaskResult.get().pageNumber + increment;
-
- final ProgressDialogX progressDialog = new ProgressDialogX(this);
- progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- progressDialog.setTitle(getString(R.string.searching_));
- progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
- public void onCancel(DialogInterface dialog) {
- killSearch();
- }
- });
- progressDialog.setMax(core.countPages());
-
- mSearchTask = new AsyncTask<Void,Integer,SearchTaskResult>() {
- @Override
- protected SearchTaskResult doInBackground(Void... params) {
- int index = startIndex;
-
- while (0 <= index && index < core.countPages() && !isCancelled()) {
- publishProgress(index);
- RectF searchHits[] = core.searchPage(index, mSearchText.getText().toString());
-
- if (searchHits != null && searchHits.length > 0)
- return new SearchTaskResult(mSearchText.getText().toString(), index, searchHits);
-
- index += increment;
- }
- return null;
- }
-
- @Override
- protected void onPostExecute(SearchTaskResult result) {
- progressDialog.cancel();
- if (result != null) {
- // Ask the ReaderView to move to the resulting page
- mDocView.setDisplayedViewIndex(result.pageNumber);
- SearchTaskResult.set(result);
- // Make the ReaderView act on the change to mSearchTaskResult
- // via overridden onChildSetup method.
- mDocView.resetupChildren();
- } else {
- mAlertBuilder.setTitle(SearchTaskResult.get() == null ? R.string.text_not_found : R.string.no_further_occurences_found);
- AlertDialog alert = mAlertBuilder.create();
- alert.setButton(AlertDialog.BUTTON_POSITIVE, "Dismiss",
- (DialogInterface.OnClickListener)null);
- alert.show();
- }
- }
-
- @Override
- protected void onCancelled() {
- super.onCancelled();
- progressDialog.cancel();
- }
-
- @Override
- protected void onProgressUpdate(Integer... values) {
- super.onProgressUpdate(values);
- progressDialog.setProgress(values[0].intValue());
- }
-
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- mHandler.postDelayed(new Runnable() {
- public void run() {
- if (!progressDialog.isCancelled())
- {
- progressDialog.show();
- progressDialog.setProgress(startIndex);
- }
- }
- }, SEARCH_PROGRESS_DELAY);
- }
- };
-
- mSearchTask.execute();
+ int displayPage = mDocView.getDisplayedViewIndex();
+ SearchTaskResult r = SearchTaskResult.get();
+ int searchPage = r != null ? r.pageNumber : -1;
+ mSearchTask.go(mSearchText.getText().toString(), direction, displayPage, searchPage);
}
@Override
diff --git a/android/src/com/artifex/mupdfdemo/SearchTask.java b/android/src/com/artifex/mupdfdemo/SearchTask.java
new file mode 100644
index 00000000..c6548d1f
--- /dev/null
+++ b/android/src/com/artifex/mupdfdemo/SearchTask.java
@@ -0,0 +1,128 @@
+package com.artifex.mupdfdemo;
+
+import android.app.AlertDialog;
+import android.app.ProgressDialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.graphics.RectF;
+import android.os.Handler;
+
+class ProgressDialogX extends ProgressDialog {
+ public ProgressDialogX(Context context) {
+ super(context);
+ }
+
+ private boolean mCancelled = false;
+
+ public boolean isCancelled() {
+ return mCancelled;
+ }
+
+ @Override
+ public void cancel() {
+ mCancelled = true;
+ super.cancel();
+ }
+}
+
+public abstract class SearchTask {
+ private static final int SEARCH_PROGRESS_DELAY = 200;
+ private final Context mContext;
+ private final MuPDFCore mCore;
+ private final Handler mHandler;
+ private final AlertDialog.Builder mAlertBuilder;
+ private AsyncTask<Void,Integer,SearchTaskResult> mSearchTask;
+
+ public SearchTask(Context context, MuPDFCore core) {
+ mContext = context;
+ mCore = core;
+ mHandler = new Handler();
+ mAlertBuilder = new AlertDialog.Builder(context);
+ }
+
+ protected abstract void onTextFound(SearchTaskResult result);
+
+ public void stop() {
+ if (mSearchTask != null) {
+ mSearchTask.cancel(true);
+ mSearchTask = null;
+ }
+ }
+
+ public void go(final String text, int direction, int displayPage, int searchPage) {
+ if (mCore == null)
+ return;
+ stop();
+
+ final int increment = direction;
+ final int startIndex = searchPage == -1 ? displayPage : searchPage + increment;
+
+ final ProgressDialogX progressDialog = new ProgressDialogX(mContext);
+ progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
+ progressDialog.setTitle(mContext.getString(R.string.searching_));
+ progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
+ public void onCancel(DialogInterface dialog) {
+ stop();
+ }
+ });
+ progressDialog.setMax(mCore.countPages());
+
+ mSearchTask = new AsyncTask<Void,Integer,SearchTaskResult>() {
+ @Override
+ protected SearchTaskResult doInBackground(Void... params) {
+ int index = startIndex;
+
+ while (0 <= index && index < mCore.countPages() && !isCancelled()) {
+ publishProgress(index);
+ RectF searchHits[] = mCore.searchPage(index, text);
+
+ if (searchHits != null && searchHits.length > 0)
+ return new SearchTaskResult(text, index, searchHits);
+
+ index += increment;
+ }
+ return null;
+ }
+
+ @Override
+ protected void onPostExecute(SearchTaskResult result) {
+ progressDialog.cancel();
+ if (result != null) {
+ onTextFound(result);
+ } else {
+ mAlertBuilder.setTitle(SearchTaskResult.get() == null ? R.string.text_not_found : R.string.no_further_occurences_found);
+ AlertDialog alert = mAlertBuilder.create();
+ alert.setButton(AlertDialog.BUTTON_POSITIVE, "Dismiss",
+ (DialogInterface.OnClickListener)null);
+ alert.show();
+ }
+ }
+
+ @Override
+ protected void onCancelled() {
+ progressDialog.cancel();
+ }
+
+ @Override
+ protected void onProgressUpdate(Integer... values) {
+ progressDialog.setProgress(values[0].intValue());
+ }
+
+ @Override
+ protected void onPreExecute() {
+ super.onPreExecute();
+ mHandler.postDelayed(new Runnable() {
+ public void run() {
+ if (!progressDialog.isCancelled())
+ {
+ progressDialog.show();
+ progressDialog.setProgress(startIndex);
+ }
+ }
+ }, SEARCH_PROGRESS_DELAY);
+ }
+ };
+
+ mSearchTask.execute();
+ }
+}
diff --git a/android/src/com/artifex/mupdfdemo/SearchTaskResult.java b/android/src/com/artifex/mupdfdemo/SearchTaskResult.java
new file mode 100644
index 00000000..b2d361c1
--- /dev/null
+++ b/android/src/com/artifex/mupdfdemo/SearchTaskResult.java
@@ -0,0 +1,25 @@
+package com.artifex.mupdfdemo;
+
+import android.graphics.RectF;
+
+public class SearchTaskResult {
+ public final String txt;
+ public final int pageNumber;
+ public final RectF searchBoxes[];
+ static private SearchTaskResult singleton;
+
+ SearchTaskResult(String _txt, int _pageNumber, RectF _searchBoxes[]) {
+ txt = _txt;
+ pageNumber = _pageNumber;
+ searchBoxes = _searchBoxes;
+ }
+
+ static public SearchTaskResult get() {
+ return singleton;
+ }
+
+ static public void set(SearchTaskResult r) {
+ singleton = r;
+ }
+}
+