Java Code Examples for android.os.BatteryManager#BATTERY_STATUS_FULL

The following examples show how to use android.os.BatteryManager#BATTERY_STATUS_FULL . 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: DeviceState.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * Conversion from charging status int to String can be done through this method.
 *
 * @param status integer representing the charging status.
 * @return String representing the charging status.
 */
private String getStatus(int status) {
    String statusString = UNKNOWN;
    switch (status) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
            statusString = CHARGING;
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            statusString = DISCHARGING;
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            statusString = FULL;
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            statusString = NOT_CHARGING;
            break;
    }
    return statusString;
}
 
Example 2
Source File: SubsonicSyncAdapter.java    From Popeens-DSub with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
	String invalidMessage = isNetworkValid();
	if(invalidMessage != null) {
		Log.w(TAG, "Not running sync: " + invalidMessage);
		return;
	}

	// Make sure battery > x% or is charging
	IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
	Intent batteryStatus = context.registerReceiver(null, intentFilter);
	int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
	if (status != BatteryManager.BATTERY_STATUS_CHARGING && status != BatteryManager.BATTERY_STATUS_FULL) {
		int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
		int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

		if ((level / (float) scale) < 0.15) {
			Log.w(TAG, "Not running sync, battery too low");
			return;
		}
	}

	executeSync(context);
}
 
Example 3
Source File: DefaultVideoController.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
private void dealBattery(Intent intent) {
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
            BatteryManager.BATTERY_STATUS_UNKNOWN);
    if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
        // 充电中
        battery.setImageResource(R.drawable.battery_charging);
    } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
        // 充电完成
        battery.setImageResource(R.drawable.battery_full);
    } else {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
        int percentage = (int) (((float) level / scale) * 100);
        if (percentage <= 10) {
            battery.setImageResource(R.drawable.battery_10);
        } else if (percentage <= 20) {
            battery.setImageResource(R.drawable.battery_20);
        } else if (percentage <= 50) {
            battery.setImageResource(R.drawable.battery_50);
        } else if (percentage <= 80) {
            battery.setImageResource(R.drawable.battery_80);
        } else if (percentage <= 100) {
            battery.setImageResource(R.drawable.battery_100);
        }
    }
}
 
Example 4
Source File: RunConditionMonitor.java    From syncthing-android with Mozilla Public License 2.0 5 votes vote down vote up
@TargetApi(16)
private boolean isCharging_API16() {
    Intent batteryIntent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    return status == BatteryManager.BATTERY_STATUS_CHARGING ||
        status == BatteryManager.BATTERY_STATUS_FULL;
}
 
Example 5
Source File: TrojanReceiver.java    From Trojan with Apache License 2.0 5 votes vote down vote up
private void showBatteryState(Intent intent) {
    int status = intent.getIntExtra("status", 0);
    int level = intent.getIntExtra("level", 0);
    String statusResult = "discharging";
    switch (status) {
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            statusResult = "discharging";
            break;
        case BatteryManager.BATTERY_STATUS_CHARGING:
            statusResult = "charging";
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            statusResult = "discharging";
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            statusResult = "discharging";
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            statusResult = "charging";
            break;
    }

    List<String> msgList = new LinkedList<>();
    msgList.add(String.valueOf((level * 1.00 / 100)));
    msgList.add(statusResult);
    Trojan.log(LogConstants.BATTERY_TAG, msgList);
}
 
Example 6
Source File: BatteryIconData.java    From Status with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(BatteryIconData icon, Intent intent) {
    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);

    int iconLevel = (int) (((float) level / scale) * 6) + 1;

    if (status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL)
        iconLevel += 7;

    icon.onIconUpdate(iconLevel);
    icon.onTextUpdate(String.valueOf((int) (((double) level / scale) * 100)) + "%");
}
 
