Posted by: Cliff | March 8, 2013

When refactoring goes horribly wrong


You’ve been down this road before. You’ve added a parameter to a constructor and now you have 2 parameters with similar names. You merely want to distinguish between them both. A quick lookup to see how the original parameter is used reveals that it is not an “id” rather it is a shortcut. After refactor/rename action in your IDE  you wait for satisfaction which should only take seconds. Several minutes tick away while a progress bar fills, refills and is eventually joined by several other progress bars each taking their time to fill to completion. Hi, I’m Cliff. You’re here because you’ve been staring at your screen patiently waiting for several progress bars to bring you refactoring pleasure. I’m here for much of the same reason.

Every so often I hit an area in my code that doesn’t refactor exactly as I had anticipated. It happens much less frequently since I’ve moved back into Java development and can use the world’s best refactoring IDE. Still it does happen from time to time. This morning I was bit when I attempted to rename a class member named “id”. Apparently the my IDE is having trouble tracking down all 5 usages in the current file as it scans my entire project for these 2 very common characters. I’m not sure why it’s happening (it’s still running as I type) but I am guessing it is because I inadvertently left the “scan all non-code usages” option checked. I’ve had much worse war stories of refactoring under XCode which I will save for the future but I felt compelled to share today’s horror story since it is so rare that I have such headaches these days.  Refactoring is actually my favorite part of development as I incrementally massage horrific code into easily managble bite-sized chunks but every so often it pays to watch which options you leave checked in the refactor dialog.

Posted by: Cliff | March 5, 2013

IntelliJ Screencast


You have a killer idea for an Android app. You’ve discussed it with the few folks who matter most, your buddy, your barber, and the clerk at the local convenience store where you buy your lotto tickets on Thursdays. They all agree you have a winner now all you need to do is create it! Hi, I’m Cliff. You’re here because you’re moments away from pushing the next $$multi-million app to the Android Market. I’m here to prepare you with the correct tools and know-how. Below is my 1st screencast, an introductory tutorial for using IntelliJ Idea. IntelliJ is what I consider to be the best IDE on the planet.

Posted by: Cliff | February 25, 2013

There’s “no such method” to my madness, man…


So you’re writing a web application and it, like, needs to run on one of the many mobile devices invading society these days. You need to invoke a function or a method that doesn’t exist on the class you’re dealing with. You won’t know the name or arguments of the function until the Jaavscript begins executing on the mobile device. Hi, I’m Cliff. You’re here because you can’t predict your functions and methods in advance of coding your application. I’m here because I wanted to post a really quick tip for those of you dealing with crazy Javascript on a modern browser.

Ruby, Python, Groovy, and even Objective-C have a menas of catching and interpreting functions or methods that have not yet been defined. It’s one of those new cool-kid programming tricks that these languages employ which allow developers to define programming languages that take the shape of the problem they are trying to solve. (See write-ups on BDD, and DSL to understand further.) Javascript, historically, has lacked such features. Notice I say historically… I’ve been studying just such a feature enabled by a magic method called __noSuchMethod_. I’ll be spending the next few days understanding its feasibility and limitations on a mobile web browser. That along with the Java to Javascript bridge feature in Android. Check it out and you can thank me later…

Posted by: Cliff | February 24, 2013

I’d like to show you my Weinre


…and if that didn’t get your attention maybe this will. I was looking for a way to interactively debug Javascript running on my Android phone when someone else went and showed me his Weinre. It is completely “Banonkers” (That’s “bananas” and “zonkers” together, I just heard a political correspondent coin this phrase on some news show, no joke…) and totally useful if you have anything web related and of reasonable complexity that needs debugging/tracing through the client.

Posted by: Cliff | January 21, 2013

Nexus 7 stuck in Reboot mode


You left your Nexus 7 on all day and, unbeknownst to you, there isn’t enough power left in the device to draw the letter “A” much less boot the device. You plug it in and while noticing the familiar black screen, instinctively hold the lock screen button in an attempt to power it on. The Google logo followed multi-colored “X” which is eventually followed the home screen whets your appetite for some advanced Droid play only to hear an unexpected chime followed a power down notice followed by  by static. Hi, I’m Cliff. You’re here because you’re locked out of your Nexus 7 and all of its robotic goodness. I’m here to comfort you, reassuring that everything is ok, there is always a way back in. Read below and you’ll be shaking hands with R2-D2 in no time.

