Skip to content

Commit

Permalink
Merge pull request #10 from jrouault/fix/asyncDeleteCleanup
Browse files Browse the repository at this point in the history
Async deleteFiles and cleanup
  • Loading branch information
jrouault committed Jul 9, 2015
2 parents 051f0e1 + e4e60a0 commit 1f8d54c
Show file tree
Hide file tree
Showing 4 changed files with 447 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @copyright Wizcorp Inc. [ Incorporated Wizards ] 2014
* @file - wizAssetManager.java
* @about - JavaScript download and update asset example for PhoneGap
*/
*/
package jp.wizcorp.phonegap.plugin.WizAssets;

import java.io.File;
Expand All @@ -35,16 +35,18 @@ public class WizAssetManager {
private String TAG = "WizAssetManager";
private String DATABASE_EXTERNAL_FILE_PATH;
private String DATABASE_INTERNAL_FILE_PATH = "www/phonegap/plugin/wizAssets/";

// to manipulate the home of the db change this string
private String DATABASE_NAME = "assets.db";
private static final String DATABASE_NAME = "assets.db";
private static final String DATABASE_TABLE_NAME = "assets";
private SQLiteDatabase database;

boolean initialiseDatabase;
Context that;

public WizAssetManager(Context context) {
Log.d(TAG, "Booting Wizard Asset Manager.");

// context is application context
that = context;
DATABASE_EXTERNAL_FILE_PATH = that.getCacheDir().getAbsolutePath();
Expand All @@ -57,9 +59,9 @@ public WizAssetManager(Context context) {
Log.d(TAG, "DB already initiated.");
}
}

public JSONObject getAllAssets() {

// try to open database from external storage (we should have moved it there),
// if nothing in the external storage move the app version out to external
// if not existing internal, return empty object and we can stream the assets in
Expand All @@ -69,10 +71,10 @@ public JSONObject getAllAssets() {
try {
// select all and put to JSONObject
Cursor cursor;
cursor = database.rawQuery("select * from assets", null);
cursor = database.rawQuery("select * from " + DATABASE_TABLE_NAME, null);
String uri;
String filePath;

Log.d(TAG, "move cursor");
while (cursor.moveToNext()) {
uri = cursor.getString(cursor.getColumnIndex("uri"));
Expand All @@ -85,7 +87,7 @@ public JSONObject getAllAssets() {
Log.e(TAG, "JSON error -- " + e.getMessage(), e);
}
}

cursor.close();
Log.d(TAG, "returnObject -> " + returnObject.toString());

Expand All @@ -96,18 +98,18 @@ public JSONObject getAllAssets() {
}
return returnObject;
}

private void buildDB() {
// Init DB from bundle assets out to storage
Log.d(TAG, "Init DB");

// Open the .db file from assets directory
try {
InputStream is = that.getAssets().open(DATABASE_INTERNAL_FILE_PATH + DATABASE_NAME);

// Copy the database into the destination
OutputStream os = new FileOutputStream(DATABASE_EXTERNAL_FILE_PATH + File.separator + DATABASE_NAME);

byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0){
Expand All @@ -117,42 +119,40 @@ private void buildDB() {

os.close();
is.close();

database = SQLiteDatabase.openDatabase(DATABASE_EXTERNAL_FILE_PATH + File.separator + DATABASE_NAME, null, SQLiteDatabase.OPEN_READWRITE);
Log.d(TAG, "Init DB Finish");

} catch (IOException e1) {
// log, ignore and send back nothing
Log.e(TAG, "IO error -- " + e1.getMessage(), e1);
}

}

public void downloadedAsset(String relativePath, String absolutePath) {
// Downloaded file, add / edit database
Log.d(TAG, "Downloaded Asset - relativePath: " + relativePath + " - absolutePath: " + absolutePath);

try {
// Will replace if exists
String sqlInsert = "insert or replace into assets values(?,?);";
String sqlInsert = "insert or replace into " + DATABASE_TABLE_NAME + " values(?,?)";
database.execSQL(sqlInsert, new Object[] { relativePath, absolutePath });
} catch (Exception e) {
Log.e(TAG, "error -- " + e.getMessage(), e);
}
}

public String getFile(String relpath) {
// returns file path to asset from database
Cursor cursor = null;
try {
// Will replace if exists
String sqlsearch = "select filePath from assets where uri= ?;";
cursor = database.rawQuery(sqlsearch, new String[] { relpath });
String sqlSearch = "select filePath from " + DATABASE_TABLE_NAME + " where uri= ?";
cursor = database.rawQuery(sqlSearch, new String[] { relpath });
} catch (Exception e) {
Log.e(TAG, "error -- " + e.getMessage(), e);
return "";
}

String result;
try {
if (cursor.moveToFirst()) {
Expand All @@ -177,8 +177,22 @@ public String getFile(String relpath) {
public void deleteFile(String filePath) {
try {
// Delete file
String sqlsearch = "delete from assets where filePath= ?;";
database.rawQuery(sqlsearch, new String[] { filePath }).moveToFirst();;
database.delete(DATABASE_TABLE_NAME, "filePath= ?", new String[] { filePath });
} catch (Exception e) {
Log.e(TAG, "Delete file error -- " + e.getMessage(), e);
}
}

public void deleteFolder(String filePath) {
try {
String pathPattern;
if (filePath.charAt(filePath.length() - 1) != File.separatorChar) {
pathPattern = filePath + File.separatorChar + '%';
} else {
pathPattern = filePath.concat("%");
}
// Delete all entries starting with the file path
database.delete(DATABASE_TABLE_NAME, "filePath like ?", new String[] { pathPattern });
} catch (Exception e) {
Log.e(TAG, "Delete file error -- " + e.getMessage(), e);
}
Expand Down
Loading

0 comments on commit 1f8d54c

Please sign in to comment.