Author Archives: Josh Hoffman

Static on my Android

This post is mostly to document a pretty tricky little ‘gotcha’ in Android, but I’ll give some background on how I discovered it as well. I’ve been wrestling with a minor bug in MtgHelper lately, mainly for something to do / something to keep me sharp. (RTMg.mob is in a holding pattern at present until Rob has time to tweak the CGI a little.)

Essentially the bug deals with how my two main application modes interact. Normally, MtgHelper operates from an Activity bearing the same name. There is an alternate view mode which operates from the Head2HeadMode Activity which splits the screen and changes how some methods work as a result. In particular, Head to Head ignores orientation changes.

The bug occurs in head to head mode when the user ‘changes orientation’ and then presses the back button. A second copy of the Head2HeadMode activity gets created, so the app gives the illusion of needing to press the Back button twice to get back to the home screen.

I ran into a very interesting snag when trying a solution to correct this bug. I created a static boolean variable (so that I could share it across my two activities without bundling it with my other data). Instead of painstakingly coding the state-saving stuff in the several places it would be needed, I wanted the variable to live and die with the application’s Activities. Interestingly, due to the static variable declaration, the variable was seeming to outlive the application!

As it turns out, even when you use the Back button in Android to completely close an application by taking all of the activities off of the stack, there can still be an Activity process lingering around for a while. This process is every bit as entitled as anything else to have static access to your variable. This results in unpredictable behavior wherein the variable appears to save state sometimes, but may not always. If Android decides to garbage collect my lingering process, the static goes with it.

Using windows ‘safely’

This topic has come up a few times in the lab recently, so I decided to write a guide on it. I’ll omit the Linux advertisement, but if you are going to (or have to) use windows for something, here are some good practices. All of these tools are (at the time of this writing) free to the public for home use.

Back up important stuff. This goes for everyone, actually. (Never mind the bit about me not practicing what I preach…)

Use avast! antivirus. It is fairly unobtrusive and has its virus definitions updated really often. If you don’t like avast!, people alternatively use AVG (although they’re getting better and better at hiding the free version). I believe that both programs come set up automatically, but you should make sure that your software is A. updating itself regularly and B. actually performing scans at some point.

Anti-spyware stuff tends to be good too. Antivirus will catch some of this, and they’re better and better at it lately, but I still prefer to use a cocktail of anti-bad-stuff on my windows machines. I recommend using a combination of Spybot Search & Destroy and Ad-Aware. NOTE: it seems that, unfortunately, Ad-Aware has gone the way of ZoneAlarm. The link to the free version exists but is broken. Gross. If you can find a free version of it please post the link. Otherwise, I must unfortunately recommend that readers do not install Ad-Aware by jumping through the hoop of their stupid “complete one offer” scam. Be sure to update your anti-spyware after you install it. Spybot in particular has an “Immunize” function in addition to scanning. This is perhaps the biggest motivator for installing spybot. Blocking spyware entirely is awesome, and makes sure you waste less time removing stuff over and over.

Use Firefox. (Chrome seems cool too, but I don’t have any tips for securing Chrome, or much knowledge about it in general). Lock down your Firefox using NoScript and AdBlock Plus. NOTE: After installing these two plugins, websites are going to look a little…different. Particularly NoScript. There will be a little ‘S’ in the bottom right hand corner of your Firefox now that you will often have to click and tell it to ‘allow’ scripts from certain trusted websites. This can be annoying at first, but is worth the added security, especially if you tell it to remember your choice and always trust,,, or whatever. Basically, any site you know isn’t trying to do something to your computer you can trust, but beware that the plugin is only as good as your decisions are.

I also recommend WebOfTrust for finding out what websites are shady or not in the first place.

I hope these links help you secure your Windows install. If you have any questions about using the aforementioned software, feel free to ask in the comments or email me and I’ll do my best to help!

Making Android Videos

Since I agonized over how to make these videos both times I did it, I thought I’d write up a guide. I’ll assume that readers have already developed an Android application (or whatever else they want to take video of) and know how to run it for display.

To capture the video, I reccomend using Ubuntu and downloading gtk-recordMyDesktop. All of the mac/windows utilities I’ve found are paid and while some (read: Fraps) are pretty reasonably priced for the quality of the tool, free is always nice. Gtk will also record screen regions easily, which is ideal if, like me, you only want a portion of your screen recorded. In my case, I draw the record region around the mock cell phone displayed by the Android emulator. The program will output a .ogv file when recording is done.

To get this file into a format that your video editing software will care about, you have to do a little file format dance as follows. Open a terminal window. Write: ffmpeg -i inputFileName -b 5000k outputFileName.desiredFormat┬á (inputFileName is your output file from gtk. outputFileName is what you want the transcoder to spit out at the end. desiredFormat is .mov if you’re using iMovie.)

I’d like to note that I didn’t come up with that nice command-line jutsu on my own. I found it after Googling around for a while. I’m not entirely sure what all of those flags are for as I didn’t actually read the man page on ffmpeg.

