Friday, May 27, 2011

More Tasker tricks

Continuing the theme of automation with Tasker, I've come up with a couple of new things to share...

Damn you, autorotate!

For the most part, I use my phone in portrait mode. It's just the most comfortable way to hold such a device. I think that might have something to do with why they made it that way. :) But, there are a lot of times landscape's a better fit for what you're doing...like Angry Birds. And, maybe some web sites.

Now, if you're a portrait kind of guy like me, you might find the phone's auto-rotation feature to be a bit of a hassle at times. Like, when I'm using Navigation and it switches because I put the phone down on the seat next to me and now I can't tell where I'm supposed to turn because the damn thing's too narrow to display the road far enough ahead. I could turn off auto-rotate, sure. But, then I lose the functionality in those few apps where I actually want it. Oh, the quandary!

Then, the other day, I came across an app called Smart Rotator which provides a solution: it toggles auto-rotate on an app-by-app basis. Nice! And, it works pretty well. Unfortunately, it's yet another thing I need to have running, so I decided to do some poking to see if I could mimic the features using Tasker. Fortunately, it couldn't be easier.

First, I created a new Tasker profile in the App Launch context (the little rocket ship icon). I chose Dolphin Browser HD and Calculator as the apps to watch for. Then, create a new task called "Turn On Rotate" and select Display - > Display Rotation -> Set to On. Save it. Create an Exit Task called "Turn Off Rotate" and repeat except change it to Set to Off. Then, tap the Menu button, then Settings -> Display and uncheck Auto-rotate screen.

Now, auto-rotate is off by default, but when I launch my browser or calculator it's available. When they close, it goes back to off. I chose Calculator just to show you could configure multiple apps with just one profile. If I find another app that I want to be able to auto-rotate, I just go in and edit the profile.

Don't Track On Me!

Recently, I switched over to CyanogenMOD 7 on my Evo (love it!). There is, however, a bigish bug on the Evo in the version I'm using: GPS can't keep a lock properly. There are workarounds and a solution that worked when I put CM7 on my wife's phone, but not on mine. For the time being, I've found my own workaround:

Create a new profile in the Event context (the orange triangle) and call it Display Unlocked. Then, Display -> Display Unlocked. I set the priority to Normal and haven't had any issue. Create a new task called Display Unlocked (I like originality). Misc -> GPS -> Set to On. Create a second profile in the same context, but call it Display Off. Then, Display -> Display Off. I'll leave it as an exercise to figure out what to do for a task. :)

So, now, when I unlock my phone, it turns GPS on automatically and turns it off when I've turned the screen off. This has actually worked quite nicely and I haven't had any complaints about GPS since. The one drawback is Google Latitude doesn't work as well. However, as I'm constantly checking my phone anyway, it seems to keep me mostly up to date. Plus, no tracking by the feds! Double-win!

My Dynamic DNS Hack

After the GoDaddy CEO mercilessly and needlessly slaughtered an elephant for his sick amusement, I decided to take advantage of the offer in that article and switch my domains over to Namecheap.com. Aside from no longer supporting a company whose CEO should have his stepped on by that elephant's mate, I've gained some nifty benefits by switching to NameCheap (Not the least of which is better service at a lower price! Seriously, switch. You won't regret it.) One of them is dynamic DNS.

A little background: dynamic DNS is a service that allows you to assign a host name to an IP address that's constantly changing. A lot of folks use dynamic DNS so they can get to their home machines without having to worry about their ip address changing regularly.

The service I've used for years is No-IP.com, and frankly I have nothing bad to say about them at all. I've always had reliable service from them and have never had to contact them for support. It's a service that just works. That being said, if my registrar is giving me free dynamic DNS service along side my basic registration, well it just makes sense to try and migrate to that!

There are a couple of apps in the Market that will perform as client for your dynamic DNS provider, but none worked directly with Namecheap.com. But, as I thought about it, I realized here was another chance to find my own solution. Unfortunately, it wasn't as elegant as I'd like, but it works more or less the way I want. Perhaps if it becomes irritating enough, I'll fix it up further...

First, a caveat: I'm not sure how much of this would require you to have root access on your phone. I've gone through it again in my head, and I don't think there's anything below that needs special permissions. But, if it doesn't work on your first try, that might just be the reason.

The first piece you'll need is the Scripting Layer 4 Android. The SL4A is a great little project to bring a number of scripting languages to the Android platform to make it easier to develop things like...well, this! At current count, it supports Python, PHP, Ruby, JavaBeans and shell scripting, which we'll be using. Follow the instructions at the link to get it installed. Since my solution just uses a shell script, you don't have to install any other interpreters just yet. Fire up a text editor on your machine and copy in the following:


#!/system/xbin/bash

source /data/local/tmp/ipaddress

test=`netcfg | grep UP | grep -v 127.0.0.1 | awk '{ print $3 }'`

if [ "$test" != "$ipadd" ]; then

domain="domain.com"
username="canbeanything"
apikey="namecheapkey"
hostname="mobile"
hosturl="http://dynamicdns.park-your-domain.com/"
command="update?domain=$domain&username=$username&password=$apikey&host=$hostname&ip=$test"

url="$hosturl$command"

wget -q -O- $url
echo ipadd=$test > /data/local/tmp/ipaddress
fi


If you've been following along with other articles, you know that I've got Tasker profiles which disables the mobile radio when wifi is on and vice versa. As a result, the above script is a bit quick-n-dirty as it assumes there's only one radio on at any given time, and thus "UP". You'll have to do some tweaking if you don't have your phone setup like mine.

So, what does it do? It reads a text file I've created that simply contains the line "ipadd=192.168.1.1" (or whatever the IP was the last time it ran). It runs the netcfg command which is similar to Linux' ifconfig command. It finds the line for the interface that's up and not the loopback connector and snips out the IP address it finds. It then compares it with the one that's in the text file. If they match (which means the current IP at Namecheap.com is most likely what's currently on the phone), it does nothing. If they don't, it builds a url and uses wget to ping it. Namecheap.com will then update their records.

Note: I am specifically pushing the IP to Namecheap because if you don't they use whatever IP the request comes from. As a result, when I'm at home on my NATted network, it'll use the IP of the external interface of my router and not the internal IP I'm using on Wifi. Part of the reason I'm setting this up is to automate some tasks at home to push stuff to the phone, so I need the right address there.

So, what about Tasker? Remember the GPS hack you read about 2 minutes ago? We're going to leverage that. Edit the task for the Display Unlocked profile and add a new item. Plugin -> SL4A. Yes, SL4A has been kind enough to give us a Tasker plugin! Hit the Edit button and select the script above. Done! Now, when you unlock your phone, it'll update your IP if need be. A rockin'ly simple solution that seems to work just spiffy!

Hopefully, these will inspire you to try some new stuff with Tasker you hadn't considered before. If you do, drop me a line below and tell me about 'em!

2 comments:

  1. Hey,

    Thank you for posting the guide to controlling auto rotation in tasker! Much appreciated!

    Regards
    John
    Sweden

    ReplyDelete
  2. Magnificent site. A lot of helpful info here. I'm sending it to a few pals ans additionally sharing in delicious.
    And certainly, thanks on your effort!

    ReplyDelete