com.polyvi.xface.extension.XBatteryExt.java Source code

Java tutorial

Introduction

Here is the source code for com.polyvi.xface.extension.XBatteryExt.java

Source

/*
 This file was modified from or inspired by Apache Cordova.
    
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements. See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership. The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License. You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied. See the License for the
 specific language governing permissions and limitations
 under the License.
*/

package com.polyvi.xface.extension;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.polyvi.xface.util.XLog;

public class XBatteryExt extends XExtension {

    private static final String CLASS_NAME = XBatteryExt.class.getSimpleName();

    private static final String COMMAND_START = "start";
    /**< js?start*/

    private static final String COMMAND_STOP = "stop";
    /**< js?stop*/

    private BroadcastReceiver mBroadcastReceiver;

    /**< */

    public XBatteryExt() {
        mBroadcastReceiver = null;
    }

    @Override
    public void sendAsyncResult(String result) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean isAsync(String action) {
        return true;
    }

    @Override
    public XExtensionResult exec(String action, JSONArray args, XCallbackContext callbackCtx) throws JSONException {
        XExtensionResult.Status status = XExtensionResult.Status.INVALID_ACTION;
        String result = "Unsupported Operation: " + action;
        if (COMMAND_START.equals(action)) {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
            final XCallbackContext cbCtx = callbackCtx;
            if (null == mBroadcastReceiver) {
                mBroadcastReceiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        XLog.d(CLASS_NAME, "received broadcast of battery changing");
                        updateBatteryInfo(intent, cbCtx);
                    }
                };
                getContext().registerReceiver(mBroadcastReceiver, intentFilter);
            }
            XExtensionResult extensionResult = new XExtensionResult(XExtensionResult.Status.NO_RESULT);
            extensionResult.setKeepCallback(true);
            return extensionResult;
        } else if (COMMAND_STOP.equals(action)) {
            removeBatteryListener();
            sendUpdate(new JSONObject(), callbackCtx, false);
            return new XExtensionResult(XExtensionResult.Status.OK);
        }
        return new XExtensionResult(status, result);
    }

    /**
     * ?
     * @param batteryIntent ??intent
     * @param callbackCtx 
     * */
    private void updateBatteryInfo(Intent batteryIntent, XCallbackContext callbackCtx) {
        sendUpdate(getBatteryInfo(batteryIntent), callbackCtx, true);
    }

    /**
     * ???
     * */
    private void removeBatteryListener() {
        if (null != mBroadcastReceiver) {
            try {
                getContext().unregisterReceiver(mBroadcastReceiver);
                mBroadcastReceiver = null;
            } catch (Exception e) {
                XLog.e(CLASS_NAME, "Error unregistering battery receiver: " + e.getMessage(), e);
            }
        }
    }

    /**
     * ????ExtensionResult???js
     *
     * @param info ?json?
     * @param keepCallback ???
     * @param callbackCtx 
     */
    private void sendUpdate(JSONObject info, XCallbackContext callbackCtx, boolean keepCallback) {
        XExtensionResult result = new XExtensionResult(XExtensionResult.Status.OK, info);
        result.setKeepCallback(keepCallback);
        callbackCtx.sendExtensionResult(result);
    }

    /**
     * ????json
     *
     * @param batteryIntent ?????intent
     * @return ??json
     */
    private JSONObject getBatteryInfo(Intent batteryIntent) {
        JSONObject obj = new JSONObject();
        try {
            obj.put("level", batteryIntent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, 0));
            obj.put("isPlugged",
                    batteryIntent.getIntExtra(android.os.BatteryManager.EXTRA_PLUGGED, -1) > 0 ? true : false);
        } catch (JSONException e) {
            XLog.e(CLASS_NAME, e.getMessage(), e);
        }
        return obj;
    }

    @Override
    public void destroy() {
        removeBatteryListener();
    }
}