Next, boot up your mac or whatever, transfer the files over, and edit away! Happy movie making!

AAAS Breakfast w/ Scientists

This morning I went to the AAAS breakfast with Rob, Liz, and Ramy. I can’t say enough good things about the experience! The venue was great, the food was good, and the students were awesome!

I was really impressed by the quality of their research. It seemed like a lot of the projects were very much real, useful research endeavors that demonstrate a wealth of potential. I found that I got a lot out of speaking with the students, and I did my best to give them worthwhile tips about college and research. (The tips about research basically amounted to: “do research. it is awesome. free trips!”)

More than anything I was surprised by how far along a lot of these students are, compared to what I was doing when I was in high school. I truly feel that they all have promising careers in science ahead of them should they continue on that path. I would highly recommend attending future breakfasts; you will get at least as much out of it as the students do! And besides, you can’t beat a free breakfast, right? Right?!

Side Project Progress

I’m finally making some headway on my customizable tables side project. I spent most of today working on the Javadoc stuff that I’m (still) stuck on with RTMg.mob, but eventually got fed up with it. Significantly, I found a relevant discussion of the problem on an Android Google Group and posted there. Hopefully that will turn up something useful to me in the next few days. Meanwhile, I’ll keep trying to figure out solutions to it myself and devote spare time to the customizable tables.

As for the side project, previously I had been doing some work to create and manage data structures to keep track of my custom UI. That turned out to be pretty difficult and kind of a nightmare to manage. I went around in circles trying to make my management scheme less horrible before I decided to try an orthogonal approach. If I could stick custom UI into a ListView, Android would manage all of the references to my UI elements for me. Additionally, it would give me an easily regulated vertical layout to push data into. This just happens to be an ideal way to format my tables.

I found a couple of approaches to create ListViews with custom Views as their contents. Of course, the problem with these approaches is that they again assume that I know what I want at compile time. When this is known, the xml resources for creating layouts are absolutely stellar at making layout creation easy. Unfortunately, by definition I can’t know this, nor can I have my program write xml for Android at run-time. My solution (so far) is to take the second example I found and go hack, hack, hack on it.

Specifically, I mimicked the row.xml layout by creating an hierarchy of View objects in the java code, changed all of the references to point to my objects instead of the xml, and then deleted the row.xml entirely. I was fortunate enough to have this not only compile, but actually just work the first time I ran it. For you non-computer scientists out there, that’s the programming equivalent of hitting the jackpot. It only happens every now and then to me (if I’m working on something that I don’t already know how to do) and it is always a really nice feeling.

The next step in moving this project forward is to come up with a clever, reasonable scheme for programmatically generating these types of layouts at runtime. I already know that I’ll be using an SQL database which I’m cheating using JSON shenanigans. The piece that is still missing is to parse that JSON into meaningful layout data. I also have to write the JSON based on user input specs, but these are two halves of the same problem. Essentially I need to create a JSON ‘code’ for describing UI layouts. Overall, I definitely must admit that this is the craziest thing I’ve ever tried to make Android do. As I was describing the lengths to which I’ve gone to create this weird little app, I realized that it is a lot of unconventional, weird code. This leads me to believe that the project will either end up crashing and burning completely, or becoming something cool and unique. Lets hope for the latter! ­čÖé

Edit: I thought for a minute I was being a dummy for using ListView instead of TableView, but it turns out that I may not be. ListView allows me to easily hook my UI up to my database, which is a lot more convenient than managing the queries myself. I had read that ListView did not allow for the items contained within to be clicked on, but this is definitely not the case (I just tested it). Furthermore, each row in a ListView is itself clickable and can be assigned an OnClickListener.

The new face of RTMg

Many in the lab are probably already aware of this, but I realized that a lot of it happened quietly and informally in conversations between Rob, Daniel, and myself.

Given the push towards releasing information to the public about RTMg, OS Metagenomics, and Mobile Metagenomics, we sat down to talk about a few things. First, we wanted a cohesive name for all the junk I just typed. We settled on keeping the RTMg name, but branching it out to be an umbrella for all three services as follows:

  • RTMg -> RTMg.web
  • MM -> RTMg.mob
  • OS M -> RTMg.os

Furthermore, given that Daniel and Rob were contacted by a magazine editor to do a small press release about the project, we wanted to have a nice pretty place for people to check out the RTMg applications suite, and check for updates. In a flurry of activity, I have created such a place.

RTMg now lives here!

If you have any suggestions as to things I should add to the site, let me know. I’ll do my best to add them!

(a big thanks goes out to the creators of the website template I used – Arcsin. Check them out if you ever need free website templates!)

A fun new side project

Given that I’m in the lab full-time lately, and most everyone else has a lot of other commitments now due to the semester having started, I’ve found myself with a bit of spare worktime. I wanted to do something productive while I wait for an opportunity to collaborate with Rob and Daniel on making all of the RTMg User Interfaces cohesive. I’ve had an idea in mind for an app that I’d like that involves my dorky tabletop gaming hobbies. As I was thinking about that app after work one day, I realized that it is actually a great opportunity to develop a potentially useful custom database technique for Android. Without going into detail about my own personal interests in having this sort of capability, I thought I’d post about what I’m hoping to accomplish. Check it out behind the read more!

