From 7ba796c27a3d7f96a397bab5099ca2e9d1ba20ed Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Sun, 5 Apr 2020 15:42:05 +0200 Subject: [PATCH] Added back PowerManager hacks + quick link to battery optimized apps list --- .../fragments/AdvancedSettingsFragment.kt | 27 +++ .../viewmodels/AdvancedSettingsViewModel.kt | 20 +++ .../org/linphone/utils/PowerManagerUtils.kt | 162 ++++++++++++++++++ .../res/layout/settings_advanced_fragment.xml | 23 +++ app/src/main/res/values/strings.xml | 3 + 5 files changed, 235 insertions(+) create mode 100644 app/src/main/java/org/linphone/utils/PowerManagerUtils.kt diff --git a/app/src/main/java/org/linphone/activities/main/settings/fragments/AdvancedSettingsFragment.kt b/app/src/main/java/org/linphone/activities/main/settings/fragments/AdvancedSettingsFragment.kt index e8e7ef6d0..6ec408252 100644 --- a/app/src/main/java/org/linphone/activities/main/settings/fragments/AdvancedSettingsFragment.kt +++ b/app/src/main/java/org/linphone/activities/main/settings/fragments/AdvancedSettingsFragment.kt @@ -19,6 +19,7 @@ */ package org.linphone.activities.main.settings.fragments +import android.content.ActivityNotFoundException import android.content.Intent import android.net.Uri import android.os.Bundle @@ -34,7 +35,10 @@ import androidx.lifecycle.ViewModelProvider import androidx.navigation.fragment.findNavController import org.linphone.R import org.linphone.activities.main.settings.viewmodels.AdvancedSettingsViewModel +import org.linphone.core.tools.Log +import org.linphone.core.tools.compatibility.DeviceUtils import org.linphone.databinding.SettingsAdvancedFragmentBinding +import org.linphone.utils.PowerManagerUtils class AdvancedSettingsFragment : Fragment() { private lateinit var binding: SettingsAdvancedFragmentBinding @@ -72,6 +76,29 @@ class AdvancedSettingsFragment : Fragment() { } }) + viewModel.backgroundModeEnabled.value = !DeviceUtils.isAppUserRestricted(requireContext()) + + viewModel.goToBatterySettingsEvent.observe(viewLifecycleOwner, Observer { it.consume { + try { + val intent = Intent("android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS") + startActivity(intent) + } catch (anfe: ActivityNotFoundException) { + Log.e("[Advanced Settings] ActivityNotFound exception: ", anfe) + } + } }) + + viewModel.powerManagerSettingsVisibility.value = PowerManagerUtils.getDevicePowerManagerIntent(requireContext()) != null + viewModel.goToPowerManagerSettingsEvent.observe(viewLifecycleOwner, Observer { it.consume { + val intent = PowerManagerUtils.getDevicePowerManagerIntent(requireActivity()) + if (intent != null) { + try { + startActivity(intent) + } catch (se: SecurityException) { + Log.e("[Advanced Settings] Security exception: ", se) + } + } + } }) + viewModel.goToAndroidSettingsEvent.observe(viewLifecycleOwner, Observer { it.consume { val intent = Intent() intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS diff --git a/app/src/main/java/org/linphone/activities/main/settings/viewmodels/AdvancedSettingsViewModel.kt b/app/src/main/java/org/linphone/activities/main/settings/viewmodels/AdvancedSettingsViewModel.kt index add780572..9f59bb897 100644 --- a/app/src/main/java/org/linphone/activities/main/settings/viewmodels/AdvancedSettingsViewModel.kt +++ b/app/src/main/java/org/linphone/activities/main/settings/viewmodels/AdvancedSettingsViewModel.kt @@ -23,6 +23,7 @@ import androidx.lifecycle.MutableLiveData import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.R import org.linphone.activities.main.settings.SettingListenerStub +import org.linphone.mediastream.Version import org.linphone.utils.Event class AdvancedSettingsViewModel : GenericSettingsViewModel() { @@ -45,6 +46,7 @@ class AdvancedSettingsViewModel : GenericSettingsViewModel() { } } val backgroundMode = MutableLiveData() + val backgroundModeEnabled = MutableLiveData() val autoStartListener = object : SettingListenerStub() { override fun onBoolValueChanged(newValue: Boolean) { @@ -91,6 +93,22 @@ class AdvancedSettingsViewModel : GenericSettingsViewModel() { } val logsServerUrl = MutableLiveData() + val goToBatterySettingsListener = object : SettingListenerStub() { + override fun onClicked() { + goToBatterySettingsEvent.value = Event(true) + } + } + val goToBatterySettingsEvent = MutableLiveData>() + val batterySettingsVisibility = MutableLiveData() + + val goToPowerManagerSettingsListener = object : SettingListenerStub() { + override fun onClicked() { + goToPowerManagerSettingsEvent.value = Event(true) + } + } + val goToPowerManagerSettingsEvent = MutableLiveData>() + val powerManagerSettingsVisibility = MutableLiveData() + val goToAndroidSettingsListener = object : SettingListenerStub() { override fun onClicked() { goToAndroidSettingsEvent.value = Event(true) @@ -113,5 +131,7 @@ class AdvancedSettingsViewModel : GenericSettingsViewModel() { deviceName.value = prefs.deviceName remoteProvisioningUrl.value = core.provisioningUri logsServerUrl.value = core.logCollectionUploadServerUrl + + batterySettingsVisibility.value = Version.sdkAboveOrEqual(Version.API23_MARSHMALLOW_60) } } diff --git a/app/src/main/java/org/linphone/utils/PowerManagerUtils.kt b/app/src/main/java/org/linphone/utils/PowerManagerUtils.kt new file mode 100644 index 000000000..cf3f03610 --- /dev/null +++ b/app/src/main/java/org/linphone/utils/PowerManagerUtils.kt @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2010-2020 Belledonne Communications SARL. + * + * This file is part of linphone-android + * (see https://www.linphone.org). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.linphone.utils + +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.content.pm.ResolveInfo +import android.net.Uri + +class PowerManagerUtils { + companion object { + // https://stackoverflow.com/questions/31638986/protected-apps-setting-on-huawei-phones-and-how-to-handle-it + private val POWER_MANAGER_INTENTS = arrayOf( + Intent() + .setComponent( + ComponentName( + "com.miui.securitycenter", + "com.miui.permcenter.autostart.AutoStartManagementActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.letv.android.letvsafe", + "com.letv.android.letvsafe.AutobootManageActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.huawei.systemmanager", + "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.huawei.systemmanager", + "com.huawei.systemmanager.optimize.process.ProtectActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.coloros.safecenter", + "com.coloros.safecenter.permission.startup.StartupAppListActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.coloros.safecenter", + "com.coloros.safecenter.startupapp.StartupAppListActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.oppo.safe", + "com.oppo.safe.permission.startup.StartupAppListActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.iqoo.secure", + "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.iqoo.secure", + "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.vivo.permissionmanager", + "com.vivo.permissionmanager.activity.BgStartUpManagerActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.samsung.android.lool", + "com.samsung.android.sm.ui.battery.BatteryActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.htc.pitroad", + "com.htc.pitroad.landingpage.activity.LandingPageActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.asus.mobilemanager", "com.asus.mobilemanager.MainActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.asus.mobilemanager", + "com.asus.mobilemanager.autostart.AutoStartActivity" + ) + ), + Intent() + .setComponent( + ComponentName( + "com.asus.mobilemanager", + "com.asus.mobilemanager.entry.FunctionActivity" + ) + ) + .setData(Uri.parse("mobilemanager://function/entry/AutoStart")), + Intent() + .setComponent( + ComponentName( + "com.dewav.dwappmanager", + "com.dewav.dwappmanager.memory.SmartClearupWhiteList" + ) + ) + ) + + fun getDevicePowerManagerIntent(context: Context): Intent? { + for (intent in POWER_MANAGER_INTENTS) { + if (isIntentCallable(context, intent)) { + return intent + } + } + return null + } + + private fun isIntentCallable(context: Context, intent: Intent): Boolean { + val list: List = context.packageManager + .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY) + return list.isNotEmpty() + } + } +} diff --git a/app/src/main/res/layout/settings_advanced_fragment.xml b/app/src/main/res/layout/settings_advanced_fragment.xml index cf0d75fb8..d5c71a2c7 100644 --- a/app/src/main/res/layout/settings_advanced_fragment.xml +++ b/app/src/main/res/layout/settings_advanced_fragment.xml @@ -74,6 +74,7 @@ + + + + + + Logs upload server URL Do not edit unless you know what you are doing! + Android + Battery optimization settings + Power manager settings Android app settings