From f6c51d5cfc2de26c499958df12a12169ac9600d1 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 4 Nov 2016 16:01:57 +0900 Subject: [PATCH] Added item to disable battery optimizations (fixes #75, #113) --- play-services-core/build.gradle | 2 +- .../src/main/AndroidManifest.xml | 2 + .../org/microg/gms/ui/SettingsActivity.java | 14 +++++ .../microg/tools/selfcheck/SystemChecks.java | 60 +++++++++++++++++++ .../src/main/res/values/strings.xml | 4 ++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 play-services-core/src/main/java/org/microg/tools/selfcheck/SystemChecks.java diff --git a/play-services-core/build.gradle b/play-services-core/build.gradle index c474cbda..e3d06f72 100644 --- a/play-services-core/build.gradle +++ b/play-services-core/build.gradle @@ -80,7 +80,7 @@ android { lintOptions { // TODO: Remove MissingTranslation once we have stable strings and proper translations. - disable 'MissingTranslation', 'InvalidPackage' + disable 'MissingTranslation', 'InvalidPackage', 'BatteryLife' } buildTypes { diff --git a/play-services-core/src/main/AndroidManifest.xml b/play-services-core/src/main/AndroidManifest.xml index 810a4705..ca1ee6d1 100644 --- a/play-services-core/src/main/AndroidManifest.xml +++ b/play-services-core/src/main/AndroidManifest.xml @@ -80,6 +80,8 @@ + + LOLLIPOP_MR1) { checks.add(new PermissionCheckGroup(ACCESS_COARSE_LOCATION, WRITE_EXTERNAL_STORAGE, GET_ACCOUNTS, READ_PHONE_STATE)); } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + checks.add(new SystemChecks()); + } checks.add(new NlpOsCompatChecks()); checks.add(new NlpStatusChecks()); } @@ -119,6 +125,14 @@ public class SettingsActivity extends AppCompatActivity { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { reset(LayoutInflater.from(getContext())); } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == SystemChecks.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) + reset(LayoutInflater.from(getContext())); + else + super.onActivityResult(requestCode, resultCode, data); + } } public static class MyAboutFragment extends AbstractAboutFragment { diff --git a/play-services-core/src/main/java/org/microg/tools/selfcheck/SystemChecks.java b/play-services-core/src/main/java/org/microg/tools/selfcheck/SystemChecks.java new file mode 100644 index 00000000..c4150ee4 --- /dev/null +++ b/play-services-core/src/main/java/org/microg/tools/selfcheck/SystemChecks.java @@ -0,0 +1,60 @@ +/* + * Copyright 2013-2016 microG Project Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.microg.tools.selfcheck; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.PowerManager; +import android.provider.Settings; +import android.support.v4.app.Fragment; + +import com.google.android.gms.R; + +import static org.microg.tools.selfcheck.SelfCheckGroup.Result.Negative; +import static org.microg.tools.selfcheck.SelfCheckGroup.Result.Positive; + +@TargetApi(23) +public class SystemChecks implements SelfCheckGroup, SelfCheckGroup.CheckResolver { + + public static final int REQUEST_IGNORE_BATTERY_OPTIMIZATIONS = 417; + + @Override + public String getGroupName(Context context) { + return context.getString(R.string.self_check_cat_system); + } + + @Override + public void doChecks(Context context, ResultCollector collector) { + isBatterySavingDisabled(context, collector); + } + + private void isBatterySavingDisabled(final Context context, ResultCollector collector) { + PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + collector.addResult(context.getString(R.string.self_check_name_battery_optimizations), + pm.isIgnoringBatteryOptimizations(context.getPackageName()) ? Positive : Negative, + context.getString(R.string.self_check_resolution_battery_optimizations), this); + } + + @Override + public void tryResolve(Fragment fragment) { + Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); + intent.setData(Uri.parse("package:" + fragment.getActivity().getPackageName())); + fragment.startActivityForResult(intent, REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); + } +} diff --git a/play-services-core/src/main/res/values/strings.xml b/play-services-core/src/main/res/values/strings.xml index 7d01429d..943c405e 100644 --- a/play-services-core/src/main/res/values/strings.xml +++ b/play-services-core/src/main/res/values/strings.xml @@ -99,6 +99,10 @@ This can take a couple of minutes." Either the installed %1$s is not compatible or signature spoofing is not active for it. Please check the documentation on which applications and ROMs are compatible. Extended access to Google services + System + Battery optimizations ignored: + Touch here to disable battery optimizations. Not doing this may result in misbehaving applications + microG Services Core: Lacking permission %1$s Select this location