summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/BrowserBookmarksAdapter.java
blob: be3c21126018d320760d79ef31e0b1b366f52c19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.browser;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.provider.BrowserContract.Bookmarks;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.android.browser.util.ThreadedCursorAdapter;
import com.android.browser.view.BookmarkContainer;

public class BrowserBookmarksAdapter extends
        ThreadedCursorAdapter<BrowserBookmarksAdapterItem> {

    LayoutInflater mInflater;
    int mCurrentView;
    Context mContext;

    /**
     *  Create a new BrowserBookmarksAdapter.
     */
    public BrowserBookmarksAdapter(Context context, int defaultView) {
        // Make sure to tell the CursorAdapter to avoid the observer and auto-requery
        // since the Loader will do that for us.
        super(context, null);
        mInflater = LayoutInflater.from(context);
        mContext = context;
        selectView(defaultView);
    }

    @Override
    public View newView(Context context, ViewGroup parent) {
        if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
            return mInflater.inflate(R.layout.bookmark_list, parent, false);
        } else {
            return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
        }
    }

    @Override
    public void bindView(View view, BrowserBookmarksAdapterItem object) {
        BookmarkContainer container = (BookmarkContainer) view;
        container.setIgnoreRequestLayout(true);
        if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
            bindListView(view, mContext, object);
        } else {
            bindGridView(view, mContext, object);
        }
        container.setIgnoreRequestLayout(false);
    }

    void clearView(View view) {
        if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
            ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
            favicon.setImageBitmap(null);
        } else {
            ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
            thumb.setImageBitmap(null);
        }
    }

    CharSequence getTitle(Cursor cursor) {
        int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE);
        switch (type) {
        case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER:
            return mContext.getText(R.string.other_bookmarks);
        }
        return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
    }

    void bindGridView(View view, Context context, BrowserBookmarksAdapterItem item) {
        // We need to set this to handle rotation and other configuration change
        // events. If the padding didn't change, this is a no op.
        int padding = context.getResources()
                .getDimensionPixelSize(R.dimen.combo_horizontalSpacing);
        view.setPadding(padding, view.getPaddingTop(),
                padding, view.getPaddingBottom());
        ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
        TextView tv = (TextView) view.findViewById(R.id.label);

        tv.setText(item.title);
        if (item.is_folder) {
            // folder
            thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo);
            thumb.setScaleType(ScaleType.FIT_END);
            thumb.setBackground(null);
        } else {
            thumb.setScaleType(ScaleType.CENTER_CROP);
            if (item.thumbnail == null) {
                thumb.setImageResource(R.drawable.browser_thumbnail);
            } else {
                thumb.setImageDrawable(item.thumbnail);
            }
            thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo);
        }
    }

    void bindListView(View view, Context context, BrowserBookmarksAdapterItem item) {
        ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
        TextView tv = (TextView) view.findViewById(R.id.label);

        tv.setText(item.title);
        if (item.is_folder) {
            // folder
            favicon.setImageResource(R.drawable.ic_folder_holo_dark);
            favicon.setBackground(null);
        } else {
            if (item.favicon == null) {
                favicon.setImageResource(R.drawable.app_web_browser_sm);
            } else {
                favicon.setImageDrawable(item.favicon);
            }
            favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg);
        }
    }

    public void selectView(int view) {
        if (view != BrowserBookmarksPage.VIEW_LIST
                && view != BrowserBookmarksPage.VIEW_THUMBNAILS) {
            throw new IllegalArgumentException("Unknown view specified: " + view);
        }
        mCurrentView = view;
    }

    public int getViewMode() {
        return mCurrentView;
    }

    @Override
    public BrowserBookmarksAdapterItem getRowObject(Cursor c,
            BrowserBookmarksAdapterItem item) {
        if (item == null) {
            item = new BrowserBookmarksAdapterItem();
        }
        Bitmap favicon = item.favicon != null ? item.favicon.getBitmap() : null;
        Bitmap thumbnail = item.thumbnail != null ? item.thumbnail.getBitmap() : null;
        favicon = BrowserBookmarksPage.getBitmap(c,
                BookmarksLoader.COLUMN_INDEX_FAVICON, favicon);
        thumbnail = BrowserBookmarksPage.getBitmap(c,
                BookmarksLoader.COLUMN_INDEX_THUMBNAIL, thumbnail);
        if (favicon != null
                && (item.favicon == null || item.favicon.getBitmap() != favicon)) {
            item.favicon = new BitmapDrawable(mContext.getResources(), favicon);
        }
        if (thumbnail != null
                && (item.thumbnail == null || item.thumbnail.getBitmap() != thumbnail)) {
            item.thumbnail = new BitmapDrawable(mContext.getResources(), thumbnail);
        }
        item.is_folder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
        item.title = getTitle(c);
        item.url = c.getString(BookmarksLoader.COLUMN_INDEX_URL);
        return item;
    }

    @Override
    public BrowserBookmarksAdapterItem getLoadingObject() {
        BrowserBookmarksAdapterItem item = new BrowserBookmarksAdapterItem();
        return item;
    }
}