I found the key to Nexus lockout on another forum and it totally restored my ability to power up in peace:

- Assuming your unit can boot long enough to do this, press and hold all the buttons (that is the lock button and both volume buttons) to give the device a hard reset.
- Keep holding the buttons till it reboots to the bootloader.
- Using the volume buttons, cycle through the menu selections and choose to power off the device. (The power off option will point to the lock button which you click to power it off)
- Unplug then re-plug the charger.
- You should now see the battery-charging icon. It shouldn’t boot up on its own anymore. Just let it sit and charge to full capacity.

Posted by: Cliff | January 6, 2013

Talking TCP to a server on an Android emulator


You had a really slick idea. You wrote this TCP server that accepts socket connections and it runs on an Android device! You’ve been testing it all day and debugging it until it was bullet proof. You arrive home ready to test the last 4 liner modification you made just before leaving the office when you realize you left your device in the office! No worries, fire up the emulator and connect the test client on your mac to…? You test client expects an IP address and a port! Hi, I’m Cliff. You’re here because you left your Android phone on your desk underneath the 5oz bag of Lays BBQ chips. I’m here to give you some brief info on how to get your emulated Droid talking TCP  to its host environment via a telnet session and a redirect command.

The first thing you need to do is determine which port your emulator is bound to. The port number is usually listed in the title bar of the Window your emulator runs in. (Unless you’ve specified an override, the 1st emulator instance you start will bind to port 5554, and the next will bind to 5555 and so forth.)

Emulator Screen

Next, open your terminal (or “cmd” command prompt on Windows) and start a telnet session on localhost. This will connect you the emulator control console where you can enter port redirect commands. The command for redirect is:

redir add <protocol>:<host-port>:<guest-port>

So let’s say your socket server is running on port 8080 on the emulator and you want to redirect all traffic going to port 8081 on your Mac (or Windows PC), you would enter:

redir add tcp:8081:808

You can now “exit” the telnet session and from thenceforth all traffic hitting localhost:8081 will be forwarded directly to the emulator on its port 8080! When you are done with port forwarding you can connect to the emulator control console again via telnet and remove the port forward with the following command:

redir del tcp:8081

These comands work equally well with UDP. Use:

redir add udp:9001:9000

To forward UDP traffic from 9001 on localhost to the emulator’s port 9000 and use:

redir del udp:9001

To undo the change when you are done. There are various other things you can do with the emulator console. For more info see the Android developer docs on Using the Emulator Console.


A short post for those who are wondering if I’m still alive. I got an error trying to run unit tests in an Android project under IntelliJ Idea and I got stumped. After a few minutes of Googling it dawned on me that I had chosen to run the test using a JUnit run configuration instead of an Android run configuration. That is when I right clicked the test method, my mind insisted on chosing the more familiar white window-looking icon with the red and green arrows instead of the Android icon, also with red and green arrows. I was associating the android icon with execute apk and not run tests. I now notice the little chevron-looking arrows and realize they refers to the red, green, refactor life cycle of development.. duh!

Posted by: Cliff | October 2, 2012

Make an iPhone ringtone!


I’ve been away from XCode and iOS for some time, doing my bid in Android land. (I’ve also seriously neglected my blog, apologies to both of my loyal readers, I know there’s only two of you!) The interesting thing is how much you realize you knew when you go back. Even more interesting is how much you forgot and has much changes in a brief matter of months. I overheard some of my former iOS coworkers talking about lsd errors and thought they were taking drugs. At any rate, today’s topic is a simple tip that I keep forgetting.

Ringtones for iPhones!
This is a two step process involving conversion and consumption. Ringtones must be in the m4a file format but use a “.m4r” extension. The easiest way make them is to use “afconvert” to convert any existing audio file to “m4a” format using this syntax:

afconvert -v -f m4af ./MyInputFile.wav ./MyOutputFile.m4r

