mirror of
https://gitee.com/shuto/cordova-plugin-dbcopy.git
synced 2026-04-13 00:00:13 +08:00
Format Code
This commit is contained in:
+96
-100
@@ -1,5 +1,12 @@
|
||||
package me.rahul.plugins.sqlDB;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.PluginResult;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
@@ -7,34 +14,24 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.PluginResult;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
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);
|
||||
public static String dbname = "dbname";
|
||||
PluginResult plresult = new PluginResult(PluginResult.Status.NO_RESULT);
|
||||
|
||||
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) {
|
||||
if (error) {
|
||||
plresult = new PluginResult(PluginResult.Status.ERROR,
|
||||
response);
|
||||
} else {
|
||||
plresult = new PluginResult(PluginResult.Status.OK,response);
|
||||
plresult = new PluginResult(PluginResult.Status.OK, response);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
// TODO Auto-generated catch block
|
||||
@@ -45,85 +42,85 @@ public class sqlDB extends CordovaPlugin {
|
||||
callbackContext.sendPluginResult(plresult);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, JSONArray args,
|
||||
CallbackContext callbackContext) throws JSONException {
|
||||
@Override
|
||||
public boolean execute(String action, JSONArray args,
|
||||
CallbackContext callbackContext) throws JSONException {
|
||||
|
||||
if (action.equalsIgnoreCase("copy")) {
|
||||
this.copyDB(args.getString(0), "www", callbackContext);
|
||||
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) {
|
||||
sendPluginResponse(200,"Database Deleted",false, callbackContext);
|
||||
} else {
|
||||
sendPluginResponse(400,"Unable to Delete",true, callbackContext);
|
||||
}
|
||||
} else {
|
||||
if (action.equalsIgnoreCase("copy")) {
|
||||
this.copyDB(args.getString(0), "www", callbackContext);
|
||||
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) {
|
||||
sendPluginResponse(200, "Database Deleted", false, callbackContext);
|
||||
} else {
|
||||
sendPluginResponse(400, "Unable to Delete", true, callbackContext);
|
||||
}
|
||||
} else {
|
||||
sendPluginResponse(404, "Invalid DB Location or DB Doesn't Exists", true, callbackContext);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
} 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 if (action.equalsIgnoreCase("copyDbFromStorage")) {
|
||||
String db = args.getString(0);
|
||||
String src = args.getString(2);
|
||||
this.copyDbFromStorage(db, src, callbackContext);
|
||||
return true;
|
||||
} else {
|
||||
plresult = new PluginResult(PluginResult.Status.INVALID_ACTION);
|
||||
callbackContext.sendPluginResult(plresult);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
plresult = new PluginResult(PluginResult.Status.INVALID_ACTION);
|
||||
callbackContext.sendPluginResult(plresult);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void copyDB(String dbName, final String src, final CallbackContext callbackContext) {
|
||||
private void copyDB(String dbName, final String src, final CallbackContext callbackContext) {
|
||||
|
||||
final File dbpath;
|
||||
dbname = dbName;
|
||||
JSONObject response = new JSONObject();
|
||||
final DatabaseHelper dbhelper = new DatabaseHelper(this.cordova
|
||||
.getActivity().getApplicationContext());
|
||||
dbpath = this.cordova.getActivity().getDatabasePath(dbname);
|
||||
Boolean dbexists = dbpath.exists();
|
||||
//Log.d("CordovaLog", "DatabasePath = " + dbpath + "&&&& dbname = " + dbname + "&&&&DB Exists =" + dbexists);
|
||||
final File dbpath;
|
||||
dbname = dbName;
|
||||
JSONObject response = new JSONObject();
|
||||
final DatabaseHelper dbhelper = new DatabaseHelper(this.cordova
|
||||
.getActivity().getApplicationContext());
|
||||
dbpath = this.cordova.getActivity().getDatabasePath(dbname);
|
||||
Boolean dbexists = dbpath.exists();
|
||||
//Log.d("CordovaLog", "DatabasePath = " + dbpath + "&&&& dbname = " + dbname + "&&&&DB Exists =" + dbexists);
|
||||
|
||||
if (dbexists) {
|
||||
sendPluginResponse(516,"DB Already Exists", true, callbackContext);
|
||||
} else {
|
||||
cordova.getThreadPool().execute(new Runnable() {
|
||||
if (dbexists) {
|
||||
sendPluginResponse(516, "DB Already Exists", true, callbackContext);
|
||||
} else {
|
||||
cordova.getThreadPool().execute(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
PluginResult plResult;
|
||||
// TODO Auto-generated method stub
|
||||
try {
|
||||
dbhelper.createdatabase(dbpath,src, callbackContext);
|
||||
@Override
|
||||
public void run() {
|
||||
PluginResult plResult;
|
||||
// TODO Auto-generated method stub
|
||||
try {
|
||||
dbhelper.createdatabase(dbpath, src, callbackContext);
|
||||
// plResult = new PluginResult(PluginResult.Status.OK);
|
||||
// callbackContext.sendPluginResult(plResult);
|
||||
//sendPluginResponse(200,"Db Copied", false, callbackContext);
|
||||
} catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
|
||||
// plResult = new PluginResult(PluginResult.Status.ERROR,
|
||||
// e.getMessage());
|
||||
// callbackContext.sendPluginResult(plResult);
|
||||
sendPluginResponse(400,e.getMessage(), true, callbackContext);
|
||||
}
|
||||
}
|
||||
sendPluginResponse(400, e.getMessage(), true, callbackContext);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void newCopyDB(File source, File destination, CallbackContext callbackContext){
|
||||
private void newCopyDB(File source, File destination, CallbackContext callbackContext) {
|
||||
try {
|
||||
InputStream myInput = new FileInputStream(source);
|
||||
OutputStream myOutput = new FileOutputStream(destination);
|
||||
@@ -142,46 +139,45 @@ public class sqlDB extends CordovaPlugin {
|
||||
}
|
||||
|
||||
private void copyDbFromStorage(String db, String src, final CallbackContext callbackContext) {
|
||||
File source;
|
||||
if(src.indexOf("file://") != -1){
|
||||
source = new File(src.replace("file://",""));
|
||||
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);
|
||||
}
|
||||
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){
|
||||
private void copyDbToStorage(String dbname, String dest, final CallbackContext callbackContext) {
|
||||
File source = cordova.getActivity().getDatabasePath(dbname);
|
||||
File destFolder;
|
||||
File destination;
|
||||
|
||||
if(dest.indexOf("file://") != -1){
|
||||
destination = new File(dest.replace("file://","") + dbname);
|
||||
destFolder = new File(dest.replace("file://",""));
|
||||
File destFolder;
|
||||
File destination;
|
||||
|
||||
if (dest.indexOf("file://") != -1) {
|
||||
destination = new File(dest.replace("file://", "") + dbname);
|
||||
destFolder = new File(dest.replace("file://", ""));
|
||||
} else {
|
||||
destination = new File(dest + dbname);
|
||||
destFolder = new File(dest);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(!destFolder.exists()){
|
||||
destFolder.mkdirs();
|
||||
}
|
||||
|
||||
if(!destFolder.exists()) {
|
||||
sendPluginResponse(404, "Invalid output DB Location", true, callbackContext);
|
||||
return;
|
||||
destFolder = new File(dest);
|
||||
}
|
||||
|
||||
if(source.exists()) {
|
||||
this.newCopyDB(source,destination,callbackContext);
|
||||
|
||||
if (!destFolder.exists()) {
|
||||
destFolder.mkdirs();
|
||||
}
|
||||
|
||||
if (!destFolder.exists()) {
|
||||
sendPluginResponse(404, "Invalid output DB Location", true, callbackContext);
|
||||
return;
|
||||
}
|
||||
|
||||
if (source.exists()) {
|
||||
this.newCopyDB(source, destination, callbackContext);
|
||||
} else {
|
||||
sendPluginResponse(404, "Invalid DB Location or DB Doesn't Exists", true, callbackContext);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user