コード例 #1
0
 private static long getFolderId(BookmarkNode folder) {
   return folder != null ? folder.id() : ChromeBrowserProviderClient.INVALID_BOOKMARK_ID;
 }
コード例 #2
0
    @Override
    public RemoteViews getViewAt(int position) {
      if (mCurrentFolder == null) {
        Log.w(TAG, "No current folder data available.");
        return null;
      }

      BookmarkNode bookmark = getBookmarkForPosition(position);
      if (bookmark == null) {
        Log.w(TAG, "Couldn't get bookmark for position " + position);
        return null;
      }

      if (bookmark == mCurrentFolder && bookmark.parent() == null) {
        Log.w(TAG, "Invalid bookmark data: loop detected.");
        return null;
      }

      String title = bookmark.name();
      String url = bookmark.url();
      long id = (bookmark == mCurrentFolder) ? bookmark.parent().id() : bookmark.id();

      // Two layouts are needed because RemoteView does not supporting changing the scale type
      // of an ImageView: boomarks crop their thumbnails, while folders stretch their icon.
      RemoteViews views =
          !bookmark.isUrl()
              ? new RemoteViews(
                  mContext.getPackageName(), R.layout.bookmark_thumbnail_widget_item_folder)
              : new RemoteViews(mContext.getPackageName(), R.layout.bookmark_thumbnail_widget_item);

      // Set the title of the bookmark. Use the url as a backup.
      views.setTextViewText(R.id.label, TextUtils.isEmpty(title) ? url : title);

      if (!bookmark.isUrl()) {
        int thumbId =
            (bookmark == mCurrentFolder)
                ? R.drawable.thumb_bookmark_widget_folder_back_holo
                : R.drawable.thumb_bookmark_widget_folder_holo;
        views.setImageViewResource(R.id.thumb, thumbId);
        views.setImageViewResource(R.id.favicon, R.drawable.ic_bookmark_widget_bookmark_holo_dark);
      } else {
        // RemoteViews require a valid bitmap config.
        Options options = new Options();
        options.inPreferredConfig = Config.ARGB_8888;

        byte[] favicon = bookmark.favicon();
        if (favicon != null && favicon.length > 0) {
          views.setImageViewBitmap(
              R.id.favicon, BitmapFactory.decodeByteArray(favicon, 0, favicon.length, options));
        } else {
          views.setImageViewResource(R.id.favicon, org.chromium.chrome.R.drawable.globe_favicon);
        }

        byte[] thumbnail = bookmark.thumbnail();
        if (thumbnail != null && thumbnail.length > 0) {
          views.setImageViewBitmap(
              R.id.thumb, BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length, options));
        } else {
          views.setImageViewResource(R.id.thumb, R.drawable.browser_thumbnail);
        }
      }

      Intent fillIn;
      if (!bookmark.isUrl()) {
        fillIn =
            new Intent(getChangeFolderAction(mContext))
                .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId)
                .putExtra(BookmarkColumns._ID, id);
      } else {
        fillIn = new Intent(Intent.ACTION_VIEW);
        if (!TextUtils.isEmpty(url)) {
          fillIn = fillIn.addCategory(Intent.CATEGORY_BROWSABLE).setData(Uri.parse(url));
        } else {
          fillIn = fillIn.addCategory(Intent.CATEGORY_LAUNCHER);
        }
      }
      views.setOnClickFillInIntent(R.id.list_item, fillIn);
      return views;
    }