Skip to content

Commit b6e4c80

Browse files
committed
ServiceExtensions: BatteryManager Extensions
Signed-off-by: Fung <fython@163.com>
1 parent 4426a78 commit b6e4c80

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

kotlinyan-common/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ android {
2727
dependencies {
2828
compile fileTree(dir: 'libs', include: ['*.jar'])
2929
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
30+
compile 'com.android.support:support-annotations:25.3.1'
3031
}
3132
repositories {
3233
mavenCentral()

kotlinyan-common/src/main/kotlin/moe/feng/kotlinyan/common/ServiceExtensions.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import android.nfc.NfcManager
3535
import android.os.*
3636
import android.os.storage.StorageManager
3737
import android.print.PrintManager
38+
import android.support.annotation.RequiresApi
3839
import android.telecom.TelecomManager
3940
import android.telephony.CarrierConfigManager
4041
import android.telephony.SubscriptionManager
@@ -165,6 +166,8 @@ interface ServiceExtensions {
165166

166167
val Context.systemServices: Services get() = Services(this)
167168

169+
// WindowManager Extensions
170+
168171
val WindowManager.screenHeight: Int
169172
get() = DisplayMetrics().apply { defaultDisplay.getRealMetrics(this) }.heightPixels
170173

@@ -213,6 +216,23 @@ interface ServiceExtensions {
213216
return dpi
214217
}
215218

219+
// BatteryManager Extensions
220+
221+
val BatteryManager.capacity: Int
222+
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) get() = getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
223+
224+
val BatteryManager.chargeCounter: Int
225+
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) get() = getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER)
226+
227+
val BatteryManager.currentAverage: Int
228+
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) get() = getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE)
229+
230+
val BatteryManager.currentNow: Int
231+
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) get() = getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)
232+
233+
val BatteryManager.energyCounter: Long
234+
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) get() = getLongProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER)
235+
216236
class Services(private val context: Context) {
217237

218238
operator fun <T> get(serviceName: String): T = context.getSystemService(serviceName) as T

0 commit comments

Comments
 (0)