Subversion Repository Public Repository

Nextrek

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
package nextrek.lib;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;

public class SICKey {
    private static final String TAG = "SICKey";

    public SICKey(String name, int L0_downsample) {
        this.name = name;
        this.L0_downsample = L0_downsample;
        hashedKey = HTTPHelper.getFileNameFromUrl(name);
    }

    private String name;
    private int L0_downsample;
    private String hashedKey;

    public String getName() {
        return name;
    }

    /*
     * Checks if key is store in L0 cache
     */
    public boolean inL0() {
        synchronized (SIC.instance.L0cache) {
            return SIC.instance.L0cache.get(L0_downsample + hashedKey) != null;
        }
    }

    /*
     * Checks if key is store in L1 cache
     */
    public boolean inL1() {
        synchronized (SIC.instance.L1cache) {
            return SIC.instance.L1cache.get(hashedKey) != null;
        }
    }

    public boolean inL2() {
        synchronized (SIC.instance.L2cache) {
            return SIC.instance.L2cache.containsKey(hashedKey);
        }
    }

    public BitmapDrawable getDrawable() {
        BitmapDrawable drawable = null;

        /*
         * tries L0
         */

        drawable = getL0();

        if (drawable != null) {
            if (SIC.DEBUG)
                Log.d(TAG, "from L0 " + hashedKey);
            return drawable;
        }

        /*
         * tries L1
         */
        byte[] value = null;

        value = getL1();

        /*
         * tries L2
         */
        if (value == null) {
            value = getL2();
            if (value != null) {
                if (SIC.DEBUG)
                    Log.d(TAG, "from L2 " + hashedKey);
                // once read from disk.. promote to L1
                putL1(value);
            }
        } else {
            if (SIC.DEBUG)
                Log.d(TAG, "from L1 " + hashedKey);
        }

        if (value != null) {
            // decode

            Bitmap bitmap = decodeImage(value);

            if (bitmap == null) {
                Log.e(TAG, "cannot decode " + getName());
                return null;
            }

            drawable = new BitmapDrawable(bitmap);
            bitmap = null;

            // promote to L0
            putL0(drawable);
        } else {
            // cache miss !!
            if (SIC.DEBUG)
                Log.d(TAG, "miss");
        }

        return drawable;
    }

    public Bitmap decodeImage(byte[] value) {
        Options opts = new Options();

        opts.inPreferredConfig = Bitmap.Config.RGB_565;
        opts.inSampleSize = L0_downsample;
        opts.inDensity = SIC.instance.targetDensity;
        opts.inDither = false;
        opts.inPurgeable = true;
        opts.inInputShareable = true;
        opts.inScaled = false;
        Bitmap bitmap = BitmapFactory.decodeByteArray(value, 0, value.length, opts);
        if (bitmap != null) {
            bitmap.prepareToDraw();
            if (SIC.DEBUG) {
                Log.d(TAG, "decoded (" + L0_downsample + ") image " + bitmap.getDensity() + "dpi " + bitmap.getWidth() + "x" + bitmap.getHeight());
            }
        }
        opts = null;

        return bitmap;
    }

    public void putL0(BitmapDrawable drawable) {
        synchronized (SIC.instance.L0cache) {
            SIC.instance.L0cache.put(L0_downsample + hashedKey, drawable);
        }
    }

    public BitmapDrawable getL0() {
        synchronized (SIC.instance.L0cache) {
            return SIC.instance.L0cache.get(L0_downsample + hashedKey);
        }
    }

    public void putL1(byte[] data) {
        synchronized (SIC.instance.L1cache) {
            SIC.instance.L1cache.put(hashedKey, data);
        }
    }

    public byte[] getL1() {
        synchronized (SIC.instance.L1cache) {
            return SIC.instance.L1cache.get(hashedKey);
        }
    }

    public void putL2(byte[] data) {
        synchronized (SIC.instance.L2cache) {
            SIC.instance.L2cache.put(hashedKey, data);
        }
    }

    public byte[] getL2() {
        synchronized (SIC.instance.L2cache) {
            return SIC.instance.L2cache.get(hashedKey);
        }
    }

    public BitmapDrawable get() {
        /*
         * try first from memory/disk cache.. maybe another process loaded it :D
         */
        try {
            BitmapDrawable drawable = getDrawable();
            if (drawable != null) {
                return drawable;
            }
        } catch (Exception e) {
            e.printStackTrace();
            // continue, try to load from network
        }

        /*
         * load from network
         */

        byte[] data = null;
        try {
            data = HTTPHelper.getDataFromUrl(getName());
            if (data == null) {
                Log.e(TAG, "cannot fetch " + getName());
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        assert(data != null);
        
        putL1(data);

        BitmapDrawable drawable = null;
        try {
            Bitmap bitmap = decodeImage(data);

            if (bitmap == null) {
                putL2(data);
                data = null;
                return null;
            }

            drawable = new BitmapDrawable(bitmap);
            bitmap = null;
        } catch (Exception e) {
            e.printStackTrace();
            putL2(data);
            data = null;
            return null;
        }

        putL0(drawable);

        putL2(data);
        data = null;

        return drawable;
    };
}

Commits for Nextrek/Android/LibrerieNextrek/src/nextrek/lib/SICKey.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000