Skip to content

Commit 76d20b9

Browse files
committed
1. Added new method for getting battery status.
1 parent e3de7f0 commit 76d20b9

File tree

1 file changed

+62
-17
lines changed

1 file changed

+62
-17
lines changed

app/src/main/java/com/amit/utilities/Utils.java

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.amit.utilities;
22

33
import android.Manifest;
4+
import android.annotation.SuppressLint;
45
import android.app.Activity;
56
import android.content.Context;
7+
import android.content.Intent;
8+
import android.content.IntentFilter;
69
import android.content.pm.PackageManager;
710
import android.content.res.Resources;
811
import android.content.res.TypedArray;
@@ -13,6 +16,7 @@
1316
import android.graphics.Typeface;
1417
import android.graphics.drawable.BitmapDrawable;
1518
import android.graphics.drawable.Drawable;
19+
import android.os.BatteryManager;
1620
import android.os.Build;
1721
import android.os.Environment;
1822
import android.support.annotation.AttrRes;
@@ -42,6 +46,8 @@
4246
/**
4347
* https://github.com/jaydeepw/android-utils/tree/master/Utils
4448
**/
49+
@SuppressLint("HardwareIds")
50+
@SuppressWarnings("unused")
4551
public class Utils
4652
{
4753
private static final String TAG = Utils.class.getSimpleName();
@@ -54,7 +60,7 @@ public class Utils
5460
* @return - true or false
5561
* if sd card available then will return true
5662
* else will return false
57-
**/
63+
**/
5864
@CheckResult
5965
public static boolean isSdCardMounted()
6066
{
@@ -79,7 +85,7 @@ public static boolean isSdCardMounted()
7985
*
8086
* @return - it will return IMEI number if permission granted
8187
* else if no permission granted then will return empty string.
82-
**/
88+
**/
8389
@CheckResult
8490
public static String getIMEINumber(Context context)
8591
{
@@ -214,7 +220,7 @@ public static SpannableStringBuilder toBold(String sourceText)
214220
* Pass null to bold entire string.
215221
*
216222
* @return - {@link android.text.SpannableString} in Bold TypeFace
217-
**/
223+
**/
218224
public static SpannableStringBuilder toBold(String string, String subString)
219225
{
220226
try
@@ -263,7 +269,7 @@ public static SpannableStringBuilder toBold(String string, String subString)
263269
* this method will hide the keyboard
264270
*
265271
* @param context - context of the application
266-
**/
272+
**/
267273
public static void hideKeyboard(Context context)
268274
{
269275
try
@@ -291,7 +297,7 @@ public static void hideKeyboard(Context context)
291297
* @param stringToHash - string to convert to hash.
292298
*
293299
* @return string converted to hash value.
294-
**/
300+
**/
295301
public static String getSha512Hash(String stringToHash)
296302
{
297303
try
@@ -319,7 +325,7 @@ public static String getSha512Hash(String stringToHash)
319325
* @param dataToHash - byte array to convert to hash value
320326
*
321327
* @return string converted into hash value.
322-
**/
328+
**/
323329
public static String getSha512Hash(byte[] dataToHash)
324330
{
325331
MessageDigest md = null;
@@ -353,7 +359,7 @@ public static String getSha512Hash(byte[] dataToHash)
353359
* @param id - drawable id
354360
*
355361
* @return returns drawable
356-
**/
362+
**/
357363
public static Drawable getDrawable(@NonNull Context context, int id)
358364
{
359365
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
@@ -374,7 +380,7 @@ public static Drawable getDrawable(@NonNull Context context, int id)
374380
* @param id - id of the color resource
375381
*
376382
* @return int - color in integer.
377-
**/
383+
**/
378384
public static int getColorWrapper(@NonNull Context context, @ColorRes int id)
379385
{
380386
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
@@ -398,7 +404,7 @@ public static int getColorWrapper(@NonNull Context context, @ColorRes int id)
398404
* @param minutes - minutes to convert
399405
*
400406
* @return String with time appended with AM/PM
401-
**/
407+
**/
402408
public static String getTimeWithAMPM(int hours, int minutes)
403409
{
404410
try
@@ -441,7 +447,7 @@ else if (hours == 12)
441447
* @param dp - dp to convert into pixels
442448
*
443449
* @return - pixels in integer form
444-
**/
450+
**/
445451
public static int dpToPx(Context context, int dp)
446452
{
447453
if (xdpi == Float.MIN_VALUE)
@@ -479,7 +485,7 @@ public static int getThemeAttrColor(@NonNull Context context, @AttrRes int attri
479485
* @param dp - dp to convert into pixels
480486
*
481487
* @return - pixels in integer form
482-
**/
488+
**/
483489
public static float convertDpToPixel(float dp, Context context)
484490
{
485491
Resources resources = context.getResources();
@@ -495,7 +501,7 @@ public static float convertDpToPixel(float dp, Context context)
495501
* @param px - pixels to be convert into dp
496502
*
497503
* @return - dp in float form
498-
**/
504+
**/
499505
public static float convertPixelsToDp(float px, Context context)
500506
{
501507
Resources resources = context.getResources();
@@ -509,7 +515,7 @@ public static float convertPixelsToDp(float px, Context context)
509515
*
510516
* @param drawable - drawable to be converted into bitmap
511517
* @return bitmap
512-
**/
518+
**/
513519
public static Bitmap drawableToBitmap (Drawable drawable)
514520
{
515521
if (drawable instanceof BitmapDrawable)
@@ -536,7 +542,7 @@ public static Bitmap drawableToBitmap (Drawable drawable)
536542
* @param dpValue - dpValue to be convert into pixels
537543
*
538544
* @return - pixels in integer form
539-
**/
545+
**/
540546
public static int dp2px(Context context, float dpValue)
541547
{
542548
final float scale = context.getResources().getDisplayMetrics().density;
@@ -551,7 +557,7 @@ public static int dp2px(Context context, float dpValue)
551557
* @param pxValue - pxValue to be convert into dp
552558
*
553559
* @return - dp in float form
554-
**/
560+
**/
555561
public static int px2dp(Context context, float pxValue)
556562
{
557563
final float scale = context.getResources().getDisplayMetrics().density;
@@ -564,12 +570,17 @@ public static int px2dp(Context context, float pxValue)
564570
*
565571
* @param context - context of the application
566572
* @return size of the screen as Point
567-
**/
573+
**/
568574
public static Point getScreenSize(Context context)
569575
{
570576
Point point = new Point();
571577
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
572-
wm.getDefaultDisplay().getSize(point);
578+
579+
if (wm != null)
580+
{
581+
wm.getDefaultDisplay().getSize(point);
582+
}
583+
573584
return point;
574585
}
575586

@@ -606,4 +617,38 @@ public static String leftPadding(String strText, int length)
606617
{
607618
return String.format("%" + length + "." + length + "s", strText);
608619
}
620+
621+
/**
622+
* 2018 September 18 - Tuesday - 04:54 PM
623+
* get battery percentage method
624+
*
625+
* this method will get the percentage of battery remaining
626+
*
627+
* @param context - context of the application
628+
* @return battery percentage in int or 0
629+
**/
630+
private static int getBatteryPercentage(Context context)
631+
{
632+
try
633+
{
634+
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
635+
Intent batteryStatus = context.registerReceiver(null, intentFilter);
636+
637+
int level = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1;
638+
int scale = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;
639+
640+
float batteryPercentage = level / (float) scale;
641+
int batteryLevel = (int) (batteryPercentage * 100);
642+
643+
Log.e(TAG, "getBatteryPercentage: current battery level is: " + batteryLevel);
644+
return batteryLevel;
645+
646+
}
647+
catch (Exception e)
648+
{
649+
Log.e(TAG, "getBatteryPercentage: exception while getting battery percentage:\n");
650+
e.printStackTrace();
651+
return 0;
652+
}
653+
}
609654
}

0 commit comments

Comments
 (0)