blob: 408ba591268465d904b38475ecc6ece64991e953 [file] [log] [blame]
nicolasroarda9f280f2012-10-23 11:26:38 -07001/*
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 */
nicolasroard0d7cdf82012-09-25 14:27:56 -070016
17package com.android.gallery3d.filtershow.cache;
18
19import android.os.Handler;
20import android.os.Handler.Callback;
21import android.os.HandlerThread;
22import android.os.Message;
23import android.os.Process;
24
25public 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;
nicolasroard81eb9972012-10-05 15:55:56 -070032 private final Handler mUIHandler = new Handler() {
nicolasroard0d7cdf82012-09-25 14:27:56 -070033 @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
nicolasroard81eb9972012-10-05 15:55:56 -070059 public DelayedPresetCache(ImageLoader loader, int size) {
60 super(loader, size);
nicolasroard0d7cdf82012-09-25 14:27:56 -070061 mHandlerThread = new HandlerThread("ImageProcessing", Process.THREAD_PRIORITY_BACKGROUND);
62 mHandlerThread.start();
63 mProcessingHandler = new Handler(mHandlerThread.getLooper(), this);
64 }
65
nicolasroard81eb9972012-10-05 15:55:56 -070066 @Override
nicolasroard0d7cdf82012-09-25 14:27:56 -070067 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}