Java Code Examples for android.os.BatteryManager#BATTERY_PLUGGED_USB

The following examples show how to use android.os.BatteryManager#BATTERY_PLUGGED_USB . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceSucker.java    From CameraV with GNU General Public License v3.0 7 votes vote down vote up
private void getPlugState(Intent intent) {
    String parse = null;
    int plugged_state = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

    Logger.d(LOG, String.format("GETTING PLUG STATE: %d", plugged_state));

    switch(plugged_state) {
        case BatteryManager.BATTERY_PLUGGED_AC:
            parse = "battery plugged AC";
            break;
        case BatteryManager.BATTERY_PLUGGED_USB:
            parse = "battery plugged USB";
            break;
    }

    if(parse != null) {
        ILogPack logPack = new ILogPack();

        logPack.put(Keys.PLUG_EVENT_TYPE, parse);
        logPack.put(Keys.PLUG_EVENT_CODE, plugged_state);

        sendToBuffer(logPack);
    }
}
 
Example 2
Source File: BatteryService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private boolean isPoweredLocked(int plugTypeSet) {
    // assume we are powered if battery state is unknown so
    // the "stay on while plugged in" option will work.
    if (mHealthInfo.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
        return true;
    }
    if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mHealthInfo.chargerAcOnline) {
        return true;
    }
    if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mHealthInfo.chargerUsbOnline) {
        return true;
    }
    if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mHealthInfo.chargerWirelessOnline) {
        return true;
    }
    return false;
}
 
Example 3
Source File: BatteryInfo.java    From MobileInfo with Apache License 2.0 6 votes vote down vote up
private static String batteryPlugged(int status) {
    String healthBat = BaseData.UNKNOWN_PARAM;
    switch (status) {
        case BatteryManager.BATTERY_PLUGGED_AC:
            healthBat = "ac";
            break;
        case BatteryManager.BATTERY_PLUGGED_USB:
            healthBat = "usb";
            break;
        case BatteryManager.BATTERY_PLUGGED_WIRELESS:
            healthBat = "wireless";
            break;
        default:
            break;
    }
    return healthBat;
}
 
Example 4
Source File: AndroidUtils.java    From Android-Next with Apache License 2.0 6 votes vote down vote up
public static String getBatteryInfo(Intent batteryIntent) {
    int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
            status == BatteryManager.BATTERY_STATUS_FULL;
    int chargePlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
    boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    float batteryPct = level / (float) scale;
    return "Battery Info: isCharging=" + isCharging
            + " usbCharge=" + usbCharge + " acCharge=" + acCharge
            + " batteryPct=" + batteryPct;
}
 
Example 5
Source File: PolicyManagementFragment.java    From android-testdpc with Apache License 2.0 6 votes vote down vote up
/**
 * Update the preference switch for {@link Settings.Global#STAY_ON_WHILE_PLUGGED_IN} setting.
 *
 * <p>
 * If either one of the {@link BatteryManager#BATTERY_PLUGGED_AC},
 * {@link BatteryManager#BATTERY_PLUGGED_USB}, {@link BatteryManager#BATTERY_PLUGGED_WIRELESS}
 * values is set, we toggle the preference to true and update the setting value to
 * {@link #BATTERY_PLUGGED_ANY}
 * </p>
 */
private void updateStayOnWhilePluggedInPreference() {
    if (!mStayOnWhilePluggedInSwitchPreference.isEnabled()) {
        return;
    }

    boolean checked = false;
    final int currentState = Settings.Global.getInt(getActivity().getContentResolver(),
            Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0);
    checked = (currentState &
            (BatteryManager.BATTERY_PLUGGED_AC |
            BatteryManager.BATTERY_PLUGGED_USB |
            BatteryManager.BATTERY_PLUGGED_WIRELESS)) != 0;
    mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
            Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
            checked ? BATTERY_PLUGGED_ANY : DONT_STAY_ON);
    mStayOnWhilePluggedInSwitchPreference.setChecked(checked);
}
 
Example 6
Source File: PowerUtils.java    From HeadsUp with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return true is device is plugged at this moment, false otherwise.
 * @see #isPlugged(android.content.Context)
 */
