Search in sources :

Example 26 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class BatteryListener method execute.

/**
     * Executes the request.
     *
     * @param action        	The action to execute.
     * @param args          	JSONArry of arguments for the plugin.
     * @param callbackContext 	The callback context used when calling back into JavaScript.
     * @return              	True if the action was valid, false if not.
     */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    if (action.equals("start")) {
        if (this.batteryCallbackContext != null) {
            callbackContext.error("Battery listener already running.");
            return true;
        }
        this.batteryCallbackContext = callbackContext;
        // We need to listen to power events to update battery status
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        if (this.receiver == null) {
            this.receiver = new BroadcastReceiver() {

                @Override
                public void onReceive(Context context, Intent intent) {
                    updateBatteryInfo(intent);
                }
            };
            cordova.getActivity().registerReceiver(this.receiver, intentFilter);
        }
        // Don't return any result now, since status results will be sent when events come in from broadcast receiver
        PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
        pluginResult.setKeepCallback(true);
        callbackContext.sendPluginResult(pluginResult);
        return true;
    } else if (action.equals("stop")) {
        removeBatteryListener();
        // release status callback in JS side
        this.sendUpdate(new JSONObject(), false);
        this.batteryCallbackContext = null;
        callbackContext.success();
        return true;
    }
    return false;
}
Also used : Context(android.content.Context) CallbackContext(org.apache.cordova.api.CallbackContext) IntentFilter(android.content.IntentFilter) PluginResult(org.apache.cordova.api.PluginResult) JSONObject(org.json.JSONObject) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver)

Example 27 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class BatteryListener method sendUpdate.

/**
     * Create a new plugin result and send it back to JavaScript
     *
     * @param connection the network info to set as navigator.connection
     */
private void sendUpdate(JSONObject info, boolean keepCallback) {
    if (this.batteryCallbackContext != null) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, info);
        result.setKeepCallback(keepCallback);
        this.batteryCallbackContext.sendPluginResult(result);
    }
}
Also used : PluginResult(org.apache.cordova.api.PluginResult)

Aggregations

PluginResult (org.apache.cordova.api.PluginResult)27 JSONException (org.json.JSONException)10 JSONObject (org.json.JSONObject)9 IOException (java.io.IOException)5 FileNotFoundException (java.io.FileNotFoundException)4 AlertDialog (android.app.AlertDialog)3 DialogInterface (android.content.DialogInterface)3 MalformedURLException (java.net.MalformedURLException)3 CordovaInterface (org.apache.cordova.api.CordovaInterface)3 Intent (android.content.Intent)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 HostnameVerifier (javax.net.ssl.HostnameVerifier)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)2 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)2 JSONArray (org.json.JSONArray)2