Wednesday, July 20, 2011

Android AsyncTask management

Warning: Coding ahead...

Releasing my own Android app has taught me an amazing amount about Java, Android and the state of commercial app libraries in general. I now have a deep and profound respect for what I considered "trivial" apps, just a few short months ago.

My first app, which still isn't in its final release form (though it's on the Android Market in beta form) had one pretty large problem: it loads images, and at times it would get stuck, forever downloading an image. The solution was to actively manage tasks, of course, but I'm new to Java, and I didn't know much about its task management facilities. I had assumed that it was fairly straightforward, and I would simply:

 task.cancel();

Alas, that's not it. as I quickly discovered, the AsyncTask class's cancel method is more of a hint. It tells the task that it should wrap up what it's doing, by setting a semaphore (or whatever the underlying mechanism is) and it's the job of the child task to check in with the isCanceled method and react accordingly.

This presented a great deal of complication for me. When the user presses the "back" button or clicks outside of the progress dialog to cancel a download, I don't want to leave a stranded download going, but the download code isn't mine, it's part of the BitmapFactory class. How can I tell it to stop what it's doing? The solution is fairly complicated, and I hope that I'll refine it or find there's a better way over time. Here's what I arrived at (after the break):

Saturday, July 16, 2011

Why defaulting isn't an option

I was in a hotel room tonight, so I found myself channel surfing, and Bill Maher was on. His guests were trying to understand the urgency of the debt ceiling crisis (a crisis that exists only because our politicians choose to make it a crisis). Several times they got confused, with one guest arguing that it didn't matter if we defaulted for a short period, because it would just trigger increased interest rates, which the Fed could just turn around and modify.

