Opened 3 months ago

Closed 7 days ago

#2607 closed defect (fixed)

Disable Android battery optimizations

Reported by: zzz Owned by: Meeh
Priority: minor Milestone: 0.9.43
Component: apps/android Version: 0.9.41
Keywords: Cc:
Parent Tickets: Sensitive: no

Description

according to orignal, this prevents android 8/9 from killing the app

ref: i2pd https://github.com/PurpleI2P/i2pd/issues/1400

Subtickets

Change History (4)

comment:2 Changed 6 weeks ago by zzz

On Pie:
W/ActivityManager(2182): Stopping service due to app idle: u0a269 -2m5s865ms net.i2p.android.debug/net.i2p.android.router.service.RouterService?

i2pd did it the 'risky' way by using the "REQUEST" permission and ACTION_REQUEST_IGNORE. Alternative is the "safe" way with ACTION_IGNORE but it's fairly unpleasant for the user. i2pd is not in play store so no reason for them to do the safe way. Most reports of banning are from 2015.

not clear what the right approach is.

comment:3 Changed 6 weeks ago by idk

This is the "Safe" way. I don't think the other way is actually much different but here's what I've got.

#
# old_revision [4aa5b90701b61c9dd91a10fa7d12673e412502d7]
#
# patch "app/src/main/java/net/i2p/android/I2PActivityBase.java"
#  from [befc0c12e1a5277d6c32ac49a1f758c17c5eae40]
#    to [0ec0387045a88938da31f6d97ab29c84ccba07f6]
# 
# patch "app/src/main/java/net/i2p/android/router/MainFragment.java"
#  from [b6d53a9da7a552964fc256d20d5eaa623c5a06a9]
#    to [be048a85770fa6ebd8f8e2f56716b58a6be513a2]
# 
# patch "app/src/main/res/values/strings.xml"
#  from [63d5ce39e807cf44c3425a53d2ad94927ddd4168]
#    to [4cd6dcf82422185f6047a3cda29c232dde0814b4]
#
============================================================
--- app/src/main/java/net/i2p/android/I2PActivityBase.java	befc0c12e1a5277d6c32ac49a1f758c17c5eae40
+++ app/src/main/java/net/i2p/android/I2PActivityBase.java	0ec0387045a88938da31f6d97ab29c84ccba07f6
@@ -91,6 +91,13 @@ public abstract class I2PActivityBase ex
         return edit.commit();
     }
 
+    /**
+     * @return true if preference is present, false if not.
+     */
+    public boolean hasPref(String pref){
+        return _sharedPrefs.contains(pref);
+    }
+
     @Override
     public void onResume() {
         Util.d(this + " onResume called");
============================================================
--- app/src/main/java/net/i2p/android/router/MainFragment.java	b6d53a9da7a552964fc256d20d5eaa623c5a06a9
+++ app/src/main/java/net/i2p/android/router/MainFragment.java	be048a85770fa6ebd8f8e2f56716b58a6be513a2
@@ -9,7 +9,9 @@ import android.os.Handler;
 import android.graphics.Typeface;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.PowerManager;
 import android.preference.PreferenceManager;
+import android.provider.Settings;
 import android.support.v4.content.LocalBroadcastManager;
 import android.support.v7.app.AlertDialog;
 import android.view.Gravity;
@@ -69,6 +71,7 @@ public class MainFragment extends I2PFra
     private TextView vAdvStatusText;
 
     private static final String PREF_CONFIGURE_BROWSER = "app.dialog.configureBrowser";
+    private static final String PREF_CONFIGURE_BATTERY = "app.dialog.configureBattery";
     private static final String PREF_FIRST_START = "app.router.firstStart";
     private static final String PREF_SHOW_STATS = "i2pandroid.main.showStats";
     protected static final String PROP_NEW_INSTALL = "i2p.newInstall";
@@ -614,16 +617,48 @@ public class MainFragment extends I2PFra
                             ab.setPref(PREF_CONFIGURE_BROWSER, false);
                             Intent hi = new Intent(getActivity(), BrowserConfigActivity.class);
                             startActivity(hi);
+                            checkDialog();
                         }
                     })
                     .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int i) {
-                            dialog.cancel();
+                            dialog.dismiss();
                             ab.setPref(PREF_CONFIGURE_BROWSER, false);
+                            checkDialog();
                         }
                     })
                     .show();
+        } else if (!ab.hasPref(PREF_CONFIGURE_BATTERY) || ab.getPref(PREF_CONFIGURE_BATTERY, true)) {
+            final Intent intent = new Intent();
+            final Context mContext = ab.getApplicationContext();
+            String packageName = mContext.getPackageName();
+            PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
+            if (pm.isIgnoringBatteryOptimizations(packageName)) {
+                intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
+            } else {
+                AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
+                b.setTitle(R.string.configure_no_doze_title)
+                        .setMessage(R.string.configure_no_doze)
+                        .setCancelable(false)
+                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
+                            @Override
+                            public void onClick(DialogInterface dialog, int i) {
+                                dialog.dismiss();
+                                ab.setPref(PREF_CONFIGURE_BATTERY, true);
+                                intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
+                                mContext.startActivity(intent);
+                            }
+                        })
+                        .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
+                            @Override
+                            public void onClick(DialogInterface dialog, int i) {
+                                dialog.cancel();
+                                ab.setPref(PREF_CONFIGURE_BATTERY, false);
+                            }
+                        })
+                        .show();
+            }
         }
         /*VersionDialog dialog = new VersionDialog();
         String oldVersion = ((I2PActivityBase) getActivity()).getPref(PREF_INSTALLED_VERSION, "??");
============================================================
--- app/src/main/res/values/strings.xml	63d5ce39e807cf44c3425a53d2ad94927ddd4168
+++ app/src/main/res/values/strings.xml	4cd6dcf82422185f6047a3cda29c232dde0814b4
@@ -67,6 +67,8 @@
 
     <string name="configure_browser_title">Configure browser?</string>
     <string name="configure_browser_for_i2p">Would you like to configure a browser to view I2P sites? (You can also do this later from the help menu.)</string>
+    <string name="configure_no_doze_title">Manage Battery Optimizations?</string>
+    <string name="configure_no_doze">I2P works best if you run it for a long time. In order to prevent Android from closing I2P when trying to save power, you can add it to the battery exceptions list.</string>
     <string name="first_start_title">Congratulations on getting I2P installed!</string>
     <string name="first_start_welcome"><b>Welcome to I2P!</b> Please <b>have patience</b> as I2P boots up and finds peers.</string>
     <string name="first_start_read">While you are waiting, please read the release notes and welcome page.</string>

comment:4 Changed 7 days ago by zzz

Milestone: undecided0.9.43
Resolution: fixed
Status: newclosed

Fixed by idk in 0.9.43

Note: See TracTickets for help on using tickets.