Convert smartspace data sources to callbackFlow

This commit is contained in:
Suphon Thanakornpakapong committed 2022-05-14 02:15:27 +07:00
1 parent c055cd069a
commit 66b4fccce8
4 files changed
+39 -40

No files matched your search

@@ -1,6 +1,5 @@
package app.lawnchair.smartspace.provider
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
@@ -8,35 +7,24 @@ import android.os.BatteryManager
import app.lawnchair.smartspace.model.SmartspaceAction
import app.lawnchair.smartspace.model.SmartspaceScores
import app.lawnchair.smartspace.model.SmartspaceTarget
import app.lawnchair.util.broadcastReceiverFlow
import com.android.launcher3.R
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
class BatteryStatusProvider(context: Context) : SmartspaceDataSource(context, { smartspaceBatteryStatus }) {
private val batteryReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
override val internalTargets = broadcastReceiverFlow(context, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
.map { intent ->
val status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
charging = status == BatteryManager.BATTERY_STATUS_CHARGING
full = status == BatteryManager.BATTERY_STATUS_FULL
level = (100f
val charging = status == BatteryManager.BATTERY_STATUS_CHARGING
val full = status == BatteryManager.BATTERY_STATUS_FULL
val level = (100f
* intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
/ intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100)).toInt()
targetsFlow.value = listOfNotNull(getSmartspaceTarget())
listOfNotNull(getSmartspaceTarget(charging, full, level))
}
}
private var charging = false
private var full = false
private var level = 100
private val targetsFlow = MutableStateFlow(emptyList<SmartspaceTarget>())
override val internalTargets get() = targetsFlow
init {
context.registerReceiver(batteryReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
}
private fun getSmartspaceTarget(): SmartspaceTarget? {
private fun getSmartspaceTarget(charging: Boolean, full: Boolean, level: Int): SmartspaceTarget? {
val title = when {
full -> return null
charging -> context.getString(R.string.smartspace_battery_charging)
@@ -55,8 +43,4 @@ class BatteryStatusProvider(context: Context) : SmartspaceDataSource(context, {
featureType = SmartspaceTarget.FeatureType.FEATURE_CALENDAR
)
}
override fun destroy() {
context.unregisterReceiver(batteryReceiver)
}
}
@@ -12,6 +12,8 @@ import android.service.notification.StatusBarNotification;
import android.util.Log;
import android.view.KeyEvent;
import androidx.core.util.Consumer;
import app.lawnchair.NotificationManager;
import app.lawnchair.util.FlowCollector;
@@ -35,9 +37,9 @@ public class MediaListener extends MediaController.Callback {
private final FlowCollector<List<StatusBarNotification>> mFlowCollector;
private List<StatusBarNotification> mNotifications = Collections.emptyList();
public MediaListener(Context context, Runnable onChange) {
public MediaListener(Context context, Consumer<MediaListener> onChange) {
mContext = context;
mOnChange = onChange;
mOnChange = () -> onChange.accept(this);
NotificationManager notificationManager = NotificationManager.INSTANCE.get(context);
mFlowCollector = new FlowCollector<>(
notificationManager.getNotifications(),
@@ -8,17 +8,22 @@ import app.lawnchair.smartspace.model.SmartspaceAction
import app.lawnchair.smartspace.model.SmartspaceScores
import app.lawnchair.smartspace.model.SmartspaceTarget
import com.android.launcher3.R
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
class NowPlayingProvider(context: Context) : SmartspaceDataSource(context, { smartspaceNowPlaying }) {
private val media = MediaListener(context, this::reload).also { it.onResume() }
private val defaultIcon = Icon.createWithResource(context, R.drawable.ic_music_note)
private val targetsFlow = MutableStateFlow(emptyList<SmartspaceTarget>())
override val internalTargets get() = targetsFlow
override val internalTargets = callbackFlow {
val mediaListener = MediaListener(context) {
trySend(listOfNotNull(getSmartspaceTarget(it)))
}
mediaListener.onResume()
awaitClose { mediaListener.onPause() }
}
private fun getSmartspaceTarget(): SmartspaceTarget? {
private fun getSmartspaceTarget(media: MediaListener): SmartspaceTarget? {
val tracking = media.tracking ?: return null
val title = tracking.info.title ?: return null
@@ -47,12 +52,4 @@ class NowPlayingProvider(context: Context) : SmartspaceDataSource(context, { sma
featureType = SmartspaceTarget.FeatureType.FEATURE_MEDIA
)
}
private fun reload() {
targetsFlow.value = listOfNotNull(getSmartspaceTarget())
}
override fun destroy() {
media.onPause()
}
}
@@ -1,8 +1,14 @@
package app.lawnchair.util
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
@@ -10,3 +16,13 @@ fun <T> Flow<T>.firstBlocking() = runBlocking { first() }
@Composable
fun <T> Flow<T>.collectAsStateBlocking() = collectAsState(initial = firstBlocking())
fun broadcastReceiverFlow(context: Context, filter: IntentFilter) = callbackFlow<Intent> {
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
trySend(intent)
}
}
context.registerReceiver(receiver, filter)
awaitClose { context.unregisterReceiver(receiver) }
}