Making Gmail Tap
Posted: April 1, 2012 Filed under: Code | Tags: Android, gmail tap, google 2 Comments »This morning my brother posted a message on Google+ about Gmail Tap – Google’s April Fools joke this year. I looked at it and though “I can do that” – so I did. Here’s the Google Play link (until they pull it!)
–
EDIT
After less than 24 hours the app got pulled from the store by Google, but still managed to get an impressive 5,646 downloads with 143 comments and a 4.7 / 5 user rating. I’ve removed all the Google branding and repackaged it – Here’s the Google Play link
Learning Vim
Posted: March 23, 2012 Filed under: Code | Tags: learning, vim Leave a comment »Today I decided to level up and start learning Vim (VI improved) – I know it’s a long road ahead but there are many benefits:
- I’ve heard it’s stupidly powerful once you know how to work it
- It’s installed, or can be installed, on pretty much every computer
- Hundreds of posts on ‘why should you learn vim’ can’t be wrong…can they?
I’m going to start out using Learn Vim Progressively, but I’m going to adapt as I go. So, from that page – first lessons:
i→ Insert mode. TypeESCto return to Normal mode.x→ Delete the char under the cursor:wq→ Save and Quit (:w save, :q quit)dd→ Delete (and copy) current linep→ Paste- Recommended:
hjkl(highly recommended but not mandatory) → basic cursor move (←↓↑→). Hint: j look like a down arrow.:help→ Show help about , you can start using :helpwithout anything else.
And a few of my own:
u→ Undo the last change:q!→ force quit without saving
Reversing primitives without creating a temp
Posted: February 15, 2012 Filed under: Uncategorized Leave a comment »I’ve been working my way through a load of practise challenges recently to make sure I’m keeping my brain active and I worked out a way of swapping two primitive values without the overhead of creating a third value to use as a temporary storage value.
Lets say you have two characters, to swap them you would execute a series of binary XORs:
char charOne = 'i';
char charTwo = 'H';
charOne ^= charTwo;
charTwo ^= charOne;
charOne ^= charTwo;
System.out.println(String.valueOf(new char[] {charOne, charTwo}));
you would get an output of ‘Hi’.
Note that
charOne ^= charTwo;
is the shorthand version of
charOne = (char) (charOne ^ charTwo);
If you have a look at what’s happening: charOne is ‘i’ which is 1101001 in binary and charTwo is ‘H’ which is 1001000 in binary.
First we XOR the two values and assign back to charOne:
| charOne: | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
| charTwo: | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
| charOne: | 0 | 1 | 0 | 0 | 0 | 0 | 1 |
charOne is reassigned the binary representation of 0100001.
We then XOR the new value of charOne with the initial value of charTwo and assign to charTwo:
| charTwo: | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
| charOne: | 0 | 1 | 0 | 0 | 0 | 0 | 1 |
| charTwo: | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
so now charTwo has a binary representation of 1101001, which was the initial value of charOne.
we then XOR the third time and assign back to charOne:
| charOne: | 0 | 1 | 0 | 0 | 0 | 0 | 1 |
| charTwo: | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
| charOne: | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
and we see that the binary representations of the two characters have swapped.
Just thought that was a pretty nice way of doing it.
How to install packages from the command line in (K)Ubuntu
Posted: February 13, 2012 Filed under: Code | Tags: cli, kubuntu, packages Leave a comment »This is more a note for myself, but recently I had an instance where I had a deb package to install but it needed me to accept a license – thing is that it wouldn’t work using the GUI – it’d just display a window saying ‘Done’. I worked out I had to install from the CLI to get it to show the license so that I could accept it.
Anyway – here’s the command :
sudo dpkg -i <location of deb file>
Andru the Android charger
Posted: February 7, 2012 Filed under: Uncategorized Leave a comment »
I really want one of these – but obviously it won’t work here in the UK. Perhaps when I move to Canada I can pick one up
He’s a little Android but doubles up as a USB wall charger when you need him to and he’s got light-up eyes – they really need to glow red if you plug in an iPhone.
http://www.powerbygen.com/USB-Phone-Charger_p_8.html
Easy way to reduce the size of png files
Posted: February 6, 2012 Filed under: Code | Tags: bash, png, python Leave a comment »I’ve been doing some work with some hefty png files recently and was pointed to a page on my colleagues website where he explains how to compress pngs without losing any quality. I won’t go through the details of what he’s doing – you can go have a read up of his site for that, but I’ve seen reductions in size from about 400Kb to about 110Kb without any loss of image quality.
Make sure you backup your files before you do any of this – I won’t be held responsible for any issues you have if this messes up for you!
I’ve replicated the steps below but formatted them for use in bash and included a little python script I wrote to sort through and pick out the smallest files as I found that not all the steps reduced the file size of all the png files, depending on the original file.
for i in *.png; do pngquant -ext -convert1.png 256 $i; done
for i in *-convert1.png; do pngout -c3 -d8 -y -force $i ${i%%convert1.png}convert2.png; done
for i in *-convert2.png; do pngcrush -bit_depth 8 -brute -rem alla -reduce $i ${i%%convert2.png}convert3.png; done
pngcrush takes ages on my machine, so make sure you have enough time if you have lots of files to crunch.
After this you should have a list of files:
- landscape.png <- this being your original file
- landscape-convert1.png
- landscape-convert2.png
- landscape-convert3.png
Now all you need to do is pick out the smallest, which I’ve written the following Python script for. It’s not optimised, but it works. It’ll run through all your files and remove all but the smallest and rename it to the original name of the png:
#!/usr/bin/python
import os, re
class PngFile(object):
def __init__(self, filename):
self.name = filename
self.path = path + filename
self.size = os.path.getsize(self.path)
path="<INSERT PATH HERE>"
dirList=os.listdir(path)
out = ''
convertString = '-convert'
pngString = '.png'
reobj = re.compile('([^.]+)\.png$')
reobj2 = re.compile('[^.]+convert.\.png$')
filename = ''
count = 0
sizesaved = 0
for fname in dirList:
if reobj.match(fname) is not None:
if reobj2.match(fname) is None:
files = [PngFile(fname)]
count+=1
filename = reobj.match(fname).group(1)
if(os.path.exists(path + filename + convertString + '1' + pngString)):
files.append(PngFile(filename + convertString + '1' + pngString))
if(os.path.exists(path + filename + convertString + '2' + pngString)):
files.append(PngFile(filename + convertString + '2' + pngString))
if(os.path.exists(path + filename + convertString + '3' + pngString)):
files.append(PngFile(filename + convertString + '3' + pngString))
smallestFileSize = min(files, key=lambda pngFile: pngFile.size).size
biggestFileSize = max(files, key=lambda pngFile: pngFile.size).size
smallestFilePath = min(files, key=lambda pngFile: pngFile.size).path
saved = biggestFileSize - smallestFileSize
sizesaved += saved
print "{2} - smallest file found {0} - saving {1} - {3}".format(smallestFilePath, saved, filename, fname)
for file in files:
if file.path is not smallestFilePath:
print "remove {0}".format(file.name)
os.remove(file.path)
print "RENAME {0} -> {1}".format(smallestFilePath, path + fname)
os.rename(smallestFilePath,path + fname)
print "files found={0} - saving {1}".format(count, sizesaved)
The best email I’ve ever received
Posted: January 30, 2012 Filed under: Misc Leave a comment »This morning I received in my inbox what I can only describe as a stream of awesomeness. Here’s the Google Translate English version (original below):
Hi dear Martyn,
Longer Bengbeng Tiao, wish to Fortune; Longer Sahuan fly, wish good health; Longer sleep well, help out and elegant; LongerDragonscale more, everything is auspicious; wish you and your family, Pepsi, everything fun up, every day, Wahaha, Pepsi musicmonth, year after year of high Lego, and I feel like Sprite, always smart, happy, proceeds smoothly! Happy New Year to you myself!
On the occasion of Lunar New Year occasion, I sincerely wish you and your family, fun, healthy, happy and prosperous!
Cheers,
Bruce
Shanghai Consulting Executive Search Consultant secret information
Original:
Hi dear Martyn,
龙儿蹦蹦跳,恭祝鸿运到;龙儿撒欢飞,恭祝身体好;龙儿睡得香,出门贵人帮;龙儿龙鳞多,万事都吉祥;祝您及您的家人百事可乐,万事芬达,天天娃哈哈,月月乐百事,年年高乐高,心情似雪碧,永远都醒目,开开心心,顺顺利利!给您拜年喽!
值此新春佳节之际, 我衷心祝福您及全家, 合家欢乐,身体健康,万事如意,恭喜发财!
Cheers,
Bruce
上海密讯咨询高级猎头顾问
Copying files whilst flattening the directory structure
Posted: January 24, 2012 Filed under: Code Leave a comment »I just had to copy a load of PNGs, whilst at the same time flattening the directory structure. After a while of playing about I came up with this:
find . -iname '*.png' -exec cp \{\} "<PATH TO DESTINATION>" \;
This recursively takes every png file from the current directory hierarchy and copies it to the destination directory.
Handy, no?
Getting a YouTube clip to run inside your Android application
Posted: November 4, 2011 Filed under: Code | Tags: Android, code, Java, YouTube Leave a comment »Recently I’ve been working on an application for a company based near to where I live – part of their application involves some video in the app. Now Android has a 50 meg limit on apk files in the market, so I was tasked with streaming the videos from YouTube, which doesn’t seem to be a massive problem up front.
How wrong I was.
Getting YouTube videos to stream inside an Android app is actually really difficult – I hunted for hours on StackOverflow.com looking for something which would work, only to be given half baked code or ill-thought-out solutions. I got something working by faking the browser agent and loading the video directly, but this would only work in Android 2.x and then only if the user has Flash installed.
So I wrote to my client – telling them that I’d already spent quite a lot of time on this, and that I wanted them to make a call on how best to proceed…and about 30 minutes later I found the answer. Open YouTube Player is a fantastic bit of code, written by Keyes Labs which you can pretty much drop in to your code to get working. It looks like it’s dependant on the way YouTube encodes their videos, so if that changes then it may break, but the project is active on and it’s the best solution I’ve found.
I hope this saves someone else a lot of hunting the internet for a solution to this problem, and a massive thanks to Keyes Labs.
Android Sources plugin for Eclipse
Posted: October 20, 2011 Filed under: Uncategorized Leave a comment »Found this today – adds the Android Source code to Eclipse for you
Much easier than having to grab the code and attach it manually.
Android Sources
This plugin helps you to add source to android libraries in Eclipse.In ADT >=8.0.0 you can add Android sources to Android container for all your project with installing the Android source feature using http://adt-addons.googlecode.com/svn/trunk/source/com.android.ide.eclipse.source.update/ update site After installing the Android source feature all your existing projects as well as new created projects which is targeted for Android 2.3.4, 2.3, 2.2, 2.1, 2.0.1, 1.6 and 1.5 will have attached the source jar.
The plugin includes sources for the following API levels:
10 – Android 2.3.4
9 – Android 2.3
8 – Android 2.2
7 – Android 2.1
6 – Android 2.0.1
4 – Android 1.6
3 – Android 1.5
It’s part of the Additional Eclipse plugins for Android pack.