OSDN Git Service

Add background processing service
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / pipeline / ProcessingTaskController.java
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
17 package com.android.gallery3d.filtershow.pipeline;
18
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;
24
25 import java.util.HashMap;
26
27 public 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
41         @Override
42         public void handleMessage(Message msg) {
43             ProcessingTask task = mTasks.get(msg.what);
44             if (task != null) {
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);
49                 } else {
50                     Log.w(LOGTAG, "received unknown message! " + msg.arg1);
51                 }
52             }
53         }
54     };
55
56     @Override
57     public boolean handleMessage(Message msg) {
58         ProcessingTask task = mTasks.get(msg.what);
59         if (task != null) {
60             task.processRequest((ProcessingTask.Request) msg.obj);
61             return true;
62         }
63         return false;
64     }
65
66     public ProcessingTaskController(Context context) {
67         mContext = context;
68         mHandlerThread = new HandlerThread("ProcessingTaskController",
69                 android.os.Process.THREAD_PRIORITY_FOREGROUND);
70         mHandlerThread.start();
71         mProcessingHandler = new Handler(mHandlerThread.getLooper(), this);
72     }
73
74     public Handler getProcessingHandler() {
75         return mProcessingHandler;
76     }
77
78     public Handler getResultHandler() {
79         return mResultHandler;
80     }
81
82     public int getReservedType() {
83         return mCurrentType++;
84     }
85
86     public Context getContext() {
87         return mContext;
88     }
89
90     public void add(ProcessingTask task) {
91         task.added(this);
92         mTasks.put(task.getType(), task);
93     }
94
95     public void quit() {
96         mHandlerThread.quit();
97     }
98 }