Skip to content

Commit aecd10b

Browse files
committed
Added option to delete locally stored product database and query the size of it
1 parent ed91991 commit aecd10b

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

core/src/main/java/io/snabble/sdk/ProductDatabase.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.os.Handler;
1313
import android.os.Looper;
1414
import android.os.SystemClock;
15+
import android.text.format.Formatter;
1516

1617
import org.apache.commons.io.FileUtils;
1718
import org.apache.commons.io.IOUtils;
@@ -196,8 +197,9 @@ private boolean open(boolean allowCopyFromBundle) {
196197
return false;
197198
}
198199
} else {
199-
Logger.d("Loaded product database revision %d, schema version %d.%d",
200-
revisionId, schemaVersionMajor, schemaVersionMinor);
200+
Logger.d("Loaded product database revision %d, schema version %d.%d, %s",
201+
revisionId, schemaVersionMajor, schemaVersionMinor,
202+
Formatter.formatFileSize(application, size()));
201203
}
202204

203205
parseLastUpdateTimestamp();
@@ -217,10 +219,12 @@ private void close() {
217219
}
218220

219221
private void putMetaData(String key, String value) {
220-
ContentValues contentValues = new ContentValues();
221-
contentValues.put("key", key);
222-
contentValues.put("value", value);
223-
db.replace("metadata", null, contentValues);
222+
if(db != null) {
223+
ContentValues contentValues = new ContentValues();
224+
contentValues.put("key", key);
225+
contentValues.put("value", value);
226+
db.replace("metadata", null, contentValues);
227+
}
224228
}
225229

226230
private String getMetaData(String key) {
@@ -478,6 +482,34 @@ protected void onError() {
478482
});
479483
}
480484

485+
/**
486+
* Closes and deletes the locally stored database and falls back to online only mode.
487+
*/
488+
public void delete() {
489+
if(db != null){
490+
close();
491+
application.deleteDatabase(dbName);
492+
db = null;
493+
revisionId = -1;
494+
schemaVersionMinor = -1;
495+
schemaVersionMajor = -1;
496+
497+
Logger.d("Deleted database: " + dbName);
498+
}
499+
}
500+
501+
/**
502+
* @return Size of the database in bytes.
503+
*/
504+
public long size() {
505+
if(db == null){
506+
return 0;
507+
}
508+
509+
File dbFile = application.getDatabasePath(dbName);
510+
return dbFile.length();
511+
}
512+
481513
/**
482514
* Updates the current database in the background by requesting new data from the backend.
483515
* <p>

sample/src/main/java/io/snabble/testapp/HomeFragment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public void onClick(View v) {
3636
App.get().getSnabbleSdk().getProductDatabase().update();
3737
}
3838
});
39+
40+
v.findViewById(R.id.delete_db).setOnClickListener(new View.OnClickListener() {
41+
@Override
42+
public void onClick(View v) {
43+
App.get().getSnabbleSdk().getProductDatabase().delete();
44+
}
45+
});
3946
return v;
4047
}
4148
}

sample/src/main/res/layout/fragment_home.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@
2424
android:padding="8dp"
2525
android:id="@+id/update_db"
2626
android:text="Update DB" />
27+
28+
<Button
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:padding="8dp"
32+
android:id="@+id/delete_db"
33+
android:text="Delete DB" />
2734
</LinearLayout>

0 commit comments

Comments
 (0)