Jason Connery

no time.

Refreshing

Just blowing the dust of the old static site generator, and seeing it even still works

Dev Tool: Accented Strings

I’m working on an app at the moment that will be localised into multiple languages. Android support for multiple languages is decent. You can provide alternate strings files, alternate layouts, images, etc. But even with the provided support old habits die hard and it can happen that you hard code a string in somewhere and forget about it. Perhaps it was just a temporary dev thing, but you forgot about it. Temporary has a habit of becoming permanent.

There are some lint warnings for this, but an additional option available is a special locale zz-ZZ called Accented English. One can provide alternate resources with the zz qualifier, values-zz , with alternate english. Maybe longer and more verbose strings to test layout flexibility, accented characters like Sëttîñgš , so one can see at a glance when testing on device what strings are coming from resource files, and what might be hardcoded / server side. So you provide alternate versions, and any time you notice a non-funky string you can ponder whose responsibility it is to provide alternate versions.

Typing all those by hand is annoying, so I wanted a shortcut. While googling, I found several ways to go from accented to normal english, like Sëttîñgš –> Settings. But I couldn’t find any tools to go the other way around. Nothing for converting normal strings to accented strings.

So I wrote a little groovy script to do it for me. It really just does the bare minimum, but might come in handy. it works like so:

If you have an android strings file, you can generate a new one you can include in your values-zz folder. If we take a sample input:

1
2
3
4
5
6
7
8
9
$ cat strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">SampleApp</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="number_of_fish">You have %d fish!</string>
    <string name="hello_name">Hello, %s!</string>
</resources>

We can pass the file to the script to create a new one. The output is just printed, so redirect to a file or something if it looks ok

1
2
3
4
5
6
7
8
9
10
 $ groovy Accenter.groovy ../../../../examples/strings.xml

  <?xml version="1.0" encoding="utf-8"?>
  <resources>
      <string name="app_name">SámpłëÄpp</string>
      <string name="action_settings">Sëttîñgš</string>
      <string name="hello_world">Hëłłö wörłd!</string>
      <string name="number_of_fish">Yöü hávë %d fîšh!</string>
      <string name="hello_name">Hëłłö, %s!</string>
</resources>

Notice that formatting strings aren’t affected to %s doesn’t become %š

Grab the code

You can fork it on github right here

it might have a tonne of bugs and shortcomings, and it might mangle things it shouldn’t like http links. I’ve only added a few exceptions so far.

Why groovy?

With the new android build system using gradle and groovy, I thought it’d be fun to try it out. Seems like an interesting language. I wonder if something like this, generating -zz resources could be done as a plugin as part of the assemble processs.

Recovering Old Files in Git

We had a relatively minor problem this week in work. Previously, we replaced an image with a newer version with different dimensions, assuming the old one was no longer needed. a few days (and git commits) later we realised that we could actually use both versions of the image.

Now, not a hard problem to solve. We probably had an old copy lying around in email from our designer. We could just as easily check out an old commit from the repo, copy the file to a tmp location, go back to HEAD commit copy in the file from the temp location. Any many more ways.

but I wanted to avoid the reliance on temporary files, emails, etc. Surely there was someway to easily get back the file we overwrote? Some git magic? A bit of googling revealed an answer.

First. Find when we altered the image.

git log -p some-image.png

This gives us the logs relating to that file. From this, we get our commit where the image was changed. In my case 656d674d3a7a568bac7cd0fc8cf45172da50ddee

Sweet. now we can use git’s cat-file option to get the contents at the revision.

git cat-file blob REVHASH:path/to/file.png > newfile.png

So in our case (watch out for the ~1 in the commit hash):

git cat-file blob 656d674d3a7a568bac7cd0fc8cf45172da50ddee~1:some-image.png > alt-image.png

And now we have the old image in the file alt-image! straight from git history into our working copy. Don’t forget to commit.

Notice the ~1 after the revision hash. This is a handy shortcut to the revision before the one from the log. We don’t actually want the image from the commit where it changed, but rather we want the way it was before. So ~1 gives us the previous commit.

And that’s how we restored an old git file into a new file!

OSX XML and Plist to Json Tips

two quick tips I found useful for developers using OSX this week.

Format XML quicky

You’ve copied a block of unformatted XML from somewhere. Possibly a log output from iOS/Android perhaps. you want to format it to look at it. With the unformatted XML in your clipboard, do:

pbpaste | xmllint --format - | pbcopy

This will replace the unformatted XML with formatted XML in your clipboard. Go ahead and paste it wherever.

Convert a plist to json

