[Optimus One, P500, V General] mmarz:
Part 3

Fixing power drain with a firewall!

Most of you probably don't believe the conclusion that 3G consumes the same power as airplane mode when idle. You've set Tasker or Juicedefender to turn off your 3G when the screen is off and have seen great battery savings. I know why...

Your installed programs are using your 3G even when the screen is off. It makes sense that you want to turn off your 3G to stop them and get better battery life, but you are punishing the wrong culprit. You may want some of your apps to occasionally update when the screen is off. You may want to use Sipdroid and get incoming calls. You may want your emails to be occasionally updated, but you don't want Google maps to constantly update the CIA to your precise location every minute. If you want to control exactly which programs have access to 3G/Wifi...

...here is what you should do:

You will need root access to do this. This will setup two script files, one to run when the screen turns off and one to run when the screen turns on. These will "enable" and "disable" the firewall. The firewall is never actually disabled. You are merely changing the rules that apply, either to give access to a limited number of apps or to allow all apps through. Because of the requirement that the scripts be executed with root access, there are actually five script files required. Two are created using Droidwall and are unique to your phone. Three scripts are attached in the zip below and are universal.

Install Root Explorer or a similar app. You can also use adb.exe, but it is up to you to figure out the correct commands.

Install Droidwall .

Figure out which apps you want to grant internet access to when your screen is off. If none, then select none.

Set the rules in Droidwall and enable the firewall.

Using Root Explorer to navigate to this folder: /data/data/com.googlecode.droidwall/app_bin/

Copy the file droidwall.sh and place it on your microSD card.

Rename it to droidwall-on.sh

Open Droidwall again, uncheck all the apps you had selected, and instead check the boxes for "(any application)". This will be the profile that is applied when you actually want to use your phone and the screen is on.

You can choose to limit access here as well, but it is safer to choose "(any application)" for now. You can come back later and recreate a more restrictive profile once you have everything working.

Apply the rules and then turn the firewall off and then back on again.

It is important that the firewall is still enabled.

Navigate back to the folder: /data/data/com.googlecode.droidwall/app_bin/

Copy the new droidwall.sh and place it on your microSD card.

Rename it to droidwall-off.sh

These two script files will "enable" and "disable" the firewall.

Use Gscript to test the scripts to make sure they work. Run droidwall-on.sh, and test an app you didn't give 3G or Wifi access to. It should be blocked from accessing the internet. Now run droidwall-off.sh. That same app should now have internet access. If this test does not work, go back and repeat the steps above. This is also the point where you check that all your critical apps function correctly with either script activated. You must make sure the scripts work as intended before proceeding. If you don't know how to add/run scripts using Gscript, learn.

While you can run these scripts as root in Gscript, running them as root when the screen turns off/on is a bit harder. The following steps have to all be done in order for the scripts to be executed correctly.

Install Tasker

Install SL4A. (more info)

Run SL4A once so that it creates the following folders on your microSD card or create them yourself: /sdcard/sl4a/scripts/

Copy droidwall-off.sh and droidwall-on.sh into the "scripts" folder.

Copy screen-off.sh and screen-on.sh from the zip file attached to this post to the "scripts" folder.

Copy "sush" from the attached zip file to the folder "/system/bin/" and set the permissions to "rwxr-xr-x".

adb remount
adb push sush /system/bin/
adb shell chmod 755 /system/bin/sush

Read here about why we need this file to run the scripts as root.

Open Tasker -> New -> Ok -> Event -> Display -> Display On -> Done -> New Task -> Ok -> + -> Misc -> Run Script

Where it says "Name" select the search button. You should see the four scripts we placed in the "/sdcard/sl4a/scripts/" folder. If not, retrace your steps to see what you did wrong. Select "screen-on.sh". Don't select the Droidwall scripts because they can't run as root as they are written.

Done -> Done

Repeat the steps to add a profile for when the screen turns off and "screen-off.sh". When you have both profiles, select Apply at the bottom.

Now when your screen is off only the programs you selected will be allowed to use your 3G or Wifi.

You no longer need to run Droidwall, but you can use it to update your droidwall-on.sh script. Don't uninstall Droidwall. If anything goes wrong and you lose all of your internet access, try deleting the scripts in the "/sdcard/sl4a/scripts/" folder, and then allowing all apps to have internet access using Droidwall.

You can actually setup any number of profiles to control your firewall using location, time of day, ...etc. To do so, create the correct ".sh" file using Droidwall. Place it inside the "/sdcard/sl4a/scripts/" folder. Then add a secondary script to run your ".sh" as root. Use "screen-on.sh" as a template to create your secondary script. Then create the proper profile inside Tasker to run the secondary script. If what I just said confuses you, don't attempt to do this. You can really mess up your phone if this is done incorrectly!

Warning 1: You are running scripts as root. This can cause all sorts of problems if the scripts are written poorly or if someone/something malicious changes your scripts. Do not try to alter the scripts unless you know what you are doing. I take no responsibility if you ruin your phone. You have been warned.

Warning 2: You can't use a droidwall-on.sh script that has been created on another phone. You must create it yourself because your apps have their own unique app number assigned to them. If you ever update a program that you gave access to, you must recreate your droidwall-on.sh file using the steps mentioned above. If you do not, your app will be blocked.

To have optimal battery life, use Wifi when the screen is on and internet usage is high, and use 3G for when the screen is off and your internet usage is mostly idle. This can be accomplished by setting the Wifi sleep policy to turn Wifi off when the screen turns off. This might be the reason it is the default setting on ALL Android phones. If you want to receive calls using sipdroid or any other voip app, create a Tasker profile that disables Wifi if you are on 3G and have an incoming voip call. And then create a second Tasker profile that re-enables Wifi when your voip call is finished. Otherwise, your incoming voip call that rings over 3G will be disconnected when you answer your phone, the screen turns on, and your phone switches to Wifi. This you can figure out how to do on your own.

I hope this helps some of you. You have a smartphone and a data plan for a reason. Use them. :)