Java Code Examples for android.os.BatteryManager#BATTERY_PLUGGED_AC
The following examples show how to use
android.os.BatteryManager#BATTERY_PLUGGED_AC .
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: PolicyManagementFragment.java From android-testdpc with Apache License 2.0 | 6 votes |
/** * 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 2
Source File: EasyBatteryMod.java From easydeviceinfo with Apache License 2.0 | 6 votes |
/** * Gets charging source. * * @return the charging source */ @ChargingVia public final int getChargingSource() { Intent batteryStatus = getBatteryStatusIntent(); int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); switch (chargePlug) { case BatteryManager.BATTERY_PLUGGED_AC: return ChargingVia.AC; case BatteryManager.BATTERY_PLUGGED_USB: return ChargingVia.USB; case BatteryManager.BATTERY_PLUGGED_WIRELESS: return ChargingVia.WIRELESS; default: return ChargingVia.UNKNOWN_SOURCE; } }
Example 3
Source File: DeviceState.java From product-emm with Apache License 2.0 | 6 votes |
/** * Conversion from plugged type int to String can be done through this method. * * @param plugged integer representing the plugged type. * @return String representing the plugged type. */ private String getPlugType(int plugged) { String plugType = UNKNOWN; switch (plugged) { case BatteryManager.BATTERY_PLUGGED_AC: plugType = AC; break; case BatteryManager.BATTERY_PLUGGED_USB: plugType = USB; break; case BatteryManager.BATTERY_PLUGGED_WIRELESS: plugType = WIRELESS; break; } return plugType; }
Example 4
Source File: UploadDocs.java From pearl with Apache License 2.0 | 6 votes |
public static boolean isPhonePluggedIn(Context context){ boolean charging = false; final Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean batteryCharge = status==BatteryManager.BATTERY_STATUS_CHARGING; int chargePlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; if (batteryCharge) charging=true; if (usbCharge) charging=true; if (acCharge) charging=true; return charging; }
Example 5
Source File: ShootActivity.java From TimeLapse with MIT License | 6 votes |
private String getBatteryPercentage() { IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = registerReceiver(null, ifilter); int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL || chargePlug == BatteryManager.BATTERY_PLUGGED_USB || chargePlug == BatteryManager.BATTERY_PLUGGED_AC; String s = ""; if(isCharging) s = "c "; return s + (int)(level / (float)scale * 100) + "%"; }
Example 6
Source File: RunConditionMonitor.java From syncthing-android with Mozilla Public License 2.0 | 5 votes |
@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 7
Source File: AndroidTools.java From sdl_java_suite with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Checks if the usb cable is physically connected or not * Note: the intent here is a sticky intent so registerReceiver is actually a synchronous call and doesn't register a receiver on each call * @param context a context instance * @return boolean value that represents whether the usb cable is physically connected or not */ public static boolean isUSBCableConnected(Context context) { Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); if (intent == null ) { return false; } int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB; }
Example 8
Source File: TypeTranslators.java From under-the-hood with Apache License 2.0 | 5 votes |
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 9
Source File: Utils.java From ForceDoze with GNU General Public License v3.0 | 5 votes |
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 10
Source File: PowerUtils.java From AcDisplay with GNU General Public License v2.0 | 5 votes |
/** * @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 11
Source File: RetroWatchService.java From retrowatch with Apache License 2.0 | 5 votes |
@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); // 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 * 100 / scale, chargingStatus); if(co != null) sendContentsToDevice(co); } }
Example 12
Source File: TelemetryUtils.java From mapbox-events-android with MIT License | 5 votes |
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 13
Source File: PowerMonitor.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
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 14
Source File: HomeScreenActivity.java From BaldPhone with Apache License 2.0 | 5 votes |
@Override public void onReceive(Context context, Intent intent) { if (batteryView != null) { final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); final int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); final int batteryPct = Math.round(level / (float) scale * 100); final int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); final boolean charged = chargePlug == BatteryManager.BATTERY_PLUGGED_AC || chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS || chargePlug == BatteryManager.BATTERY_PLUGGED_USB; batteryView.setLevel(batteryPct, charged); if (lowBatteryAlert) getWindow().setStatusBarColor((batteryPct < D.LOW_BATTERY_LEVEL && !charged) ? ContextCompat.getColor(context, R.color.battery_low) : D.DEFAULT_STATUS_BAR_COLOR); } }
Example 15
Source File: PDevice.java From PHONK with GNU General Public License v3.0 | 4 votes |
/** * 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 16
Source File: BatteryWire.java From tinybus with Apache License 2.0 | 4 votes |
public boolean isPluggedAc() { return getPlugged() == BatteryManager.BATTERY_PLUGGED_AC; }
Example 17
Source File: HostBatteryManager.java From DeviceConnect-Android with MIT License | 4 votes |
/** * バッテリーの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 18
Source File: RetroWatchService.java From retrowatch with Apache License 2.0 | 4 votes |
@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: Inspector.java From batteryhub with Apache License 2.0 | 4 votes |
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 20
Source File: PowerConnectionReceiver.java From batteryhub with Apache License 2.0 | 4 votes |
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action == null) return; boolean isCharging = false; String batteryCharger = ""; if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) { isCharging = true; final Intent mIntent = context.getApplicationContext() .registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); if (mIntent == null) return; int chargePlug = mIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; boolean wirelessCharge = false; if (Build.VERSION.SDK_INT >= 21) { wirelessCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS; } if (acCharge) { batteryCharger = "ac"; EventBus.getDefault().post(new PowerSourceEvent("ac")); } else if (usbCharge) { batteryCharger = "usb"; EventBus.getDefault().post(new PowerSourceEvent("usb")); } else if (wirelessCharge) { batteryCharger = "wireless"; EventBus.getDefault().post(new PowerSourceEvent("wireless")); } } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) { isCharging = false; EventBus.getDefault().post(new PowerSourceEvent("unplugged")); } // Post to subscribers & update notification int batteryRemaining = (int) (Battery.getRemainingBatteryTime(context, isCharging, batteryCharger) / 60); int batteryRemainingHours = batteryRemaining / 60; int batteryRemainingMinutes = batteryRemaining % 60; EventBus.getDefault().post( new BatteryTimeEvent(batteryRemainingHours, batteryRemainingMinutes, isCharging) ); // Notifier.remainingBatteryTimeAlert( // context, // batteryRemainingHours + "h " + batteryRemainingMinutes + "m", isCharging // ); try { // Save a new Battery Session to the mDatabase GreenHubDb database = new GreenHubDb(); LogUtils.logI(TAG, "Getting new session"); database.saveSession(Inspector.getBatterySession(context, intent)); database.close(); } catch (IllegalStateException | RealmMigrationNeededException e) { LogUtils.logE(TAG, "No session was created"); e.printStackTrace(); } }