Example 7
Source File: BatteryTileService.java    From SystemUITuner2 with MIT License 5 votes vote down vote up
private void setBat(Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

        mIsCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;

//        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        mBatteryPercent = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

        Tile battery = getQsTile();

        int resId;

        if (mBatteryPercent >= 95) {
             resId = mIsCharging ? R.drawable.ic_battery_charging_full : R.drawable.ic_battery;
        } else if (mBatteryPercent >= 85) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_90 : R.drawable.ic_battery_90;
        } else if (mBatteryPercent >= 70) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_80 : R.drawable.ic_battery_80;
        } else if (mBatteryPercent >= 55) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_60 : R.drawable.ic_battery_60;
        } else if (mBatteryPercent >= 40) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_50 : R.drawable.ic_battery_50;
        } else if (mBatteryPercent >= 25) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_30 : R.drawable.ic_battery_30;
        } else if (mBatteryPercent >= 15) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_20 : R.drawable.ic_battery_20;
        } else {
            resId = mIsCharging ? R.drawable.ic_battery_charging_20 : R.drawable.ic_battery_alert;
        }

        battery.setIcon(Icon.createWithResource(this, resId));
        battery.setLabel(String.valueOf(mBatteryPercent).concat("%"));
        battery.updateTile();
    }
 
Example 8
Source File: OfflinePageUtils.java    From delion with Apache License 2.0 5 votes vote down vote up
private static boolean isPowerConnected(Intent batteryStatus) {
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isConnected = (status == BatteryManager.BATTERY_STATUS_CHARGING
            || status == BatteryManager.BATTERY_STATUS_FULL);
    Log.d(TAG, "Power connected is " + isConnected);
    return isConnected;
}
 
Example 9
Source File: BatteryUtils.java    From SimpleSmsRemote with MIT License 5 votes vote down vote up
/**
 * check if battery is charging
 *
 * @param context app context
 * @return true if battery is charging
 * @throws Exception
 */
public static boolean IsBatteryCharging(Context context) throws Exception {
    Intent batteryStatus = getBatteryStatusIntent(context);
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    if (status == -1)
        throw new Exception("failed to get battery status extra");

    return status == BatteryManager.BATTERY_STATUS_CHARGING ||
            status == BatteryManager.BATTERY_STATUS_FULL;
}
 
Example 10
Source File: MyActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void listing6_20 () {
  // Listing 6-20: Determining battery and charge state information
  IntentFilter batIntentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  Intent battery = registerReceiver(null, batIntentFilter);

  int status = battery.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  boolean isCharging =
    status == BatteryManager.BATTERY_STATUS_CHARGING ||
      status == BatteryManager.BATTERY_STATUS_FULL;
}
 
Example 11
Source File: Requirements.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private boolean checkChargingRequirement(Context context) {
  if (!isChargingRequired()) {
    return true;
  }
  Intent batteryStatus =
      context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  if (batteryStatus == null) {
    return false;
  }
  int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  return status == BatteryManager.BATTERY_STATUS_CHARGING
      || status == BatteryManager.BATTERY_STATUS_FULL;
}
 
Example 12
Source File: SystemUtil.java    From KeyboardView with Apache License 2.0 5 votes vote down vote up
/**
 * 判断手机当前是否在充电状态
 *
 * @param context
 * @return
 */
public static boolean isBatteryCharging(Context context) {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, filter);

    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    if ((status == BatteryManager.BATTERY_STATUS_CHARGING) || (status == BatteryManager.BATTERY_STATUS_FULL)) {
        return true;
    }
    return false;
}
 
Example 13
Source File: Device.java    From OsmGo with MIT License 5 votes vote down vote up
private boolean isCharging() {
  IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  Intent batteryStatus = getContext().registerReceiver(null, ifilter);

  if (batteryStatus != null) {
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
  }
  return false;
}
 
Example 14
Source File: PlayerTopControl.java    From android-jungle-mediaplayer with Apache License 2.0 5 votes vote down vote up
private void updatePowerStatus(Intent intent) {
    float current = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    float total = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);

    View powerIconView = findViewById(R.id.player_power_icon);
    View inChargeView = findViewById(R.id.player_in_charge_icon);
    boolean inChargeNow = status == BatteryManager.BATTERY_STATUS_FULL
            || status == BatteryManager.BATTERY_STATUS_CHARGING;
    inChargeView.setVisibility(inChargeNow ? View.VISIBLE : View.GONE);

    int resId = R.drawable.battery0;
    float percent = current / total;
    if (percent <= 0.1) {
        resId = R.drawable.battery0;
    } else if (percent <= 0.3) {
        resId = R.drawable.battery1;
    } else if (percent <= 0.5) {
        resId = R.drawable.battery2;
    } else if (percent <= 0.8) {
        resId = R.drawable.battery3;
    } else {
        resId = R.drawable.battery4;
    }

    powerIconView.setBackgroundResource(resId);
}
 
