1/*
2 * Copyright (C) 2013 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 */
16
17package com.android.gallery3d.filtershow.pipeline;
18
19import android.content.Context;
20import android.os.Handler;
21import android.os.HandlerThread;
22import android.os.Message;
23import android.util.Log;
24
25import java.util.HashMap;
26
27public class ProcessingTaskController implements Handler.Callback {
28    private static final String LOGTAG = "ProcessingTaskController";
29
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>();
35
36    public final static int RESULT = 1;
37    public final static int UPDATE = 2;
38
39    private final Handler mResultHandler = new Handler() {
40        @Override
41        public void handleMessage(Message msg) {
42            ProcessingTask task = mTasks.get(msg.what);
43            if (task != null) {
44                if (msg.arg1 == RESULT) {
45                    task.onResult((ProcessingTask.Result) msg.obj);
46                } else if (msg.arg1 == UPDATE) {
47                    task.onUpdate((ProcessingTask.Update) msg.obj);
48                } else {
49                    Log.w(LOGTAG, "received unknown message! " + msg.arg1);
50                }
51            }
52        }
53    };
54
55    @Override
56    public boolean handleMessage(Message msg) {
57        ProcessingTask task = mTasks.get(msg.what);
58        if (task != null) {
59            task.processRequest((ProcessingTask.Request) msg.obj);
60            return true;
61        }
62        return false;
63    }
64
65    public ProcessingTaskController(Context context) {
66        mContext = context;
67        mHandlerThread = new HandlerThread("ProcessingTaskController",
68                android.os.Process.THREAD_PRIORITY_FOREGROUND);
69        mHandlerThread.start();
70        mProcessingHandler = new Handler(mHandlerThread.getLooper(), this);
71    }
72
73    public Handler getProcessingHandler() {
74        return mProcessingHandler;
75    }
76
77    public Handler getResultHandler() {
78        return mResultHandler;
79    }
80
81    public int getReservedType() {
82        return mCurrentType++;
83    }
84
85    public Context getContext() {
86        return mContext;
87    }
88
89    public void add(ProcessingTask task) {
90        task.added(this);
91        mTasks.put(task.getType(), task);
92    }
93
94    public void quit() {
95        mHandlerThread.quit();
96    }
97}
98