His confusion surrounds the imprecise use of the term, "interest rates." There are a lot of interest rates that the government interacts with. One of them is the prime lending rate, which is the rate at which banks loan money (it's actually a starting point which can be modified by circumstances, term, etc.) The prime lending rate is based on the rate that banks will charge each other for a loan, called the federal funds rate. This rate is currently 0-0.25%, which means that banks are essentially loaning each other money without or with very low interest, allowing prime lending rates of approximately 3.25%.

That's the number that Bill Maher's guests were talking about, but it has nothing to do with the debt ceiling crisis (at least directly). To understand why, we need to look at a second type of loan: treasury bills are IOU notes that the government writes over short periods of time. An investor buys one of these T-bills for some amount less than they are worth and in 3 to 12 months depending on the T-bill, they "mature", meaning that the government pays off the investment at its face value. So, if you bought a $1000, 3 month T-bill for $990, you would make a $10 (1%) profit in 3 months when the government paid it off.

The rate is based on a number of factors including supply (the amount the government wants to borrow) demand (the amount investors want to invest) and the risk that the note won't be paid off. In the case of the U.S. government, that risk is considered to be as near to zero as it is possible to get in the realm of investment. Because U.S. government debt is considered the standard for low-risk investment, much of the market is geared toward treating the T-bill rates as a baseline.

A change to that baseline (e.g. because of a single default) would radically change the investment landscape in a way that we can't fully know, because it's never happened before. This is because investors (not the Fed) would not be willing to buy T-bills at the same rate. So, what's wrong with a higher interest rate on our debt? Well, for starters, it means that all of our current projections for the federal deficit over the next several years would rocket up. What's more, all of the large, institutional investors (including nations) who buy U.S. debt would have to scramble to determine if it even made sense any longer, or if they should be buying someone else's debt, possibly reducing demand, and further driving up interest rates. The impact of this on the value of the dollar, inflation and other aspects of our economy is a matter for debate, but it would likely produce a shock wave through every aspect of the economy.

Ultimately, the U.S. would continue to find buyers for its debt, but not before the market had to absorb a fundamental shift in its underlying assumptions at a time when we're just coming out of a major recession. Such economic turmoil at a time like this would be very likely to trigger a return to recession or worse. It could also destabilize investment firms, causing another wave of failures. During the last wave of failures, we relied on the U.S. government's excellent credit rating to borrow funds to see us through the crisis. That, of course, would be changed this time around.

So, as you can see, the "interest rate" on T-bills is very different from the Fed-controlled federal funds rate, and the lack of concern showed on Maher's program is just the result of a misunderstanding about how much impact this process could have. I'm no economist, and I'll admit to ignorance on some of the details, here, but what's important to understand is that ignorance is the basic problem, here. We just don't know how bad this would be because it's unprecedented. We just know that it's a pretty awful idea, and there's no excuse for not paying our bills.

Saturday, July 2, 2011

Google+ problems with apps

If you are having a problem signing up for Google+ and you use Google Apps along with a regular Google/Gmail account, try copying the invite link, and then signing out of your Google Apps account, and then visiting the invite link. This worked for me.

Transformers 3: Dark of the Moon review

Transformers 3: Dark of the Moon was a good movie.

OK, now that I have your attention, let me explain. I've seen better movies, but I haven't seen a better Transformers movie, and even when compared to blow-em-up summer tentpole movies, this one is pretty excellent. First, let me cover its flaws, and then I'll get into a spoiler-free list of what I liked.

The biggest problem with this film is its running time. At 157 minutes, it's about 45 minutes too long, at least. The plot isn't that complicated, but there are a lot of moving parts, and every character has their establishing scene, some cute jokes and a later thread in the climax. There's some weak scripting work here, where a good and disciplined screenwriter could have combined some of those threads and still done justice to all of the characters.

That said, there are some characters that need to go, though. The parents just aren't funny, and I know everyone loves the goofy spy, but I'd forgive Bay if he flushed this character down the drain. Ditto the cutesy tiny Autobots.

The replacement hot babe is forgettable. She seems capable of saying her lines, posing and screaming on command, but she's never given anything else to do, which is a shame. Perhaps less time spent on her underwear and more time spent using her like a Doctor Who companion would have helped negate the need for a good deal of Sam's parents and his own whining. Speaking of Sam's whining, the alternating whining/bravado thing gets old around minute 7 and stays that way until about minute 155.

So, why did I like such a flawed movie? Where to start? It's a 3D movie that's enriched by 3D, but absolutely doesn't need to be seen that way. It's funny. The sheer volume of self-aware, self-referential, and genre in-joke one-liners in this movie is shocking. The CG is art. Flat out, art. If you come away from this movie saying, "great CG, but there's no art to it," then you're just stuck in the mid-20th century. I'll grant that, like the great album covers of the 80s, Transformers's art is communicating in the language of male teen heroic fantasy, but it's still art.

I'm also stunned by the cameos, great supporting characters and some of the nods that are made to previous appearances. Just reviewing IMDB (no spoilers) you have Leodard Nimoy whose last Transformers appearance was in the 80s animated movie as Galvatron. There's also John Malkovich and Alan Tudyk, both of whom could probably get me to go see a movie I would otherwise avoid, and they don't disappoint, here.

The action scenes are truly impressive. There's one slow-motion sequence that's pure genius, and must be seen to be believed. You'll know it when you see it, but just in case you were not sure, it involves a tire being batted aside.

My only real complaint that I voiced coming out of the movie, besides the running time, was the physics. Yes, I know that it's a fantasy, and we're not supposed to be questioning the physics, but it's pretty glaring. There are times when Sam should be turned to paste because he's experiencing G-forces far beyond what a human can tolerate. There are the usual stupid "run through window and live" scenes, but it's even worse, here. Still, you didn't go in expecting accurate physics, right?

Overall, I'd give it about 4 out of 5 stars. Much as I enjoyed it, I can't reasonably give a full 5 stars to a movie that drags on as much as this one does.

PS: Bonus points to anyone who points out at least 2 Star Trek references from this film in the comments.