Home | History | Annotate | Download | only in pipeline
      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.Message;
     22 
     23 public abstract class ProcessingTask {
     24     private ProcessingTaskController mTaskController;
     25     private Handler mProcessingHandler;
     26     private Handler mResultHandler;
     27     private int mType;
     28     private static final int DELAY = 300;
     29 
     30     static interface Request {}
     31     static interface Update {}
     32     static interface Result {}
     33 
     34     public boolean postRequest(Request message) {
     35         Message msg = mProcessingHandler.obtainMessage(mType);
     36         msg.obj = message;
     37         if (isPriorityTask()) {
     38             if (mProcessingHandler.hasMessages(getType())) {
     39                 return false;
     40             }
     41             mProcessingHandler.sendMessageAtFrontOfQueue(msg);
     42         } else if (isDelayedTask()) {
     43             if (mProcessingHandler.hasMessages(getType())) {
     44                 mProcessingHandler.removeMessages(getType());
     45             }
     46             mProcessingHandler.sendMessageDelayed(msg, DELAY);
     47         } else {
     48             mProcessingHandler.sendMessage(msg);
     49         }
     50         return true;
     51     }
     52 
     53     public void postUpdate(Update message) {
     54         Message msg = mResultHandler.obtainMessage(mType);
     55         msg.obj = message;
     56         msg.arg1 = ProcessingTaskController.UPDATE;
     57         mResultHandler.sendMessage(msg);
     58     }
     59 
     60     public void processRequest(Request message) {
     61         Object result = doInBackground(message);
     62         Message msg = mResultHandler.obtainMessage(mType);
     63         msg.obj = result;
     64         msg.arg1 = ProcessingTaskController.RESULT;
     65         mResultHandler.sendMessage(msg);
     66     }
     67 
     68     public void added(ProcessingTaskController taskController) {
     69         mTaskController = taskController;
     70         mResultHandler = taskController.getResultHandler();
     71         mProcessingHandler = taskController.getProcessingHandler();
     72         mType = taskController.getReservedType();
     73     }
     74 
     75     public int getType() {
     76         return mType;
     77     }
     78 
     79     public Context getContext() {
     80         return mTaskController.getContext();
     81     }
     82 
     83     public abstract Result doInBackground(Request message);
     84     public abstract void onResult(Result message);
     85     public void onUpdate(Update message) {}
     86     public boolean isPriorityTask() { return false; }
     87     public boolean isDelayedTask() { return false; }
     88 }
     89