nicolasroard | 0d7cdf8 | 2012-09-25 14:27:56 -0700 | [diff] [blame] | 1 | |
| 2 | package com.android.gallery3d.filtershow.cache; |
| 3 | |
| 4 | import android.os.Handler; |
| 5 | import android.os.Handler.Callback; |
| 6 | import android.os.HandlerThread; |
| 7 | import android.os.Message; |
| 8 | import android.os.Process; |
| 9 | |
| 10 | public class DelayedPresetCache extends DirectPresetCache implements Callback { |
| 11 | private HandlerThread mHandlerThread = null; |
| 12 | |
| 13 | private final static int NEW_PRESET = 0; |
| 14 | private final static int COMPUTE_PRESET = 1; |
| 15 | |
| 16 | private Handler mProcessingHandler = null; |
nicolasroard | 81eb997 | 2012-10-05 15:55:56 -0700 | [diff] [blame] | 17 | private final Handler mUIHandler = new Handler() { |
nicolasroard | 0d7cdf8 | 2012-09-25 14:27:56 -0700 | [diff] [blame] | 18 | @Override |
| 19 | public void handleMessage(Message msg) { |
| 20 | switch (msg.what) { |
| 21 | case NEW_PRESET: { |
| 22 | CachedPreset cache = (CachedPreset) msg.obj; |
| 23 | didCompute(cache); |
| 24 | break; |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | @Override |
| 31 | public boolean handleMessage(Message msg) { |
| 32 | switch (msg.what) { |
| 33 | case COMPUTE_PRESET: { |
| 34 | CachedPreset cache = (CachedPreset) msg.obj; |
| 35 | compute(cache); |
| 36 | Message uimsg = mUIHandler.obtainMessage(NEW_PRESET, cache); |
| 37 | mUIHandler.sendMessage(uimsg); |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | return false; |
| 42 | } |
| 43 | |
nicolasroard | 81eb997 | 2012-10-05 15:55:56 -0700 | [diff] [blame] | 44 | public DelayedPresetCache(ImageLoader loader, int size) { |
| 45 | super(loader, size); |
nicolasroard | 0d7cdf8 | 2012-09-25 14:27:56 -0700 | [diff] [blame] | 46 | mHandlerThread = new HandlerThread("ImageProcessing", Process.THREAD_PRIORITY_BACKGROUND); |
| 47 | mHandlerThread.start(); |
| 48 | mProcessingHandler = new Handler(mHandlerThread.getLooper(), this); |
| 49 | } |
| 50 | |
nicolasroard | 81eb997 | 2012-10-05 15:55:56 -0700 | [diff] [blame] | 51 | @Override |
nicolasroard | 0d7cdf8 | 2012-09-25 14:27:56 -0700 | [diff] [blame] | 52 | protected void willCompute(CachedPreset cache) { |
| 53 | if (cache == null) { |
| 54 | return; |
| 55 | } |
| 56 | cache.setBusy(true); |
| 57 | Message msg = mProcessingHandler.obtainMessage(COMPUTE_PRESET, cache); |
| 58 | mProcessingHandler.sendMessage(msg); |
| 59 | } |
| 60 | } |