Home | History | Annotate | Download | only in data
      1 package com.android.gallery3d.filtershow.data;
      2 
      3 import android.os.Handler;
      4 import android.os.HandlerThread;
      5 import android.os.Message;
      6 import com.android.gallery3d.R;
      7 import com.android.gallery3d.filtershow.FilterShowActivity;
      8 import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
      9 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
     10 
     11 import java.util.ArrayList;
     12 
     13 public class UserPresetsManager implements Handler.Callback {
     14 
     15     private static final String LOGTAG = "UserPresetsManager";
     16 
     17     private FilterShowActivity mActivity;
     18     private HandlerThread mHandlerThread = null;
     19     private Handler mProcessingHandler = null;
     20     private FilterStackSource mUserPresets;
     21 
     22     private static final int LOAD = 1;
     23     private static final int LOAD_RESULT = 2;
     24     private static final int SAVE = 3;
     25     private static final int DELETE = 4;
     26     private static final int UPDATE = 5;
     27 
     28     private ArrayList<FilterUserPresetRepresentation> mRepresentations;
     29 
     30     private final Handler mResultHandler = new Handler() {
     31         @Override
     32         public void handleMessage(Message msg) {
     33             switch (msg.what) {
     34                 case LOAD_RESULT:
     35                     resultLoad(msg);
     36                     break;
     37             }
     38         }
     39     };
     40 
     41     @Override
     42     public boolean handleMessage(Message msg) {
     43         switch (msg.what) {
     44             case LOAD:
     45                 processLoad();
     46                 return true;
     47             case SAVE:
     48                 processSave(msg);
     49                 return true;
     50             case DELETE:
     51                 processDelete(msg);
     52                 return true;
     53             case UPDATE:
     54                 processUpdate(msg);
     55                 return true;
     56         }
     57         return false;
     58     }
     59 
     60     public UserPresetsManager(FilterShowActivity context) {
     61         mActivity = context;
     62         mHandlerThread = new HandlerThread(LOGTAG,
     63                 android.os.Process.THREAD_PRIORITY_BACKGROUND);
     64         mHandlerThread.start();
     65         mProcessingHandler = new Handler(mHandlerThread.getLooper(), this);
     66         mUserPresets = new FilterStackSource(mActivity);
     67         mUserPresets.open();
     68     }
     69 
     70     public ArrayList<FilterUserPresetRepresentation> getRepresentations() {
     71         return mRepresentations;
     72     }
     73 
     74     public void load() {
     75         Message msg = mProcessingHandler.obtainMessage(LOAD);
     76         mProcessingHandler.sendMessage(msg);
     77     }
     78 
     79     public void close() {
     80         mUserPresets.close();
     81         mHandlerThread.quit();
     82     }
     83 
     84     static class SaveOperation {
     85         String json;
     86         String name;
     87     }
     88 
     89     public void save(ImagePreset preset, String name) {
     90         Message msg = mProcessingHandler.obtainMessage(SAVE);
     91         SaveOperation op = new SaveOperation();
     92         op.json = preset.getJsonString(ImagePreset.JASON_SAVED);
     93         op.name = name;
     94         msg.obj = op;
     95         mProcessingHandler.sendMessage(msg);
     96     }
     97 
     98     public void delete(int id) {
     99         Message msg = mProcessingHandler.obtainMessage(DELETE);
    100         msg.arg1 = id;
    101         mProcessingHandler.sendMessage(msg);
    102     }
    103 
    104     static class UpdateOperation {
    105         int id;
    106         String name;
    107     }
    108 
    109     public void update(FilterUserPresetRepresentation representation) {
    110         Message msg = mProcessingHandler.obtainMessage(UPDATE);
    111         UpdateOperation op = new UpdateOperation();
    112         op.id = representation.getId();
    113         op.name = representation.getName();
    114         msg.obj = op;
    115         mProcessingHandler.sendMessage(msg);
    116     }
    117 
    118     private void processLoad() {
    119         ArrayList<FilterUserPresetRepresentation> list = mUserPresets.getAllUserPresets();
    120         Message msg = mResultHandler.obtainMessage(LOAD_RESULT);
    121         msg.obj = list;
    122         mResultHandler.sendMessage(msg);
    123     }
    124 
    125     private void resultLoad(Message msg) {
    126         mRepresentations =
    127                 (ArrayList<FilterUserPresetRepresentation>) msg.obj;
    128         mActivity.updateUserPresetsFromManager();
    129     }
    130 
    131     private void processSave(Message msg) {
    132         SaveOperation op = (SaveOperation) msg.obj;
    133         mUserPresets.insertStack(op.name, op.json.getBytes());
    134         processLoad();
    135     }
    136 
    137     private void processDelete(Message msg) {
    138         int id = msg.arg1;
    139         mUserPresets.removeStack(id);
    140         processLoad();
    141     }
    142 
    143     private void processUpdate(Message msg) {
    144         UpdateOperation op = (UpdateOperation) msg.obj;
    145         mUserPresets.updateStackName(op.id, op.name);
    146         processLoad();
    147     }
    148 
    149 }
    150