summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/cache/DelayedPresetCache.java
blob: 361190b7805e7fb948f28d465e70d4a85631c4c6 (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

package com.android.gallery3d.filtershow.cache;

import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Message;
import android.os.Process;

public class DelayedPresetCache extends DirectPresetCache implements Callback {
    private HandlerThread mHandlerThread = null;

    private final static int NEW_PRESET = 0;
    private final static int COMPUTE_PRESET = 1;

    private Handler mProcessingHandler = null;
    private final Handler mUIHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NEW_PRESET: {
                    CachedPreset cache = (CachedPreset) msg.obj;
                    didCompute(cache);
                    break;
                }
            }
        }
    };

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case COMPUTE_PRESET: {
                CachedPreset cache = (CachedPreset) msg.obj;
                compute(cache);
                Message uimsg = mUIHandler.obtainMessage(NEW_PRESET, cache);
                mUIHandler.sendMessage(uimsg);
                break;
            }
        }
        return false;
    }

    public DelayedPresetCache(ImageLoader loader, int size) {
        super(loader, size);
        mHandlerThread = new HandlerThread("ImageProcessing", Process.THREAD_PRIORITY_BACKGROUND);
        mHandlerThread.start();
        mProcessingHandler = new Handler(mHandlerThread.getLooper(), this);
    }

    @Override
    protected void willCompute(CachedPreset cache) {
        if (cache == null) {
            return;
        }
        cache.setBusy(true);
        Message msg = mProcessingHandler.obtainMessage(COMPUTE_PRESET, cache);
        mProcessingHandler.sendMessage(msg);
    }
}