Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Created January 2, 2012 14:09
Show Gist options
  • Save dhavaln/1550811 to your computer and use it in GitHub Desktop.
Save dhavaln/1550811 to your computer and use it in GitHub Desktop.
/**
* Application Communication Interface from JS to Native
*/
var AppCommControl = function() {
};
AppCommControl.prototype.addUser = function(args, successCallback, failureCallback) {
return PhoneGap.exec( successCallback, //Success callback from the plugin
failureCallback, //Error callback from the plugin
'AppComm', //Tell PhoneGap to run Plugin
'addUser', //Tell plugin, which action we want to perform
args); //Passing list of args to the plugin
};
AppCommControl.prototype.getUpdates = function(successCallback, failureCallback) {
return PhoneGap.exec( successCallback, //Success callback from the plugin
failureCallback, //Error callback from the plugin
'AppComm', //Tell PhoneGap to run Plugin
'getUpdates', //Tell plugin, which action we want to perform
[]); //Passing list of args to the plugin
};
AppCommControl.prototype.hasUpdates = function(successCallback, failureCallback) {
return PhoneGap.exec( successCallback, //Success callback from the plugin
failureCallback, //Error callback from the plugin
'AppComm', //Tell PhoneGap to run Plugin
'hasUpdates', //Tell plugin, which action we want to perform
[]); //Passing list of args to the plugin
};
AppCommControl.prototype.clearUpdates = function(successCallback, failureCallback) {
return PhoneGap.exec( successCallback, //Success callback from the plugin
failureCallback, //Error callback from the plugin
'AppComm', //Tell PhoneGap to run Plugin
'clearUpdates', //Tell plugin, which action we want to perform
[]); //Passing list of args to the plugin
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("appcomm", new AppCommControl());
});
<?xml version="1.0" encoding="utf-8"?>
<plugins>
<plugin name="App" value="com.phonegap.App"/>
<plugin name="Geolocation" value="com.phonegap.GeoBroker"/>
<plugin name="Device" value="com.phonegap.Device"/>
<plugin name="Accelerometer" value="com.phonegap.AccelListener"/>
<plugin name="Compass" value="com.phonegap.CompassListener"/>
<plugin name="Media" value="com.phonegap.AudioHandler"/>
<plugin name="Camera" value="com.phonegap.CameraLauncher"/>
<plugin name="Contacts" value="com.phonegap.ContactManager"/>
<plugin name="Crypto" value="com.phonegap.CryptoHandler"/>
<plugin name="File" value="com.phonegap.FileUtils"/>
<plugin name="Network Status" value="com.phonegap.NetworkManager"/>
<plugin name="Notification" value="com.phonegap.Notification"/>
<plugin name="Storage" value="com.phonegap.Storage"/>
<plugin name="Temperature" value="com.phonegap.TempListener"/>
<plugin name="FileTransfer" value="com.phonegap.FileTransfer"/>
<plugin name="Capture" value="com.phonegap.Capture"/>
<plugin name="Keyboard" value="com.dhavaln.mobile.android.plugin.KeyboardControl"/>
<plugin name="AppComm" value="com.dhavaln.mobile.android.plugin.AppCommControl"/>
<plugin name="SyncThread" value="com.dhavaln.mobile.android.plugin.SyncThread"/>
</plugins>
package com.dhavaln.mobile.android.plugin;
import java.util.Date;
import org.json.JSONArray;
import android.util.Log;
import com.phonegap.api.PhonegapActivity;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
public class SyncThread extends Plugin {
public static final String ACTION_START = "start";
private String syncCallBackId;
private Thread t;
private boolean stop;
public SyncThread() {
}
@Override
public void setContext(PhonegapActivity ctx) {
super.setContext(ctx);
this.syncCallBackId = null;
}
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
Log.i("SyncThread", action + " Action called with callback id "
+ callbackId );
PluginResult.Status status = PluginResult.Status.INVALID_ACTION;
String result = "Unsupported Operation: " + action;
// either start or stop the listener...
if (action.equals(ACTION_START)) {
if (this.syncCallBackId != null) {
return new PluginResult(PluginResult.Status.ERROR,
"Thread already running.");
}
this.syncCallBackId = callbackId;
PluginResult pluginResult = new PluginResult(
PluginResult.Status.NO_RESULT);
pluginResult.setKeepCallback(true);
if (t == null) {
t = new Thread() {
@Override
public void run() {
while (!stop) {
try {
Thread.sleep(15000);
Log.i("SyncThread",
"ready to call javascript for sync " + new Date() + SyncThread.this.toString());
initSync(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
t.start();
}
return pluginResult;
}
return new PluginResult(status, result); // no valid action called
}
private void initSync(boolean keepCallback) {
if (this.syncCallBackId != null) {
PluginResult result = new PluginResult(PluginResult.Status.OK);
result.setKeepCallback(keepCallback);
this.success(result, this.syncCallBackId);
}
}
@Override
public void onDestroy() {
stop = true;
t.stop();
}
}
/**
* SyncThread JS Interface
*/
var SyncThread = function() {
};
SyncThread.prototype.start = function(successCallback, failureCallback) {
return PhoneGap.exec(successCallback, failureCallback, 'SyncThread',
'start', []);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("syncthread", new SyncThread());
});
<html>
<head>
<script type="application/javascript" src="lib/js/phonegap/phonegap.js"></script>
<script type="application/javascript" src="lib/js/plugins/android/appcomm.js"></script>
<script type="application/javascript" src="lib/js/plugins/android/syncthread.js"></script>
</head>
<body>
<script type="text/javascript">
document.addEventListener('deviceready', function() {
window.plugins.syncthread.start(doSync);
});
function doSync(){
console.log("------> received native event to initiate the SYNC");
window.plugins.appcomm.hasUpdates(function(updateStatus){
if(updateStatus){
}
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment