Ask user to disable battery optimisation

on the main activity if needed
This commit is contained in:
sim 2023-03-26 17:32:18 +02:00
parent cc22abe17a
commit 09e79a7c3a
3 changed files with 72 additions and 4 deletions

View File

@ -3,12 +3,16 @@ package org.unifiedpush.distributor.nextpush.activities
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.os.PowerManager
import android.provider.Settings
import android.util.Log import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.* // ktlint-disable no-wildcard-imports import android.widget.* // ktlint-disable no-wildcard-imports
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isGone
import com.google.android.material.card.MaterialCardView
import org.unifiedpush.distributor.nextpush.R import org.unifiedpush.distributor.nextpush.R
import org.unifiedpush.distributor.nextpush.account.Account import org.unifiedpush.distributor.nextpush.account.Account
import org.unifiedpush.distributor.nextpush.account.Account.getAccount import org.unifiedpush.distributor.nextpush.account.Account.getAccount
@ -27,7 +31,6 @@ import java.lang.String.format
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private lateinit var listView: ListView private lateinit var listView: ListView
private var showLogout = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -41,11 +44,15 @@ class MainActivity : AppCompatActivity() {
findViewById<TextView>(R.id.main_account_desc).text = findViewById<TextView>(R.id.main_account_desc).text =
format(getString(R.string.main_account_desc), getAccount(this)?.name) format(getString(R.string.main_account_desc), getAccount(this)?.name)
showLogout = true
invalidateOptionsMenu() invalidateOptionsMenu()
RestartWorker.startPeriodic(this) RestartWorker.startPeriodic(this)
} }
override fun onStart() {
super.onStart()
showOptimisationWarning()
}
override fun onWindowFocusChanged(hasFocus: Boolean) { override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus) super.onWindowFocusChanged(hasFocus)
if (hasFocus) { if (hasFocus) {
@ -73,6 +80,17 @@ class MainActivity : AppCompatActivity() {
} }
} }
private fun showOptimisationWarning() {
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
findViewById<MaterialCardView>(R.id.card_battery_optimization).isGone = false
findViewById<Button>(R.id.button_disable_optimisation).setOnClickListener {
startActivity(Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS))
}
} else {
findViewById<MaterialCardView>(R.id.card_battery_optimization).isGone = true
}
}
private fun restart() { private fun restart() {
Log.d(TAG, "Restarting the Listener") Log.d(TAG, "Restarting the Listener")
FailureHandler.clearFails() FailureHandler.clearFails()

View File

@ -1,11 +1,59 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"> app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.google.android.material.card.MaterialCardView
android:id="@+id/card_battery_optimization"
android:clickable="true"
android:focusable="true"
android:checkable="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="16dp"
android:layout_margin="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="16dp"
android:paddingHorizontal="16dp"
android:paddingBottom="0dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/card_disable_optimisation_description" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/button_disable_optimisation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@string/button_disable_optimisation"
style="?attr/borderlessButtonStyle" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<TextView <TextView
android:id="@+id/main_account_title" android:id="@+id/main_account_title"
android:layout_width="0dp" android:layout_width="0dp"
@ -16,7 +64,7 @@
android:text="@string/main_account_title" android:text="@string/main_account_title"
android:textAppearance="@style/TextAppearance.AppCompat.Large" android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toBottomOf="@id/card_battery_optimization" />
<TextView <TextView
android:id="@+id/main_account_desc" android:id="@+id/main_account_desc"

View File

@ -37,4 +37,6 @@
<string name="login_hint_nextcloud_root_url">Nextcloud root url</string> <string name="login_hint_nextcloud_root_url">Nextcloud root url</string>
<string name="login_button_sign_in">Sign in</string> <string name="login_button_sign_in">Sign in</string>
<string name="login_show_password_img_description">Show password</string> <string name="login_show_password_img_description">Show password</string>
<string name="button_disable_optimisation">Disable optimisation</string>
<string name="card_disable_optimisation_description">To ensure the app functions properly, it is important to disable battery optimization. This will prevent the app from being put to sleep and causing delays in notifications.</string>
</resources> </resources>