@SuppressLint("InlinedApi")
public static boolean isPlugged(@Nullable Intent intent) {
    if (intent == null) {
        return false;
    }

    final int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC
            || plugged == BatteryManager.BATTERY_PLUGGED_USB
            || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
 
Example 7
Source File: TelemetryUtils.java    From mapbox-events-android with MIT License 5 votes vote down vote up
public static boolean isPluggedIn(Context context) {
  Intent batteryStatus = registerBatteryUpdates(context);
  if (batteryStatus == null) {
    return false;
  }

  int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, DEFAULT_BATTERY_LEVEL);
  final boolean pluggedIntoUSB = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
  final boolean pluggedIntoAC = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
  return pluggedIntoUSB || pluggedIntoAC;
}
 
Example 8
Source File: RunConditionMonitor.java    From syncthing-android with Mozilla Public License 2.0 5 votes vote down vote up
@TargetApi(17)
private boolean isCharging_API17() {
    Intent intent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC ||
        plugged == BatteryManager.BATTERY_PLUGGED_USB ||
        plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
 
Example 9
Source File: PowerMonitor.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void onBatteryChargingChanged(Intent intent) {
    if (sInstance == null) {
        // We may be called by the framework intent-filter before being fully initialized. This
        // is not a problem, since our constructor will check for the state later on.
        return;
    }
    int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    // If we're not plugged, assume we're running on battery power.
    sInstance.mIsBatteryPower = chargePlug != BatteryManager.BATTERY_PLUGGED_USB &&
                                chargePlug != BatteryManager.BATTERY_PLUGGED_AC;
    nativeOnBatteryChargingChanged();
}
 
Example 10
Source File: SensorReader.java    From homeDash with Apache License 2.0 5 votes vote down vote up
public  void getBatteryReading(SensorDataListener listener){

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, intentFilter);

        int batteryStatusIntExtra = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = batteryStatusIntExtra == BatteryManager.BATTERY_STATUS_CHARGING ||
                batteryStatusIntExtra == BatteryManager.BATTERY_STATUS_FULL;

        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        float batteryPct = level / (float)scale;

        Log.i(TAG, "AC connected: "+acCharge);
        Log.i(TAG, "USB connected: "+usbCharge);
        Log.i(TAG, "Battery charging: "+ isCharging);
        Log.i(TAG, "Battery Level: "+ batteryPct);

        ArrayMap<String, String> map = new ArrayMap<>(3);
        map.put(SENSOR, "Battery");
        map.put(VALUE, Integer.toString(level));
        map.put(UNIT, BATTERYSENSOR_UNIT);
        map.put("charging", Boolean.toString(isCharging));
        map.put("acPlugged", Boolean.toString(acCharge));
        map.put("usbPlugged", Boolean.toString(usbCharge));
        listener.sensorData(map);
    }
 
Example 11
Source File: TypeTranslators.java    From under-the-hood with Apache License 2.0 5 votes vote down vote up
public static String translateBatteryPlugged(int batteryPlugged) {
    switch (batteryPlugged) {
        case 0:
            return "UNPLUGGED";
        case BatteryManager.BATTERY_PLUGGED_AC:
            return "AC";
        case BatteryManager.BATTERY_PLUGGED_USB:
            return "USB";
        case BatteryManager.BATTERY_PLUGGED_WIRELESS:
            return "WIRELESS";
        default:
            return "UNKNOWN (" + batteryPlugged + ")";
    }
}
 
Example 12
Source File: Utils.java    From ForceDoze with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isConnectedToCharger(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (intent != null) {
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
    } else return false;
}
 
Example 13
Source File: BatteryReceiver.java    From always-on-amoled with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean charging = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
    currentBattery = level;
    Utils.logDebug(MAIN_SERVICE_LOG_TAG, "Battery level " + level);

    if (batteryTV != null)
        batteryTV.setText(String.valueOf(level) + "%");
    if (batteryIV != null) {
        int res;
        if (charging)
            res = R.drawable.ic_battery_charging;
        else {
            if (level > 90)
                res = R.drawable.ic_battery_full;
            else if (level > 70)
                res = R.drawable.ic_battery_90;
            else if (level > 50)
                res = R.drawable.ic_battery_60;
            else if (level > 30)
                res = R.drawable.ic_battery_30;
            else if (level > 20)
                res = R.drawable.ic_battery_20;
            else if (level > 0)
                res = R.drawable.ic_battery_alert;
            else
                res = R.drawable.ic_battery_unknown;
        }
        batteryIV.setImageResource(res);
    }
}
 
