aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgHtmlImg.java
blob: a21dda0eae622a7f4e440dac163cc48dfc871c17 (plain)
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
package cgeo.geocaching;

import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.IConnector;
import cgeo.geocaching.utils.CryptUtils;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.text.Html;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class cgHtmlImg implements Html.ImageGetter {

    final private Activity activity;
    final private String geocode;
    final private boolean placement;
    final private int reason;
    final private boolean onlySave;
    final private boolean save;
    final private BitmapFactory.Options bfOptions;
    final private Display display;
    final private int maxWidth;
    final private int maxHeight;

    public cgHtmlImg(Activity activityIn, String geocodeIn, boolean placementIn, int reasonIn, boolean onlySaveIn) {
        this(activityIn, geocodeIn, placementIn, reasonIn, onlySaveIn, true);
    }

    public cgHtmlImg(Activity activityIn, String geocodeIn, boolean placementIn, int reasonIn, boolean onlySaveIn, boolean saveIn) {
        activity = activityIn;
        geocode = geocodeIn;
        placement = placementIn;
        reason = reasonIn;
        onlySave = onlySaveIn;
        save = saveIn;

        bfOptions = new BitmapFactory.Options();
        bfOptions.inTempStorage = new byte[16 * 1024];

        display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        maxWidth = display.getWidth() - 25;
        maxHeight = display.getHeight() - 25;
    }

    @Override
    public BitmapDrawable getDrawable(String url) {
        Bitmap imagePre = null;
        String dirName = null;
        String fileName = null;
        String fileNameSec = null;

        if (StringUtils.isBlank(url)) {
            return null;
        }

        final String[] urlParts = url.split("\\.");
        String urlExt = null;
        if (urlParts.length > 1) {
            urlExt = "." + urlParts[(urlParts.length - 1)];
            if (urlExt.length() > 5) {
                urlExt = "";
            }
        } else {
            urlExt = "";
        }

        if (StringUtils.isNotBlank(geocode)) {
            dirName = cgSettings.getStorage() + geocode + "/";
            fileName = cgSettings.getStorage() + geocode + "/" + CryptUtils.md5(url) + urlExt;
            fileNameSec = cgSettings.getStorageSec() + geocode + "/" + CryptUtils.md5(url) + urlExt;
        } else {
            dirName = cgSettings.getStorage() + "_others/";
            fileName = cgSettings.getStorage() + "_others/" + CryptUtils.md5(url) + urlExt;
            fileNameSec = cgSettings.getStorageSec() + "_others/" + CryptUtils.md5(url) + urlExt;
        }

        File dir = null;
        dir = new File(cgSettings.getStorage());
        if (dir.exists() == false) {
            dir.mkdirs();
        }
        dir = new File(dirName);
        if (dir.exists() == false) {
            dir.mkdirs();
        }
        dir = null;

        // load image from cache
        if (onlySave == false) {
            try {
                final Date now = new Date();

                final File file = new File(fileName);
                if (file.exists()) {
                    final long imageSize = file.length();

                    // large images will be downscaled on input to save memory
                    if (imageSize > (6 * 1024 * 1024)) {
                        bfOptions.inSampleSize = 48;
                    } else if (imageSize > (4 * 1024 * 1024)) {
                        bfOptions.inSampleSize = 16;
                    } else if (imageSize > (2 * 1024 * 1024)) {
                        bfOptions.inSampleSize = 10;
                    } else if (imageSize > (1 * 1024 * 1024)) {
                        bfOptions.inSampleSize = 6;
                    } else if (imageSize > (0.5 * 1024 * 1024)) {
                        bfOptions.inSampleSize = 2;
                    }

                    if (reason > 0 || file.lastModified() > (now.getTime() - (24 * 60 * 60 * 1000))) {
                        imagePre = BitmapFactory.decodeFile(fileName, bfOptions);
                    }
                }

                if (imagePre == null) {
                    final File fileSec = new File(fileNameSec);
                    if (fileSec.exists()) {
                        final long imageSize = fileSec.length();

                        // large images will be downscaled on input to save memory
                        if (imageSize > (6 * 1024 * 1024)) {
                            bfOptions.inSampleSize = 48;
                        } else if (imageSize > (4 * 1024 * 1024)) {
                            bfOptions.inSampleSize = 16;
                        } else if (imageSize > (2 * 1024 * 1024)) {
                            bfOptions.inSampleSize = 10;
                        } else if (imageSize > (1 * 1024 * 1024)) {
                            bfOptions.inSampleSize = 6;
                        } else if (imageSize > (0.5 * 1024 * 1024)) {
                            bfOptions.inSampleSize = 2;
                        }

                        if (reason > 0 || file.lastModified() > (now.getTime() - (24 * 60 * 60 * 1000))) {
                            imagePre = BitmapFactory.decodeFile(fileNameSec, bfOptions);
                        }
                    }
                }
            } catch (Exception e) {
                Log.w(cgSettings.tag, "cgHtmlImg.getDrawable (reading cache): " + e.toString());
            }
        }

        // download image and save it to the cache
        if ((imagePre == null && reason == 0) || onlySave) {
            Uri uri = null;
            BufferedHttpEntity bufferedEntity = null;

            try {
                // check if uri is absolute or not, if not attach geocaching.com hostname and scheme
                uri = Uri.parse(url);

                if (uri.isAbsolute() == false) {
                    IConnector connector = ConnectorFactory.getConnector(geocode);
                    url = "http://" + connector.getHost() + url;
                }
            } catch (Exception e) {
                Log.e(cgSettings.tag, "cgHtmlImg.getDrawable (parse URL): " + e.toString());
            }

            if (uri != null) {
                for (int i = 0; i < 2; i++) {
                    if (i > 0) {
                        Log.w(cgSettings.tag, "cgHtmlImg.getDrawable: Failed to download data, retrying. Attempt #" + (i + 1));
                    }

                    try {
                        final HttpGet getMethod = new HttpGet(url);
                        final HttpResponse httpResponse = cgBase.doRequest(getMethod);
                        if (httpResponse != null) {
                            final HttpEntity entity = httpResponse.getEntity();
                            bufferedEntity = new BufferedHttpEntity(entity);

                            final long imageSize = bufferedEntity.getContentLength();

                            // large images will be downscaled on input to save memory
                            if (imageSize > (6 * 1024 * 1024)) {
                                bfOptions.inSampleSize = 48;
                            } else if (imageSize > (4 * 1024 * 1024)) {
                                bfOptions.inSampleSize = 16;
                            } else if (imageSize > (2 * 1024 * 1024)) {
                                bfOptions.inSampleSize = 10;
                            } else if (imageSize > (1 * 1024 * 1024)) {
                                bfOptions.inSampleSize = 6;
                            } else if (imageSize > (0.5 * 1024 * 1024)) {
                                bfOptions.inSampleSize = 2;
                            }

                            final InputStream is = bufferedEntity.getContent();
                            try {
                                imagePre = BitmapFactory.decodeStream(is, null, bfOptions);
                            } finally {
                                is.close();
                            }
                        }

                        if (imagePre != null) {
                            break;
                        }
                    } catch (Exception e) {
                        Log.e(cgSettings.tag, "cgHtmlImg.getDrawable (downloading from web)", e);
                    }
                }
            }

            if (save) {
                try {
                    // save to memory/SD cache
                    if (bufferedEntity != null) {
                        final InputStream is = bufferedEntity.getContent();
                        try {
                            final FileOutputStream fos = new FileOutputStream(fileName);
                            try {
                                final byte[] buffer = new byte[4096];
                                int l;
                                while ((l = is.read(buffer)) != -1) {
                                    fos.write(buffer, 0, l);
                                }
                                fos.flush();
                            } finally {
                                fos.close();
                            }
                        } finally {
                            is.close();
                        }
                    }
                } catch (IOException e) {
                    Log.e(cgSettings.tag, "cgHtmlImg.getDrawable (saving to cache)", e);
                }
            }
        }

        if (onlySave) {
            return null;
        }

        // get image and return
        if (imagePre == null) {
            Log.d(cgSettings.tag, "cgHtmlImg.getDrawable: Failed to obtain image");

            if (placement == false) {
                imagePre = BitmapFactory.decodeResource(activity.getResources(), R.drawable.image_no_placement);
            } else {
                imagePre = BitmapFactory.decodeResource(activity.getResources(), R.drawable.image_not_loaded);
            }
        }

        final int imgWidth = imagePre.getWidth();
        final int imgHeight = imagePre.getHeight();

        int width;
        int height;

        if (imgWidth > maxWidth || imgHeight > maxHeight) {
            double ratio;
            if ((maxWidth / imgWidth) > (maxHeight / imgHeight)) {
                ratio = (double) maxHeight / (double) imgHeight;
            } else {
                ratio = (double) maxWidth / (double) imgWidth;
            }

            width = (int) Math.ceil(imgWidth * ratio);
            height = (int) Math.ceil(imgHeight * ratio);

            try {
                imagePre = Bitmap.createScaledBitmap(imagePre, width, height, true);
            } catch (Exception e) {
                Log.d(cgSettings.tag, "cgHtmlImg.getDrawable: Failed to scale image");
                return null;
            }
        } else {
            width = imgWidth;
            height = imgHeight;
        }

        final BitmapDrawable image = new BitmapDrawable(imagePre);
        image.setBounds(new Rect(0, 0, width, height));

        return image;
    }
}