Continue reading

White Belt in Droid-Fu

I have mastered the first steps of Droid-Fu! Mobile Metagenomics will now blissfully continue working if a user hits the home key and does something else for a while before returning to see their results. There is a bit of awkwardness involved in testing this, because it only works correctly after I do a complete build of the .apk file and manually install to the emulator via command line. (Essentially what that means is I have to pretend I’m doing a code release each time I test a change…) Things that still need work/could be added include:

  1. OTUs mode – currently doesn’t do anything. (Edited) Done!
  2. Loading from Server – currently not implemented. (Edited) Done!
  3. Notification Bar Message on results complete – not implemented, not looked into yet. Difficulty: Unkown (probably Medium)
  4. Indeterminate progress indicator in title bar during downloads 2..n – not implemented, totally optional. (Edited) Not possible from what I can determine. It seems to be either-or with this and the progress bar. Though I could’ve sworn I used to have both…

From there it is just a matter of documentation, code cleanup, youtube videos, and publishing.

Practicing Droid-Fu

I’ve had a few minor breakthroughs on the K├Ąppler code I wrote about yesterday. I now have a Droid-Fu / MM test application with a working function mode, including pretty progress dialogs and bars that actually behave correctly! (Those who were in the lab during the week that I spent getting those to work the first time know how much this success means to me…) I’ve begun migrating the new code I developed into the actual Mobile Metagenomics project and I’m having to make some adjustments.

The most drastic change I’m currently considering is abolishing the two-activity model that MM currently employs. What I mean by that is: MM currently has two major phases (actually Android activities) called MobileMetagenomics and ResultView. The first activity is the one where the user is presented with menu options to choose the type of annotation that they’d like to perform. The second is where the user goes as soon as they click the “upload” button; they are shown progress dialogs etc and eventually their results. This is all good, sensible, and very object-orientated.

Unfortunately, due to the nature of the application and the concurrency that I’m trying to implement, the two-activity model is proving problematic. My single-activity test case doesn’t have a ‘menu’ sort of activity loaded up in front of the results; consequently, it behaves just fine if I hit the home key and relaunch it a bunch of times while it does work for me. On the other hand, two-activity MobileMetagenomics is currently choking on the handoff between MobileMetagenomics and ResultView. There probably exists some clever way of making them get along, but I’m not sure it is worth the time investment to find this method.

The other solution that I’m considering involves hiding (or destroying, or something) pieces of the UI based on what the user is doing and making visible other parts of the UI as needed. ResultView already has some UI elements hiding that are only displayed when a graph is requested, so that part shouldn’t be an issue. What concerns me about this approach is how to handle the user doing one annotation and wanting to go back to the menu. Currently MM handles this just fine – the back button kills whatever work they were doing and returns from ResultView to MobileMetagenomics.

I’ve definitely got some thinking to do before I figure out the proper course of action. The good news is that the code to actually do the uploads asynchronously works. I just need to figure out how to slot that code into MM properly.

More K├Ąppler Code

A few months back I wrote a few blog entries about some Android code written by Matthias K├Ąppler, an Android developer from Germany. He has written several ‘helper’ classes for Android methods which he felt needed to be improved. While folks like myself and K├Ąppler often complain about the features of certain Android methods, I want to make it clear that we actually really appreciate the fac that Android lets us write our own methods to replace theirs. The Android team has a lot on their plate, and if programmers like K├Ąppler can help them along in certain areas, more power to them.

The real focus of my blog entry today is to talk about what is currently going on with MM. As I said in lab meeting recently, the realtime code broke and after a few days of trying, I still couldn’t figure out why. I was browsing K├Ąppler’s page on his old concurrency code that I was using and I saw a little note informing me that he had better code to replace the old classes with. I made the executive decision to bite the bullet and learn a new way to write concurrent Android code.

Since then I’ve been struggling a bit with learning the new BetterAsyncTask, but I’m finally making some headway. I have a semi-working proof of concept, and I’ll probably comment on K├Ąppler’s blog to get help with a few things I can’t figure out. Unfortunately he’s not been able to post any example code yet, so I’m left wandering through his code and javadoc to figure things out. So far, I have code that mimics the ‘Function mode’ annotations of MM, but doesn’t have any pretty dialogs or progress bars yet. K├Ąppler says he’s made these things better, and I’m sure once I figure out how to use his code, I’ll have some sweet dialogs / bars.

Edit: I found a hidden gem of knowledge about the Android Manifest that should save a lot of headache in the future. I was having an issue where new instances of my application would be launched every time I clicked their launcher. My desired functionality was to resume the original instance which I’d moved away from by hitting the Home key. Apparently, this is not the default functionality. In order to get this functionality, one must add the following line to their activity tag in the android manifest: android:launchMode=”singleTask”