summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java
blob: 24348dfe092625c794ff1e039070a8e07f51555b (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
/* -*- 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.activitystream;

import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.mozilla.gecko.R;
import org.mozilla.gecko.activitystream.ActivityStream.LabelCallback;
import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.home.HomePager;
import org.mozilla.gecko.home.activitystream.menu.ActivityStreamContextMenu;
import org.mozilla.gecko.home.activitystream.topsites.CirclePageIndicator;
import org.mozilla.gecko.home.activitystream.topsites.TopSitesPagerAdapter;
import org.mozilla.gecko.icons.IconCallback;
import org.mozilla.gecko.icons.IconResponse;
import org.mozilla.gecko.icons.Icons;
import org.mozilla.gecko.util.DrawableUtil;
import org.mozilla.gecko.util.ViewUtil;
import org.mozilla.gecko.util.TouchTargetUtil;
import org.mozilla.gecko.widget.FaviconView;

import java.util.concurrent.Future;

import static org.mozilla.gecko.activitystream.ActivityStream.extractLabel;

public abstract class StreamItem extends RecyclerView.ViewHolder {
    public StreamItem(View itemView) {
        super(itemView);
    }

    public static class HighlightsTitle extends StreamItem {
        public static final int LAYOUT_ID = R.layout.activity_stream_main_highlightstitle;

        public HighlightsTitle(View itemView) {
            super(itemView);
        }
    }

    public static class TopPanel extends StreamItem {
        public static final int LAYOUT_ID = R.layout.activity_stream_main_toppanel;

        private final ViewPager topSitesPager;

        public TopPanel(View itemView, HomePager.OnUrlOpenListener onUrlOpenListener, HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener) {
            super(itemView);

            topSitesPager = (ViewPager) itemView.findViewById(R.id.topsites_pager);
            topSitesPager.setAdapter(new TopSitesPagerAdapter(itemView.getContext(), onUrlOpenListener, onUrlOpenInBackgroundListener));

            CirclePageIndicator indicator = (CirclePageIndicator) itemView.findViewById(R.id.topsites_indicator);
            indicator.setViewPager(topSitesPager);
        }

        public void bind(Cursor cursor, int tiles, int tilesWidth, int tilesHeight) {
            final TopSitesPagerAdapter adapter = (TopSitesPagerAdapter) topSitesPager.getAdapter();
            adapter.setTilesSize(tiles, tilesWidth, tilesHeight);
            adapter.swapCursor(cursor);

            final Resources resources = itemView.getResources();
            final int tilesMargin = resources.getDimensionPixelSize(R.dimen.activity_stream_base_margin);
            final int textHeight = resources.getDimensionPixelSize(R.dimen.activity_stream_top_sites_text_height);

            ViewGroup.LayoutParams layoutParams = topSitesPager.getLayoutParams();
            layoutParams.height = tilesHeight + tilesMargin + textHeight;
            topSitesPager.setLayoutParams(layoutParams);
        }
    }

    public static class HighlightItem extends StreamItem implements IconCallback {
        public static final int LAYOUT_ID = R.layout.activity_stream_card_history_item;

        String title;
        String url;

        final FaviconView vIconView;
        final TextView vLabel;
        final TextView vTimeSince;
        final TextView vSourceView;
        final TextView vPageView;
        final ImageView vSourceIconView;

        private Future<IconResponse> ongoingIconLoad;
        private int tilesMargin;

        public HighlightItem(final View itemView,
                             final HomePager.OnUrlOpenListener onUrlOpenListener,
                             final HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener) {
            super(itemView);

            tilesMargin = itemView.getResources().getDimensionPixelSize(R.dimen.activity_stream_base_margin);

            vLabel = (TextView) itemView.findViewById(R.id.card_history_label);
            vTimeSince = (TextView) itemView.findViewById(R.id.card_history_time_since);
            vIconView = (FaviconView) itemView.findViewById(R.id.icon);
            vSourceView = (TextView) itemView.findViewById(R.id.card_history_source);
            vPageView = (TextView) itemView.findViewById(R.id.page);
            vSourceIconView = (ImageView) itemView.findViewById(R.id.source_icon);

            final ImageView menuButton = (ImageView) itemView.findViewById(R.id.menu);

            menuButton.setImageDrawable(
                    DrawableUtil.tintDrawable(menuButton.getContext(), R.drawable.menu, Color.LTGRAY));

            TouchTargetUtil.ensureTargetHitArea(menuButton, itemView);

            menuButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActivityStreamContextMenu.show(v.getContext(),
                            menuButton,
                            ActivityStreamContextMenu.MenuMode.HIGHLIGHT,
                            title, url, onUrlOpenListener, onUrlOpenInBackgroundListener,
                            vIconView.getWidth(), vIconView.getHeight());
                }
            });

            ViewUtil.enableTouchRipple(menuButton);
        }

        public void bind(Cursor cursor, int tilesWidth, int tilesHeight) {

            final long time = cursor.getLong(cursor.getColumnIndexOrThrow(BrowserContract.Highlights.DATE));
            final String ago = DateUtils.getRelativeTimeSpanString(time, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS, 0).toString();

            title = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.History.TITLE));
            url = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Combined.URL));

            vLabel.setText(title);
            vTimeSince.setText(ago);

            ViewGroup.LayoutParams layoutParams = vIconView.getLayoutParams();
            layoutParams.width = tilesWidth - tilesMargin;
            layoutParams.height = tilesHeight;
            vIconView.setLayoutParams(layoutParams);

            updateSource(cursor);
            updatePage(url);

            if (ongoingIconLoad != null) {
                ongoingIconLoad.cancel(true);
            }

            ongoingIconLoad = Icons.with(itemView.getContext())
                    .pageUrl(url)
                    .skipNetwork()
                    .build()
                    .execute(this);
        }

        private void updateSource(final Cursor cursor) {
            final boolean isBookmark = -1 != cursor.getLong(cursor.getColumnIndexOrThrow(BrowserContract.Combined.BOOKMARK_ID));
            final boolean isHistory = -1 != cursor.getLong(cursor.getColumnIndexOrThrow(BrowserContract.Combined.HISTORY_ID));

            if (isBookmark) {
                vSourceView.setText(R.string.activity_stream_highlight_label_bookmarked);
                vSourceView.setVisibility(View.VISIBLE);
                vSourceIconView.setImageResource(R.drawable.ic_as_bookmarked);
            } else if (isHistory) {
                vSourceView.setText(R.string.activity_stream_highlight_label_visited);
                vSourceView.setVisibility(View.VISIBLE);
                vSourceIconView.setImageResource(R.drawable.ic_as_visited);
            } else {
                vSourceView.setVisibility(View.INVISIBLE);
                vSourceIconView.setImageResource(0);
            }

            vSourceView.setText(vSourceView.getText());
        }

        private void updatePage(final String url) {
            extractLabel(itemView.getContext(), url, false, new LabelCallback() {
                @Override
                public void onLabelExtracted(String label) {
                    vPageView.setText(TextUtils.isEmpty(label) ? url : label);
                }
            });
        }

        @Override
        public void onIconResponse(IconResponse response) {
            vIconView.updateImage(response);
        }
    }
}