Tuesday, June 30, 2009

Fighting LiveFolders

Cupcake introduced "Live Folders" to the android world and since then I've wondered why the phone does not come with a LiveFolder implementation for bookmarks by default. In an effort to try and get code into core-android I set out to write it, but I've been struggling with all sorts of issues.

  1. ContentProvider.query returns a CursorWrapper, not a real cursor, which means if an existing content provider does not provide a live folders implementation and you want to write your own little content provider to proxy the requests with correctly formatted responses you'll have to implement your own cursor to delegate to the cursor wrapper. I resolved this by creating a MatrixCursor in my content provider into which I copy the columns from the BrowserProvider.
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                String sortOrder) {
            Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI,
                    Browser.HISTORY_PROJECTION, "bookmark = 1", null, null);
    
            String[] liveFolderColumnNames = {
                    LiveFolders._ID, LiveFolders.NAME, LiveFolders.ICON_PACKAGE,
                    LiveFolders.ICON_RESOURCE,
            };
            MatrixCursor mc = new MatrixCursor(liveFolderColumnNames, c.getCount());
    
            int columnCount = c.getColumnNames().length;
            while (c.moveToNext()) {
                Object[] row = {
                        c.getString(Browser.HISTORY_PROJECTION_ID_INDEX),
                        c.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX),
                        getContext().getPackageName(), R.drawable.ic_launcher_shortcut_browser_bookmark
                };
                mc.addRow(row);
            }
            return mc;
        }
    
  2. I can't figure out how in hell I'm supposed to use ICON_BITMAP to set an icon for each item in the LiveFolder -- Looking at the source code it looks like its expecting a byte[], which I am in fact providing, but then the provider is having difficult constructing a bitmap out of that data and NPE's trying to (and I don't know why it is doing so) thumbnail the icon!