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 */
16package com.android.gallery3d.filtershow.data;
17
18import android.content.ContentValues;
19import android.content.Context;
20import android.database.Cursor;
21import android.database.sqlite.SQLiteDatabase;
22import android.database.sqlite.SQLiteException;
23import android.util.Log;
24import android.util.Pair;
25
26import com.android.gallery3d.filtershow.data.FilterStackDBHelper.FilterStack;
27import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
28import com.android.gallery3d.filtershow.pipeline.ImagePreset;
29
30import java.util.ArrayList;
31import java.util.List;
32
33public class FilterStackSource {
34    private static final String LOGTAG = "FilterStackSource";
35
36    private SQLiteDatabase database = null;
37    private final FilterStackDBHelper dbHelper;
38
39    public FilterStackSource(Context context) {
40        dbHelper = new FilterStackDBHelper(context);
41    }
42
43    public void open() {
44        try {
45            database = dbHelper.getWritableDatabase();
46        } catch (SQLiteException e) {
47            Log.w(LOGTAG, "could not open database", e);
48        }
49    }
50
51    public void close() {
52        database = null;
53        dbHelper.close();
54    }
55
56    public boolean insertStack(String stackName, byte[] stackBlob) {
57        boolean ret = true;
58        ContentValues val = new ContentValues();
59        val.put(FilterStack.STACK_ID, stackName);
60        val.put(FilterStack.FILTER_STACK, stackBlob);
61        database.beginTransaction();
62        try {
63            ret = (-1 != database.insert(FilterStack.TABLE, null, val));
64            database.setTransactionSuccessful();
65        } finally {
66            database.endTransaction();
67        }
68        return ret;
69    }
70
71    public void updateStackName(int id, String stackName) {
72        ContentValues val = new ContentValues();
73        val.put(FilterStack.STACK_ID, stackName);
74        database.beginTransaction();
75        try {
76            database.update(FilterStack.TABLE, val, FilterStack._ID + " = ?",
77                    new String[] { "" + id});
78            database.setTransactionSuccessful();
79        } finally {
80            database.endTransaction();
81        }
82    }
83
84    public boolean removeStack(int id) {
85        boolean ret = true;
86        database.beginTransaction();
87        try {
88            ret = (0 != database.delete(FilterStack.TABLE, FilterStack._ID + " = ?",
89                    new String[] { "" + id }));
90            database.setTransactionSuccessful();
91        } finally {
92            database.endTransaction();
93        }
94        return ret;
95    }
96
97    public void removeAllStacks() {
98        database.beginTransaction();
99        try {
100            database.delete(FilterStack.TABLE, null, null);
101            database.setTransactionSuccessful();
102        } finally {
103            database.endTransaction();
104        }
105    }
106
107    public byte[] getStack(String stackName) {
108        byte[] ret = null;
109        Cursor c = null;
110        database.beginTransaction();
111        try {
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)) {
117                ret = c.getBlob(0);
118            }
119            database.setTransactionSuccessful();
120        } finally {
121            if (c != null) {
122                c.close();
123            }
124            database.endTransaction();
125        }
126        return ret;
127    }
128
129    public ArrayList<FilterUserPresetRepresentation> getAllUserPresets() {
130        ArrayList<FilterUserPresetRepresentation> ret =
131                new ArrayList<FilterUserPresetRepresentation>();
132
133        Cursor c = null;
134        database.beginTransaction();
135        try {
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);
141            if (c != null) {
142                boolean loopCheck = c.moveToFirst();
143                while (loopCheck) {
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);
148
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();
155                }
156            }
157            database.setTransactionSuccessful();
158        } finally {
159            if (c != null) {
160                c.close();
161            }
162            database.endTransaction();
163        }
164
165        return ret;
166    }
167
168    public List<Pair<String, byte[]>> getAllStacks() {
169        List<Pair<String, byte[]>> ret = new ArrayList<Pair<String, byte[]>>();
170        Cursor c = null;
171        database.beginTransaction();
172        try {
173            c = database.query(FilterStack.TABLE,
174                    new String[] { FilterStack.STACK_ID, FilterStack.FILTER_STACK },
175                    null, null, null, null, null, null);
176            if (c != null) {
177                boolean loopCheck = c.moveToFirst();
178                while (loopCheck) {
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();
183                }
184            }
185            database.setTransactionSuccessful();
186        } finally {
187            if (c != null) {
188                c.close();
189            }
190            database.endTransaction();
191        }
192        if (ret.size() <= 0) {
193            return null;
194        }
195        return ret;
196    }
197}
198