FileDocCategorySizeDatePackage
LauncherAppWidgetBinder.javaAPI DocAndroid 1.5 API9078Wed May 06 22:42:48 BST 2009com.android.settings

LauncherAppWidgetBinder

public class LauncherAppWidgetBinder extends android.app.Activity

Fields Summary
private static final String
TAG
private static final boolean
LOGD
static final String
AUTHORITY
static final String
TABLE_FAVORITES
static final String
EXTRA_BIND_SOURCES
static final String
EXTRA_BIND_TARGETS
static final String
EXTRA_APPWIDGET_BITMAPS
static final String[]
BIND_PROJECTION
static final int
INDEX_ID
static final int
INDEX_ITEM_TYPE
static final int
INDEX_APPWIDGET_ID
static final int
INDEX_ICON
static final android.content.ComponentName
BIND_PHOTO_APPWIDGET
Constructors Summary
Methods Summary
static java.lang.StringbuildOrWhereString(java.lang.String column, int[] values)
Build a query string that will match any row where the column matches anything in the values list.

        StringBuilder selectWhere = new StringBuilder();
        for (int i = values.length - 1; i >= 0; i--) {
            selectWhere.append(column).append("=").append(values[i]);
            if (i > 0) {
                selectWhere.append(" OR ");
            }
        }
        return selectWhere.toString();
    
protected voidonCreate(android.os.Bundle icicle)


    
        
        super.onCreate(icicle);
        finish();

        // This helper reaches into the Launcher database and binds any unlinked
        // widgets. If will remove any items that can't be bound successfully.
        // We protect this binder at the manifest level by asserting the caller
        // has the Launcher WRITE_SETTINGS permission.
        
        final Intent intent = getIntent();
        final Bundle extras = intent.getExtras();
        
        int[] bindSources = null;
        ArrayList<ComponentName> bindTargets = null;
        Exception exception = null;

        try {
            bindSources = extras.getIntArray(EXTRA_BIND_SOURCES);
            bindTargets = intent.getParcelableArrayListExtra(EXTRA_BIND_TARGETS);
        } catch (ClassCastException ex) {
            exception = ex;
        }
        
        if (exception != null || bindSources == null || bindTargets == null ||
                bindSources.length != bindTargets.size()) {
            Log.w(TAG, "Problem reading incoming bind request, or invalid request", exception);
            return;
        }
        
        final String selectWhere = buildOrWhereString(LauncherProvider.ITEM_TYPE, bindSources);
        
        final ContentResolver resolver = getContentResolver();
        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        
        boolean foundPhotoAppWidgets = false;
        final ArrayList<Integer> photoAppWidgetIds = new ArrayList<Integer>();
        final ArrayList<Bitmap> photoBitmaps = new ArrayList<Bitmap>();
        
        Cursor c = null;
        
        try {
            c = resolver.query(LauncherProvider.CONTENT_URI,
                    BIND_PROJECTION, selectWhere, null, null);
            
            if (LOGD) Log.d(TAG, "found bind cursor count="+c.getCount());
            
            final ContentValues values = new ContentValues();
            while (c != null && c.moveToNext()) {
                long favoriteId = c.getLong(INDEX_ID);
                int itemType = c.getInt(INDEX_ITEM_TYPE);
                int appWidgetId = c.getInt(INDEX_APPWIDGET_ID);
                byte[] iconData = c.getBlob(INDEX_ICON);
                
                // Find the binding target for this type
                ComponentName targetAppWidget = null;
                for (int i = 0; i < bindSources.length; i++) {
                    if (bindSources[i] == itemType) {
                        targetAppWidget = bindTargets.get(i);
                        break;
                    }
                }
                
                if (LOGD) Log.d(TAG, "found matching targetAppWidget="+targetAppWidget.toString()+" for favoriteId="+favoriteId);
                
                boolean bindSuccess = false;
                try {
                    appWidgetManager.bindAppWidgetId(appWidgetId, targetAppWidget);
                    bindSuccess = true;
                } catch (RuntimeException ex) {
                    Log.w(TAG, "Problem binding widget", ex);
                }
                
                // Handle special case of photo widget by loading bitmap and
                // preparing for later binding
                if (bindSuccess && iconData != null &&
                        itemType == LauncherProvider.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(iconData, 0, iconData.length);
                    
                    photoAppWidgetIds.add(appWidgetId);
                    photoBitmaps.add(bitmap);
                    foundPhotoAppWidgets = true;
                }

                if (LOGD) Log.d(TAG, "after finished, success="+bindSuccess);

                // Depending on success, update launcher or remove item
                Uri favoritesUri = ContentUris.withAppendedId(LauncherProvider.CONTENT_URI, favoriteId);
                if (bindSuccess) {
                    values.clear();
                    values.put(LauncherProvider.ITEM_TYPE, LauncherProvider.ITEM_TYPE_APPWIDGET);
                    values.putNull(LauncherProvider.ICON);
                    resolver.update(favoritesUri, values, null, null);
                } else {
                    resolver.delete(favoritesUri, null, null);
                }
                    
            }
        } catch (SQLException ex) {
            Log.w(TAG, "Problem while binding appWidgetIds for Launcher", ex);
        } finally {
            if (c != null) {
                c.close();
            }
        }
        
        if (foundPhotoAppWidgets) {
            // Convert appWidgetIds into int[]
            final int N = photoAppWidgetIds.size();
            final int[] photoAppWidgetIdsArray = new int[N];
            for (int i = 0; i < N; i++) {
                photoAppWidgetIdsArray[i] = photoAppWidgetIds.get(i);
            }
            
            // Launch intent over to handle bitmap binding, but we don't need to
            // wait around for the result.
            final Intent bindIntent = new Intent();
            bindIntent.setComponent(BIND_PHOTO_APPWIDGET);
            
            final Bundle bindExtras = new Bundle();
            bindExtras.putIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS, photoAppWidgetIdsArray);
            bindExtras.putParcelableArrayList(EXTRA_APPWIDGET_BITMAPS, photoBitmaps);
            bindIntent.putExtras(bindExtras);
            
            startActivity(bindIntent);
        }
        
        if (LOGD) Log.d(TAG, "completely finished with binding for Launcher");