android coding patterns

satya - Friday, October 23, 2009 1:17:17 PM

Test arguments to a function


public Cursor query(Uri uri, String[] projection, 
          String selection, String[] selectionArgs,
          String sortOrder) 
{
   if (!TextUtils.isEmpty(selection)) {
      throw new IllegalArgumentException(
          "selection not allowed for " + uri);
   }
   if (selectionArgs != null && selectionArgs.length != 0) {
      throw new IllegalArgumentException(
          "selectionArgs not allowed for " + uri);
   }
   if (!TextUtils.isEmpty(sortOrder)) {
      throw new IllegalArgumentException(
          "sortOrder not allowed for " + uri);
   }
....
}

satya - Friday, October 23, 2009 1:21:28 PM

Using UriMatcher


private static final UriMatcher sURIMatcher = initializeUriMatcher();

private static final int SEARCH_SUGGEST = 0;
private static final int SHORTCUT_REFRESH = 1;

private static UriMatcher initializeUriMatcher() {
   UriMatcher matcher =  new UriMatcher(UriMatcher.NO_MATCH);
   matcher.addURI(AUTHORITY, 
        SearchManager.SUGGEST_URI_PATH_QUERY, 
        SEARCH_SUGGEST);
       
   matcher.addURI(AUTHORITY, 
        SearchManager.SUGGEST_URI_PATH_QUERY + "/*", 
        SEARCH_SUGGEST);
       
   matcher.addURI(AUTHORITY, 
        SearchManager.SUGGEST_URI_PATH_SHORTCUT, 
        SHORTCUT_REFRESH);
       
   matcher.addURI(AUTHORITY, 
        SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", 
        SHORTCUT_REFRESH);
       
   return matcher;
}

satya - Friday, October 23, 2009 1:23:34 PM

Using UriMatcher in a switch statement


switch (sURIMatcher.match(uri)) 
{
   case SEARCH_SUGGEST:
      //deal with uri type 1
      .....
   case SEARCH_SUGGEST:
      //deal with uri type 2
      .....
   default:
      .....
}

satya - Friday, October 23, 2009 1:32:32 PM

Cleaninup importws in your java source file in eclipse


ctl-shift-o
or
rightclick/source/organize imports

satya - Friday, October 23, 2009 1:34:32 PM

the new java for loop structure


List<type> mylist;
for (type item : mylift)
{
   item.toString();
}

satya - Friday, October 23, 2009 1:36:41 PM

How to say some method is not supported at run time


public Uri insert(Uri uri, ContentValues values) {
        throw new UnsupportedOperationException();
    }

satya - Friday, October 23, 2009 1:37:54 PM

reporting mime types for a suggestion provider


public String getType(Uri uri) {
        switch (sURIMatcher.match(uri)) {
            case SEARCH_SUGGEST:
                return SearchManager.SUGGEST_MIME_TYPE;
            case SHORTCUT_REFRESH:
                return SearchManager.SHORTCUT_MIME_TYPE;
            default:
                throw new IllegalArgumentException("Unknown URL " + uri);
        }
    }

satya - Friday, October 23, 2009 1:39:28 PM

Converting an object to a string array


private Object[] columnValuesOfWord(Dictionary.Word word) {
   return new String[] {
         word.word,           // _id
         word.word,           // text1
         word.definition,     // text2
         word.word,           // intent_data
   };

satya - Thursday, October 29, 2009 2:20:42 PM

CTRL-space: How to make eclipse prompt you for code compeltion

Especially when you are in XML files, eclipse may not prompt you for code completion by default. Just press CTRL-space to tell eclipse to prompt you. Try it. You won't be disappointed.

satya - Thursday, October 29, 2009 10:49:30 PM

CTL-SHIFT-O: fix your imports

CTL-SHIFT-O: fix your imports

satya - Thursday, October 29, 2009 10:52:25 PM

R.java and Android package name

The location that R.java gets created is based on the java package name that is specified in the manifest file.

Implication is that if you were to refactor your java package name, it may not change the package name in the manifest file.

Fix this package name in the manifest to reflect your refactored java package names.


<manifest 
   xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.ai.android.search.simplesp"
...
/>

satya - Saturday, November 14, 2009 3:23:53 PM

How to quickly respond to a menu


//res/menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This group uses the default category. -->
    <group android:id="@+id/menuGroup_Main">
        <item android:id="@+id/mid_reset_suggestions"
            android:title="Reset Suggestions" />
    </group>
</menu>

//inflate the menu in the activity

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
//call the parent to attach any system level menus
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater(); //from activity
inflater.inflate(R.menu.reset_suggestions_menu, menu);
return true;
}

//Respond to it

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
    	if (item.getItemId() == R.id.mid_reset_suggestions)
    	{
            SearchRecentSuggestions suggestions = 
            	new SearchRecentSuggestions(this, 
            		SimpleSuggestionProvider.AUTHORITY, 
            		SimpleSuggestionProvider.MODE);
            suggestions.clearHistory();
    	}
    	return true;
    }

satya - Saturday, November 14, 2009 3:25:42 PM

Clearing history from a search recent suggestions provider


SearchRecentSuggestions suggestions = 
      	new SearchRecentSuggestions(this, 
      		SimpleSuggestionProvider.AUTHORITY, 
       		SimpleSuggestionProvider.MODE);
suggestions.clearHistory();