Skip to content

Commit

Permalink
feat(settings screen): add battery optimization notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunali321 committed May 21, 2023
1 parent ea4247c commit c332760
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

<queries>
<intent>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
package app.revanced.manager.compose.ui.screen

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.PowerManager
import android.provider.Settings
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.BatteryAlert
import androidx.compose.material.icons.outlined.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import app.revanced.manager.compose.R
Expand All @@ -24,6 +44,7 @@ import app.revanced.manager.compose.ui.viewmodel.SettingsViewModel
import dev.olshevski.navigation.reimagined.*
import org.koin.androidx.compose.getViewModel

@SuppressLint("BatteryLife")
@OptIn(ExperimentalMaterial3Api::class, ExperimentalAnimationApi::class)
@Composable
@Stable
Expand All @@ -33,6 +54,11 @@ fun SettingsScreen(
) {
val navController =
rememberNavController<SettingsDestination>(startDestination = SettingsDestination.Settings)

val context = LocalContext.current
val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager
var showBatteryButton by remember { mutableStateOf(!pm.isIgnoringBatteryOptimizations(context.packageName)) }

val settingsSections = listOf(
Triple(
R.string.general,
Expand Down Expand Up @@ -62,7 +88,6 @@ fun SettingsScreen(
)
NavBackHandler(navController)


AnimatedNavHost(
controller = navController
) { destination ->
Expand Down Expand Up @@ -90,8 +115,6 @@ fun SettingsScreen(
)

is SettingsDestination.Settings -> {


Scaffold(
topBar = {
AppTopBar(
Expand All @@ -107,6 +130,32 @@ fun SettingsScreen(
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
AnimatedVisibility(visible = showBatteryButton) {
Card(
onClick = {
context.startActivity(Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
data = Uri.parse("package:${context.packageName}")
})
showBatteryButton = !pm.isIgnoringBatteryOptimizations(context.packageName)
},
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
.clip(RoundedCornerShape(24.dp))
.background(MaterialTheme.colorScheme.tertiaryContainer),
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(16.dp)
) {
Icon(imageVector = Icons.Default.BatteryAlert, contentDescription = null, tint = MaterialTheme.colorScheme.onTertiaryContainer, modifier = Modifier.size(24.dp))
Text(text = stringResource(R.string.battery_optimization_notification), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onTertiaryContainer)
}
}
}
settingsSections.forEach { (titleDescIcon, destination) ->
ListItem(
modifier = Modifier.clickable { navController.navigate(destination) },
Expand All @@ -120,4 +169,4 @@ fun SettingsScreen(
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fun UpdateNotification() {
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
.clip(RoundedCornerShape(16.dp))
.clip(RoundedCornerShape(24.dp))
.background(MaterialTheme.colorScheme.secondaryContainer)
) {
Row(
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@
<string name="update_notifications_description">Dialog on app launch + badges</string>
<string name="changelog">Changelog</string>
<string name="changelog_description">Check out the latest changes in this update</string>
<string name="battery_optimization_notification">Battery optimization must be turned off in order for ReVanced Manager to work correctly in the background. Tap here to turn off.</string>
</resources>

0 comments on commit c332760

Please sign in to comment.