summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/home/TopSitesGridItemView.java
blob: c17aff209a59842b372f91dfe07e0cae829f4cdb (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.home;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.mozilla.gecko.R;
import org.mozilla.gecko.db.BrowserContract.TopSites;
import org.mozilla.gecko.icons.IconCallback;
import org.mozilla.gecko.icons.IconResponse;
import org.mozilla.gecko.icons.Icons;

import java.util.concurrent.Future;

/**
 * A view that displays the thumbnail and the title/url for a top/pinned site.
 * If the title/url is longer than the width of the view, they are faded out.
 * If there is no valid url, a default string is shown at 50% opacity.
 * This is denoted by the empty state.
 */
public class TopSitesGridItemView extends RelativeLayout implements IconCallback {
    private static final String LOGTAG = "GeckoTopSitesGridItemView";

    // Empty state, to denote there is no valid url.
    private static final int[] STATE_EMPTY = { android.R.attr.state_empty };

    private static final ScaleType SCALE_TYPE_FAVICON   = ScaleType.CENTER;
    private static final ScaleType SCALE_TYPE_RESOURCE  = ScaleType.CENTER;
    private static final ScaleType SCALE_TYPE_THUMBNAIL = ScaleType.CENTER_CROP;
    private static final ScaleType SCALE_TYPE_URL       = ScaleType.CENTER_INSIDE;

    // Child views.
    private final TextView mTitleView;
    private final TopSitesThumbnailView mThumbnailView;

    // Data backing this view.
    private String mTitle;
    private String mUrl;

    private boolean mThumbnailSet;

    // Matches BrowserContract.TopSites row types
    private int mType = -1;

    // Dirty state.
    private boolean mIsDirty;

    private Future<IconResponse> mOngoingIconRequest;

    public TopSitesGridItemView(Context context) {
        this(context, null);
    }

    public TopSitesGridItemView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.topSitesGridItemViewStyle);
    }

    public TopSitesGridItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        LayoutInflater.from(context).inflate(R.layout.top_sites_grid_item_view, this);

        mTitleView = (TextView) findViewById(R.id.title);
        mThumbnailView = (TopSitesThumbnailView) findViewById(R.id.thumbnail);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);

        if (mType == TopSites.TYPE_BLANK) {
            mergeDrawableStates(drawableState, STATE_EMPTY);
        }

        return drawableState;
    }

    /**
     * @return The title shown by this view.
     */
    public String getTitle() {
        return (!TextUtils.isEmpty(mTitle) ? mTitle : mUrl);
    }

    /**
     * @return The url shown by this view.
     */
    public String getUrl() {
        return mUrl;
    }

    /**
     * @return The site type associated with this view.
     */
    public int getType() {
        return mType;
    }

    /**
     * @param title The title for this view.
     */
    public void setTitle(String title) {
        if (mTitle != null && mTitle.equals(title)) {
            return;
        }

        mTitle = title;
        updateTitleView();
    }

    /**
     * @param url The url for this view.
     */
    public void setUrl(String url) {
        if (mUrl != null && mUrl.equals(url)) {
            return;
        }

        mUrl = url;
        updateTitleView();
    }

    public void blankOut() {
        mUrl = "";
        mTitle = "";
        updateType(TopSites.TYPE_BLANK);
        updateTitleView();
        cancelIconLoading();
        ImageLoader.with(getContext()).cancelRequest(mThumbnailView);
        displayThumbnail(R.drawable.top_site_add);

    }

    public void markAsDirty() {
        mIsDirty = true;
    }

    /**
     * Updates the title, URL, and pinned state of this view.
     *
     * Also resets our loadId to NOT_LOADING.
     *
     * Returns true if any fields changed.
     */
    public boolean updateState(final String title, final String url, final int type, final TopSitesPanel.ThumbnailInfo thumbnail) {
        boolean changed = false;
        if (mUrl == null || !mUrl.equals(url)) {
            mUrl = url;
            changed = true;
        }

        if (mTitle == null || !mTitle.equals(title)) {
            mTitle = title;
            changed = true;
        }

        if (thumbnail != null) {
            if (thumbnail.imageUrl != null) {
                displayThumbnail(thumbnail.imageUrl, thumbnail.bgColor);
            } else if (thumbnail.bitmap != null) {
                displayThumbnail(thumbnail.bitmap);
            }
        } else if (changed) {
            // Because we'll have a new favicon or thumbnail arriving shortly, and
            // we need to not reject it because we already had a thumbnail.
            mThumbnailSet = false;
        }

        if (changed) {
            updateTitleView();
            cancelIconLoading();
            ImageLoader.with(getContext()).cancelRequest(mThumbnailView);
        }

        if (updateType(type)) {
            changed = true;
        }

        // The dirty state forces the state update to return true
        // so that the adapter loads favicons once the thumbnails
        // are loaded in TopSitesPanel/TopSitesGridAdapter.
        changed = (changed || mIsDirty);
        mIsDirty = false;

        return changed;
    }

    /**
     * Try to load an icon for the given page URL.
     */
    public void loadFavicon(String pageUrl) {
        mOngoingIconRequest = Icons.with(getContext())
                .pageUrl(pageUrl)
                .skipNetwork()
                .build()
                .execute(this);
    }

    private void cancelIconLoading() {
        if (mOngoingIconRequest != null) {
            mOngoingIconRequest.cancel(true);
        }
    }

    /**
     * Display the thumbnail from a resource.
     *
     * @param resId Resource ID of the drawable to show.
     */
    public void displayThumbnail(int resId) {
        mThumbnailView.setScaleType(SCALE_TYPE_RESOURCE);
        mThumbnailView.setImageResource(resId);
        mThumbnailView.setBackgroundColor(0x0);
        mThumbnailSet = false;
    }

    /**
     * Display the thumbnail from a bitmap.
     *
     * @param thumbnail The bitmap to show as thumbnail.
     */
    public void displayThumbnail(Bitmap thumbnail) {
        if (thumbnail == null) {
            return;
        }

        mThumbnailSet = true;

        cancelIconLoading();
        ImageLoader.with(getContext()).cancelRequest(mThumbnailView);

        mThumbnailView.setScaleType(SCALE_TYPE_THUMBNAIL);
        mThumbnailView.setImageBitmap(thumbnail, true);
        mThumbnailView.setBackgroundDrawable(null);
    }

    /**
     * Display the thumbnail from a URL.
     *
     * @param imageUrl URL of the image to show.
     * @param bgColor background color to use in the view.
     */
    public void displayThumbnail(final String imageUrl, final int bgColor) {
        mThumbnailView.setScaleType(SCALE_TYPE_URL);
        mThumbnailView.setBackgroundColor(bgColor);
        mThumbnailSet = true;

        ImageLoader.with(getContext())
                   .load(imageUrl)
                   .noFade()
                   .into(mThumbnailView);
    }

    /**
     * Update the item type associated with this view. Returns true if
     * the type has changed, false otherwise.
     */
    private boolean updateType(int type) {
        if (mType == type) {
            return false;
        }

        mType = type;
        refreshDrawableState();

        int pinResourceId = (type == TopSites.TYPE_PINNED ? R.drawable.pin : 0);
        mTitleView.setCompoundDrawablesWithIntrinsicBounds(pinResourceId, 0, 0, 0);

        return true;
    }

    /**
     * Update the title shown by this view. If both title and url
     * are empty, mark the state as STATE_EMPTY and show a default text.
     */
    private void updateTitleView() {
        String title = getTitle();
        if (!TextUtils.isEmpty(title)) {
            mTitleView.setText(title);
        } else {
            mTitleView.setText(R.string.home_top_sites_add);
        }
    }

    /**
     * Display the loaded icon (if no thumbnail is set).
     */
    @Override
    public void onIconResponse(IconResponse response) {
        if (mThumbnailSet) {
            // Already showing a thumbnail; do nothing.
            return;
        }

        mThumbnailView.setScaleType(SCALE_TYPE_FAVICON);
        mThumbnailView.setImageBitmap(response.getBitmap(), false);
        mThumbnailView.setBackgroundColorWithOpacityFilter(response.getColor());
    }
}