The -v flag allows verbose output which let’s you see what the command is doing. The -f is a way of specifying the output format. Consumption of these ringtones is as simple as dragging and dropping the “.m4r” file into iTunes under the ringtone section so that the next time you synch your device it will include the new ring tone. You may also need to select your device in iTunes, click on the “Tones” tab and enable ringtone sync in iTunes for this to work.

Posted by: Cliff | May 30, 2012

Groovy DSL fun again!


So I’m writing these ad-hoc Groovy scripts to update a chart on an internal wiki page when I start feeling the urge to play with the syntax. Groovy is so much fun to use that you just can’t help but mangle with things once you get the hang of it. Hi, I’m Cliff. You’re here because you like to alter the rules of your programming language while it’s running.

It’s been some years since I fooled with GSpec and all the meta-programming stuff. So now I start my latest script (half into the project) and decide to express what I want the script to work like before I write the code. It’s a fairly straight forward task and I find it very easy to bend Groovy syntax to say exactly what I’m thinking while remaining executable. So I start with this:

def whenWeConvertItToTableMarkup(givenJson, andGivenTableMarkup, andGivenApkVersionString){
    return "I'm not converted yet! :( "
}

def then(action) { action() }

def givenJson = '''
{"my-latest.apk":
    {"totalSizeFormatted":"14.84 MB","imagesSizeFormatted":"6.22 MB",
    "textSizeFormatted":"426.37 KB",
    "resourcesSizeFormatted":"2.04 MB","executableSizeFormatted":"1.15 MB","audioFilesSizeFormatted":"376.10 KB",
    "totalSize":15562499,"imagesSize":6517513,"textSize":436603,
    "resourcesSize":2141448,"executableSize":1202497,"audioFilesSize":385126},
}'''

def andGivenTableMarkup = '''\t| Version | 2.6.0.95 | 2.7.0.907 | 2.7.0.909 | 2.7.0.911 | 2.8.0.20 | 2.8.0.16 |
\t| Executable | 574.13 | 598 | 598.94 | 598.54 | 594 | 592.80 |
\t| Resources | 1580 | 1720 | 1720 | 1720 | 1800 | 1800 |
\t| Images | 3980 | 5240 | 5240 | 5240 | 6170 | 6160 |
\t| Text | 264.35 | 278.59 | 278.57 | 278.59 | 293.30 | 293.93 |
\t| Audio Files | 376.10 | 376.10 | 376.10 | 376.10 | 376.10 | 376.10 |
\t| Total | 11550 | 13110 | 13080 | 13110 | 14160 | 14370 |
'''

def andGivenApkVersionString = '2.8.0.25'

def result = whenWeConvertItToTableMarkup(givenJson, andGivenTableMarkup, andGivenApkVersionString)
then {
    assert result == '''\t| Version | 2.6.0.95 | 2.7.0.907 | 2.7.0.909 | 2.7.0.911 | 2.8.0.20 | 2.8.0.16 | 2.8.0.25 |
\t| Executable | 574.13 | 598 | 598.94 | 598.54 | 594 | 592.80 | 1174.31 |
\t| Resources | 1580 | 1720 | 1720 | 1720 | 1800 | 1800 | 2091.26 |
\t| Images | 3980 | 5240 | 5240 | 5240 | 6170 | 6160 | 6364.76 |
\t| Text | 264.35 | 278.59 | 278.57 | 278.59 | 293.30 | 293.93 | 426.37 |
\t| Audio Files | 376.10 | 376.10 | 376.10 | 376.10 | 376.10 | 376.10 | 46.09 |
\t| Total | 11550 | 13110 | 13080 | 13110 | 14160 | 14370 | 15197.75 |
'''
}

Then taken some learning from my recent Python training I thought, “Hey, why not include this test as an expectations string at the bottom of my script, similar to how Python uses doc-strings!”

