1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
package com.artifex.mupdfdemo;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileObserver;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;
enum Purpose {
PickPDF,
PickKeyFile
}
public class ChoosePDFActivity extends ListActivity {
static public final String PICK_KEY_FILE = "com.artifex.mupdfdemo.PICK_KEY_FILE";
static private File mDirectory;
static private Map<String, Integer> mPositions = new HashMap<String, Integer>();
private File mParent;
private File [] mDirs;
private File [] mFiles;
private Handler mHandler;
private Runnable mUpdateFiles;
private ChoosePDFAdapter adapter;
private Purpose mPurpose;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPurpose = PICK_KEY_FILE.equals(getIntent().getAction()) ? Purpose.PickKeyFile : Purpose.PickPDF;
String storageState = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(storageState)
&& !Environment.MEDIA_MOUNTED_READ_ONLY.equals(storageState))
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.no_media_warning);
builder.setMessage(R.string.no_media_hint);
AlertDialog alert = builder.create();
alert.setButton(AlertDialog.BUTTON_POSITIVE,getString(R.string.dismiss),
new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alert.show();
return;
}
if (mDirectory == null)
mDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
// Create a list adapter...
adapter = new ChoosePDFAdapter(getLayoutInflater());
setListAdapter(adapter);
// ...that is updated dynamically when files are scanned
mHandler = new Handler();
mUpdateFiles = new Runnable() {
public void run() {
Resources res = getResources();
String appName = res.getString(R.string.app_name);
String version = res.getString(R.string.version);
String title = res.getString(R.string.picker_title_App_Ver_Dir);
setTitle(String.format(title, appName, version, mDirectory));
mParent = mDirectory.getParentFile();
mDirs = mDirectory.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
});
if (mDirs == null)
mDirs = new File[0];
mFiles = mDirectory.listFiles(new FileFilter() {
public boolean accept(File file) {
if (file.isDirectory())
return false;
String fname = file.getName().toLowerCase();
switch (mPurpose) {
case PickPDF:
if (fname.endsWith(".pdf"))
return true;
if (fname.endsWith(".xps"))
return true;
if (fname.endsWith(".cbz"))
return true;
if (fname.endsWith(".epub"))
return true;
if (fname.endsWith(".png"))
return true;
if (fname.endsWith(".jpe"))
return true;
if (fname.endsWith(".jpeg"))
return true;
if (fname.endsWith(".jpg"))
return true;
if (fname.endsWith(".jfif"))
return true;
if (fname.endsWith(".jfif-tbnl"))
return true;
if (fname.endsWith(".tif"))
return true;
if (fname.endsWith(".tiff"))
return true;
return false;
case PickKeyFile:
if (fname.endsWith(".pfx"))
return true;
return false;
default:
return false;
}
}
});
if (mFiles == null)
mFiles = new File[0];
Arrays.sort(mFiles, new Comparator<File>() {
public int compare(File arg0, File arg1) {
return arg0.getName().compareToIgnoreCase(arg1.getName());
}
});
Arrays.sort(mDirs, new Comparator<File>() {
public int compare(File arg0, File arg1) {
return arg0.getName().compareToIgnoreCase(arg1.getName());
}
});
adapter.clear();
if (mParent != null)
adapter.add(new ChoosePDFItem(ChoosePDFItem.Type.PARENT, getString(R.string.parent_directory)));
for (File f : mDirs)
adapter.add(new ChoosePDFItem(ChoosePDFItem.Type.DIR, f.getName()));
for (File f : mFiles)
adapter.add(new ChoosePDFItem(ChoosePDFItem.Type.DOC, f.getName()));
lastPosition();
}
};
// Start initial file scan...
mHandler.post(mUpdateFiles);
// ...and observe the directory and scan files upon changes.
FileObserver observer = new FileObserver(mDirectory.getPath(), FileObserver.CREATE | FileObserver.DELETE) {
public void onEvent(int event, String path) {
mHandler.post(mUpdateFiles);
}
};
observer.startWatching();
}
private void lastPosition() {
String p = mDirectory.getAbsolutePath();
if (mPositions.containsKey(p))
getListView().setSelection(mPositions.get(p));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mPositions.put(mDirectory.getAbsolutePath(), getListView().getFirstVisiblePosition());
if (position < (mParent == null ? 0 : 1)) {
mDirectory = mParent;
mHandler.post(mUpdateFiles);
return;
}
position -= (mParent == null ? 0 : 1);
if (position < mDirs.length) {
mDirectory = mDirs[position];
mHandler.post(mUpdateFiles);
return;
}
position -= mDirs.length;
Uri uri = Uri.fromFile(mFiles[position]);
Intent intent = new Intent(this,MuPDFActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
switch (mPurpose) {
case PickPDF:
// Start an activity to display the PDF file
startActivity(intent);
break;
case PickKeyFile:
// Return the uri to the caller
setResult(RESULT_OK, intent);
finish();
break;
}
}
@Override
protected void onPause() {
super.onPause();
if (mDirectory != null)
mPositions.put(mDirectory.getAbsolutePath(), getListView().getFirstVisiblePosition());
}
}
|