aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgeoimages.java
blob: ef3f1f8ef70832434a5b92013d6e1761f78507db (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
package cgeo.geocaching;

import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.files.LocalStorage;
import cgeo.geocaching.network.HtmlImage;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import android.app.ProgressDialog;
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.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 java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class cgeoimages extends AbstractActivity {

    private static final int UNKNOWN_TYPE = 0;
    public static final int LOG_IMAGES = 1;
    public 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;

    static private Collection<Bitmap> bitmaps = Collections.synchronizedCollection(new ArrayList<Bitmap>());

    private void loadImages(final List<cgImage> images, final int progressMessage, final boolean save, 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);

            ((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);
            }

            new AsyncImgLoader(rowView, img, save, offline).execute();
            imagesView.addView(rowView);
        }
    }

    private class AsyncImgLoader extends AsyncTask<Void, Void, BitmapDrawable> {

        final private LinearLayout view;
        final private cgImage img;
        final private boolean save;
        final boolean offline;

        public AsyncImgLoader(final LinearLayout view, final cgImage img, final boolean save, final boolean offline) {
            this.view = view;
            this.img = img;
            this.save = save;
            this.offline = offline;
        }

        @Override
        protected BitmapDrawable doInBackground(Void... params) {
            final HtmlImage imgGetter = new HtmlImage(cgeoimages.this, geocode, true, offline ? 1 : 0, false, save);
            return imgGetter.getDrawable(img.url);
        }

        @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) {
                        final File file = LocalStorage.getStorageFile(null, "temp.jpg", false);
                        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;
                        }

                        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);
            }

            synchronized (cgeoimages.this) {
                countDone++;
                progressDialog.setProgress(countDone);
                if (progressDialog.getProgress() >= count) {
                    progressDialog.dismiss();
                }
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get parameters
        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());
        final boolean save = img_type == SPOILER_IMAGES ? true : Settings.isStoreLogImages();

        loadImages(images, message, save, offline);
    }

    @Override
    public void onDestroy() {
        // Reclaim native memory faster than the finalizers would
        for (Bitmap b : bitmaps) {
            b.recycle();
        }
        bitmaps.clear();
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

}