Android and Linux

Wednesday, September 29, 2010

Linux clipboard to Android, again

(Note: I did a more complete write-up on the XDA forum which may be easier to follow than this.)

Tasker offers an improvement to my previous method of sharing clipboard contents between the phone and linux PC. It's not perfect yet, because there is no reliable way to read a whole file, but Pent said on the Tasker discussion group he'd put that on his todo list.

As of now, this will copy one line perfectly, giving it the same functionality as "Chrome to Phone" to send links to your browser. With a little customization, you could probably get it to imitate the rest of Chrome to Phone's functionality, and this would work on non-Froyo phones too, although it requires ssh with keys, at least rudimentary command line knowledge and only works on Linux and with Tasker, and only on weekdays when the sun it out and your car is full of gas.

Ok, some of that is not required.

Using my previous example in the link above, set up the paste2n1 script on your computer to export the display and grab the clipboard contents from the command line using xsel or xclip.

Once that is done, set up a script on your phone to ssh to the PC and run that script then redirect the output to a file on the sdcard. I'll use /sdacrd/homeclipboard in this example, so your phone script should look something like: ssh USER@IP -i KEYFILE 'paste2n1' > /sdcard/homeclipboard

Now here are the actions to use with Tasker:

1- using the Locale Execute Plugin, execute the script you just wrote for the phone.
2- Variable clear > %LINE
3- Variable set > %ONE to 1
4- Read line > file: homeclipboard, Line: %ONE, To Var: %LINE
5- Set Clipboard %LINE
6- Flash > %LINE
7- Browse URL > URL: %LINE if %LINE matches http*

The last three steps are optional and depend on what you want to do with this task. Step 5 copies it to your phone's clipboard, step 6 pops up a toast notification showing you what was copied and step 7 opens that line in the browser if it is a http link, imitating Chrome To Phone.

If you followed this far, it shouldn't be too hard to figure out how to send a link to the computer. In fact, you should be able to send anything in the clipboard to the computer, as the limiting factor for going the other direction is Tasker's inability to read a whole file and keep the newlines and blank lines intact. The computer doesn't have that problem, so all you'd have to do is use Tasker to write %CLIP to a file then use the Locale Execute Plugin to run a srcipt which transfers it to the computer's clipboard.

After attempting to use this over the cell network today, I realized the rest of the task runs while the first step is still executing, thus copying "old" text to the phone's clipboard. An easy fix is to add Delete File> homeclipboard as the first step, then after the "Read line" action, add a "Go To [the "Read Line"] > if %LINE matches EOF." %LINE will always be EOF until the script execution completes and the homeclipboard file is created.

This could create an infinite loop in Tasker if the connection to the computer is down, as the file would never be created. Another simple way to avoid that would be to add a conditional to the script such as ssh USER@IP -i KEYFILE 'paste2n1' > /sdcard/homeclipboard || echo "failed" > /sdcard/homeclipboard. If it successfully connects, you get the clipboard text. If it fails, you get a failure notice copied to the phone's clipboard, or you could add a step to Tasker to pop up a warning if %LINE matches "Failed" and then stop the Task. There's a lot of simple ways to go about it but, personally, my connection never fails so I'm just winging it.

Friday, September 10, 2010

Tasker car profile

At the risk of turning this into a Tasker blog, I thought I would share my new use. Now that I have a sweet car dock, I needed to set up Tasker to do a few things when I docked and undocked the phone.

When I enter the Jeep, I wanted the screen to go to full brightness, GPS to turn on and WIFI to turn off. When I exit, I want to mark my location with GPS, dim the screen to normal, turn GPS off, turn wifi back to the state it was in when I entered, and play a sound to remind me to grab the phone. I also don't want this to always happen when I plug the phone in, so I needed an option to turn it off easily.

I didn't read any other car dock profiles online and did this on my own, so I probably did it entirely the wrong way, but here goes. I did it all in several separate steps.

The first is a Task called "JeepOn" with the following steps.

1 Variable Clear %WIFISTATE
2 Variable Clear %GPSSTATE
3 Variable Set %WIFISTATE to 1 if %WIFI matches "on"
4 Variable Set %GPSSTATE to 1 if %GPS matches "on"
5 Display Brightness 255
6 Wifi Off
7 GPS On
8 Flash "Jeep Mode"

That's pretty simple. It records whether the GPS and WIFI are on, turns GPS on, WIFI off, and brightens the screen.

The second is called "JeepOff"
1 Variable Set %BEACON to %LOC
2 GPS off
3 WIFI On if %WIFISTATE equals 1
4 Flash "Jeep Mode Off.
Wifi: %WIFI
GPS: %GPS
Beacon set with %LOA m accuracy."
5 Notify Sound. [pick a sound file] Title "Jeep Mode Off"
6 Wait 5 seconds
7 Notify Cancel
8 Display Brightness 40

Here's an explanation of what this does.
1- This records my GPS position into a variable called %BEACON. I have a widget set up that simply opens Maps and take me back to that spot. This is done by selecting "Navigate To" mode "and putting %BEACON in the lat/long field.
2- Turns off GPS
3- Sets WIFI to the state it was in before. I'm not sure how useful this will be, but I have wifi set to turn on and off at certain times of the day so this should help it stick to that schedule.
4- Tells me the WIFI/GPS state and how accurate the beacon was set
5-7 - Plays a sound to remind me not to forget the phone. Since the only way to play a sound is by using a statusbar notification, which I don't really want, the next two steps are for clearing the notification without having to open the notification bar.
8 - dims the screen to the level I usually keep it on.

I'm not sure if my car charger shows up as AC or USB, but I use both at home and don't want all this to happen every time I plug the phone in, so I wanted to be able to disable Jeep Mode. I did this in two steps. The first is a new profile for "Power - source - any" called "JeepMode" with the action "Perform Task - JeepOn" and the exit action "Perform Task - JeepOff". Then I added a new Tasker widget to the homescreen and added the action Profile Status - JeepMode - Toggle. Tapping the widget turns it on if it was off, and vice versa.

So, if I'm sitting around the house I can leave JeepMode turned off and plug in the phone at will. If I'm going to drive to work and don't need JeepMode, I can plug in without it, but if I'm doing a lot of driving and getting in and out of the vehicle and think I'll need it all day long, I tap the toggle and it stays on until I tap it again.

Thursday, September 9, 2010

$8 car dock

Car docks from HTC are $60, but I just got all the parts to make my own, thanks to this great idea. The parts plus shipping cost $8.

I would post a photo, but I've torn the house apart and can't find my roll of double-sided tape anywhere, so the parts are just staring at me, wishing they could be assembled into something useful.

Followers