add power manager and improve batteryoptim checker

This commit is contained in:
IndusAryan 2024-02-16 18:43:27 +05:30
parent 853ce22d69
commit dd9030cbe5
7 changed files with 69 additions and 28 deletions

View file

@ -14,7 +14,6 @@
<uses-permission android:name="com.android.providers.tv.permission.WRITE_EPG_DATA" /> <!-- Used for Android TV watch next --> <uses-permission android:name="com.android.providers.tv.permission.WRITE_EPG_DATA" /> <!-- Used for Android TV watch next -->
<uses-permission android:name="android.permission.UPDATE_PACKAGES_WITHOUT_USER_ACTION" /> <!-- Used for updates without prompt --> <uses-permission android:name="android.permission.UPDATE_PACKAGES_WITHOUT_USER_ACTION" /> <!-- Used for updates without prompt -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <!-- Used for update service --> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <!-- Used for update service -->
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<!-- Required for getting arbitrary Aniyomi packages --> <!-- Required for getting arbitrary Aniyomi packages -->

View file

@ -10,7 +10,6 @@ import android.graphics.Rect
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.provider.Settings
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log import android.util.Log
import android.view.KeyEvent import android.view.KeyEvent
@ -24,7 +23,6 @@ import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.IdRes import androidx.annotation.IdRes
import androidx.annotation.MainThread import androidx.annotation.MainThread
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
@ -1205,13 +1203,8 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
showToast(txt(R.string.unable_to_inflate, t.message ?: ""), Toast.LENGTH_LONG) showToast(txt(R.string.unable_to_inflate, t.message ?: ""), Toast.LENGTH_LONG)
null null
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !isAppOptimised && !isTrueTvSettings()
&& !isTvSettings() && !isEmulatorSettings()) {
showBatteryOptimizationDialog()
}
changeStatusBarState(isEmulatorSettings()) changeStatusBarState(isEmulatorSettings())
// Automatically enable jsdelivr if cant connect to raw.githubusercontent.com // Automatically enable jsdelivr if cant connect to raw.githubusercontent.com
if (this.getKey<Boolean>(getString(R.string.jsdelivr_proxy_key)) == null && isNetworkAvailable()) { if (this.getKey<Boolean>(getString(R.string.jsdelivr_proxy_key)) == null && isNetworkAvailable()) {
main { main {
@ -1783,20 +1776,4 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
false false
} }
} }
@RequiresApi(Build.VERSION_CODES.M)
private fun showBatteryOptimizationDialog() {
AlertDialog.Builder(this)
.setTitle(R.string.battery_dialog_title)
.setMessage(R.string.battery_dialog_message)
.setPositiveButton(R.string.ok) { _, _ ->
isAppOptimised = true
val intent = Intent()
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
intent.data = Uri.fromParts("package", packageName, null)
startActivity(intent)
}
.setNegativeButton(R.string.cancel, null)
.show()
}
} }

View file

@ -66,6 +66,7 @@ import com.lagradost.cloudstream3.utils.AppUtils.html
import com.lagradost.cloudstream3.utils.AppUtils.isCastApiAvailable import com.lagradost.cloudstream3.utils.AppUtils.isCastApiAvailable
import com.lagradost.cloudstream3.utils.AppUtils.loadCache import com.lagradost.cloudstream3.utils.AppUtils.loadCache
import com.lagradost.cloudstream3.utils.AppUtils.openBrowser import com.lagradost.cloudstream3.utils.AppUtils.openBrowser
import com.lagradost.cloudstream3.utils.BatteryOptimizationChecker
import com.lagradost.cloudstream3.utils.ExtractorLink import com.lagradost.cloudstream3.utils.ExtractorLink
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialogInstant import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialogInstant
@ -645,6 +646,8 @@ open class ResultFragmentPhone : FullScreenPlayer() {
), ),
null null
) { click -> ) { click ->
BatteryOptimizationChecker.openBatteryOptimizationSettings(requireContext())
when (click.action) { when (click.action) {
DOWNLOAD_ACTION_DOWNLOAD -> { DOWNLOAD_ACTION_DOWNLOAD -> {
viewModel.handleAction( viewModel.handleAction(

View file

@ -19,6 +19,7 @@ import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.google.android.material.appbar.AppBarLayout import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.MaterialToolbar import com.google.android.material.appbar.MaterialToolbar
import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.databinding.MainSettingsBinding import com.lagradost.cloudstream3.databinding.MainSettingsBinding
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
@ -155,6 +156,11 @@ class SettingsFragment : Fragment() {
return getLayoutInt() == 2 return getLayoutInt() == 2
} }
// phone exclusive
fun isTruePhone(): Boolean {
return !isTrueTvSettings() && !isTvSettings() && context?.isEmulatorSettings() != true
}
private fun Context.isAutoTv(): Boolean { private fun Context.isAutoTv(): Boolean {
val uiModeManager = getSystemService(Context.UI_MODE_SERVICE) as UiModeManager? val uiModeManager = getSystemService(Context.UI_MODE_SERVICE) as UiModeManager?
// AFT = Fire TV // AFT = Fire TV

View file

@ -0,0 +1,57 @@
package com.lagradost.cloudstream3.utils
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.PowerManager
import android.provider.Settings
import android.util.Log
import androidx.appcompat.app.AlertDialog
import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.BuildConfig
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.ui.settings.SettingsFragment
object BatteryOptimizationChecker {
private const val packageName = BuildConfig.APPLICATION_ID
private fun isAppRestricted(context: Context?): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null) {
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
return !powerManager.isIgnoringBatteryOptimizations(context.packageName)
}
return false // below Marshmallow, it's always unrestricted
}
fun openBatteryOptimizationSettings(context: Context) {
if (isPhoneAndRestricted()) {
try {
showBatteryOptimizationDialog(context)
} catch (e: Exception) {
Log.e("PowerManagerAPI", "Error showing battery optimization dialog", e)
}
}
}
private fun showBatteryOptimizationDialog(context: Context) {
AlertDialog.Builder(context)
.setTitle(R.string.battery_dialog_title)
.setMessage(R.string.battery_dialog_message)
.setPositiveButton(R.string.ok) { _, _ ->
val intent = Intent()
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
intent.data = Uri.fromParts("package", packageName, null)
context.startActivity(intent, Bundle())
}
.setNegativeButton(R.string.cancel, null)
.show()
}
private fun isPhoneAndRestricted(): Boolean {
return SettingsFragment.isTruePhone() && isAppRestricted(context)
}
}

View file

@ -59,7 +59,6 @@ import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipDrawable import com.google.android.material.chip.ChipDrawable
import com.google.android.material.chip.ChipGroup import com.google.android.material.chip.ChipGroup
import com.lagradost.cloudstream3.CommonActivity.activity import com.lagradost.cloudstream3.CommonActivity.activity
import com.lagradost.cloudstream3.MainActivity
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.ui.result.UiImage import com.lagradost.cloudstream3.ui.result.UiImage

View file

@ -650,8 +650,8 @@
<string name="yes">Yes</string> <string name="yes">Yes</string>
<string name="no">No</string> <string name="no">No</string>
<string name="ok">OK</string> <string name="ok">OK</string>
<string name="battery_dialog_title">Battery Optimization</string> <string name="battery_dialog_title"> ◉ Disable Battery optimization</string>
<string name="battery_dialog_message">To ensure proper Downloads and Notifications, please set Battery Optimization for CloudStream app to Unrestricted.</string> <string name="battery_dialog_message">\nTo ensure proper Downloads and Notifications, please set battery optimization for CloudStream app to UNRESTRICTED.</string>
<string name="update_notification_downloading">Downloading app update…</string> <string name="update_notification_downloading">Downloading app update…</string>
<string name="update_notification_installing">Installing app update…</string> <string name="update_notification_installing">Installing app update…</string>
<string name="update_notification_failed">Could not install the new version of the app</string> <string name="update_notification_failed">Could not install the new version of the app</string>