Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Show Battery Optimization settings when: #5333

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions android/BOINC/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.IGNORE_BATTERY_OPTIMIZATION_SETTINGS"/>
<!--
Features required for Android TV, consoles, and set-top boxes like Nexus Player, OUYA,
Razer Forge TV, Nvidia SHIELD, etc
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of BOINC.
* http://boinc.berkeley.edu
* Copyright (C) 2022 University of California
* Copyright (C) 2023 University of California
*
* BOINC is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License
Expand All @@ -18,17 +18,25 @@
*/
package edu.berkeley.boinc

import android.app.AlertDialog
import android.app.Dialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import android.os.PowerManager
import android.os.RemoteException
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
Expand All @@ -39,7 +47,6 @@
import edu.berkeley.boinc.rpc.Result
import edu.berkeley.boinc.rpc.RpcClient
import edu.berkeley.boinc.utils.*
import java.util.*
import kotlin.collections.ArrayList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
Expand All @@ -53,15 +60,57 @@
private val mClientStatusChangeRec: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Logging.logVerbose(Logging.Category.GUI_VIEW, "TasksActivity onReceive")

loadData()
}
}
private val ifcsc = IntentFilter("edu.berkeley.boinc.clientstatuschange")

private fun showBatterySaverOptions() {
try {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
val optionName = "batterySaverOptionsShown"

Check warning on line 71 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L69-L71

Added lines #L69 - L71 were not covered by tests
if (optionName !in sharedPreferences) {
sharedPreferences.edit().putBoolean(optionName, true).apply()
val intent = Intent()

Check warning on line 74 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L73-L74

Added lines #L73 - L74 were not covered by tests
if (VERSION.SDK_INT >= VERSION_CODES.M) {
val packageName = requireActivity().packageName
val powerManager =
requireActivity().getSystemService(AppCompatActivity.POWER_SERVICE) as PowerManager

Check warning on line 78 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L76-L78

Added lines #L76 - L78 were not covered by tests
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
intent.data = Uri.parse("package:$packageName")

Check warning on line 81 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L80-L81

Added lines #L80 - L81 were not covered by tests
}
} else {
if (intent.resolveActivity(requireActivity().packageManager) != null) {
intent.action = Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS

Check warning on line 85 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L85

Added line #L85 was not covered by tests
}
}
if (intent.action != null) {
val builder = AlertDialog.Builder(requireActivity())
builder.setTitle("Battery Optimization")
builder.setMessage("This application have been optimized for battery usage. Do you want to open Battery Optimization Settings?")
builder.setPositiveButton("Yes",

Check warning on line 92 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L89-L92

Added lines #L89 - L92 were not covered by tests
DialogInterface.OnClickListener { _, _ ->
startActivity(intent)
})
builder.setNegativeButton("No",

Check warning on line 96 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L94-L96

Added lines #L94 - L96 were not covered by tests
DialogInterface.OnClickListener { dialog, _ ->
dialog.dismiss()
})

Check warning on line 99 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L98-L99

Added lines #L98 - L99 were not covered by tests

val dialog: AlertDialog = builder.create()
dialog.show()

Check warning on line 102 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L101-L102

Added lines #L101 - L102 were not covered by tests
}
}
}
catch (e: Exception) {
Logging.logException(Logging.Category.GUI_VIEW, "showBatterySaverOptions() error: ", e)

Check warning on line 107 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L106-L107

Added lines #L106 - L107 were not covered by tests
}
}

Check warning on line 109 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L109

Added line #L109 was not covered by tests

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Logging.logVerbose(Logging.Category.GUI_VIEW, "TasksFragment onCreateView")

showBatterySaverOptions()

Check warning on line 113 in android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/BOINC/app/src/main/java/edu/berkeley/boinc/TasksFragment.kt#L113

Added line #L113 was not covered by tests
// Inflate the layout for this fragment
val binding = TasksLayoutBinding.inflate(inflater, container, false)
recyclerViewAdapter = TaskRecyclerViewAdapter(this, data)
Expand Down
Loading