Example 14
Source File: Inspector.java    From batteryhub with Apache License 2.0 4 votes vote down vote up
static BatteryUsage getBatteryUsage(final Context context, Intent intent) {
    BatteryUsage usage = new BatteryUsage();
    BatteryDetails details = new BatteryDetails();

    // Battery details
    int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
    String batteryTechnology = intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
    String batteryHealth = "Unknown";
    String batteryCharger = "unplugged";
    String batteryStatus;

    usage.timestamp = System.currentTimeMillis();
    usage.id = String.valueOf(usage.timestamp).hashCode();

    switch (health) {
        case BatteryManager.BATTERY_HEALTH_DEAD:
            batteryHealth = "Dead";
            break;
        case BatteryManager.BATTERY_HEALTH_GOOD:
            batteryHealth = "Good";
            break;
        case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
            batteryHealth = "Over voltage";
            break;
        case BatteryManager.BATTERY_HEALTH_OVERHEAT:
            batteryHealth = "Overheat";
            break;
        case BatteryManager.BATTERY_HEALTH_UNKNOWN:
            batteryHealth = "Unknown";
            break;
        case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
            batteryHealth = "Unspecified failure";
            break;
    }

    switch (status) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
            batteryStatus = "Charging";
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            batteryStatus = "Discharging";
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            batteryStatus = "Full";
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            batteryStatus = "Not charging";
            break;
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            batteryStatus = "Unknown";
            break;
        default:
            batteryStatus = "Unknown";
    }

    switch (plugged) {
        case BatteryManager.BATTERY_PLUGGED_AC:
            batteryCharger = "ac";
            break;
        case BatteryManager.BATTERY_PLUGGED_USB:
            batteryCharger = "usb";
            break;
        case BatteryManager.BATTERY_PLUGGED_WIRELESS:
            batteryCharger = "wireless";
    }

    details.temperature =
            ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)) / 10;

    // current battery voltage in VOLTS
    // (the unit of the returned value by BatteryManager is millivolts)
    details.voltage =
            ((float) intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0)) / 1000;

    details.charger = batteryCharger;
    details.health = batteryHealth;
    details.technology = batteryTechnology;

    // Battery other values with API level limitations
    details.capacity = Battery.getBatteryDesignCapacity(context);
    details.chargeCounter = Battery.getBatteryChargeCounter(context);
    details.currentAverage = Battery.getBatteryCurrentAverage(context);
    details.currentNow = (int) Battery.getBatteryCurrentNow(context);
    details.energyCounter = Battery.getBatteryEnergyCounter(context);
    details.remainingCapacity = Battery.getBatteryRemainingCapacity(context);

    usage.level = (float) sCurrentBatteryLevel;
    usage.state = batteryStatus;
    usage.screenOn = Screen.isOn(context);
    usage.triggeredBy = intent.getAction();
    usage.details = details;

    return usage;
}
 
Example 15
Source File: HostBatteryManager.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * バッテリーのIntentから情報を取得.
 */
public void getBatteryInfo() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus;
    int i = 0;
    do {
        batteryStatus = getContext().registerReceiver(null, filter);
    } while (i++ < 3 && batteryStatus == null);

    if (batteryStatus == null) {
        mStatusBattery = HostBatteryManager.BATTERY_STATUS_UNKNOWN;
        mValueLevel = 0;
        mValueScale = 0;
        return;
    }

    // バッテリーの変化を取得
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    switch (status) {
    case BatteryManager.BATTERY_STATUS_UNKNOWN:
        mStatusBattery = HostBatteryManager.BATTERY_STATUS_UNKNOWN;
        break;
    case BatteryManager.BATTERY_STATUS_CHARGING:
        mStatusBattery = HostBatteryManager.BATTERY_STATUS_CHARGING;
        break;
    case BatteryManager.BATTERY_STATUS_DISCHARGING:
        mStatusBattery = HostBatteryManager.BATTERY_STATUS_DISCHARGING;
        break;
    case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
        mStatusBattery = HostBatteryManager.BATTERY_STATUS_NOT_CHARGING;
        break;
    case BatteryManager.BATTERY_STATUS_FULL:
        mStatusBattery = HostBatteryManager.BATTERY_STATUS_FULL;
        break;
    default:
        mStatusBattery = HostBatteryManager.BATTERY_STATUS_UNKNOWN;
        break;
    }

    // プラグの状態を取得
    int plugged = batteryStatus.getIntExtra("plugged", 0);
    switch (plugged) {
    case BatteryManager.BATTERY_PLUGGED_AC:
        mStatusPlugged = BATTERY_PLUGGED_AC;
        break;
    case BatteryManager.BATTERY_PLUGGED_USB:
        mStatusPlugged = BATTERY_PLUGGED_USB;
        break;
    default:
        break;
    }

    // チャージングフラグ
    mChargingFlag = (plugged != 0);

    // バッテリー残量
    mValueLevel = batteryStatus.getIntExtra("level", 0);
    mValueScale = batteryStatus.getIntExtra("scale", 0);
}
 
