Add SQLite DB service
parent
ddd8bb7af6
commit
af97d312bd
|
@ -24,11 +24,11 @@ public class MainActivity extends AppCompatActivity {
|
|||
setContentView(binding.getRoot());
|
||||
|
||||
mNativeX = new NativeX(this, binding.webview);
|
||||
initWebview();
|
||||
initializeWebView();
|
||||
}
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
protected void initWebview() {
|
||||
protected void initializeWebView() {
|
||||
binding.webview.setWebViewClient(new WebViewClient() {
|
||||
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
|
||||
view.loadUrl(request.getUrl().toString());
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package gallery.memories.service;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
public class DbService extends SQLiteOpenHelper {
|
||||
public DbService(Context context) {
|
||||
super(context, "memories", null, 11);
|
||||
}
|
||||
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
// Add table for images
|
||||
db.execSQL("CREATE TABLE images ("
|
||||
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
+ "local_id INTEGER,"
|
||||
+ "mtime INTEGER,"
|
||||
+ "date_taken INTEGER,"
|
||||
+ "dayid INTEGER,"
|
||||
+ "exif_uid TEXT,"
|
||||
+ "basename TEXT,"
|
||||
+ "flag INTEGER"
|
||||
+ ");");
|
||||
}
|
||||
|
||||
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
|
||||
database.execSQL("DROP TABLE IF EXISTS images");
|
||||
onCreate(database);
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package gallery.memories.service;
|
|||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.net.Uri;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
|
@ -14,11 +15,15 @@ import java.util.ArrayList;
|
|||
import java.util.TimeZone;
|
||||
|
||||
public class TimelineQuery {
|
||||
final static String TAG = "QueryService";
|
||||
final static String TAG = "TimelineQuery";
|
||||
Context mCtx;
|
||||
SQLiteDatabase mDb;
|
||||
|
||||
public TimelineQuery(Context context) {
|
||||
mCtx = context;
|
||||
mDb = new DbService(context).getWritableDatabase();
|
||||
|
||||
fullSyncDb();
|
||||
}
|
||||
|
||||
public JSONArray getByDayId(final long dayId) {
|
||||
|
@ -95,4 +100,61 @@ public class TimelineQuery {
|
|||
// Return JSON string of files
|
||||
return new JSONArray(files);
|
||||
}
|
||||
|
||||
protected void fullSyncDb() {
|
||||
Uri collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
||||
|
||||
// Flag all images for removal
|
||||
mDb.execSQL("UPDATE images SET flag = 1");
|
||||
|
||||
// Same fields as server response
|
||||
String[] projection = new String[] {
|
||||
MediaStore.Images.Media._ID,
|
||||
MediaStore.Images.Media.DISPLAY_NAME,
|
||||
MediaStore.Images.Media.DATE_TAKEN,
|
||||
MediaStore.Images.Media.DATE_MODIFIED,
|
||||
};
|
||||
|
||||
try (Cursor cursor = mCtx.getContentResolver().query(
|
||||
collection,
|
||||
projection,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
)) {
|
||||
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
|
||||
int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
|
||||
int dateColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN);
|
||||
int mtimeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_MODIFIED);
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
long id = cursor.getLong(idColumn);
|
||||
String name = cursor.getString(nameColumn);
|
||||
long dateTaken = cursor.getLong(dateColumn);
|
||||
long mtime = cursor.getLong(mtimeColumn);
|
||||
|
||||
// Check if file with local_id and mtime already exists
|
||||
try (Cursor c = mDb.rawQuery("SELECT id FROM images WHERE local_id = ?",
|
||||
new String[]{Long.toString(id)})) {
|
||||
if (c.getCount() > 0) {
|
||||
// File already exists, remove flag
|
||||
mDb.execSQL("UPDATE images SET flag = 0 WHERE local_id = ?", new Object[]{id});
|
||||
|
||||
Log.v(TAG, "File already exists: " + id + " / " + name);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Delete file with same local_id and insert new one
|
||||
mDb.beginTransaction();
|
||||
mDb.execSQL("DELETE FROM images WHERE local_id = ?", new Object[] { id });
|
||||
mDb.execSQL("INSERT OR IGNORE INTO images (local_id, mtime, basename, dayid) VALUES (?, ?, ?, ?)",
|
||||
new Object[] { id, mtime, name, (dateTaken / 86400000) });
|
||||
mDb.setTransactionSuccessful();
|
||||
mDb.endTransaction();
|
||||
|
||||
Log.v(TAG, "Inserted file to local DB: " + id + " / " + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue