HaloUtils { @Api(2.0) public static boolean isCharging(@NonNull Context context, @Nullable Intent intent) { Intent finalIntent = intent; if(finalIntent == null){ finalIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); assert finalIntent != null; } int plugged = finalIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB; if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { isPlugged = isPlugged || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS; } return isPlugged; } private  HaloUtils(); @Api(1.0) static boolean isNetworkConnected(Context context); @Api(2.0) static boolean isCharging(@NonNull Context context, @Nullable Intent intent); @Api(1.0) static void hideKeyboard(@Nullable View focusView); @Api(1.0) static void postUI(Runnable runnable); @Api(1.0) static boolean isAvailableForVersion(int sdkVersion); @Api(1.0) @SuppressWarnings("deprecation") static Drawable getDrawable(Context context, @DrawableRes int resource); @Api(1.0) static Uri getUriFromResource(Context context, int resourceId); @Api(1.0) static boolean hasFlag(int flags, int optionFlag); @Api(1.0) static int getArgb(String hexadecimal); @Api(1.0) static boolean isColor(String color); @Api(2.0) static String sha1(@NonNull String origin); @Api(2.0) static String convertToHex(byte[] data); @NonNull @Api(1.3) static String getEventName(@NonNull Context context, @NonNull String eventName); @Api(2.0) static String bytesToHex(@NonNull byte[] bytes); @Api(2.0) static byte[] hexToBytes(@NonNull String data); @Api(2.5) static boolean isServiceRunning(Context context, Class<?> serviceClass);  }
@Test public void thatIsChargingCheckIsTrue() { Intent intent = new Intent().putExtra(BatteryManager.EXTRA_PLUGGED, 1); assertThat(HaloUtils.isCharging(mContext, intent)).isTrue(); }
@Test public void thatIsChargingCheckIsFalswe() { Intent intent = new Intent().putExtra(BatteryManager.EXTRA_PLUGGED, -1); assertThat(HaloUtils.isCharging(mContext, intent)).isFalse(); }