-
Notifications
You must be signed in to change notification settings - Fork 152
VPN-7203 - Add a warning when not running with "unrestricted" background usage permission #11169
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1ef9f6e
VPN-7203 - Part 1: Add a QML API for Androids BatteryOptimization
strseb 6432b57
Part 2: Add a Message Card
strseb eeb8836
Part 3: Remember Popup dismissal
strseb ce4bc49
Linting
strseb d232bda
reset unrelated file
strseb fb6cf4e
Use matts cool new component
strseb 828ae0b
Update src/translations/strings.yaml
strseb 3cd7a6d
Remove unneded line
strseb bbf39a6
Update string
strseb b24ccaf
Update src/translations/strings.yaml
strseb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
android/common/src/main/java/org/mozilla/firefox/qt/common/BatteryOptimizationHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| package org.mozilla.firefox.qt.common | ||
|
|
||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.content.pm.PackageManager | ||
| import android.net.Uri | ||
| import android.os.Build | ||
| import android.os.PowerManager | ||
| import android.provider.Settings | ||
|
|
||
| object BatteryOptimizationHelper { | ||
|
|
||
| @JvmStatic | ||
| fun isIgnoringBatteryOptimizations(context: Context): Boolean { | ||
| return try { | ||
| val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager | ||
| pm.isIgnoringBatteryOptimizations(context.packageName) | ||
| } catch (e: Exception) { true } | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun hasRequestIgnoreBatteryOptimizationsPermission(context: Context): Boolean { | ||
| return try { | ||
| context.packageManager.checkPermission( | ||
| "android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS", | ||
| context.packageName) == PackageManager.PERMISSION_GRANTED | ||
| } catch (e: Exception) { false } | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun getRequestIgnoreBatteryOptimizationsIntent(context: Context): Intent? { | ||
| return try { | ||
| if (hasRequestIgnoreBatteryOptimizationsPermission(context)) { | ||
| Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply { | ||
| data = Uri.parse("package:${context.packageName}") | ||
| } | ||
| } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
| Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { | ||
| data = Uri.parse("package:${context.packageName}") | ||
| } | ||
| } else { | ||
| Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS) | ||
| } | ||
| } catch (e: Exception) { null } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| #include "androidbatteryoptimizer.h" | ||
|
|
||
| #include <QJniObject> | ||
|
|
||
| #include "androidcommons.h" | ||
|
|
||
| constexpr auto BATTERY_HELPER_CLASS = | ||
| "org/mozilla/firefox/qt/common/BatteryOptimizationHelper"; | ||
|
|
||
| // static | ||
| bool AndroidBatteryOptimizer::batteryOptimizationEnabled() { | ||
| QJniObject ctx = AndroidCommons::getActivity(); | ||
| jboolean ignoring = QJniObject::callStaticMethod<jboolean>( | ||
| BATTERY_HELPER_CLASS, "isIgnoringBatteryOptimizations", | ||
| "(Landroid/content/Context;)Z", ctx.object<jobject>()); | ||
| return !ignoring; | ||
| } | ||
|
|
||
| // static | ||
| bool AndroidBatteryOptimizer::canTriggerIntent() { | ||
| QJniObject ctx = AndroidCommons::getActivity(); | ||
| return (bool)QJniObject::callStaticMethod<jboolean>( | ||
| BATTERY_HELPER_CLASS, "hasRequestIgnoreBatteryOptimizationsPermission", | ||
| "(Landroid/content/Context;)Z", ctx.object<jobject>()); | ||
| } | ||
|
|
||
| // static | ||
| void AndroidBatteryOptimizer::triggerBatteryOptimizationIntent() { | ||
| QJniObject ctx = AndroidCommons::getActivity(); | ||
| QJniObject intent = QJniObject::callStaticObjectMethod( | ||
| BATTERY_HELPER_CLASS, "getRequestIgnoreBatteryOptimizationsIntent", | ||
| "(Landroid/content/Context;)Landroid/content/Intent;", | ||
| ctx.object<jobject>()); | ||
| if (!intent.isValid()) { | ||
| return; | ||
| } | ||
| ctx.callMethod<void>("startActivity", "(Landroid/content/Intent;)V", | ||
| intent.object<jobject>()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| #ifndef ANDROIDBATTERYOPTIMIZER_H | ||
| #define ANDROIDBATTERYOPTIMIZER_H | ||
|
|
||
| class AndroidBatteryOptimizer { | ||
| public: | ||
| // true → optimization IS enabled (VPN at risk) | ||
| // false → app is exempted (good) | ||
| static bool batteryOptimizationEnabled(); | ||
|
|
||
| // true → app holds REQUEST_IGNORE_BATTERY_OPTIMIZATIONS; system dialog shown | ||
| // directly | ||
| static bool canTriggerIntent(); | ||
|
|
||
| // Opens battery settings (system dialog or app details depending on | ||
| // permission/SDK) | ||
| static void triggerBatteryOptimizationIntent(); | ||
| }; | ||
|
|
||
| #endif // ANDROIDBATTERYOPTIMIZER_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| #ifndef BATTERYOPTIMIZER_H | ||
| #define BATTERYOPTIMIZER_H | ||
|
|
||
| #include <QObject> | ||
| #include <QQmlEngine> | ||
|
|
||
| #ifdef MZ_ANDROID | ||
| # include "platforms/android/androidbatteryoptimizer.h" | ||
| #endif | ||
|
|
||
| class BatteryOptimizer : public QObject { | ||
| Q_OBJECT | ||
| QML_ELEMENT | ||
| QML_SINGLETON | ||
|
|
||
| Q_PROPERTY(bool batteryOptimizationEnabled READ batteryOptimizationEnabled | ||
| NOTIFY stateChanged) | ||
| Q_PROPERTY(bool canTriggerIntent READ canTriggerIntent NOTIFY stateChanged) | ||
|
|
||
| public: | ||
| bool batteryOptimizationEnabled() const { | ||
| #ifdef MZ_ANDROID | ||
| return AndroidBatteryOptimizer::batteryOptimizationEnabled(); | ||
| #else | ||
| return false; | ||
| #endif | ||
| } | ||
|
|
||
| bool canTriggerIntent() const { | ||
| #ifdef MZ_ANDROID | ||
| return AndroidBatteryOptimizer::canTriggerIntent(); | ||
| #else | ||
| return false; | ||
| #endif | ||
| } | ||
|
|
||
| Q_INVOKABLE void triggerBatteryOptimizationIntent() { | ||
| #ifdef MZ_ANDROID | ||
| AndroidBatteryOptimizer::triggerBatteryOptimizationIntent(); | ||
| #endif | ||
| } | ||
|
|
||
| // Call from QML after returning from the settings screen to refresh bindings. | ||
| Q_INVOKABLE void refreshState() { emit stateChanged(); } | ||
|
|
||
| signals: | ||
| void stateChanged(); | ||
| }; | ||
|
|
||
| #endif // BATTERYOPTIMIZER_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once we set
hasDismissedBatteryOptimizationto true, the warning won't reappear, right? (e.g. if the user dismissed the system dialog)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is the idea :) - The intention is to give a hint, not to force them to enable this.