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
|
package cgeo.geocaching;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.files.LocalStorage;
import cgeo.geocaching.network.HtmlImage;
import cgeo.geocaching.utils.Log;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class cgeoimages extends AbstractActivity {
private static final int MENU_BROWSER = 2;
private static final int MENU_FILE = 1;
private static final int UNKNOWN_TYPE = 0;
private static final int LOG_IMAGES = 1;
private static final int SPOILER_IMAGES = 2;
private String geocode = null;
private LayoutInflater inflater = null;
private ProgressDialog progressDialog = null;
private LinearLayout imagesView = null;
private int count = 0;
private int countDone = 0;
private final HashMap<Integer, cgImage> images = new HashMap<Integer, cgImage>();
private BitmapDrawable currentDrawable;
private cgImage currentImage;
static private final Collection<Bitmap> bitmaps = Collections.synchronizedCollection(new ArrayList<Bitmap>());
private void loadImages(final List<cgImage> images, final int progressMessage, final boolean offline) {
count = images.size();
progressDialog = new ProgressDialog(cgeoimages.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage(res.getString(progressMessage));
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);
if (StringUtils.isNotBlank(img.getTitle())) {
((TextView) rowView.findViewById(R.id.title)).setText(Html.fromHtml(img.getTitle()));
rowView.findViewById(R.id.titleLayout).setVisibility(View.VISIBLE);
}
if (StringUtils.isNotBlank(img.getDescription())) {
final TextView descView = (TextView) rowView.findViewById(R.id.description);
descView.setText(Html.fromHtml(img.getDescription()), TextView.BufferType.SPANNABLE);
descView.setVisibility(View.VISIBLE);
}
new AsyncImgLoader(rowView, img, offline).execute();
imagesView.addView(rowView);
}
}
private class AsyncImgLoader extends AsyncTask<Void, Void, BitmapDrawable> {
final private LinearLayout view;
final private cgImage img;
final boolean offline;
public AsyncImgLoader(final LinearLayout view, final cgImage img, final boolean offline) {
this.view = view;
this.img = img;
this.offline = offline;
}
@Override
protected BitmapDrawable doInBackground(Void... params) {
final HtmlImage imgGetter = new HtmlImage(cgeoimages.this, geocode, true, offline ? 1 : 0, false);
return imgGetter.getDrawable(img.getUrl());
}
@Override
protected void onPostExecute(final BitmapDrawable image) {
if (image != null) {
bitmaps.add(image.getBitmap());
final ImageView image_view = (ImageView) inflater.inflate(R.layout.image_item, null);
final 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) {
viewImageInStandardApp(image);
}
});
cgeoimages.this.registerForContextMenu(image_view);
image_view.setImageDrawable(image);
image_view.setScaleType(ImageView.ScaleType.CENTER_CROP);
image_view.setLayoutParams(new LayoutParams(bounds.width(), bounds.height()));
view.addView(image_view);
images.put(image_view.getId(), img);
}
synchronized (cgeoimages.this) {
countDone++;
progressDialog.setProgress(countDone);
if (progressDialog.getProgress() >= count) {
progressDialog.dismiss();
}
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get parameters
final Bundle extras = getIntent().getExtras();
// try to get data from extras
int img_type = UNKNOWN_TYPE;
if (extras != null) {
geocode = extras.getString("geocode");
img_type = extras.getInt("type", 0);
}
if (extras == null || geocode == null) {
showToast("Sorry, c:geo forgot for what cache you want to load spoiler images.");
finish();
return;
}
if (img_type != SPOILER_IMAGES && img_type != LOG_IMAGES) {
showToast("Sorry, can't load unknown image type.");
finish();
return;
}
// init
setTheme();
setContentView(R.layout.spoilers);
setTitle(res.getString(img_type == SPOILER_IMAGES ? R.string.cache_spoiler_images_title : R.string.cache_log_images_title));
inflater = getLayoutInflater();
if (imagesView == null) {
imagesView = (LinearLayout) findViewById(R.id.spoiler_list);
}
final ArrayList<cgImage> images = extras.getParcelableArrayList("images");
if (CollectionUtils.isEmpty(images)) {
showToast(res.getString(R.string.warn_load_images));
finish();
return;
}
final int message = img_type == SPOILER_IMAGES ? R.string.cache_spoiler_images_loading : R.string.cache_log_images_loading;
final boolean offline = app.isOffline(geocode, null) && (img_type == SPOILER_IMAGES || Settings.isStoreLogImages());
loadImages(images, message, offline);
}
@Override
public void onDestroy() {
// Reclaim native memory faster than the finalizers would
for (Bitmap b : bitmaps) {
b.recycle();
}
bitmaps.clear();
super.onDestroy();
}
private void viewImageInStandardApp(final BitmapDrawable image) {
final File file = LocalStorage.getStorageFile(null, "temp.jpg", false, true);
try {
final FileOutputStream fos = new FileOutputStream(file);
image.getBitmap().compress(CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
Log.e(Settings.tag, "cgeoimages.handleMessage.onClick: " + e.toString());
return;
}
final Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "image/jpeg");
startActivity(intent);
if (file.exists()) {
file.deleteOnExit();
}
}
public static void startActivityLogImages(final Context fromActivity, final String geocode, ArrayList<cgImage> logImages) {
startActivity(fromActivity, geocode, logImages, cgeoimages.LOG_IMAGES);
}
private static void startActivity(final Context fromActivity, final String geocode, ArrayList<cgImage> logImages, int imageType) {
final Intent logImgIntent = new Intent(fromActivity, cgeoimages.class);
logImgIntent.putExtra("geocode", geocode.toUpperCase());
logImgIntent.putExtra("type", imageType);
logImgIntent.putParcelableArrayListExtra("images", logImages);
fromActivity.startActivity(logImgIntent);
}
public static void startActivitySpoilerImages(final Context fromActivity, String geocode, ArrayList<cgImage> spoilers) {
startActivity(fromActivity, geocode, spoilers, cgeoimages.SPOILER_IMAGES);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(res.getString(R.string.cache_image));
menu.add(0, MENU_FILE, 0, res.getString(R.string.cache_image_open_file));
menu.add(0, MENU_BROWSER, 0, res.getString(R.string.cache_image_open_browser));
final ImageView view = (ImageView) v;
currentDrawable = (BitmapDrawable) view.getDrawable();
currentImage = images.get(view.getId());
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_FILE:
viewImageInStandardApp(currentDrawable);
return true;
case MENU_BROWSER:
if (currentImage != null) {
currentImage.openInBrowser(this);
}
return true;
default:
break;
}
return super.onContextItemSelected(item);
}
}
|