|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +#include "androidbatteryoptimizer.h" |
| 6 | + |
| 7 | +#include <QJniObject> |
| 8 | + |
| 9 | +#include "androidcommons.h" |
| 10 | + |
| 11 | +constexpr auto BATTERY_HELPER_CLASS = |
| 12 | + "org/mozilla/firefox/qt/common/BatteryOptimizationHelper"; |
| 13 | + |
| 14 | +// static |
| 15 | +bool AndroidBatteryOptimizer::batteryOptimizationEnabled() { |
| 16 | + QJniObject ctx = AndroidCommons::getActivity(); |
| 17 | + jboolean ignoring = QJniObject::callStaticMethod<jboolean>( |
| 18 | + BATTERY_HELPER_CLASS, "isIgnoringBatteryOptimizations", |
| 19 | + "(Landroid/content/Context;)Z", ctx.object<jobject>()); |
| 20 | + return !ignoring; |
| 21 | +} |
| 22 | + |
| 23 | +// static |
| 24 | +bool AndroidBatteryOptimizer::canTriggerIntent() { |
| 25 | + QJniObject ctx = AndroidCommons::getActivity(); |
| 26 | + return (bool)QJniObject::callStaticMethod<jboolean>( |
| 27 | + BATTERY_HELPER_CLASS, "hasRequestIgnoreBatteryOptimizationsPermission", |
| 28 | + "(Landroid/content/Context;)Z", ctx.object<jobject>()); |
| 29 | +} |
| 30 | + |
| 31 | +// static |
| 32 | +void AndroidBatteryOptimizer::triggerBatteryOptimizationIntent() { |
| 33 | + QJniObject ctx = AndroidCommons::getActivity(); |
| 34 | + QJniObject intent = QJniObject::callStaticObjectMethod( |
| 35 | + BATTERY_HELPER_CLASS, "getRequestIgnoreBatteryOptimizationsIntent", |
| 36 | + "(Landroid/content/Context;)Landroid/content/Intent;", |
| 37 | + ctx.object<jobject>()); |
| 38 | + if (!intent.isValid()) { |
| 39 | + return; |
| 40 | + } |
| 41 | + ctx.callMethod<void>("startActivity", "(Landroid/content/Intent;)V", |
| 42 | + intent.object<jobject>()); |
| 43 | +} |
0 commit comments