Example 16
Source File: HostBatteryManager.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * バッテリーのIntentを設定.
 * 
 * @param intent Batteryの変化で取得できたIntent
 */
private void setBatteryRequest(final Intent intent) {
    String mAction = intent.getAction();

    if (Intent.ACTION_BATTERY_CHANGED.equals(mAction) || Intent.ACTION_BATTERY_LOW.equals(mAction)
            || Intent.ACTION_BATTERY_OKAY.equals(mAction)) {
        // バッテリーの変化を取得
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        switch (status) {
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            mStatusBattery = HostBatteryManager.BATTERY_STATUS_UNKNOWN;
            break;
        case BatteryManager.BATTERY_STATUS_CHARGING:
            mStatusBattery = HostBatteryManager.BATTERY_STATUS_CHARGING;
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            mStatusBattery = HostBatteryManager.BATTERY_STATUS_DISCHARGING;
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            mStatusBattery = HostBatteryManager.BATTERY_STATUS_NOT_CHARGING;
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            mStatusBattery = HostBatteryManager.BATTERY_STATUS_FULL;
            break;
        default:
            mStatusBattery = HostBatteryManager.BATTERY_STATUS_UNKNOWN;
            break;
        }

        mValueLevel = intent.getIntExtra("level", 0);
        mValueScale = intent.getIntExtra("scale", 0);

    } else if (Intent.ACTION_POWER_CONNECTED.equals(mAction) || Intent.ACTION_POWER_DISCONNECTED.equals(mAction)) {

        mChargingFlag = Intent.ACTION_POWER_CONNECTED.equals(mAction);

        // プラグの状態を取得
        int plugged = intent.getIntExtra("plugged", 0);
        switch (plugged) {
        case BatteryManager.BATTERY_PLUGGED_AC:
            mStatusPlugged = BATTERY_PLUGGED_AC;
            break;
        case BatteryManager.BATTERY_PLUGGED_USB:
            mStatusPlugged = BATTERY_PLUGGED_USB;
            break;
        default:
            break;
        }
    }
}
 
Example 17
Source File: BatteryInformation.java    From Saiy-PS with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Method to resolve the battery status
 */
private void getStatus() {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "getStatus");
    }

    if (batteryIntent != null) {

        final SaiyResources sr = new SaiyResources(mContext, sl);

        final int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
                BatteryManager.BATTERY_STATUS_UNKNOWN);

        switch (status) {

            case BatteryManager.BATTERY_STATUS_CHARGING:

                int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED,
                        BatteryManager.BATTERY_STATUS_UNKNOWN);

                switch (plugged) {
                    case BatteryManager.BATTERY_PLUGGED_AC:
                        setStatusResponse(sr.getString(ai.saiy.android.R.string.ac_charging));
                        break;
                    case BatteryManager.BATTERY_PLUGGED_USB:
                        setStatusResponse(sr.getString(ai.saiy.android.R.string.usb_charging));
                        break;
                    default:
                        setStatusResponse(sr.getString(ai.saiy.android.R.string.charging));
                        break;
                }
                break;

            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                setStatusResponse(sr.getString(ai.saiy.android.R.string.discharging));
                break;
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                setStatusResponse(sr.getString(ai.saiy.android.R.string.discharging));
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                setStatusResponse(sr.getString(ai.saiy.android.R.string.fully_charged));
                break;
            case BatteryManager.BATTERY_STATUS_UNKNOWN:
                setStatusResponse(sr.getString(ai.saiy.android.R.string.currently_indeterminable));
                break;
            default:
                setStatusResponse(sr.getString(ai.saiy.android.R.string.currently_indeterminable));
                break;
        }

        sr.reset();

    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "batteryIntent: null");
        }
        setAccessFailure();
    }
}
 
Example 18
Source File: PDevice.java    From PHONK with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Gets a callback each time there is a change in the battery status
 *
 * @param callback
 * @status TODO_EXAMPLE
 */
