Home | History | Annotate | Download | only in browser
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.browser;
     18 
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.graphics.Bitmap;
     22 import android.graphics.drawable.BitmapDrawable;
     23 import android.provider.BrowserContract.Bookmarks;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.ImageView;
     28 import android.widget.ImageView.ScaleType;
     29 import android.widget.TextView;
     30 
     31 import com.android.browser.util.ThreadedCursorAdapter;
     32 import com.android.browser.view.BookmarkContainer;
     33 
     34 public class BrowserBookmarksAdapter extends
     35         ThreadedCursorAdapter<BrowserBookmarksAdapterItem> {
     36 
     37     LayoutInflater mInflater;
     38     Context mContext;
     39 
     40     /**
     41      *  Create a new BrowserBookmarksAdapter.
     42      */
     43     public BrowserBookmarksAdapter(Context context) {
     44         // Make sure to tell the CursorAdapter to avoid the observer and auto-requery
     45         // since the Loader will do that for us.
     46         super(context, null);
     47         mInflater = LayoutInflater.from(context);
     48         mContext = context;
     49     }
     50 
     51     @Override
     52     protected long getItemId(Cursor c) {
     53         return c.getLong(BookmarksLoader.COLUMN_INDEX_ID);
     54     }
     55 
     56     @Override
     57     public View newView(Context context, ViewGroup parent) {
     58         return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
     59     }
     60 
     61     @Override
     62     public void bindView(View view, BrowserBookmarksAdapterItem object) {
     63         BookmarkContainer container = (BookmarkContainer) view;
     64         container.setIgnoreRequestLayout(true);
     65         bindGridView(view, mContext, object);
     66         container.setIgnoreRequestLayout(false);
     67     }
     68 
     69     CharSequence getTitle(Cursor cursor) {
     70         int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE);
     71         switch (type) {
     72         case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER:
     73             return mContext.getText(R.string.other_bookmarks);
     74         }
     75         return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
     76     }
     77 
     78     void bindGridView(View view, Context context, BrowserBookmarksAdapterItem item) {
     79         // We need to set this to handle rotation and other configuration change
     80         // events. If the padding didn't change, this is a no op.
     81         int padding = context.getResources()
     82                 .getDimensionPixelSize(R.dimen.combo_horizontalSpacing);
     83         view.setPadding(padding, view.getPaddingTop(),
     84                 padding, view.getPaddingBottom());
     85         ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
     86         TextView tv = (TextView) view.findViewById(R.id.label);
     87 
     88         tv.setText(item.title);
     89         if (item.is_folder) {
     90             // folder
     91             thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo);
     92             thumb.setScaleType(ScaleType.FIT_END);
     93             thumb.setBackground(null);
     94         } else {
     95             thumb.setScaleType(ScaleType.CENTER_CROP);
     96             if (item.thumbnail == null || !item.has_thumbnail) {
     97                 thumb.setImageResource(R.drawable.browser_thumbnail);
     98             } else {
     99                 thumb.setImageDrawable(item.thumbnail);
    100             }
    101             thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo);
    102         }
    103     }
    104 
    105     @Override
    106     public BrowserBookmarksAdapterItem getRowObject(Cursor c,
    107             BrowserBookmarksAdapterItem item) {
    108         if (item == null) {
    109             item = new BrowserBookmarksAdapterItem();
    110         }
    111         Bitmap thumbnail = item.thumbnail != null ? item.thumbnail.getBitmap() : null;
    112         thumbnail = BrowserBookmarksPage.getBitmap(c,
    113                 BookmarksLoader.COLUMN_INDEX_THUMBNAIL, thumbnail);
    114         item.has_thumbnail = thumbnail != null;
    115         if (thumbnail != null
    116                 && (item.thumbnail == null || item.thumbnail.getBitmap() != thumbnail)) {
    117             item.thumbnail = new BitmapDrawable(mContext.getResources(), thumbnail);
    118         }
    119         item.is_folder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
    120         item.title = getTitle(c);
    121         item.url = c.getString(BookmarksLoader.COLUMN_INDEX_URL);
    122         return item;
    123     }
    124 
    125     @Override
    126     public BrowserBookmarksAdapterItem getLoadingObject() {
    127         BrowserBookmarksAdapterItem item = new BrowserBookmarksAdapterItem();
    128         return item;
    129     }
    130 }
    131