2014-04-10 16:08:44 +05:30
|
|
|
package me.rahul.plugins.sqlDB;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
2017-01-11 15:54:07 +05:30
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
2014-04-10 16:08:44 +05:30
|
|
|
|
|
|
|
|
import org.apache.cordova.CordovaPlugin;
|
|
|
|
|
import org.apache.cordova.CallbackContext;
|
|
|
|
|
import org.apache.cordova.PluginResult;
|
|
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONException;
|
2015-01-23 20:18:08 +05:30
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
2014-04-10 16:08:44 +05:30
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class echoes a string called from JavaScript.
|
|
|
|
|
*/
|
|
|
|
|
public class sqlDB extends CordovaPlugin {
|
|
|
|
|
|
|
|
|
|
public static String dbname = "dbname";
|
|
|
|
|
PluginResult plresult = new PluginResult(PluginResult.Status.NO_RESULT);
|
|
|
|
|
|
2017-01-11 15:54:07 +05:30
|
|
|
private void sendPluginResponse(int code, String msg, boolean error, CallbackContext callbackContext) {
|
|
|
|
|
JSONObject response = new JSONObject();
|
|
|
|
|
try {
|
|
|
|
|
response.put("message", msg);
|
|
|
|
|
response.put("code", code);
|
|
|
|
|
if(error) {
|
|
|
|
|
plresult = new PluginResult(PluginResult.Status.ERROR,
|
|
|
|
|
response);
|
|
|
|
|
} else {
|
|
|
|
|
plresult = new PluginResult(PluginResult.Status.OK,response);
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
plresult = new PluginResult(PluginResult.Status.ERROR,
|
|
|
|
|
e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
callbackContext.sendPluginResult(plresult);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-23 20:18:08 +05:30
|
|
|
@Override
|
|
|
|
|
public boolean execute(String action, JSONArray args,
|
|
|
|
|
CallbackContext callbackContext) throws JSONException {
|
|
|
|
|
|
|
|
|
|
if (action.equalsIgnoreCase("copy")) {
|
2017-01-11 15:54:07 +05:30
|
|
|
this.copyDB(args.getString(0), "www", callbackContext);
|
2015-01-23 20:18:08 +05:30
|
|
|
return true;
|
|
|
|
|
} else if (action.equalsIgnoreCase("remove")) {
|
|
|
|
|
String db = args.getString(0);
|
|
|
|
|
File path = cordova.getActivity().getDatabasePath(db);
|
|
|
|
|
Boolean fileExists = path.exists();
|
|
|
|
|
if (fileExists) {
|
|
|
|
|
boolean deleted = path.delete();
|
|
|
|
|
if (deleted) {
|
2017-01-11 15:54:07 +05:30
|
|
|
sendPluginResponse(200,"Database Deleted",false, callbackContext);
|
2015-01-23 20:18:08 +05:30
|
|
|
} else {
|
2017-01-11 15:54:07 +05:30
|
|
|
sendPluginResponse(400,"Unable to Delete",true, callbackContext);
|
2015-01-23 20:18:08 +05:30
|
|
|
}
|
|
|
|
|
} else {
|
2017-01-11 15:54:07 +05:30
|
|
|
sendPluginResponse(404, "Invalid DB Location or DB Doesn't Exists", true, callbackContext);
|
2015-01-23 20:18:08 +05:30
|
|
|
}
|
|
|
|
|
return true;
|
2017-01-11 15:54:07 +05:30
|
|
|
} else if (action.equalsIgnoreCase("copyDbToStorage")) {
|
|
|
|
|
String db = args.getString(0);
|
|
|
|
|
String dest = args.getString(2);
|
|
|
|
|
this.copyDbToStorage(db, dest, callbackContext);
|
|
|
|
|
return true;
|
|
|
|
|
} else if(action.equalsIgnoreCase("copyDbFromStorage")) {
|
|
|
|
|
String db = args.getString(0);
|
|
|
|
|
String src = args.getString(2);
|
|
|
|
|
this.copyDbFromStorage(db, src, callbackContext);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
2015-01-23 20:18:08 +05:30
|
|
|
plresult = new PluginResult(PluginResult.Status.INVALID_ACTION);
|
|
|
|
|
callbackContext.sendPluginResult(plresult);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 15:54:07 +05:30
|
|
|
private void copyDB(String dbName, final String src, final CallbackContext callbackContext) {
|
2015-01-23 20:18:08 +05:30
|
|
|
|
|
|
|
|
final File dbpath;
|
|
|
|
|
dbname = dbName;
|
2017-01-11 15:54:07 +05:30
|
|
|
JSONObject response = new JSONObject();
|
2015-01-23 20:18:08 +05:30
|
|
|
final DatabaseHelper dbhelper = new DatabaseHelper(this.cordova
|
|
|
|
|
.getActivity().getApplicationContext());
|
|
|
|
|
dbpath = this.cordova.getActivity().getDatabasePath(dbname);
|
|
|
|
|
Boolean dbexists = dbpath.exists();
|
2015-02-02 11:42:57 +05:30
|
|
|
//Log.d("CordovaLog", "DatabasePath = " + dbpath + "&&&& dbname = " + dbname + "&&&&DB Exists =" + dbexists);
|
2015-01-23 20:18:08 +05:30
|
|
|
|
|
|
|
|
if (dbexists) {
|
2017-01-11 15:54:07 +05:30
|
|
|
sendPluginResponse(516,"DB Already Exists", true, callbackContext);
|
2015-01-23 20:18:08 +05:30
|
|
|
} else {
|
|
|
|
|
cordova.getThreadPool().execute(new Runnable() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2017-01-11 15:54:07 +05:30
|
|
|
PluginResult plResult;
|
2015-01-23 20:18:08 +05:30
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
try {
|
2017-01-11 15:54:07 +05:30
|
|
|
dbhelper.createdatabase(dbpath,src, callbackContext);
|
|
|
|
|
// plResult = new PluginResult(PluginResult.Status.OK);
|
|
|
|
|
// callbackContext.sendPluginResult(plResult);
|
|
|
|
|
//sendPluginResponse(200,"Db Copied", false, callbackContext);
|
2015-01-23 20:18:08 +05:30
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
2017-01-11 15:54:07 +05:30
|
|
|
// plResult = new PluginResult(PluginResult.Status.ERROR,
|
|
|
|
|
// e.getMessage());
|
|
|
|
|
// callbackContext.sendPluginResult(plResult);
|
|
|
|
|
sendPluginResponse(400,e.getMessage(), true, callbackContext);
|
2015-01-23 20:18:08 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-11 15:54:07 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
private void newCopyDB(File source, File destination, CallbackContext callbackContext){
|
|
|
|
|
try {
|
|
|
|
|
InputStream myInput = new FileInputStream(source);
|
|
|
|
|
OutputStream myOutput = new FileOutputStream(destination);
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
|
while ((myInput.read(buffer)) > -1) {
|
|
|
|
|
myOutput.write(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
myOutput.flush();
|
|
|
|
|
myOutput.close();
|
|
|
|
|
myInput.close();
|
|
|
|
|
sendPluginResponse(200, "DB Copied Successfully", false, callbackContext);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
sendPluginResponse(400, e.getMessage(), true, callbackContext);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void copyDbFromStorage(String db, String src, final CallbackContext callbackContext) {
|
|
|
|
|
File source;
|
|
|
|
|
if(src.indexOf("file://") != -1){
|
|
|
|
|
source = new File(src.replace("file://",""));
|
|
|
|
|
} else {
|
|
|
|
|
source = new File(src);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(source.exists()){
|
|
|
|
|
this.copyDB(db, source.getAbsolutePath(), callbackContext);
|
|
|
|
|
} else {
|
|
|
|
|
sendPluginResponse(404, "Invalid DB Source Location", true, callbackContext);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void copyDbToStorage(String dbname, String dest, final CallbackContext callbackContext){
|
|
|
|
|
File source = cordova.getActivity().getDatabasePath(dbname);
|
|
|
|
|
File destination;
|
|
|
|
|
if(dest.indexOf("file://") != -1){
|
|
|
|
|
destination = new File(dest.replace("file://",""));
|
|
|
|
|
} else {
|
|
|
|
|
destination = new File(dest);
|
|
|
|
|
}
|
|
|
|
|
if(!destination.exists()){
|
|
|
|
|
destination.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(source.exists()) {
|
|
|
|
|
this.newCopyDB(source,destination,callbackContext);
|
|
|
|
|
} else {
|
|
|
|
|
sendPluginResponse(404, "Invalid DB Location or DB Doesn't Exists", true, callbackContext);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-10 16:08:44 +05:30
|
|
|
}
|