aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ImageSizeMap.java
blob: fdb883bb1456676f5191a5680b49e211a29bf159 (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
package org.wordpress.android.ui.reader.utils;

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.JSONUtils;
import org.wordpress.android.util.UrlUtils;

import java.util.HashMap;
import java.util.Iterator;

/**
 * hash map of sizes of attachments in a reader post - created from the json "attachments" section
 * of the post endpoints
 */
public class ImageSizeMap extends HashMap<String, ImageSizeMap.ImageSize> {
    private static final String EMPTY_JSON = "{}";
    public ImageSizeMap(String jsonString) {
        if (TextUtils.isEmpty(jsonString) || jsonString.equals(EMPTY_JSON)) {
            return;
        }

        try {
            JSONObject json = new JSONObject(jsonString);
            Iterator<String> it = json.keys();
            if (!it.hasNext()) {
                return;
            }

            while (it.hasNext()) {
                JSONObject jsonAttach = json.optJSONObject(it.next());
                if (jsonAttach != null && JSONUtils.getString(jsonAttach, "mime_type").startsWith("image")) {
                    String normUrl = UrlUtils.normalizeUrl(UrlUtils.removeQuery(JSONUtils.getString(jsonAttach, "URL")));
                    int width = jsonAttach.optInt("width");
                    int height = jsonAttach.optInt("height");

                    // chech if data-orig-size is present and use it
                    String originalSize = jsonAttach.optString("data-orig-size", null);
                    if (originalSize != null) {
                        String[] sizes = originalSize.split(",");
                        if (sizes != null && sizes.length == 2) {
                            width = Integer.parseInt(sizes[0]);
                            height = Integer.parseInt(sizes[1]);
                        }
                    }

                    this.put(normUrl, new ImageSize(width, height));
                }
            }
        } catch (JSONException e) {
            AppLog.e(AppLog.T.READER, e);
        }
    }

    public ImageSize getImageSize(final String imageUrl) {
        if (imageUrl == null) {
            return null;
        } else {
            return super.get(UrlUtils.normalizeUrl(UrlUtils.removeQuery(imageUrl)));
        }
    }

    public String getLargestImageUrl(int minImageWidth) {
        String currentImageUrl = null;
        int currentMaxWidth = minImageWidth;
        for (Entry<String, ImageSize> attach: this.entrySet()) {
            if (attach.getValue().width > currentMaxWidth) {
                currentImageUrl = attach.getKey();
                currentMaxWidth = attach.getValue().width;
            }
        }

        return currentImageUrl;
    }

    public static class ImageSize {
        public final int width;
        public final int height;
        public ImageSize(int width, int height) {
            this.width = width;
            this.height = height;
        }
    }
}