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
293
294
295
296
297
298
299
300
301
302
|
package cgeo.geocaching;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cgeo.geocaching.activity.AbstractActivity;
public class cgeoimages extends AbstractActivity {
public static final int LOG_IMAGE = 1;
public static final int SPOILER_IMAGE = 2;
private int img_type;
private List<cgImage> images = new ArrayList<cgImage>();
private String geocode = null;
private String title = null;
private String url = null;
private LayoutInflater inflater = null;
private ProgressDialog progressDialog = null;
private ProgressDialog waitDialog = null;
private LinearLayout imagesView = null;
private int offline = 0;
private boolean save = true;
private int count = 0;
private int countDone = 0;
private String load_process_string;
private Handler loadImagesHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
if (images.isEmpty()) {
if (waitDialog != null) {
waitDialog.dismiss();
}
switch (img_type) {
case LOG_IMAGE:
showToast(res.getString(R.string.warn_load_log_image));
break;
case SPOILER_IMAGE:
showToast(res.getString(R.string.warn_load_spoiler_image));
break;
}
finish();
return;
} else {
if (waitDialog != null) {
waitDialog.dismiss();
}
if (app.isOffline(geocode, null)) {
offline = 1;
if ((img_type == LOG_IMAGE) && (settings.storelogimages == false)) {
offline = 0;
}
} else {
offline = 0;
}
count = images.size();
progressDialog = new ProgressDialog(cgeoimages.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage(load_process_string);
progressDialog.setCancelable(true);
progressDialog.setMax(count);
progressDialog.show();
LinearLayout rowView = null;
for (final cgImage img : images) {
rowView = (LinearLayout) inflater.inflate(R.layout.cache_image_item, null);
((TextView) rowView.findViewById(R.id.title)).setText(Html.fromHtml(img.title));
if (StringUtils.isNotBlank(img.description)) {
final TextView descView = (TextView) rowView.findViewById(R.id.description);
descView.setText(Html.fromHtml(img.description), TextView.BufferType.SPANNABLE);
descView.setVisibility(View.VISIBLE);
}
final Handler handler = new onLoadHandler(rowView, img);
new Thread() {
@Override
public void run() {
BitmapDrawable image = null;
try {
cgHtmlImg imgGetter = new cgHtmlImg(cgeoimages.this, geocode, true, offline, false, save);
image = imgGetter.getDrawable(img.url);
Message message = handler.obtainMessage(0, image);
handler.sendMessage(message);
} catch (Exception e) {
Log.e(cgSettings.tag, "cgeoimages.onCreate.onClick.run: " + e.toString());
}
}
}.start();
imagesView.addView(rowView);
}
}
} catch (Exception e) {
if (waitDialog != null) {
waitDialog.dismiss();
}
Log.e(cgSettings.tag, "cgeoimages.loadImagesHandler: " + e.toString());
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// init
setTheme();
setContentView(R.layout.spoilers);
// get parameters
Bundle extras = getIntent().getExtras();
// try to get data from extras
if (extras != null) {
geocode = extras.getString("geocode");
img_type = extras.getInt("type", 0);
}
// google analytics
if (img_type == SPOILER_IMAGE)
{
setTitle(res.getString(R.string.cache_spoiler_images_title));
} else if (img_type == LOG_IMAGE) {
setTitle(res.getString(R.string.cache_log_images_title));
}
if (geocode == null) {
showToast("Sorry, c:geo forgot for what cache you want to load spoiler images.");
finish();
return;
}
switch (img_type) {
case LOG_IMAGE:
title = extras.getString("title");
url = extras.getString("url");
if ((title == null) || (url == null)) {
showToast("Sorry, c:geo forgot which logimage you wanted to load.");
finish();
return;
}
break;
}
inflater = getLayoutInflater();
if (imagesView == null) {
imagesView = (LinearLayout) findViewById(R.id.spoiler_list);
}
switch (img_type) {
case SPOILER_IMAGE:
load_process_string = res.getString(R.string.cache_spoiler_images_loading);
save = true;
break;
case LOG_IMAGE:
load_process_string = res.getString(R.string.cache_log_images_loading);
if (settings.storelogimages) {
save = true;
} else {
save = false;
}
break;
default:
load_process_string = "Loading...";
}
waitDialog = ProgressDialog.show(this, null, load_process_string, true);
waitDialog.setCancelable(true);
switch (img_type) {
case LOG_IMAGE:
cgImage logimage = new cgImage();
logimage.title = title;
logimage.url = url;
logimage.description = "";
images.add(logimage);
try {
loadImagesHandler.sendMessage(new Message());
} catch (Exception e) {
Log.e(cgSettings.tag, "cgeoimages.loadImagesHandler.sendMessage: " + e.toString());
}
break;
case SPOILER_IMAGE:
(new loadSpoilers()).start();
break;
default:
showToast("Sorry, can't load unknown image type.");
finish();
}
}
@Override
public void onResume() {
super.onResume();
settings.load();
}
private class loadSpoilers extends Thread {
@Override
public void run() {
try {
images = app.loadSpoilers(geocode);
loadImagesHandler.sendMessage(new Message());
} catch (Exception e) {
Log.e(cgSettings.tag, "cgeoimages.loadSpoilers.run: " + e.toString());
}
}
}
private class onLoadHandler extends Handler {
LinearLayout view = null;
public onLoadHandler(LinearLayout view, cgImage image) {
this.view = view;
}
@Override
public void handleMessage(Message message) {
final BitmapDrawable image = (BitmapDrawable) message.obj;
if (image != null) {
ImageView image_view = null;
image_view = (ImageView) inflater.inflate(R.layout.image_item, null);
Rect bounds = image.getBounds();
image_view.setImageResource(R.drawable.image_not_loaded);
image_view.setClickable(true);
image_view.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
final String directoryTarget = Environment.getExternalStorageDirectory() + "/" + cgSettings.cache + "/" + "temp.jpg";
final File file = new File(directoryTarget);
try {
final FileOutputStream fos = new FileOutputStream(file);
image.getBitmap().compress(CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
Log.e(cgSettings.tag, "cgeoimages.handleMessage.onClick: " + e.toString());
return;
}
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "image/jpg");
startActivity(intent);
if (file.exists())
file.deleteOnExit();
}
});
image_view.setImageDrawable(image);
image_view.setScaleType(ImageView.ScaleType.CENTER_CROP);
image_view.setLayoutParams(new LayoutParams(bounds.width(), bounds.height()));
view.addView(image_view);
}
countDone++;
progressDialog.setProgress(countDone);
if (progressDialog.getProgress() >= count) {
progressDialog.dismiss();
}
}
}
}
|