Ejemplo n.º 1
0
    private boolean addAppWidget(
        SQLiteDatabase db, ContentValues values, ComponentName cn, int spanX, int spanY) {
      boolean allocatedAppWidgets = false;
      final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);

      try {
        int appWidgetId = mAppWidgetHost.allocateAppWidgetId();

        values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
        values.put(Favorites.SPANX, spanX);
        values.put(Favorites.SPANY, spanY);
        values.put(Favorites.APPWIDGET_ID, appWidgetId);
        db.insert(TABLE_FAVORITES, null, values);

        allocatedAppWidgets = true;

        appWidgetManager.bindAppWidgetId(appWidgetId, cn);
      } catch (RuntimeException ex) {
        Log.e(TAG, "Problem allocating appWidgetId", ex);
      }

      return allocatedAppWidgets;
    }
Ejemplo n.º 2
0
    /** Upgrade existing clock and photo frame widgets into their new widget equivalents. */
    private void convertWidgets(SQLiteDatabase db) {
      final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
      final int[] bindSources =
          new int[] {
            Favorites.ITEM_TYPE_WIDGET_CLOCK,
            Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
            Favorites.ITEM_TYPE_WIDGET_SEARCH,
          };

      final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);

      Cursor c = null;

      db.beginTransaction();
      try {
        // Select and iterate through each matching widget
        c =
            db.query(
                TABLE_FAVORITES,
                new String[] {Favorites._ID, Favorites.ITEM_TYPE},
                selectWhere,
                null,
                null,
                null,
                null);

        if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());

        final ContentValues values = new ContentValues();
        while (c != null && c.moveToNext()) {
          long favoriteId = c.getLong(0);
          int favoriteType = c.getInt(1);

          // Allocate and update database with new appWidgetId
          try {
            int appWidgetId = mAppWidgetHost.allocateAppWidgetId();

            if (LOGD) {
              Log.d(TAG, "allocated appWidgetId=" + appWidgetId + " for favoriteId=" + favoriteId);
            }
            values.clear();
            values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
            values.put(Favorites.APPWIDGET_ID, appWidgetId);

            // Original widgets might not have valid spans when upgrading
            if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
              values.put(LauncherSettings.Favorites.SPANX, 4);
              values.put(LauncherSettings.Favorites.SPANY, 1);
            } else {
              values.put(LauncherSettings.Favorites.SPANX, 2);
              values.put(LauncherSettings.Favorites.SPANY, 2);
            }

            String updateWhere = Favorites._ID + "=" + favoriteId;
            db.update(TABLE_FAVORITES, values, updateWhere, null);

            if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
              appWidgetManager.bindAppWidgetId(
                  appWidgetId,
                  new ComponentName(
                      "com.android.alarmclock", "com.android.alarmclock.AnalogAppWidgetProvider"));
            } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
              appWidgetManager.bindAppWidgetId(
                  appWidgetId,
                  new ComponentName(
                      "com.android.camera", "com.android.camera.PhotoAppWidgetProvider"));
            } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
              appWidgetManager.bindAppWidgetId(appWidgetId, getSearchWidgetProvider());
            }
          } catch (RuntimeException ex) {
            Log.e(TAG, "Problem allocating appWidgetId", ex);
          }
        }

        db.setTransactionSuccessful();
      } catch (SQLException ex) {
        Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
      } finally {
        db.endTransaction();
        if (c != null) {
          c.close();
        }
      }
    }