Example 15
Source File: BatteryDrawer.java    From meter with Apache License 2.0 4 votes vote down vote up
/**
 * Parse the battery charging status from the battery change intent
 */
public boolean getBatteryIsCharging(Intent intent){
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    return status == BatteryManager.BATTERY_STATUS_CHARGING ||
            status == BatteryManager.BATTERY_STATUS_FULL;
}
 
Example 16
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 17
Source File: LockDeviceService.java    From SecondScreen with Apache License 2.0 4 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    super.onHandleIntent(intent);

    // Close the notification drawer
    Intent closeDrawer = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    sendBroadcast(closeDrawer);

    // Determine current charging status
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = registerReceiver(null, ifilter);
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
            status == BatteryManager.BATTERY_STATUS_FULL;

    // Get current UI mode
    UiModeManager mUiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
    int uiMode = mUiModeManager.getCurrentModeType();

    // Determine current dock state, based on the current UI mode
    boolean isDocked;
    switch(uiMode) {
        case Configuration.UI_MODE_TYPE_DESK:
            isDocked = true;
            break;
        case Configuration.UI_MODE_TYPE_CAR:
            isDocked = true;
            break;
        default:
            isDocked = false;
    }

    // In order to ensure that the device locks itself when the following code is run,
    // we need to temporarily set the lock screen lock after timeout value.
    // For a smooth transition into the daydream, we set this value to one millisecond,
    // locking the device at the soonest opportunity after the transition completes.
    int timeout = Settings.Secure.getInt(getContentResolver(), "lock_screen_lock_after_timeout", 5000);
    if(timeout != 1) {
        SharedPreferences prefMain = U.getPrefMain(this);
        SharedPreferences.Editor editor = prefMain.edit();
        editor.putInt("timeout", Settings.Secure.getInt(getContentResolver(), "lock_screen_lock_after_timeout", 5000));
        editor.apply();

        U.runCommand(this, U.timeoutCommand + "1");
    }

    // Schedule TimeoutService to reset lock screen timeout to original value
    Intent timeoutService = new Intent(this, TimeoutService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 123456, timeoutService, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pendingIntent);

    // If Daydreams is enabled and the device is charging, then lock the device by launching the daydream.
    if(isCharging
            && !U.castScreenActive(this)
            && Settings.Secure.getInt(getContentResolver(), "screensaver_enabled", 0) == 1
            && ((Settings.Secure.getInt(getContentResolver(), "screensaver_activate_on_dock", 0) == 1 && isDocked)
            || Settings.Secure.getInt(getContentResolver(), "screensaver_activate_on_sleep", 0) == 1)) {
        // Send intent to launch the current daydream manually
        Intent lockIntent = new Intent(Intent.ACTION_MAIN);
        lockIntent.setComponent(ComponentName.unflattenFromString("com.android.systemui/.Somnambulator"));
        lockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            startActivity(lockIntent);
        } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
    } else
        // Otherwise, send a power button keystroke to lock the device normally
        U.lockDevice(this);
}
 
