summaryrefslogtreecommitdiff
path: root/app/src/main/java/uk/co/blatech/blaupload/util/ImageLoader.java
blob: 4a2dbc58336b3ebf6e72038c1d7c9e132c86eea4 (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
package uk.co.blatech.blaupload.util;

import android.annotation.TargetApi;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.opengl.GLES10;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

import javax.microedition.khronos.opengles.GL10;

import uk.co.blatech.blaupload.R;
import uk.co.blatech.blaupload.data.ImageItem;

/**
 * This class is used for loading images in the background
 *
 * Created by joe on 02/08/14.
 */
public class ImageLoader extends AsyncTask<String, Void, ImageItem> {

    private Resources res;

    //Get the resources object from the UI thread so we can load a placeholder image
    public ImageLoader(Resources resources) {
        this.res = resources;
    }

    /**
     *
     * @param args host, filename, id
     *             host - the hostname to download from, eg http://www.blaupload.co.uk
     *             filename - the filename with no path, eg image.jpg
     *             id - the ID of the image used by the UI to order the images
     *
     * TODO: Some generic way of adding the suffix used for thumbnails
     *
     * @return an ImageItem object which contains the ID, a Bitmap image object, and the filename
     */
    @Override
    protected ImageItem doInBackground(String... args) {

        Bitmap bmp = null;
        String host = args[0];
        String filename = args[1];
        int id = Integer.parseInt(args[2]);
        int[] maxTextureSize = new int[1];
        maxTextureSize[0] = 4096;
        GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0);

        //Add an extra .png on the end to match the current thumbnail filenames
        String url = host + filename;

        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);


        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            String[] parts = filename.split("\\.");
            String extension = parts[parts.length-1];
            //Create the Bitmap if the file exists
            if (statusCode == 200 && isImageExtension(extension)) {
                // If the thumbnail wasn't found, show a placeholder
                HttpEntity entity = response.getEntity();
                byte[] bytes = EntityUtils.toByteArray(entity);
                try {
                    bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                } catch (Exception e) {
                    Log.e(ImageLoader.class.toString(), "Failed decoding " + filename);
                    e.printStackTrace();
                    bmp = BitmapFactory.decodeResource(res, R.drawable.x);
                    ImageItem image = new ImageItem(id, bmp, filename);
                    return image;
                }
                int bmpHeight = bmp.getHeight();
                int bmpWidth = bmp.getWidth();
                if (bmpWidth > maxTextureSize[0]) {

                    float ratio = (float)maxTextureSize[0]/(float)bmpWidth;
                    Matrix matrix = new Matrix();
                    bmpWidth = maxTextureSize[0];
                    bmpHeight = Math.round(bmpHeight * ratio);
                    matrix.postScale(ratio, ratio);
                    bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false);
                } else if (bmpHeight > maxTextureSize[0]) {
                    float ratio = (float)maxTextureSize[0]/(float)bmpHeight;
                    bmpHeight = maxTextureSize[0];
                    bmpWidth = Math.round(bmpWidth * ratio);
                    Matrix matrix = new Matrix();
                    matrix.postScale(ratio, ratio);
                    bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false);
                }
            } else {
                //If the file doesn't exist, use the placeholder instead
                bmp = BitmapFactory.decodeResource(res, R.drawable.x);

            }

        //TODO: Error handling (Not sure how we get here)
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ImageItem image = new ImageItem(id, bmp, filename);
        return image;
    }

    @Override
    protected void onPostExecute(ImageItem result) {
        EventBus.getInstance().post(new ImageLoaderResultEvent(result));

    }

    private boolean isImageExtension(String extension) {
        if (extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") ||
                extension.equalsIgnoreCase("gif") || extension.equalsIgnoreCase("png")) {
            return true;
        } else {
            return false;
        }
    }

}