/*
* Copyright (C) 2014 AChep@xda <ynkr.wang@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package com.bullmobi.base.utils.power;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.bullmobi.base.Device;
/**
* Helper class with utils related to power.
*
* @author Artem Chepurnoy
*/
public class PowerUtils {
/**
* @return true is device is plugged at this moment, false otherwise.
* @see #isPlugged(android.content.Intent)
*/
public static boolean isPlugged(@NonNull Context context) {
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
return isPlugged(context.registerReceiver(null, intentFilter));
}
/**
* @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;
}
/**
* Gets Power Manager by context and checks if screen is on.
*
* @return True if screen is on atm, False otherwise.
*/
public static boolean isScreenOn(@NonNull Context context) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return isScreenOn(pm);
}
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static boolean isScreenOn(@NonNull PowerManager pm) {
return Device.hasKitKatWatchApi()
? pm.isInteractive()
: pm.isScreenOn();
}
/**
* @see #isPlugged(android.content.Context)
* @see #isPlugged(android.content.Intent)
*/
@SuppressLint("InlinedApi")
public static int getBatteryLevel(@Nullable Intent intent) {
if (intent == null) {
return 100;
}
final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
final int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
return level * 100 / scale;
}
}