def expectations = """
def whenWeConvertItToTableMarkup(givenJson, andGivenTableMarkup, andGivenApkVersionString){
    return "I'm not converted yet! :( "
}

def then(action) { action() }

def givenJson = '''
{"my-latest.apk":
    {"totalSizeFormatted":"14.84 MB","imagesSizeFormatted":"6.22 MB",
    "textSizeFormatted":"426.37 KB",
    "resourcesSizeFormatted":"2.04 MB","executableSizeFormatted":"1.15 MB","audioFilesSizeFormatted":"376.10 KB",
    "totalSize":15562499,"imagesSize":6517513,"textSize":436603,
    "resourcesSize":2141448,"executableSize":1202497,"audioFilesSize":385126},
}'''

def andGivenTableMarkup = '''\t| Version | 2.6.0.95 | 2.7.0.907 | 2.7.0.909 | 2.7.0.911 | 2.8.0.20 | 2.8.0.16 |
\t| Executable | 574.13 | 598 | 598.94 | 598.54 | 594 | 592.80 |
\t| Resources | 1580 | 1720 | 1720 | 1720 | 1800 | 1800 |
\t| Images | 3980 | 5240 | 5240 | 5240 | 6170 | 6160 |
\t| Text | 264.35 | 278.59 | 278.57 | 278.59 | 293.30 | 293.93 |
\t| Audio Files | 376.10 | 376.10 | 376.10 | 376.10 | 376.10 | 376.10 |
\t| Total | 11550 | 13110 | 13080 | 13110 | 14160 | 14370 |
'''

def andGivenApkVersionString = '2.8.0.25'

def result = whenWeConvertItToTableMarkup(givenJson, andGivenTableMarkup, andGivenApkVersionString)
then {
    assert result == '''\t| Version | 2.6.0.95 | 2.7.0.907 | 2.7.0.909 | 2.7.0.911 | 2.8.0.20 | 2.8.0.16 | 2.8.0.25 |
\t| Executable | 574.13 | 598 | 598.94 | 598.54 | 594 | 592.80 | 1174.31 |
\t| Resources | 1580 | 1720 | 1720 | 1720 | 1800 | 1800 | 2091.26 |
\t| Images | 3980 | 5240 | 5240 | 5240 | 6170 | 6160 | 6364.76 |
\t| Text | 264.35 | 278.59 | 278.57 | 278.59 | 293.30 | 293.93 | 426.37 |
\t| Audio Files | 376.10 | 376.10 | 376.10 | 376.10 | 376.10 | 376.10 | 46.09 |
\t| Total | 11550 | 13110 | 13080 | 13110 | 14160 | 14370 | 15197.75 |
'''
}
"""

println expectations

Taking it a step further I can pass the expectation string to an eval call (or whatever the Groovy “eval” equivalent is, I’ve been mixing my scripting languages a lot these days…) and execute the test while passing the current object into the evaluated context. I can do all of this without frameworks or anything fancy. If I sprinkled just a little meta-programming into the mix then I could remove the definitions for the “then” and the “whenWeConvertItToTableMarkup” methods and have it read like a real spec!

Posted by: Cliff | May 27, 2012

What’s missing from Siri


I love Siri. I use her on my daily bike ride to work, asking her what time it is. I use her while driving to text my wife and read incoming messages. I’ve asked her where the closest bike shop was, and use her to find all sorts of interesting info. But there’s a piece missing from the experience. Maybe it’s me just being an engineer. Hi, I’m Cliff. You’re here because you’ve noticed something missing from Siri but you can’t quite put a finger on it. I’m here to explain what that piece is.

Yesterday I asked Siri to set an alarm to remind me when to pull the lasagna out of the oven. I believed I had the alarm set properly but when it went off the lasagna was far from cooked. It needed 1 hour and 55 minutes and I started to wonder if Siri only caught the 55 minutes part of my earlier request. I wanted to go back and see when I had asked for the alarm to be set and that’s when it hit me. Of all the conversations I’ve had with Siri, (some loony and ridiculous) I’ve never bee able to go back and recall or check the times that they were held. Conversations between me and Siri seem to roll off into the abyss. I believe that much like the web browser there is a case for history in Siri. I Binged and Googled for an answer and I even went as far as Yahoo but I couldn’t find a way to lookup old conversations between myself and Siri. Maybe after this year’s WWDC?

Older Posts »

Categories

Follow

Get every new post delivered to your Inbox.

Join 212 other followers