1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-22 19:01:30 +00:00

Request user to disable battery optimization for Syncthing

This commit is contained in:
Felix Ableitner 2016-11-10 22:57:18 +09:00
parent 9930f93311
commit f005fcd1d2
4 changed files with 49 additions and 1 deletions

View file

@ -15,6 +15,9 @@
<!-- We don't care about Google App Indexing -->
<issue id="GoogleAppIndexingWarning" severity="ignore" />
<!-- Disabling battery optimizations is optional, so this warning doesn't apply to us -->
<issue id="BatteryLife" severity="ignore" />
<!-- Ignore types in translations (not handled by us) -->
<issue id="Typos">
<ignore path="src/main/res/values-ar" />

View file

@ -13,6 +13,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<application
android:allowBackup="false"

View file

@ -1,16 +1,23 @@
package com.nutomic.syncthingandroid.activities;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
@ -23,8 +30,13 @@ import android.support.v7.app.ActionBarDrawerToggle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.*;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.fragments.DeviceListFragment;
import com.nutomic.syncthingandroid.fragments.DrawerFragment;
@ -84,6 +96,7 @@ public class MainActivity extends SyncthingActivity
case ACTIVE:
dismissDisabledDialog();
dismissLoadingDialog();
showBatteryOptimizationDialogIfNecessary();
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerFragment.requestGuiUpdate();
if (new Date().getTime() > getFirstStartTime() + USAGE_REPORTING_DIALOG_DELAY &&
@ -103,6 +116,31 @@ public class MainActivity extends SyncthingActivity
}
}
@TargetApi(23)
private void showBatteryOptimizationDialogIfNecessary() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean dontShowAgain = sp.getBoolean("battery_optimization_dont_show_again", false);
if (dontShowAgain || Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
pm.isIgnoringBatteryOptimizations(getPackageName())) {
return;
}
new AlertDialog.Builder(this)
.setTitle(R.string.dialog_disable_battery_optimization_title)
.setMessage(R.string.dialog_disable_battery_optimization_message)
.setPositiveButton(R.string.dialog_disable_battery_optimization_turn_off, (d, i) -> {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
})
.setNeutralButton(R.string.dialog_disable_battery_optimization_later, null)
.setNegativeButton(R.string.dialog_disable_battery_optimization_dont_show_again, (d, i) -> {
sp.edit().putBoolean("battery_optimization_dont_show_again", true).apply();
})
.show();
}
private void dismissDisabledDialog() {
if (mDisabledDialog != null) {
mDisabledDialog.dismiss();

View file

@ -57,6 +57,12 @@ Please report any problems you encounter via Github.</string>
<string name="folder_rejected">Device %1$s wants to share folder %2$s</string>
<string name="dialog_disable_battery_optimization_title">Battery Optimization</string>
<string name="dialog_disable_battery_optimization_message">Android may stop synchronization after some time. To prevent this, turn off battery optimization</string>
<string name="dialog_disable_battery_optimization_later">Later</string>
<string name="dialog_disable_battery_optimization_dont_show_again">Don\'t show again</string>
<string name="dialog_disable_battery_optimization_turn_off">Turn off for Syncthing</string>
<!-- FoldersFragment -->