Initial Commit

This commit is contained in:
Rahul Pandey 2014-04-10 16:08:44 +05:30
commit ba2c4879a6
5 changed files with 261 additions and 0 deletions

40
plugin.xml Normal file
View File

@ -0,0 +1,40 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="me.rahul.plugins.sqlDB"
version="1.0.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>sqlDB</name>
<description>Copy SQLite Database to Document Directory</description>
<js-module name="sqlDB" src="www/sqlDB.js">
<clobbers target="window.plugins.sqlDB" />
</js-module>
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="sqlDB">
<param name="android-package" value="me.rahul.plugins.sqlDB.sqlDB" />
</feature>
</config-file>
<config-file parent="/manifest" target="AndroidManifest.xml">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</config-file>
<source-file src="src/android/sqlDB.java" target-dir="src/me/rahul/plugins/sqlDB/" />
<source-file src="src/android/DatabaseHelper.java" target-dir="src/me/rahul/plugins/sqlDB/" />
</platform>
<platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="sqlDB">
<param name="ios-package" value="sqlDB" />
</feature>
</config-file>
<source-file src="src/ios/sqlDB.m" />
</platform>
</plugin>

View File

@ -0,0 +1,74 @@
package me.rahul.plugins.sqlDB;
import java.io.File;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private Context myContext;
public DatabaseHelper(Context context) {
super(context, sqlDB.dbname, null, 1);
this.myContext = context;
// TODO Auto-generated constructor stub
}
public void createdatabase(File dbPath) throws IOException {
Log.d("CordovaLog","Inside CreateDatabase = "+dbPath);
this.getReadableDatabase();
try {
copyDatabase(dbPath);
} catch (IOException e) {
throw new Error("Create Database Exception ============================ "+e);
}
}
private void copyDatabase(File database) throws IOException {
Log.d("CordovaLog","Inside copydatabase = "+database+"dbname = "+sqlDB.dbname);
InputStream myInput = myContext.getAssets().open(sqlDB.dbname);
OutputStream myOutput = new FileOutputStream(database);
byte[] buffer = new byte[1024];
Log.d("CordovaLog","Inside copydatabase = "+database+"dbname = "+sqlDB.dbname);
int length;
while ((myInput.read(buffer))>-1){
myOutput.write(buffer);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}

69
src/android/sqlDB.java Normal file
View File

@ -0,0 +1,69 @@
package me.rahul.plugins.sqlDB;
import java.io.File;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
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);
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equalsIgnoreCase("copy")) {
this.copyDB(args.getString(0), callbackContext);
return true;
} else {
plresult = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(plresult);
return false;
}
}
private void copyDB(String dbName, final CallbackContext callbackContext)
{
final File dbpath;
dbname = dbName;
final DatabaseHelper dbhelper = new DatabaseHelper(this.cordova.getActivity().getApplicationContext());
dbpath = this.cordova.getActivity().getDatabasePath(dbname);
Boolean dbexists = dbpath.exists();
Log.d("CordovaLog", "Inside Plugin");
Log.d("CordovaLog", "DatabasePath = "+dbpath+"&&&& dbname = "+dbname+"&&&&DB Exists ="+dbexists);
if(dbexists){
plresult = new PluginResult(PluginResult.Status.ERROR);
callbackContext.sendPluginResult(plresult);
} else {
cordova.getThreadPool().execute(new Runnable(){
@Override
public void run() {
PluginResult plResult = new PluginResult(PluginResult.Status.NO_RESULT);
// TODO Auto-generated method stub
try
{
dbhelper.createdatabase(dbpath);
plResult = new PluginResult(PluginResult.Status.OK);
callbackContext.sendPluginResult(plResult);
} catch (Exception e){
plResult = new PluginResult(PluginResult.Status.ERROR);
callbackContext.sendPluginResult(plResult);
}
}
});
}
}
}

73
src/ios/sqlDB.m Normal file
View File

@ -0,0 +1,73 @@
//
// sqlDB Implementation File
// sqlDB Phonegap/Cordova Plugin
//
// Created by Rahul Pandey on 09/04/14.
//
//
#import <Cordova/CDV.h>
#import <Cordova/CDVPlugin.h>
#import <Foundation/Foundation.h>
@interface sqlDB : CDVPlugin {
// Member variables go here.
NSString* callbackId;
NSFileManager* fileManager;
NSArray* paths;
NSString* documentsDirectory;
}
@property (nonatomic, copy) NSString* callbackId;
- (void)copy:(CDVInvokedUrlCommand*)command;
@end
@implementation sqlDB
- (void)copy:(CDVInvokedUrlCommand*)command
{
self.callbackId = command.callbackId;
BOOL success;
NSError *error = nil;
CDVPluginResult *result = nil;
NSString *dbname = [command argumentAtIndex:0];
NSLog(@"[sqlDB] Dbname = %@",dbname);
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:dbname];
fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:dbPath isDirectory:NO];
if (!success) {
NSLog(@"[sqlDB] copying db file");
NSString *dbPathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname];
NSLog(@"[sqlDB] Source: %@",dbPathFromApp);
NSLog(@"[sqlDB] Destinatiion: %@",dbPath);
if (!([fileManager copyItemAtPath:dbPathFromApp toPath:dbPath error:&error])) {
NSLog(@"[sqlDB] Could not copy file from %@ to %@. Error = %@",dbPathFromApp,dbPath,error);
result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"File Copied"];
}
}
else
{
NSLog(@"[sqlDB] File Aready Exists");
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"File Already Exists"];
}
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}
@end

5
www/sqlDB.js Normal file
View File

@ -0,0 +1,5 @@
var exec = require('cordova/exec');
exports.copy = function(dbname, success, error) {
exec(success, error, "sqlDB", "copy", [dbname]);
};