• Get a list of Months by Locale in Android…

    by Matt Terry

    I was working with a client trying to decide on long and short DateFormats for an international mobile application.  (Yes, that’s a mouthful.)

     

    I wasn’t able to quickly find any documentation on the Java SimpleDateFormat strings by locale…so I did what any Passionate Programmer would do, and I wrote a method to do it for me.

     

    OutputStreamWriter out = new OutputStreamWriter(openFileOutput(
            "months-by-locale.txt", 0));

    for (Locale l : Locale.getAvailableLocales())
    {
        out.write("Locale = " + l.getDisplayLanguage() + ", " + l.getDisplayCountry());
        out.write("\n");
       
        out.write("    Long Months...");
        out.write("\n");
        String[] months = new DateFormatSymbols(l).getMonths();
        for (String m : months)
        {
            out.write("        " + m);
            out.write("\n");
        }
        out.write("\n");
       
        out.write("    Short Months...");
        out.write("\n");
        String[] smonths = new DateFormatSymbols(l).getShortMonths();
        for (String m : smonths)
        {
            out.write("        " + m);
            out.write("\n");
        }
        out.write("\n");
    }
    out.close();

    Then, I needed to get this file off of my emulator. To do that, I opened the DDMS perspective, selected my emulator in the Devices tab (emulator-5554), and navigated to data\data\(applicaton name)\files\(file name).

  • Android Handler, in a nutshell…

    by Matt Terry

    The most basic way to handle multiple threads is to use the standard Java threads.  I won’t cover those in detail in this series; however, there are some good docs and examples online to help with those.

    By default, Android apps run solely in a single thread – the UI Thread.  For most applications, this is perfectly fine.  There are, however, some good reasons that your application needs to be multithreaded.  For example -

    • lots of data access – reading/writing to a database is slow
    • download information from the internet – mobile networks can be spotty, or slow
    • sync with other components – for example, performing Bluetooth pairing or communicating with a service

    If you want your thread to be able to interact with the UI and still perform operations in the background, then you’ll need to use some Android specific classes.  The simplest form in Android is the Handler.  A Handler allows you to send/queue messages, execute Runnable objects, and even perform some fine grained scheduling.

    One of the best uses for a Handler is for displaying progress.  When communicating progress via a dialog, or progress bar, or whatever, it is good to use a Handler because the user can still interact with your UI.  For example, if the user decides they don’t want to wait, they could press the back button.  Or, perhaps the operation you’re doing doesn’t require the user to wait, they can continue to use your app while information gets updated asynchronously.

    Thinking of the G+, FB, or TweetDeck apps that try to backfill your stream…if those apps blocked your UI, and made you wait, you’d most likely never use those apps again.  Or worse, you might go as far as leaving negative feedback in the app store!

    So, MaTT, how do we do this?

    The example below uses a Handler to set the progress on the UI.  So while the Thread is doing some lengthy operation (mProgressStatus = doWork();), the Handler is able to update the UI by calling the Post method, and setting the progress.

    <pre>         // Start lengthy operation in a background thread
             new Thread(new Runnable() {
                 public void run() {
                     while (mProgressStatus < 100) {
                         mProgressStatus = doWork();

                         // Update the progress bar
                         mHandler.post(new Runnable() {
                             public void run() {
                                 mProgress.setProgress(mProgressStatus);
                             }
                         });
                     }
                 }
             }).start();</pre>

    The reason you NEED a Handler here, is because you can’t access the UI from the Thread object.  The Android UI toolkit is not threadsafe by default.  If you try to access the View to update it, there is a good chance you’ll get some amazing Null Reference exceptions.

    Each Activity only needs one handler, so you can use that Handler for different tasks or for updating multiple UI components.

    So, MaTT, when do we use this?

    This approach is great for simple, yet long, operations.  You are still able to show progress to your user and you don’t block the UI while it happens.  Using Handlers won’t give you 5 stars in the market, but it can help keep you from getting 1 star.  Handlers are ideal for one-off situations where you need to do something, but don’t want to interrupt the user.  The other beautiful part about using a Handler is, you don’t have to worry about creating some complex messaging architecture that takes a week to develop and test.

    If you find yourself putting Handlers all over the place, consider using a more robust solution like AsyncTasks which I will cover in more detail in my next post.

     

    Android Thread Series:

    1. Overview
    2. Handlers
    3. AsyncTasks
    4. Services and Messengers
    5. Conclusion

     

  • Too many options for Android threads? Let me help!

    by Matt Terry

    As smartphones continue to gain popularity, the demand for mobile applications also continues to grow.  The increase in usage of smartphones elevated the issue of “multitasking” on mobile devices.  (I’m not going to go into details of my opinions on the myth and illusion of multitasking.)

    Ultimately, users want to have multiple applications running, syncing data, and connecting to the internet.

    One solution to meet this demand is to block the UI and potentially display progress.  This can be a problem because users who perceive an application to be slow, sluggish, or non-responsive will give your app poor reviews in the market.  A more ideal solution is to perform long running, slow, or blocking tasks in the background.  The same work is being done, but the UI is snappy, or responsive.  An added benefit is that you won’t end up having one of these gems while trying to get the latest news updates about the NFL Lockout… <sigh/>

     

     

     

     

     

     

    Android has many options for performing background tasks.  Specifically, Android gives you the opportunity to define what process your component runs in, control threads, and communicate remotely with components and processes.  Great!  Let’s use this to make awesome applications.  What’s the name of this magical background task thingy?  Well, get ready, because there’s a list…

    • Process
    • Thread
    • Synchronized
    • Handler
    • Messenger
    • AsyncTask
    • Service
    • BoundServices
    • IntentServices

    In theory, this is great!  However, there are so many options that it can be overwhelming.  And you can’t just start moving everything to a new thread, because multithreaded applications introduce new challenges, like…

    • What about concurrency?  Is that a problem? (That’s a rhetorical question.  Of course concurrency is an issue!)
    • Deadlock, livelock, and  starvation (“Oh my!”)
    • What do my users expect?  If I do things in the background, does that break the communication loop?
    • I’ve heard lifecycle is important, what does this mean?

    In a series of posts, I’ll cover the options, explain possible approaches, and make recommendations on when to use which options. I will even put up some sample code to help illustrate my points.  Along the way, I will answer the above questions and more.  After all, you want to be sure that you’re using the right tool for the job, right?

     

    Android Thread Series:

    1. Overview
    2. Handlers
    3. AsyncTasks
    4. Services and Messengers
    5. Conclusion

     

    1. 3Q Book Review: Persuasive Business Proposals

      by Matt Terry

      Persuasive Business Proposals by Dr. Tom Sant

      What’s the point? The point of this book is to write clearly and concisely, to differentiate yourself, and to better equip yourself with tools for writing persuasively.  Ultimately, this book is designed to help you and your company write winning proposals.

      How was it? Good.  I honestly felt that this book was easy to read.  Many times, there is even some humor written into the book.  Two of the best tools that I took away from this book were NOSE (Needs, Outcomes, Solution, Evidence) and Cognitive Webbing.  These tools seemed to help structure my writing and organize my thoughts.

      Who should read it? Anyone who wants to be involved in writing proposals.  Anyone who is responsible for writing, reading, or approving proposals.  Anyone who is on a team that is involved with communicating about proposals.  Even if you don’t agree with everything Dr. Sant wrote, it gives us some common language to work from.

    2. Today Mt. Hood Was Stolen!

      by Matt Terry

      Today is the day that I stopped thinking about doing something new, and started actually doing it.  I’m talking about Ruby on Rails.

      I wanted to learn something outside of my comfort zone (not too difficult for an embedded guy to get out of his comfort zone).  I tried to learn Ruby once before, but that was well before I knew how to learn.

      Back then, I didn’t really know what being a software engineer was all about.  In the book, Apprenticeship Patterns, there is a group of patterns called “Perpetual Learning“.  An apprentice needs to replace ignorance with concrete skills in order to continue to improve and get better, and ultimately, Walk The Long Road.

      One way to replace ignorance is by expanding your bandwidth.  To me, this basically means – learn how to learn more (hence the name of the group of patterns is Perpetual Learning).

      When I first tried to learn RoR, I didn’t know how to learn new things.  I didn’t know where to look for manuals and tutorials (short of going to Google and letting them guide me).  Now, a few years later, I feel like I am more resourceful, and my bandwidth is larger; therefore, allowing me to learn new things.

      Burke definitely gave me some nudges in the right direction, and over the last couple of days I have gotten completely set up with Ruby and Rails.  I’ve also completely exhausted TryRuby and am actively working through RailsForZombies.  (Which are both pretty fun, I might add…yes, I said fun!)

      If you have worked through the TryRuby guide, then you’ll understand the title of this blog.  This style of learning aligns well with many of the Apprenticeship Patterns.  I hope to continue learning, expanding my bandwidth, and practicing.

      Today, Mt. Hood was stolen…tomorrow, I will aim a little higher.

    3. Variety is not an option…

      by Matt Terry

      Many times I hear that people want a variety, and that variety is a good thing.  With everything – food, work, exercise, hobbies, etc.

      It dawned on me this morning that in the past year at work, I’ve worked on an embedded C project, dabbled in C# and .NET, done some LabView work, evaluated a Matlab project, built a prototype in WPF, and touched on multiple mobile platforms.

      Wow!  And that’s only the technologies that I’ve worked in, that doesn’t include all the different and new processes I’ve been involved with.

      One of our VP’s, Raman, once said that variety is not an option, it’s a requirement at SEP.  I get what he means, now.  I also understand why it’s good for someone to get variety, continue to learn, and continue to do new things.

      It is challenging, but exciting all at the same time.  I wouldn’t claim that I’m an expert in any of those areas, but I’m able to be on a team and be productive, and that’s the important part.  There have definitely been some late nights where I’m doing research on my own, but that’s what I have to do in order to keep up with my peers.

      Do you get variety in your day to day?  What are some of the challenges of variety?  How do you overcome those challenges?

    4. How do you decide!?!

      by Matt Terry

      Lately I’ve found myself digging deeper into Heuristics. This is FASCINATING to me! I’ve been reading more and more about different ways that humans make decisions, and it continues to amaze me.

      Today, I was talking with Chris at work about completely un-related topics, when it occurred to me that I was framing, or “anchoring“, my question and Chris pointed out that he would have asked the question differently (without leading or anchoring to a given bias).  I’ve also noticed what spins, leads, or twists marketing groups include in their commercials to help persuade us to do, believe, or purchase something.

      The value in understanding how humans make decisions is incredible, in my opinion.  I continue to see it in my life time and time again.  I plan on learning more about heuristics and use that knowledge to my advantage!

      Have you seen any heuristics in your day to day? If so, which ones and how did it realize? What was the outcome?

    5. Conversations in Outlook…

      by Matt Terry

      If you’re anything like me, you probably get a TON of email each and every day. In addition, you probably get emails that span a conversation over multiple days.

      Well, Outlook 2010 has this amazing feature called, Conversations. This will transform your inbox to look/feel more like a threaded forum (or, more like your gmail inbox if you have one).

      To enable this feature, simply go to the View tab and check the “Show as Conversations” box. You will then get prompted to choose a single folder, or to apply this change to your entire account.

      Another awesome part about this feature is that you can customize how it behaves. You can thread messages that are in different folders, if you want. This is important for me, because I use folders like mad; however, sometimes it takes me a while to flush out my inbox and messages appear in different folders.

      This is always a great way to avoid the “top-down” reader pitfall. You may be the type of person who reads your email top-down, and not in FIFO (first-in, first-out) order. CAUTION! YOU MAY BE READING EMAILS THAT DON’T MAKE SENSE!

      Threading definitely helps you here, because you can see which messages are unread, in that entire “conversation”.

      One thing struck me as odd about this new conversation feature…when I was beta testing the Office 2010 package, this feature was enabled by default. It wasn’t until I got a new PC here at work that I realized the released version of Office 2010 did NOT have this feature enabled by default. Hopefully this isn’t another one of those features that gets overlooked.

      What are your thoughts? Do you guys know of any other awesome “hidden” features in your favorite MS Office tool?

    6. Story first, then design…

      by Matt Terry

      I had my first opportunity to apply some of my recent training on discovery and usability.   I had a very short stint on an internal project, where I got the chance to perform some design work on a new calendar integration feature that was going to be implemented.  The feature was so new, and so undefined, that I had no idea what to design!

      When I finally took a step back from the problem, I remembered putting together some scenarios and stories during the Jeff Patton training.  I simply wrote a little paragraph about what “Tom the Time Keeper” (I’ve never claimed that creativity was one of my strong qualities, fyi) would do with this new calendar integration; and eventually, I even came up with some ideas about how Tom would configure the calendars.

      This story then morphed into a set of use cases.  From these use cases, I could better determine what work actually needed to be done.

      My lesson learned here is that if I write a story first, then try to design some new set of features…I can get a much better idea of what it was I needed to implement.  I can immediately see how this could also benefit some of the smaller estimation efforts I’ve taken on as well.  This is definitely an approach I will be repeating!

    7. More bragging about SEP…

      by Matt Terry

      Okay, I don’t think it’s a secret that I REALLY enjoy working at SEP. However, I’m going to continue to brag about this company.

      Recently, SEP was awarded the #1 BEST place to work in Indiana! That is some exciting stuff.
      Today, when I went to Google and searched for “beast place to work in Indiana”, these are the results I got…

      http://www.insideindianabusiness.com/newsitem.asp?ID=41552

      http://www.valpolife.com/index.php?option=com_content&view=article&id=7327&catid=92&Itemid=186

      http://www.indianachamber.com/index.php/2010-best-places-to-work-in-indiana-rankings-announced

      http://www.bestplacestoworkin.com/index.php?option=com_content&task=view&id=43

      …granted, most of these are copies of the original article by the Indiana Chamber, but seeing SEP’s name all over the place like that gives me chills!

       

      At any rate, I simply wanted to brag a little bit more about this small, powerful, employee owned company called SEP :-D