I was working with a plist file in an iOS app. I wanted to bundle the same data in android, but didn’t want to be parsing the plist format. seeing as a plist is just describing dictionaries, arrays, etc wouldn’t it be cool to quickly convert a plist into a json representation? turns out that’s pretty simple:

plutil -convert json Countrys.plist -o Countrys.json

and there you have it.

Trying Octopress

I’m trying octopuses instead of the old manual holding page. Seems nice.

EditText Not Showing a Keyboard After Styling?

You are doing some android development. You’ve just made a style for your controls on a screen.

You apply it with something like this :

<EditText android:id="whatever" style="@style/myNewStyle" />

And everything looks good. But then, the keyboard doesn’t work. Make sure your style has the default android style as it’s parent. There’s some stuff relating to touch in here. like this:

<style name="myNewStyle" parent="@android:style/Widget.EditText">...</style>

I just spent a while trying to figure out why my edit text wasn’t working after I changed the background via a style in my theme. Didn’t find anything on google, so maybe someone will stumble across this.

My DIY iPad Stylus

I once bought a stylus for my iPad in HMV. It only cost 5 euro, but it was completey shite. Some rubber tipped thing, required insane amout of pressure to actually work. Most of the time there’s no need for a stylus, but if you are trying to do any drawing or writing on the iPad with your fingers it gets old very quick. One of my favourite apps is Penultimate, and I think that it would benefit from having a decent stylus. I hoped there would be a better one out there than that cheap one.

I recently backed a kickstarter project for a touchscreen stylus, called the Cosmonaut ( The Cosmonaut ). The page also linked to a blog post on making a stylus from a white board marker , here http://www.therussiansusedapencil.com/post/1521469842/the-right-feel-for-an-i…

There are also many other examples around the web and videos showing various people attempts to craft their own.

The key part to making them is having some conductive foam. But who has conductive foam just lying around the place? Whats conductive foam for anyway? Well, turns out that it’s used for storing electronics. Finally I realised that the box I used to store my college project was full of the stuff, to protect the ICs and development boards for the components I was using. I’m embarrsed to admit I didn’t know that it was special foam, I thought it regular foam which was there for only for padding. The conductive foam is also meant to reduce static damage I believe.

So with some of this conductive foam just lying around, I took a shot at making my own. I know there is nothing new here, it’s all been done before many times; but it’s fun to just play around and do something for yourself. I wasn’t going to buy anything special to do this. It was just a bit of fun on a saturday afternoon. I rooted around for suitable bodies, and tried various pens, tubes, whatever was metal and might hold onto some foam. I tried the cap of a space pen, an old laser pointer and some other metal bodied pends and pencils. I finally settled on a cheap paintbrush, removed the bristles and stuck foam on each end.

The nice thing is, this cost me 0 euro, and is miles better than the one from HMV.

.Net : Reusing Types Across Multiple WebServices (Using svcutil.exe)

For a long time I had an issue. I had a server .net project that had multiple .asmx web services in it. In the server project, these different services shared and returned various types: for example a Product class. Just say we have Service1.asmx and Service2.asmx

When consuming these in another solution by adding a service reference in VS2008, The problem was that Visual Studio was creating a name space and types for each service, and not reusing them, so you’d end up with the classes

MyNamespace.Service1.Product
MyNamespace.Service2.Product

and Soap clients like

MyNamespace.Service1.Service1SoapClient
MyNamespace.Service2.Service2SoapClient

Which was annoying, as you would have to be sure to create the correct type of product object depending on what service you were about to use. What I wanted was

SomeNamespace.API.Product 
SomeNamespace.API.Service1SoapClient
SomeNamespace.API.Service2SoapClient

Turning to google, I found many suggestions to add a dll with the orginal type to the project, and that the services would reuse this type. They didn’t. I found tip after tip referring to WCF, or DataContact and various attributes, but that didn’t work either.

I found suggestions to use commands like :

svcutil /r:servertypes.dll /out:Service1.cs http://localhost/Service1.asmx
svcutil /r:servertypes.dll /out:Service2.cs http://localhost/Service2.asmx

which would reuse types. It didn’t. I’m putting it down to not using WCF on the server, but that’s out of my hands.

But I figured it out. And the answer is fairly simple.

svcutil http://localhost/Service1.asmx http://localhost/Service2.asmx /out:api.cs /namespace:\*,SomeNamespace.API

Now, when the code is generated, you’ll get a error that Product is already defined, but it’s more of a warning. But because the output is combined, it works! Unlike calling it once for each service. Perhaps I’ll look at newer solutions for newer projects, but thankfully, the annoyance is less for now.