Friday, January 27, 2012

offline android development

if you ever find yourself with time on your hands and no internet (maybe waiting at the airport and like me, too cheap to pay $5 for a measly couple hours of internet), have no fear, you can still do android development. but you'll have to plan ahead, and set up a few things while you do have internet:

  • git
    so we all know git is a great, super-fast way to keep track of revisions in a decentralized way. because it's decentralized, you can commit changes to a git repository without any internet access. just make commits as you normally would, and then the next time you have internet you can push them to a remote repository like github.
    if you won't have internet access on your development machine, you can still update a remote repository by using a flash or other external drive:


    1. plug in your flash drive, and change to a directory where you wish to store a clone of your git repository:
      cd /media/my-flash-drive/some-folder


    2. clone your repository onto the flash drive:
      git clone git://example.com/myrepo.git


    3. now that you've got a clone of the repository, next time you're developing offline, you can pull local changes into the clone of the repository on your flash drive, like so:
      cd /media/my-flash-drive/some-folder
      git pull /home/myuser/path/to/repo


      if it gives you an error about specifying a branch, try tagging "master" to the end of the command, like so:
      git pull /home/myuser/path/to/repo master



    4. then, next time you're online, plug your flash drive in, and push the repo to your remote repository:
      cd /media/my-flash-drive/some-folder
      git push -u origin master


    I've got some basic git documentation on my wiki, but github's got better documentation on their help site.


  • android developer documentation
    unless you've got the android developer documentation memorized, you might find it handy to be able to access it offline. this one's really easy if you've followed their instructions on setting up the android SDK:


    1. open the android SDK manager using the instructions here: http://developer.android.com/sdk/adding-components.html#launching


    2. expand the latest version of the android API, underneath it check the Documentation for Android SDK, click Install, and follow the instructions.


    3. when it's finished installing, using a web browser navigate to the docs folder in the folder you installed the SDK, and open offline.html. the cool thing is that search works, even offline!









of course you'll need to have the SDK installed as well, but I don't mention that here because you kinda need to have that installed anyway.


have fun!

Thursday, January 19, 2012

Why Linux?

Let's be honest, sometimes when I'm using Linux I get pretty frustrated. Especially with distributions like Ubuntu who have been focusing so much on new bells and whistles that they've been lackadaisical when it comes to fixing bugs.

...and then I have to use Windows for something or other (normally gaming), and I'm reminded why I use Linux in the first place. So I thought I'd write down some of the advantages, for myself, if nothing else:

  • No more viruses! (or spyware)
    Not only do you not have viruses in Linux, an even bigger advantage in my opinion is that you're not dedicating gobs of processor and memory to antivirus/antispyware software. This is all the more important on older/slower computers.

  • It's free!
    Free as in you can see the source code and modify it however you like, as well as free as in cost, Which you'll remember next time you have to purchase a retail copy of any Microsoft software. Which brings me to my next point...

  • No more licenses/activation!
    Have you ever tried installing Windows and the key wouldn't work because it was a Dell key with OEM media? Or installed office only to discover you've misplaced your key? Since Linux itself as well as most Linux software is free, so no licenses to fuss with!

  • Less rebooting
    Not much needs to be said about the ridiculousness of having to reboot a "modern" operating system (windows) every time it's updated. With Linux, you only need to reboot when updating the kernel. And even then, it doesn't nag you every 15 minutes to reboot.

  • More security
    Linux gets security patches every day, not once a month like Windows.

  • No more startup cramming
    Windows computers gradually get slower the longer you use them, because half of the programs you install (Adobe Reader, Office, iTunes, even Google software) decide they want to run at startup. Not so with Linux.

  • Less use of system resources
    Unlike the latest version of Windows, the latest versions of Linux will actually run on old computers, especially if you use a lightweight desktop environment like Xfce, LXDE, or Enlightenment.

  • Easier software acquisition/updates
    Want to install software in Linux? Almost everything you need is simply an apt-get (or yum) away! This includes codecs, which can be annoying to hunt down in Windows. Software is also automatically updated via the centralized package manager.

  • Easier development
    Everything you need for development is available through a package manager as well (C compiler, JRE, git, etc.). Python? Already comes with Linux. Plus, development software integrates much better in Linux because the terminal isn't just an afterthought like it is in Windows.

  • Automation
    This is more important for servers than for desktops, but every single task in Linux can be automated with free software (Python, shell scripting, Puppet...).

  • Nicer font rendering
    With windows 7, Microsoft is finally starting to catch up, but Linux has had much nicer font rendering for a long time now.
Next time I'm annoyed with Linux I can look at my list and remember life could be worse--I could be using Windows ;)

Monday, January 9, 2012

Using LIMIT with android ContentProvider

So you're using a content provider in your android app, and you want to supply a LIMIT parameter. The problem is, ContentProvider's query method doesn't have a limit parameter. Well, fortunately there's an easy solution; put the limit into a query parameter:
  1. First, in your content provider, create a constant for the query parameter key:
    public class MyProvider extends ContentProvider {
        public static final String QUERY_PARAMETER_LIMIT = "limit";

  2. In the query method of your content provider, get the limit from the Uri:
    public Cursor query(...
        String limit = uri.getQueryParameter(QUERY_PARAMETER_LIMIT);

  3. Still in the query method of your content provider, you can pass the limit on when creating the cursor:
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder, limit);

  4. Lastly, when calling your content provider (via getContentResolver.query(), a CursorLoader, etc), you can append the limit to the content uri like so:
    String someLimit = "15";
    cursor = getContentResolver().query(
        // add the limit to the content uri
        MyProvider.CONTENT_URI.buildUpon().appendQueryParameter(
            MyProvider.QUERY_PARAMETER_LIMIT,
            someLimit).build(),
            ...
The cool thing is you can use this same technique to pass other parameters to your query, like DISTINCT, for instance.