@PhonkMethod
public void battery(final ReturnInterface callback) {
    batteryReceiver = new BroadcastReceiver() {
        int scale = -1;
        int level = -1;
        int voltage = -1;
        int temp = -1;
        boolean isConnected = false;
        private int status;
        private final boolean alreadyKilled = false;

        @Override
        public void onReceive(Context context, Intent intent) {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
            // isCharging =
            // intent.getBooleanExtra(BatteryManager.EXTRA_PLUGGED, false);
            // status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            status = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

            if (status == BatteryManager.BATTERY_PLUGGED_AC) {
                isConnected = true;
            } else isConnected = status == BatteryManager.BATTERY_PLUGGED_USB;

            ReturnObject o = new ReturnObject();

            o.put("level", level);
            o.put("temperature", temp);
            o.put("connected", isConnected);
            o.put("scale", scale);
            o.put("temperature", temp);
            o.put("voltage", voltage);

            callback.event(o);
        }
    };

    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    getContext().registerReceiver(batteryReceiver, filter);
}
 
Example 19
Source File: OBBatteryReceiver.java    From GLEXP-Team-onebillion with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive (Context context, Intent intent)
{
    if (MainActivity.mainActivity == null) return;
    //
    Boolean isNowCharging = false;
    //
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    int chargePlug = -1;
    if(status != -1)
    {
        chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        //
        // BatteryManager.BATTERY_STATUS_CHARGING cannot be trusted. It happens on occasion, but it's thrown while the tablet is not charging
        isNowCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL )&& chargePlug > 0;
        //
        usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
        //
        batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        batteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        //
        MainActivity.log("OBBatteryReceiver.onReceive: battery level : " + batteryLevel + ", battery scale: " + batteryScale + " " + (isNowCharging ? "CHARGING " : ""));
    }
    //
    MainActivity.log("OBBatteryReceiver.onReceive: " + printStatus());
    //
    if(MainActivity.mainActivity != null)
    {
        MainActivity.mainActivity.onBatteryStatusReceived(getBatteryLevel(),cablePluggedIn());
    }
    //
    if (OBSystemsManager.sharedManager != null)
    {
        OBSystemsManager.sharedManager.refreshStatus();
        //
        MainActivity.log("OBBatteryReceiver.onReceive: chargePlug flag value: " + chargePlug + ", battery status: " + status);
        //
        Boolean actionIsRequired = !isCharging && isNowCharging;
        isCharging = isNowCharging;
        //
        String chargerType = (usbCharge) ? OBAnalytics.Params.BATTERY_CHARGER_STATE_PLUGGED_USB : (acCharge) ? OBAnalytics.Params.BATTERY_CHARGER_STATE_PLUGGED_AC : "";
        OBAnalyticsManager.sharedManager.batteryState(getBatteryLevel(), isCharging, chargerType);
        OBAnalyticsManager.sharedManager.deviceGpsLocation();
        OBAnalyticsManager.sharedManager.deviceStorageUse();
        //
        if (actionIsRequired)
        {
            MainActivity.log("OBBatteryReceiver.onReceive: it is now charging and/or plugged in. Action is required");
            //
            if (OBConfigManager.sharedManager.isBackupWhenChargingEnabled())
            {
                if (OBSystemsManager.sharedManager.isBackupRequired())
                {
                    MainActivity.log("OBBatteryReceiver.onReceive: Backup is required. Synchronising time and data.");
                    OBSystemsManager.sharedManager.connectToWifiAndSynchronizeTimeAndData();
                }
                else
                {
                    if (OBConfigManager.sharedManager.isTimeServerEnabled())
                    {
                        MainActivity.log("OBBatteryReceiver.onReceive: Backup is NOT required. Synchronising time");
                        OBSystemsManager.sharedManager.connectToWifiAndSynchronizeTime();
                    }
                    else
                    {
                        MainActivity.log("OBBatteryReceiver.onReceive: Time server is disabled. Suspending time synchronisation");
                    }
                }
            }
            else
            {
                if (OBConfigManager.sharedManager.isTimeServerEnabled())
                {
                    MainActivity.log("OBBatteryReceiver.onReceive: Shouldn't send backup when connecting, just synchronising time");
                    OBSystemsManager.sharedManager.connectToWifiAndSynchronizeTime();
                }
                else
                {
                    MainActivity.log("OBBatteryReceiver.onReceive: Time server is disabled. Suspending time synchronisation");
                }
            }
        }
        else
        {
            MainActivity.log("OBBatteryReceiver.onReceive: State hasn't changed, No action is required for now");
        }
    }
    else
    {
        MainActivity.log("OBBatteryReceiver:onReceive: OBSystemsManager hasn't been created yet. Aborting");
    }
}
 
Example 20
Source File: PowerReceiver.java    From JobSchedulerCompat with Apache License 2.0 4 votes vote down vote up
private static boolean isCharging(Context context) {
    Intent i = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = i.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}