diff options
Diffstat (limited to 'src/com/android/gallery3d/filtershow/data')
3 files changed, 447 insertions, 0 deletions
diff --git a/src/com/android/gallery3d/filtershow/data/FilterStackDBHelper.java b/src/com/android/gallery3d/filtershow/data/FilterStackDBHelper.java new file mode 100644 index 000000000..e18d3104f --- /dev/null +++ b/src/com/android/gallery3d/filtershow/data/FilterStackDBHelper.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.filtershow.data; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +public class FilterStackDBHelper extends SQLiteOpenHelper { + + public static final int DATABASE_VERSION = 1; + public static final String DATABASE_NAME = "filterstacks.db"; + private static final String SQL_CREATE_TABLE = "CREATE TABLE "; + + public static interface FilterStack { + /** The row uid */ + public static final String _ID = "_id"; + /** The table name */ + public static final String TABLE = "filterstack"; + /** The stack name */ + public static final String STACK_ID = "stack_id"; + /** A serialized stack of filters. */ + public static final String FILTER_STACK= "stack"; + } + + private static final String[][] CREATE_FILTER_STACK = { + { FilterStack._ID, "INTEGER PRIMARY KEY AUTOINCREMENT" }, + { FilterStack.STACK_ID, "TEXT" }, + { FilterStack.FILTER_STACK, "BLOB" }, + }; + + public FilterStackDBHelper(Context context, String name, int version) { + super(context, name, null, version); + } + + public FilterStackDBHelper(Context context, String name) { + this(context, name, DATABASE_VERSION); + } + + public FilterStackDBHelper(Context context) { + this(context, DATABASE_NAME); + } + + @Override + public void onCreate(SQLiteDatabase db) { + createTable(db, FilterStack.TABLE, CREATE_FILTER_STACK); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + dropTable(db, FilterStack.TABLE); + onCreate(db); + } + + protected static void createTable(SQLiteDatabase db, String table, String[][] columns) { + StringBuilder create = new StringBuilder(SQL_CREATE_TABLE); + create.append(table).append('('); + boolean first = true; + for (String[] column : columns) { + if (!first) { + create.append(','); + } + first = false; + for (String val : column) { + create.append(val).append(' '); + } + } + create.append(')'); + db.beginTransaction(); + try { + db.execSQL(create.toString()); + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + } + + protected static void dropTable(SQLiteDatabase db, String table) { + db.beginTransaction(); + try { + db.execSQL("drop table if exists " + table); + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + } +} diff --git a/src/com/android/gallery3d/filtershow/data/FilterStackSource.java b/src/com/android/gallery3d/filtershow/data/FilterStackSource.java new file mode 100644 index 000000000..d283771b4 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/data/FilterStackSource.java @@ -0,0 +1,197 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.gallery3d.filtershow.data; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteException; +import android.util.Log; +import android.util.Pair; + +import com.android.gallery3d.filtershow.data.FilterStackDBHelper.FilterStack; +import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; +import com.android.gallery3d.filtershow.pipeline.ImagePreset; + +import java.util.ArrayList; +import java.util.List; + +public class FilterStackSource { + private static final String LOGTAG = "FilterStackSource"; + + private SQLiteDatabase database = null; + private final FilterStackDBHelper dbHelper; + + public FilterStackSource(Context context) { + dbHelper = new FilterStackDBHelper(context); + } + + public void open() { + try { + database = dbHelper.getWritableDatabase(); + } catch (SQLiteException e) { + Log.w(LOGTAG, "could not open database", e); + } + } + + public void close() { + database = null; + dbHelper.close(); + } + + public boolean insertStack(String stackName, byte[] stackBlob) { + boolean ret = true; + ContentValues val = new ContentValues(); + val.put(FilterStack.STACK_ID, stackName); + val.put(FilterStack.FILTER_STACK, stackBlob); + database.beginTransaction(); + try { + ret = (-1 != database.insert(FilterStack.TABLE, null, val)); + database.setTransactionSuccessful(); + } finally { + database.endTransaction(); + } + return ret; + } + + public void updateStackName(int id, String stackName) { + ContentValues val = new ContentValues(); + val.put(FilterStack.STACK_ID, stackName); + database.beginTransaction(); + try { + database.update(FilterStack.TABLE, val, FilterStack._ID + " = ?", + new String[] { "" + id}); + database.setTransactionSuccessful(); + } finally { + database.endTransaction(); + } + } + + public boolean removeStack(int id) { + boolean ret = true; + database.beginTransaction(); + try { + ret = (0 != database.delete(FilterStack.TABLE, FilterStack._ID + " = ?", + new String[] { "" + id })); + database.setTransactionSuccessful(); + } finally { + database.endTransaction(); + } + return ret; + } + + public void removeAllStacks() { + database.beginTransaction(); + try { + database.delete(FilterStack.TABLE, null, null); + database.setTransactionSuccessful(); + } finally { + database.endTransaction(); + } + } + + public byte[] getStack(String stackName) { + byte[] ret = null; + Cursor c = null; + database.beginTransaction(); + try { + c = database.query(FilterStack.TABLE, + new String[] { FilterStack.FILTER_STACK }, + FilterStack.STACK_ID + " = ?", + new String[] { stackName }, null, null, null, null); + if (c != null && c.moveToFirst() && !c.isNull(0)) { + ret = c.getBlob(0); + } + database.setTransactionSuccessful(); + } finally { + if (c != null) { + c.close(); + } + database.endTransaction(); + } + return ret; + } + + public ArrayList<FilterUserPresetRepresentation> getAllUserPresets() { + ArrayList<FilterUserPresetRepresentation> ret = + new ArrayList<FilterUserPresetRepresentation>(); + + Cursor c = null; + database.beginTransaction(); + try { + c = database.query(FilterStack.TABLE, + new String[] { FilterStack._ID, + FilterStack.STACK_ID, + FilterStack.FILTER_STACK }, + null, null, null, null, null, null); + if (c != null) { + boolean loopCheck = c.moveToFirst(); + while (loopCheck) { + int id = c.getInt(0); + String name = (c.isNull(1)) ? null : c.getString(1); + byte[] b = (c.isNull(2)) ? null : c.getBlob(2); + String json = new String(b); + + ImagePreset preset = new ImagePreset(); + preset.readJsonFromString(json); + FilterUserPresetRepresentation representation = + new FilterUserPresetRepresentation(name, preset, id); + ret.add(representation); + loopCheck = c.moveToNext(); + } + } + database.setTransactionSuccessful(); + } finally { + if (c != null) { + c.close(); + } + database.endTransaction(); + } + + return ret; + } + + public List<Pair<String, byte[]>> getAllStacks() { + List<Pair<String, byte[]>> ret = new ArrayList<Pair<String, byte[]>>(); + Cursor c = null; + database.beginTransaction(); + try { + c = database.query(FilterStack.TABLE, + new String[] { FilterStack.STACK_ID, FilterStack.FILTER_STACK }, + null, null, null, null, null, null); + if (c != null) { + boolean loopCheck = c.moveToFirst(); + while (loopCheck) { + String name = (c.isNull(0)) ? null : c.getString(0); + byte[] b = (c.isNull(1)) ? null : c.getBlob(1); + ret.add(new Pair<String, byte[]>(name, b)); + loopCheck = c.moveToNext(); + } + } + database.setTransactionSuccessful(); + } finally { + if (c != null) { + c.close(); + } + database.endTransaction(); + } + if (ret.size() <= 0) { + return null; + } + return ret; + } +} diff --git a/src/com/android/gallery3d/filtershow/data/UserPresetsManager.java b/src/com/android/gallery3d/filtershow/data/UserPresetsManager.java new file mode 100644 index 000000000..114cd3ebc --- /dev/null +++ b/src/com/android/gallery3d/filtershow/data/UserPresetsManager.java @@ -0,0 +1,149 @@ +package com.android.gallery3d.filtershow.data; + +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Message; +import com.android.gallery3d.R; +import com.android.gallery3d.filtershow.FilterShowActivity; +import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; +import com.android.gallery3d.filtershow.pipeline.ImagePreset; + +import java.util.ArrayList; + +public class UserPresetsManager implements Handler.Callback { + + private static final String LOGTAG = "UserPresetsManager"; + + private FilterShowActivity mActivity; + private HandlerThread mHandlerThread = null; + private Handler mProcessingHandler = null; + private FilterStackSource mUserPresets; + + private static final int LOAD = 1; + private static final int LOAD_RESULT = 2; + private static final int SAVE = 3; + private static final int DELETE = 4; + private static final int UPDATE = 5; + + private ArrayList<FilterUserPresetRepresentation> mRepresentations; + + private final Handler mResultHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case LOAD_RESULT: + resultLoad(msg); + break; + } + } + }; + + @Override + public boolean handleMessage(Message msg) { + switch (msg.what) { + case LOAD: + processLoad(); + return true; + case SAVE: + processSave(msg); + return true; + case DELETE: + processDelete(msg); + return true; + case UPDATE: + processUpdate(msg); + return true; + } + return false; + } + + public UserPresetsManager(FilterShowActivity context) { + mActivity = context; + mHandlerThread = new HandlerThread(LOGTAG, + android.os.Process.THREAD_PRIORITY_BACKGROUND); + mHandlerThread.start(); + mProcessingHandler = new Handler(mHandlerThread.getLooper(), this); + mUserPresets = new FilterStackSource(mActivity); + mUserPresets.open(); + } + + public ArrayList<FilterUserPresetRepresentation> getRepresentations() { + return mRepresentations; + } + + public void load() { + Message msg = mProcessingHandler.obtainMessage(LOAD); + mProcessingHandler.sendMessage(msg); + } + + public void close() { + mUserPresets.close(); + mHandlerThread.quit(); + } + + static class SaveOperation { + String json; + String name; + } + + public void save(ImagePreset preset) { + Message msg = mProcessingHandler.obtainMessage(SAVE); + SaveOperation op = new SaveOperation(); + op.json = preset.getJsonString(mActivity.getString(R.string.saved)); + op.name= mActivity.getString(R.string.filtershow_new_preset); + msg.obj = op; + mProcessingHandler.sendMessage(msg); + } + + public void delete(int id) { + Message msg = mProcessingHandler.obtainMessage(DELETE); + msg.arg1 = id; + mProcessingHandler.sendMessage(msg); + } + + static class UpdateOperation { + int id; + String name; + } + + public void update(FilterUserPresetRepresentation representation) { + Message msg = mProcessingHandler.obtainMessage(UPDATE); + UpdateOperation op = new UpdateOperation(); + op.id = representation.getId(); + op.name = representation.getName(); + msg.obj = op; + mProcessingHandler.sendMessage(msg); + } + + private void processLoad() { + ArrayList<FilterUserPresetRepresentation> list = mUserPresets.getAllUserPresets(); + Message msg = mResultHandler.obtainMessage(LOAD_RESULT); + msg.obj = list; + mResultHandler.sendMessage(msg); + } + + private void resultLoad(Message msg) { + mRepresentations = + (ArrayList<FilterUserPresetRepresentation>) msg.obj; + mActivity.updateUserPresetsFromManager(); + } + + private void processSave(Message msg) { + SaveOperation op = (SaveOperation) msg.obj; + mUserPresets.insertStack(op.name, op.json.getBytes()); + processLoad(); + } + + private void processDelete(Message msg) { + int id = msg.arg1; + mUserPresets.removeStack(id); + processLoad(); + } + + private void processUpdate(Message msg) { + UpdateOperation op = (UpdateOperation) msg.obj; + mUserPresets.updateStackName(op.id, op.name); + processLoad(); + } + +} |