Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7859 views
1
package com.artifex.mupdfdemo;
2
3
import android.app.AlertDialog;
4
import android.app.ProgressDialog;
5
import android.content.Context;
6
import android.content.DialogInterface;
7
import android.graphics.RectF;
8
import android.os.Handler;
9
10
class ProgressDialogX extends ProgressDialog {
11
public ProgressDialogX(Context context) {
12
super(context);
13
}
14
15
private boolean mCancelled = false;
16
17
public boolean isCancelled() {
18
return mCancelled;
19
}
20
21
@Override
22
public void cancel() {
23
mCancelled = true;
24
super.cancel();
25
}
26
}
27
28
public abstract class SearchTask {
29
private static final int SEARCH_PROGRESS_DELAY = 200;
30
private final Context mContext;
31
private final MuPDFCore mCore;
32
private final Handler mHandler;
33
private final AlertDialog.Builder mAlertBuilder;
34
private AsyncTask<Void,Integer,SearchTaskResult> mSearchTask;
35
36
public SearchTask(Context context, MuPDFCore core) {
37
mContext = context;
38
mCore = core;
39
mHandler = new Handler();
40
mAlertBuilder = new AlertDialog.Builder(context);
41
}
42
43
protected abstract void onTextFound(SearchTaskResult result);
44
45
public void stop() {
46
if (mSearchTask != null) {
47
mSearchTask.cancel(true);
48
mSearchTask = null;
49
}
50
}
51
52
public void go(final String text, int direction, int displayPage, int searchPage) {
53
if (mCore == null)
54
return;
55
stop();
56
57
final int increment = direction;
58
final int startIndex = searchPage == -1 ? displayPage : searchPage + increment;
59
60
final ProgressDialogX progressDialog = new ProgressDialogX(mContext);
61
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
62
progressDialog.setTitle(mContext.getString(R.string.searching_));
63
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
64
public void onCancel(DialogInterface dialog) {
65
stop();
66
}
67
});
68
progressDialog.setMax(mCore.countPages());
69
70
mSearchTask = new AsyncTask<Void,Integer,SearchTaskResult>() {
71
@Override
72
protected SearchTaskResult doInBackground(Void... params) {
73
int index = startIndex;
74
75
while (0 <= index && index < mCore.countPages() && !isCancelled()) {
76
publishProgress(index);
77
RectF searchHits[] = mCore.searchPage(index, text);
78
79
if (searchHits != null && searchHits.length > 0)
80
return new SearchTaskResult(text, index, searchHits);
81
82
index += increment;
83
}
84
return null;
85
}
86
87
@Override
88
protected void onPostExecute(SearchTaskResult result) {
89
progressDialog.cancel();
90
if (result != null) {
91
onTextFound(result);
92
} else {
93
mAlertBuilder.setTitle(SearchTaskResult.get() == null ? R.string.text_not_found : R.string.no_further_occurrences_found);
94
AlertDialog alert = mAlertBuilder.create();
95
alert.setButton(AlertDialog.BUTTON_POSITIVE, mContext.getString(R.string.dismiss),
96
(DialogInterface.OnClickListener)null);
97
alert.show();
98
}
99
}
100
101
@Override
102
protected void onCancelled() {
103
progressDialog.cancel();
104
}
105
106
@Override
107
protected void onProgressUpdate(Integer... values) {
108
progressDialog.setProgress(values[0].intValue());
109
}
110
111
@Override
112
protected void onPreExecute() {
113
super.onPreExecute();
114
mHandler.postDelayed(new Runnable() {
115
public void run() {
116
if (!progressDialog.isCancelled())
117
{
118
progressDialog.show();
119
progressDialog.setProgress(startIndex);
120
}
121
}
122
}, SEARCH_PROGRESS_DELAY);
123
}
124
};
125
126
mSearchTask.execute();
127
}
128
}
129
130