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

import nextrek.imagecache.DiskLruCache;
import nextrek.imagecache.LruCache;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;

public class SIC {

    public static final boolean DEBUG = false;

    /*
     * L0 cache (memory Drawable)
     * --------------------------
     * Provides decoded BitmapDrawable ready to be used
     */
    private static final int L0_BYTE_BUDGET = 1 * 1024 * 1024; // 2MiB

    public static final int L0_THUMBNAIL_DOWNSAMPLE = 3;

    /*
     * L1 cache (memory byte[])
     * ------------------------
     * Provides encoded raw images to be decoded in order to be used. Once decoded L1 element goes to L0.
     * Because decoding is a CPU intensive work, a thread will take care of the process
     */
    private static final int L1_BYTE_BUDGET = 2 * 1024 * 1024; // 2MiB

    /*
     * L2 cache (disk byte[])
     * ----------------------
     * Provides encoded raw images to be decoded from disk in order to be used. Once read L2 element goes to L1.
     * Because I/O access is slow, a thread will take care of the process
     */

    private static final int L2_BYTE_BUDGET = 8 * 1024 * 1024;

    /*
     * ============
     * INTERFACES
     * ============
     */

    public static synchronized void InstallCache(Context context) {
        if (instance == null) {
            instance = new SIC(context);
        } else {
            // already installed
        }
    }

    public static synchronized void FlushCache() {
        System.out.println("onPause: flushing cache *** BEGIN");
        if (instance.L0cache != null) {

            instance.L0cache.evictAll();
        }

        if (instance.L1cache != null) {
            instance.L1cache.evictAll();
        }

        if (instance.L2cache != null) {
            instance.L2cache.flush();
        }
        System.out.println("onPause: flushing cache *** END");
    }

    /*
     * ================
     * IMPLEMENTATION
     * ================
     */

    public LruCache<String, BitmapDrawable> L0cache = null;
    public LruCache<String, byte[]> L1cache = null;
    public DiskLruCache L2cache = null;

    public int targetDensity;

    public static SIC instance = null;

    private static final String TAG = "SIC";

    private SIC(Context context) {
        if (SIC.DEBUG)
            Log.d(TAG, "Installing L0");
        L0cache = new LruCache<String, BitmapDrawable>("L0", L0_BYTE_BUDGET) {
            @Override
            protected int sizeOf(final String key, final BitmapDrawable value) {
                return value.getIntrinsicWidth() * 4 * value.getIntrinsicHeight();// RGBA: worst case
            }
        };

        if (SIC.DEBUG)
            Log.d(TAG, "Installing L1");
        L1cache = new LruCache<String, byte[]>("L1", L1_BYTE_BUDGET) {
            @Override
            protected int sizeOf(final String key, final byte[] value) {
                return value.length;
            }
        };

        if (SIC.DEBUG)
            Log.d(TAG, "Installing L2");
        L2cache = new DiskLruCache(context, "L2", L2_BYTE_BUDGET);

        targetDensity = context.getResources().getDisplayMetrics().densityDpi;

        if (SIC.DEBUG)
            Log.d(TAG, "setting target density to " + targetDensity + " dpi");

        if (SIC.DEBUG)
            Log.d(TAG, "Installation complete");
    }

    public static void preload(String imageUrl, int l0ThumbnailDownsample) {
        SICKey key = new SICKey(imageUrl, SIC.L0_THUMBNAIL_DOWNSAMPLE);
        key.get();
    }
    
    public static String getTempDir() {
        return instance.L2cache.getTempDir();
    }

}

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

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