Skip to content

Commit

Permalink
Update battery broadcast receiver
Browse files Browse the repository at this point in the history
Fix battery pct receiver and show proper user state
  • Loading branch information
cp-megh-l committed Jun 20, 2024
2 parents 4c10b9e + 68441a5 commit 3ce744e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 23 deletions.
9 changes: 0 additions & 9 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@
tools:node="remove" />
</provider>

<receiver
android:name=".domain.receiver.BatteryBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
<action android:name="android.intent.action.BATTERY_OKAY" />
</intent-filter>
</receiver>

<receiver
android:name=".domain.receiver.BootCompleteReceiver"
android:exported="true">
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/canopas/yourspace/YourSpaceApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.canopas.yourspace
import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import androidx.hilt.work.HiltWorkerFactory
import androidx.lifecycle.DefaultLifecycleObserver
Expand All @@ -17,6 +19,7 @@ import com.canopas.yourspace.domain.fcm.FcmRegisterWorker
import com.canopas.yourspace.domain.fcm.YOURSPACE_CHANNEL_GEOFENCE
import com.canopas.yourspace.domain.fcm.YOURSPACE_CHANNEL_MESSAGES
import com.canopas.yourspace.domain.fcm.YOURSPACE_CHANNEL_PLACES
import com.canopas.yourspace.domain.receiver.BatteryBroadcastReceiver
import com.google.android.libraries.places.api.Places
import com.google.firebase.crashlytics.FirebaseCrashlytics
import dagger.hilt.android.HiltAndroidApp
Expand Down Expand Up @@ -55,11 +58,26 @@ class YourSpaceApplication :
authService.addListener(this)
setNotificationChannel()

registerBatteryBroadcastReceiver()

if (userPreferences.currentUser != null) {
geoFenceRepository.init()
}
}

override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
unregisterReceiver(BatteryBroadcastReceiver(authService))
}

private fun registerBatteryBroadcastReceiver() {
val filter = IntentFilter().apply {
addAction(Intent.ACTION_BATTERY_CHANGED)
}
val batteryBroadcastReceiver = BatteryBroadcastReceiver(authService)
registerReceiver(batteryBroadcastReceiver, filter)
}

private fun setNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ class BatteryBroadcastReceiver @Inject constructor(
) : BroadcastReceiver() {

override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == Intent.ACTION_BATTERY_OKAY || intent?.action == Intent.ACTION_BATTERY_LOW) {
if (intent?.action == Intent.ACTION_BATTERY_CHANGED) {
CoroutineScope(Dispatchers.IO).launch {
try {
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
val batteryPct = level / scale.toFloat() * 100
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
val batteryPct = level.toFloat()
authService.updateBatteryStatus(batteryPct)
} catch (e: Exception) {
Timber.e(e, "Failed to update battery status")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.canopas.yourspace.R
Expand All @@ -33,7 +32,7 @@ fun UserBatteryStatus(
}

val color = if (batteryPrc > 70f) {
Color.Green
AppTheme.colorScheme.successColor
} else if (batteryPrc > 30f) {
AppTheme.colorScheme.permissionWarning
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImagePainter
Expand Down Expand Up @@ -68,7 +69,7 @@ fun SelectedUserDetail(userInfo: UserInfo?, onDismiss: () -> Unit, onTapTimeline
) {
MemberProfileView(user.profile_image, user.firstChar, userInfo.user)

MemberInfoView(userName = user.fullName, userInfo.location) { onTapTimeline() }
MemberInfoView(user = user, userInfo.location) { onTapTimeline() }
}

Row(
Expand Down Expand Up @@ -132,10 +133,24 @@ private fun MemberProfileView(profileUrl: String?, name: String, user: ApiUser?)
}

@Composable
private fun MemberInfoView(userName: String, location: ApiLocation?, onTapTimeline: () -> Unit) {
private fun MemberInfoView(user: ApiUser, location: ApiLocation?, onTapTimeline: () -> Unit) {
val context = LocalContext.current
var address by remember { mutableStateOf("") }
val time = timeAgo(location?.created_at ?: 0)
val userStateText = if (user.noNetwork) {
stringResource(R.string.map_selected_user_item_no_network_state)
} else if (user.locationPermissionDenied) {
stringResource(R.string.map_selected_user_item_location_off_state)
} else {
stringResource(R.string.map_selected_user_item_online_state)
}
val userStateTextColor = if (user.noNetwork) {
AppTheme.colorScheme.textSecondary
} else if (user.locationPermissionDenied) {
AppTheme.colorScheme.alertColor
} else {
AppTheme.colorScheme.successColor
}

LaunchedEffect(location) {
withContext(Dispatchers.IO) {
Expand All @@ -145,11 +160,17 @@ private fun MemberInfoView(userName: String, location: ApiLocation?, onTapTimeli
}
Column(modifier = Modifier.padding(start = 16.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = userName,
style = AppTheme.appTypography.subTitle1.copy(color = AppTheme.colorScheme.textPrimary),
modifier = Modifier.padding(bottom = 16.dp)
)
Column {
Text(
text = user.fullName,
style = AppTheme.appTypography.subTitle1.copy(color = AppTheme.colorScheme.textPrimary),
modifier = Modifier.padding(bottom = 4.dp)
)
Text(
text = userStateText,
style = AppTheme.appTypography.caption.copy(color = userStateTextColor)
)
}

Spacer(modifier = Modifier.weight(1f))
ActionIconButton(
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
<string name="map_user_item_location_updated_hours_ago">%s hr ago</string>
<string name="map_user_item_location_updated_since_days">Since %s</string>
<string name="map_user_item_location_unknown">Location not found</string>
<string name="map_selected_user_item_no_network_state">No network or phone off</string>
<string name="map_selected_user_item_location_off_state">Location off</string>
<string name="map_selected_user_item_online_state">Online</string>

<string name="member_detail_location_history">Location History</string>
<string name="member_detail_empty_location_history">No location history found!</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ data class ApiUser(
val firstChar: String get() = fullName.trim().firstOrNull()?.toString() ?: "?"

@get:Exclude
val noNetwork: Boolean get() = state != USER_STATE_NO_NETWORK_OR_PHONE_OFF
val noNetwork: Boolean get() = state == USER_STATE_NO_NETWORK_OR_PHONE_OFF

@get:Exclude
val locationPermissionDenied: Boolean get() = state == USER_STATE_LOCATION_PERMISSION_DENIED
Expand Down

0 comments on commit 3ce744e

Please sign in to comment.