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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
package cgeo.geocaching;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.util.ArrayList;
public class cgeogpxes extends ListActivity {
private ArrayList<File> files = new ArrayList<File>();
private cgeoapplication app = null;
private cgSettings settings = null;
private cgBase base = null;
private cgWarning warning = null;
private Activity activity = null;
private cgGPXListAdapter adapter = null;
private ProgressDialog waitDialog = null;
private ProgressDialog parseDialog = null;
private Resources res = null;
private loadFiles searchingThread = null;
private boolean endSearching = false;
private int listId = 1;
private int imported = 0;
final private Handler changeWaitDialogHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.obj != null && waitDialog != null) {
waitDialog.setMessage(res.getString(R.string.gpx_import_searching_in) + " " + (String) msg.obj);
}
}
};
final private Handler changeParseDialogHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.obj != null && parseDialog != null) {
parseDialog.setMessage(res.getString(R.string.gpx_import_loading_stored) + " " + (Integer) msg.obj);
}
}
};
final private Handler loadFilesHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
if (files == null || files.isEmpty()) {
if (waitDialog != null) {
waitDialog.dismiss();
}
warning.showToast(res.getString(R.string.gpx_import_no_files));
finish();
return;
} else {
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
if (waitDialog != null) {
waitDialog.dismiss();
}
} catch (Exception e) {
if (waitDialog != null) {
waitDialog.dismiss();
}
Log.e(cgSettings.tag, "cgeogpxes.loadFilesHandler: " + e.toString());
}
}
};
final private Handler loadCachesHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
if (parseDialog != null) {
parseDialog.dismiss();
}
warning.helpDialog(res.getString(R.string.gpx_import_title_caches_imported), imported + " " + res.getString(R.string.gpx_import_caches_imported));
imported = 0;
} catch (Exception e) {
if (parseDialog != null) {
parseDialog.dismiss();
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// init
activity = this;
res = this.getResources();
app = (cgeoapplication) this.getApplication();
settings = new cgSettings(this, getSharedPreferences(cgSettings.preferences, 0));
base = new cgBase(app, settings, getSharedPreferences(cgSettings.preferences, 0));
warning = new cgWarning(this);
// set layout
if (settings.skin == 1) {
setTheme(R.style.light);
} else {
setTheme(R.style.dark);
}
setContentView(R.layout.gpx);
base.setTitle(activity, res.getString(R.string.gpx_import_title));
// google analytics
base.sendAnal(activity, "/gpx-import");
Bundle extras = getIntent().getExtras();
if (extras != null) {
listId = extras.getInt("list");
}
if (listId <= 0) {
listId = 1;
}
setAdapter();
waitDialog = ProgressDialog.show(
this,
res.getString(R.string.gpx_import_title_searching),
res.getString(R.string.gpx_import_searching),
true,
true,
new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface arg0) {
if (searchingThread != null && searchingThread.isAlive()) {
searchingThread.notifyEnd();
}
if (files.isEmpty() == true) {
finish();
}
}
}
);
endSearching = false;
searchingThread = new loadFiles();
searchingThread.start();
}
@Override
public void onResume() {
super.onResume();
settings.load();
}
private void setAdapter() {
if (adapter == null) {
adapter = new cgGPXListAdapter(this, settings, files);
setListAdapter(adapter);
}
}
private class loadFiles extends Thread {
public void notifyEnd() {
endSearching = true;
}
@Override
public void run() {
ArrayList<File> list = new ArrayList<File>();
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) == true) {
final File gpx = new File(Environment.getExternalStorageDirectory().toString() + "/gpx");
if (gpx.exists() && gpx.isDirectory()) {
listDir(list, gpx);
} else {
listDir(list, Environment.getExternalStorageDirectory());
}
} else {
Log.w(cgSettings.tag, "No external media mounted.");
}
} catch (Exception e) {
Log.e(cgSettings.tag, "cgeogpxes.loadFiles.run: " + e.toString());
}
final Message msg = new Message();
msg.obj = "loaded directories";
changeWaitDialogHandler.sendMessage(msg);
files.addAll(list);
list.clear();
loadFilesHandler.sendMessage(new Message());
}
}
private void listDir(ArrayList<File> list, File directory) {
if (directory == null || directory.isDirectory() == false || directory.canRead() == false) {
return;
}
final File[] listPre = directory.listFiles();
if (listPre != null && listPre.length > 0) {
final int listCnt = listPre.length;
for (int i = 0; i < listCnt; i++) {
if (endSearching == true) {
return;
}
if (listPre[i].canRead() == true && listPre[i].isFile() == true) {
final String[] nameParts = listPre[i].getName().split("\\.");
if (nameParts.length > 1) {
final String extension = nameParts[(nameParts.length - 1)].toLowerCase();
if (extension.equals("gpx") == false) {
continue;
}
} else {
continue; // file has no extension
}
list.add(listPre[i]); // add file to list
} else if (listPre[i].canRead() == true && listPre[i].isDirectory() == true) {
final Message msg = new Message();
String name = listPre[i].getName();
if (name.substring(0, 1).equals(".") == true) {
continue; // skip hidden directories
}
if (name.length() > 16) {
name = name.substring(0, 14) + "...";
}
msg.obj = name;
changeWaitDialogHandler.sendMessage(msg);
listDir(list, listPre[i]); // go deeper
}
}
}
return;
}
public void loadGPX(File file) {
if (waitDialog != null) {
waitDialog.dismiss();
}
parseDialog = ProgressDialog.show(
activity,
res.getString(R.string.gpx_import_title_reading_file),
res.getString(R.string.gpx_import_loading),
true,
false);
new loadCaches(file).start();
}
private class loadCaches extends Thread {
File file = null;
public loadCaches(File fileIn) {
file = fileIn;
}
@Override
public void run() {
final long searchId = base.parseGPX(app, file, listId, changeParseDialogHandler);
imported = app.getCount(searchId);
loadCachesHandler.sendMessage(new Message());
}
}
public void goHome(View view) {
base.goHome(activity);
}
}
|