/**
*
* Phonegap backup plugin for Android
* Luis Balam 2012
* @version 0.0.5
*
*/
package org.opencorebanking.plugins;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
//import android.os.*;
//import android.net.*;
import java.io.*;
//import java.util.*;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
public class LocalBackup extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
try {
JSONObject jo = args.getJSONObject(0);
String pkg = jo.getString("packageName");
String dest = jo.getString("output");
try {
goBackup(pkg, dest);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new PluginResult(PluginResult.Status.OK);
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
private boolean goBackup(String strPkg, String strOut) throws FileNotFoundException{
String DBPATH = "/data/data/" + strPkg + "/app_database/file__0/";
String DBNAME = "0000000000000001.db";
String myPath = DBPATH + DBNAME;
Boolean result = false;
//Open your local db as the input stream
InputStream myInput = new FileInputStream(myPath);
// Path to the just created empty db
String outFileName = strOut;
//Open the empty db as the output stream
OutputStream myOut;
try {
myOut = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOut.write(buffer, 0, length);
}
//Close the streams
myOut.flush();
myOut.close();
myInput.close();
result = true;
} catch (FileNotFoundException e) {
// Auto-generated catch block
e.printStackTrace();
result = false;
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
result = false;
}
return result;
}
}