2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.pipeline;
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.HandlerThread;
22 import android.os.Message;
23 import android.util.Log;
25 import java.util.HashMap;
27 public class ProcessingTaskController implements Handler.Callback {
28 private static final String LOGTAG = "ProcessingTaskController";
30 private Context mContext;
31 private HandlerThread mHandlerThread = null;
32 private Handler mProcessingHandler = null;
33 private int mCurrentType;
34 private HashMap<Integer, ProcessingTask> mTasks = new HashMap<Integer, ProcessingTask>();
36 public final static int RESULT = 1;
37 public final static int UPDATE = 2;
39 private final Handler mResultHandler = new Handler() {
42 public void handleMessage(Message msg) {
43 ProcessingTask task = mTasks.get(msg.what);
45 if (msg.arg1 == RESULT) {
46 task.onResult((ProcessingTask.Result) msg.obj);
47 } else if (msg.arg1 == UPDATE) {
48 task.onUpdate((ProcessingTask.Update) msg.obj);
50 Log.w(LOGTAG, "received unknown message! " + msg.arg1);
57 public boolean handleMessage(Message msg) {
58 ProcessingTask task = mTasks.get(msg.what);
60 task.processRequest((ProcessingTask.Request) msg.obj);
66 public ProcessingTaskController(Context context) {
68 mHandlerThread = new HandlerThread("ProcessingTaskController",
69 android.os.Process.THREAD_PRIORITY_FOREGROUND);
70 mHandlerThread.start();
71 mProcessingHandler = new Handler(mHandlerThread.getLooper(), this);
74 public Handler getProcessingHandler() {
75 return mProcessingHandler;
78 public Handler getResultHandler() {
79 return mResultHandler;
82 public int getReservedType() {
83 return mCurrentType++;
86 public Context getContext() {
90 public void add(ProcessingTask task) {
92 mTasks.put(task.getType(), task);
96 mHandlerThread.quit();