summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2012-10-16 18:17:47 +0400
committerAndrew Dolgov <[email protected]>2012-10-16 18:17:47 +0400
commit28b20b97b5ee3a7c0a39bad3753c030436d6d26a (patch)
tree02f5a44b56d3deefa90bee34a21087d69353709a /src
parent6a678360e603d85314b5241c43424b174dbee6af (diff)
store cache in SQLite
Diffstat (limited to 'src')
-rw-r--r--src/org/fox/ttcomics/ComicListFragment.java12
-rw-r--r--src/org/fox/ttcomics/CommonActivity.java174
-rw-r--r--src/org/fox/ttcomics/DatabaseHelper.java38
3 files changed, 187 insertions, 37 deletions
diff --git a/src/org/fox/ttcomics/ComicListFragment.java b/src/org/fox/ttcomics/ComicListFragment.java
index d307354..80e242f 100644
--- a/src/org/fox/ttcomics/ComicListFragment.java
+++ b/src/org/fox/ttcomics/ComicListFragment.java
@@ -256,11 +256,12 @@ public class ComicListFragment extends Fragment implements OnItemClickListener {
if (archive.isDirectory() && m_mode == 0) {
m_files.add(filePath);
- } else if (archive.getName().toLowerCase().matches(".*\\.(cbz|zip)") && isAdded() && m_activity != null) {
+ } else if (archive.getName().toLowerCase().matches(".*\\.(cbz|zip)") && isAdded() && m_activity != null &&
+ m_activity.getWritableDb() != null && m_activity.getWritableDb().isOpen()) {
try {
int size = m_activity.getSize(filePath);
- if (m_activity.isStorageWritable() && (size == -1 || fullRescan)) {
+ if (size == -1 || fullRescan) {
CbzComicArchive cba = new CbzComicArchive(filePath);
@@ -268,11 +269,11 @@ public class ComicListFragment extends Fragment implements OnItemClickListener {
// Get cover
try {
- InputStream is = cba.getItem(0);
-
File thumbnailFile = new File(m_activity.getCacheFileName(filePath));
- if (!thumbnailFile.exists() || fullRescan) {
+ if (m_activity.isStorageWritable() && (!thumbnailFile.exists() || fullRescan)) {
+ InputStream is = cba.getItem(0);
+
FileOutputStream fos = new FileOutputStream(thumbnailFile);
byte[] buffer = new byte[1024];
@@ -284,6 +285,7 @@ public class ComicListFragment extends Fragment implements OnItemClickListener {
fos.close();
is.close();
}
+
} catch (IOException e) {
e.printStackTrace();
}
diff --git a/src/org/fox/ttcomics/CommonActivity.java b/src/org/fox/ttcomics/CommonActivity.java
index d7b420f..7590f9e 100644
--- a/src/org/fox/ttcomics/CommonActivity.java
+++ b/src/org/fox/ttcomics/CommonActivity.java
@@ -7,9 +7,11 @@ import java.util.Date;
import android.accounts.Account;
import android.accounts.AccountManager;
-import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteStatement;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
@@ -19,7 +21,6 @@ import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Display;
import android.view.MenuItem;
-import android.widget.ShareActionProvider;
import android.widget.Toast;
public class CommonActivity extends FragmentActivity {
@@ -39,11 +40,16 @@ public class CommonActivity extends FragmentActivity {
private boolean m_storageAvailable;
private boolean m_storageWritable;
+ private SQLiteDatabase m_readableDb;
+ private SQLiteDatabase m_writableDb;
+
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+
+ initDatabase();
}
@Override
@@ -100,53 +106,133 @@ public class CommonActivity extends FragmentActivity {
//
}
- public void setSize(String fileName, int size) {
- SharedPreferences lastread = getSharedPreferences("lastread", 0);
+ public Cursor findComicByFileName(String fileName) {
+ Cursor c = getReadableDb().query("comics_cache", null,
+ "filename = ?",
+ new String[] { fileName }, null, null, null);
- SharedPreferences.Editor editor = lastread.edit();
- editor.putInt(fileName + ":size", size);
-
- editor.commit();
+ if (c.moveToFirst()) {
+ return c;
+ } else {
+ c.close();
+
+ SQLiteStatement stmt = getWritableDb().compileStatement("INSERT INTO comics_cache " +
+ "(filename, position, max_position, size) VALUES (?, 0, 0, -1)");
+ stmt.bindString(1, fileName);
+ stmt.execute();
+
+ c = getReadableDb().query("comics_cache", null,
+ "filename = ?",
+ new String[] { fileName }, null, null, null);
+
+ if (c.moveToFirst()) {
+ return c;
+ } else {
+ c.close();
+ }
+ }
+
+ return null;
+ }
+
+ public void setSize(String fileName, int size) {
+ //Log.d(TAG, "setSize:" + fileName + "=" + size);
+
+ Cursor c = findComicByFileName(fileName);
+
+ if (c != null) {
+ c.close();
+ SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE comics_cache SET size = ? WHERE filename = ?");
+ stmt.bindLong(1, size);
+ stmt.bindString(2, fileName);
+ stmt.execute();
+ stmt.close();
+ }
}
public void setLastPosition(String fileName, int position) {
- SharedPreferences lastread = getSharedPreferences("lastread", 0);
-
- int lastPosition = getLastPosition(fileName);
-
- SharedPreferences.Editor editor = lastread.edit();
- editor.putInt(fileName + ":last", position);
- editor.putInt(fileName + ":max", Math.max(lastPosition, position));
-
- editor.commit();
+ int lastPosition = getLastPosition(fileName);
+
+ Cursor c = findComicByFileName(fileName);
+
+ if (c != null) {
+ c.close();
+
+ SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE comics_cache SET position = ?, max_position = ? WHERE filename = ?");
+ stmt.bindLong(1, position);
+ stmt.bindLong(2, Math.max(position, lastPosition));
+ stmt.bindString(3, fileName);
+ stmt.execute();
+ stmt.close();
+ }
+
}
public void setLastMaxPosition(String fileName, int position) {
- SharedPreferences lastread = getSharedPreferences("lastread", 0);
-
- SharedPreferences.Editor editor = lastread.edit();
- editor.putInt(fileName + ":max", position);
-
- editor.commit();
+
+ Cursor c = findComicByFileName(fileName);
+
+ if (c != null) {
+ c.close();
+
+ SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE comics_cache SET max_position = ? WHERE filename = ?");
+ stmt.bindLong(1, position);
+ stmt.bindString(2, fileName);
+ stmt.execute();
+ stmt.close();
+ }
}
public int getLastPosition(String fileName) {
- SharedPreferences lastread = getSharedPreferences("lastread", 0);
-
- return lastread.getInt(fileName + ":last", 0);
+ int position = 0;
+
+ Cursor c = getReadableDb().query("comics_cache", new String[] { "position" },
+ "filename = ?",
+ new String[] { fileName }, null, null, null);
+
+ if (c.moveToFirst()) {
+ position = c.getInt(c.getColumnIndex("position"));
+ }
+
+ c.close();
+
+ return position;
+
}
public int getMaxPosition(String fileName) {
- SharedPreferences lastread = getSharedPreferences("lastread", 0);
-
- return lastread.getInt(fileName + ":max", 0);
+ int position = 0;
+
+ Cursor c = getReadableDb().query("comics_cache", new String[] { "max_position" },
+ "filename = ?",
+ new String[] { fileName }, null, null, null);
+
+ if (c.moveToFirst()) {
+ position = c.getInt(c.getColumnIndex("max_position"));
+ }
+
+ c.close();
+
+ return position;
}
public int getSize(String fileName) {
- SharedPreferences lastread = getSharedPreferences("lastread", 0);
-
- return lastread.getInt(fileName + ":size", -1);
+ int size = -1;
+
+ Cursor c = getReadableDb().query("comics_cache", new String[] { "size" },
+ "filename = ?",
+ new String[] { fileName }, null, null, null);
+
+ if (c.moveToFirst()) {
+ size = c.getInt(c.getColumnIndex("size"));
+ }
+
+ c.close();
+
+ Log.d(TAG, "getSize:" + fileName + "=" + size);
+
+ return size;
}
public void onComicSelected(String fileName, int position) {
@@ -293,4 +379,28 @@ public class CommonActivity extends FragmentActivity {
public boolean isStorageWritable() {
return m_storageWritable;
}
+
+ private void initDatabase() {
+ DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
+
+ m_writableDb = dh.getWritableDatabase();
+ m_readableDb = dh.getReadableDatabase();
+ }
+
+ public synchronized SQLiteDatabase getReadableDb() {
+ return m_readableDb;
+ }
+
+ public synchronized SQLiteDatabase getWritableDb() {
+ return m_writableDb;
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+
+ m_readableDb.close();
+ m_writableDb.close();
+ }
+
}
diff --git a/src/org/fox/ttcomics/DatabaseHelper.java b/src/org/fox/ttcomics/DatabaseHelper.java
new file mode 100644
index 0000000..6108e55
--- /dev/null
+++ b/src/org/fox/ttcomics/DatabaseHelper.java
@@ -0,0 +1,38 @@
+package org.fox.ttcomics;
+
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.provider.BaseColumns;
+
+public class DatabaseHelper extends SQLiteOpenHelper {
+
+ private final String TAG = this.getClass().getSimpleName();
+
+ public static final String DATABASE_NAME = "ComicsCache.db";
+ public static final int DATABASE_VERSION = 1;
+
+ public DatabaseHelper(Context context) {
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
+ }
+
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ db.execSQL("DROP TABLE IF EXISTS comics_cache;");
+
+ db.execSQL("CREATE TABLE IF NOT EXISTS comics_cache (" +
+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
+ "filename TEXT, " +
+ "size INTEGER, " +
+ "position INTEGER, " +
+ "max_position INTEGER" +
+ ");");
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ //
+ }
+
+}