2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.gallery3d.filtershow.data;
18 import android.content.ContentValues;
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.database.sqlite.SQLiteException;
23 import android.util.Log;
24 import android.util.Pair;
26 import com.android.gallery3d.filtershow.data.FilterStackDBHelper.FilterStack;
27 import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
28 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
30 import java.util.ArrayList;
31 import java.util.List;
33 public class FilterStackSource {
34 private static final String LOGTAG = "FilterStackSource";
36 private SQLiteDatabase database = null;
37 private final FilterStackDBHelper dbHelper;
39 public FilterStackSource(Context context) {
40 dbHelper = new FilterStackDBHelper(context);
45 database = dbHelper.getWritableDatabase();
46 } catch (SQLiteException e) {
47 Log.w(LOGTAG, "could not open database", e);
56 public boolean insertStack(String stackName, byte[] stackBlob) {
58 ContentValues val = new ContentValues();
59 val.put(FilterStack.STACK_ID, stackName);
60 val.put(FilterStack.FILTER_STACK, stackBlob);
61 database.beginTransaction();
63 ret = (-1 != database.insert(FilterStack.TABLE, null, val));
64 database.setTransactionSuccessful();
66 database.endTransaction();
71 public void updateStackName(int id, String stackName) {
72 ContentValues val = new ContentValues();
73 val.put(FilterStack.STACK_ID, stackName);
74 database.beginTransaction();
76 database.update(FilterStack.TABLE, val, FilterStack._ID + " = ?",
77 new String[] { "" + id});
78 database.setTransactionSuccessful();
80 database.endTransaction();
84 public boolean removeStack(int id) {
86 database.beginTransaction();
88 ret = (0 != database.delete(FilterStack.TABLE, FilterStack._ID + " = ?",
89 new String[] { "" + id }));
90 database.setTransactionSuccessful();
92 database.endTransaction();
97 public void removeAllStacks() {
98 database.beginTransaction();
100 database.delete(FilterStack.TABLE, null, null);
101 database.setTransactionSuccessful();
103 database.endTransaction();
107 public byte[] getStack(String stackName) {
110 database.beginTransaction();
112 c = database.query(FilterStack.TABLE,
113 new String[] { FilterStack.FILTER_STACK },
114 FilterStack.STACK_ID + " = ?",
115 new String[] { stackName }, null, null, null, null);
116 if (c != null && c.moveToFirst() && !c.isNull(0)) {
119 database.setTransactionSuccessful();
124 database.endTransaction();
129 public ArrayList<FilterUserPresetRepresentation> getAllUserPresets() {
130 ArrayList<FilterUserPresetRepresentation> ret =
131 new ArrayList<FilterUserPresetRepresentation>();
134 database.beginTransaction();
136 c = database.query(FilterStack.TABLE,
137 new String[] { FilterStack._ID,
138 FilterStack.STACK_ID,
139 FilterStack.FILTER_STACK },
140 null, null, null, null, null, null);
142 boolean loopCheck = c.moveToFirst();
144 int id = c.getInt(0);
145 String name = (c.isNull(1)) ? null : c.getString(1);
146 byte[] b = (c.isNull(2)) ? null : c.getBlob(2);
147 String json = new String(b);
149 ImagePreset preset = new ImagePreset();
150 preset.readJsonFromString(json);
151 FilterUserPresetRepresentation representation =
152 new FilterUserPresetRepresentation(name, preset, id);
153 ret.add(representation);
154 loopCheck = c.moveToNext();
157 database.setTransactionSuccessful();
162 database.endTransaction();
168 public List<Pair<String, byte[]>> getAllStacks() {
169 List<Pair<String, byte[]>> ret = new ArrayList<Pair<String, byte[]>>();
171 database.beginTransaction();
173 c = database.query(FilterStack.TABLE,
174 new String[] { FilterStack.STACK_ID, FilterStack.FILTER_STACK },
175 null, null, null, null, null, null);
177 boolean loopCheck = c.moveToFirst();
179 String name = (c.isNull(0)) ? null : c.getString(0);
180 byte[] b = (c.isNull(1)) ? null : c.getBlob(1);
181 ret.add(new Pair<String, byte[]>(name, b));
182 loopCheck = c.moveToNext();
185 database.setTransactionSuccessful();
190 database.endTransaction();
192 if (ret.size() <= 0) {