nicolasroard | a9f280f | 2012-10-23 11:26:38 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
nicolasroard | 0d7cdf8 | 2012-09-25 14:27:56 -0700 | [diff] [blame] | 16 | |
| 17 | package com.android.gallery3d.filtershow.cache; |
| 18 | |
| 19 | import android.os.Handler; |
| 20 | import android.os.Handler.Callback; |
| 21 | import android.os.HandlerThread; |
| 22 | import android.os.Message; |
| 23 | import android.os.Process; |
| 24 | |
| 25 | public class DelayedPresetCache extends DirectPresetCache implements Callback { |
| 26 | private HandlerThread mHandlerThread = null; |
| 27 | |
| 28 | private final static int NEW_PRESET = 0; |
| 29 | private final static int COMPUTE_PRESET = 1; |
| 30 | |
| 31 | private Handler mProcessingHandler = null; |
nicolasroard | 81eb997 | 2012-10-05 15:55:56 -0700 | [diff] [blame] | 32 | private final Handler mUIHandler = new Handler() { |
nicolasroard | 0d7cdf8 | 2012-09-25 14:27:56 -0700 | [diff] [blame] | 33 | @Override |
| 34 | public void handleMessage(Message msg) { |
| 35 | switch (msg.what) { |
| 36 | case NEW_PRESET: { |
| 37 | CachedPreset cache = (CachedPreset) msg.obj; |
| 38 | didCompute(cache); |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | @Override |
| 46 | public boolean handleMessage(Message msg) { |
| 47 | switch (msg.what) { |
| 48 | case COMPUTE_PRESET: { |
| 49 | CachedPreset cache = (CachedPreset) msg.obj; |
| 50 | compute(cache); |
| 51 | Message uimsg = mUIHandler.obtainMessage(NEW_PRESET, cache); |
| 52 | mUIHandler.sendMessage(uimsg); |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
nicolasroard | 81eb997 | 2012-10-05 15:55:56 -0700 | [diff] [blame] | 59 | public DelayedPresetCache(ImageLoader loader, int size) { |
| 60 | super(loader, size); |
nicolasroard | 0d7cdf8 | 2012-09-25 14:27:56 -0700 | [diff] [blame] | 61 | mHandlerThread = new HandlerThread("ImageProcessing", Process.THREAD_PRIORITY_BACKGROUND); |
| 62 | mHandlerThread.start(); |
| 63 | mProcessingHandler = new Handler(mHandlerThread.getLooper(), this); |
| 64 | } |
| 65 | |
nicolasroard | 81eb997 | 2012-10-05 15:55:56 -0700 | [diff] [blame] | 66 | @Override |
nicolasroard | 0d7cdf8 | 2012-09-25 14:27:56 -0700 | [diff] [blame] | 67 | protected void willCompute(CachedPreset cache) { |
| 68 | if (cache == null) { |
| 69 | return; |
| 70 | } |
| 71 | cache.setBusy(true); |
| 72 | Message msg = mProcessingHandler.obtainMessage(COMPUTE_PRESET, cache); |
| 73 | mProcessingHandler.sendMessage(msg); |
| 74 | } |
| 75 | } |