Example 18
Source File: RetroWatchService.java    From retrowatch with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	String action = intent.getAction();
	if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
		int plugType = intent.getIntExtra("plugged", 0);
		int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
		
		int chargingStatus = EmergencyObject.BATT_STATE_UNKNOWN;
		if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
			if (plugType > 0) {
				chargingStatus = ((plugType == BatteryManager.BATTERY_PLUGGED_AC) 
						? EmergencyObject.BATT_STATE_AC_CHARGING : EmergencyObject.BATT_STATE_USB_CHARGING);
			}
		} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
			chargingStatus = EmergencyObject.BATT_STATE_DISCHARGING;
		} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
			chargingStatus = EmergencyObject.BATT_STATE_NOT_CHARGING;
		} else if (status == BatteryManager.BATTERY_STATUS_FULL) {
			chargingStatus = EmergencyObject.BATT_STATE_FULL;
		} else {
			chargingStatus = EmergencyObject.BATT_STATE_UNKNOWN;
		}
		
		int level = intent.getIntExtra("level", 0);
		int scale = intent.getIntExtra("scale", 100);
		
		Logs.d("# mBatteryInfoReceiver : level = " + level);
		
		// WARNING: Battery service makes too many broadcast.
		// Process data only when there's change in battery level or status.
		if(mContentManager.getBatteryLevel() == level
				&& mContentManager.getBatteryChargingState() == status)
			return;
		
		ContentObject co = mContentManager.setBatteryInfo(level, chargingStatus);
		if(co != null && level < 10) {
			Logs.d("# mBatteryInfoReceiver - reserve update");
			reserveRemoteUpdate(DEFAULT_UPDATE_DELAY);
		}
	}
}
 
Example 19
Source File: BatteryFragment.java    From DeviceInfo with Apache License 2.0 4 votes vote down vote up
private void getBatteryInfo() {

        if (fabBatteryCharging != null) {
            if (deviceStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
                fabBatteryCharging.setVisibility(View.VISIBLE);
                fabBatteryCharging.setImageResource(R.drawable.ic_battery);
            }

            if (deviceStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
                fabBatteryCharging.setVisibility(View.GONE);
            }

            if (deviceStatus == BatteryManager.BATTERY_STATUS_FULL) {
                fabBatteryCharging.setVisibility(View.GONE);
            }

            if (deviceStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
                fabBatteryCharging.setVisibility(View.GONE);
            }

            if (deviceStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
                fabBatteryCharging.setVisibility(View.GONE);
            }
        }

        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            int counter = 0;
            @Override
            public void run() {
                try {
                    if (counter <= level) {
                        progressBar.setProgress(counter);
                        progressBar.postDelayed(this, 10000);
                        counter++;
                        handler.postDelayed(this, 20);
                    } else {
                        handler.removeCallbacks(this);
                    }
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        };
        handler.postDelayed(runnable, 20);
        tvBatteryTemperature.setText("".concat(String.valueOf(temperature)).concat(mActivity.getResources().getString(R.string.c_symbol)));

        if (Validation.isRequiredField(technology)) {
            tvBatteryType.setText("".concat(technology));
        }

        tvBatteryVoltage.setText("".concat(String.valueOf(voltage).concat("mV")));
        tvBatteryScale.setText("".concat(String.valueOf(scale)));
        tvBatteryLevel.setText("".concat(String.valueOf(level)).concat("%"));

        if (health == 1) {
            tvBatteryHealth.setText(mResources.getString(R.string.unknown));
        } else if (health == 2) {
            tvBatteryHealth.setText(mResources.getString(R.string.good));
        } else if (health == 3) {
            tvBatteryHealth.setText(mResources.getString(R.string.over_heated));
        } else if (health == 4) {
            tvBatteryHealth.setText(mResources.getString(R.string.dead));
        } else if (health == 5) {
            tvBatteryHealth.setText(mResources.getString(R.string.over_voltage));
        } else if (health == 6) {
            tvBatteryHealth.setText(mResources.getString(R.string.failed));
        } else {
            tvBatteryHealth.setText(mResources.getString(R.string.cold));
        }

        if (plugged == 1) {
            tvPowerSource.setText(mResources.getString(R.string.ac_power));
        } else {
            tvPowerSource.setText(mResources.getString(R.string.battery));
        }
    }
 
Example 20
Source File: BatteryWire.java    From tinybus with Apache License 2.0 4 votes vote down vote up
public boolean isCharging() {
	final int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
	return status == BatteryManager.BATTERY_STATUS_CHARGING ||
                  status == BatteryManager.BATTERY_STATUS_FULL;
}