Skip to content

Commit

Permalink
fix(battery_plus): Use ContextCompat on Android to register broadcast…
Browse files Browse the repository at this point in the history
… receiver (#1811)
  • Loading branch information
reidbaker committed May 15, 2023
1 parent d7f9c28 commit b901615
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/battery_plus/battery_plus/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "androidx.core:core-ktx:1.10.1"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.fluttercommunity.plus.battery

import android.annotation.SuppressLint
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.EventChannel
import io.flutter.embedding.engine.plugins.FlutterPlugin
Expand All @@ -20,6 +21,9 @@ import java.util.Locale
import android.os.PowerManager
import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.RECEIVER_NOT_EXPORTED


/** BatteryPlusPlugin */
class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, FlutterPlugin {
Expand Down Expand Up @@ -77,9 +81,15 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
}
}

@SuppressLint("WrongConstant") // Error in ContextCompat for RECEIVER_NOT_EXPORTED
override fun onListen(arguments: Any?, events: EventSink) {
chargingStateChangeReceiver = createChargingStateChangeReceiver(events)
applicationContext?.registerReceiver(chargingStateChangeReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
applicationContext?.let {
ContextCompat.registerReceiver(
it, chargingStateChangeReceiver,
IntentFilter(Intent.ACTION_BATTERY_CHANGED), RECEIVER_NOT_EXPORTED
)
}
val status = getBatteryStatus()
publishBatteryStatus(events, status)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}

0 comments on commit b901615

Please sign in to comment.