summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/home/PanelRecyclerView.java
blob: 9145ab1e1ca42cae3f13a50b38aa109523ec661a (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
/* -*- 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 org.mozilla.gecko.R;
import org.mozilla.gecko.home.PanelLayout.DatasetBacked;
import org.mozilla.gecko.home.PanelLayout.PanelView;
import org.mozilla.gecko.widget.RecyclerViewClickSupport;
import org.mozilla.gecko.widget.RecyclerViewClickSupport.OnItemClickListener;
import org.mozilla.gecko.widget.RecyclerViewClickSupport.OnItemLongClickListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * RecyclerView implementation for grid home panels.
 */
@SuppressLint("ViewConstructor") // View is only created from code
public class PanelRecyclerView extends RecyclerView
        implements DatasetBacked, PanelView, OnItemClickListener, OnItemLongClickListener {
    private final PanelRecyclerViewAdapter adapter;
    private final GridLayoutManager layoutManager;
    private final PanelViewItemHandler itemHandler;
    private final float columnWidth;
    private final boolean autoFit;
    private final HomeConfig.ViewConfig viewConfig;

    private PanelLayout.OnItemOpenListener itemOpenListener;
    private HomeContextMenuInfo contextMenuInfo;
    private HomeContextMenuInfo.Factory contextMenuInfoFactory;

    public PanelRecyclerView(Context context, HomeConfig.ViewConfig viewConfig) {
        super(context);

        this.viewConfig = viewConfig;

        final Resources resources = context.getResources();

        int spanCount;
        if (viewConfig.getItemType() == HomeConfig.ItemType.ICON) {
            autoFit = false;
            spanCount = getResources().getInteger(R.integer.panel_icon_grid_view_columns);
        } else {
            autoFit = true;
            spanCount = 1;
        }

        columnWidth = resources.getDimension(R.dimen.panel_grid_view_column_width);
        layoutManager = new GridLayoutManager(context, spanCount);
        adapter = new PanelRecyclerViewAdapter(context, viewConfig);
        itemHandler = new PanelViewItemHandler();

        layoutManager.setSpanSizeLookup(new PanelSpanSizeLookup());

        setLayoutManager(layoutManager);
        setAdapter(adapter);

        int horizontalSpacing = (int) resources.getDimension(R.dimen.panel_grid_view_horizontal_spacing);
        int verticalSpacing = (int) resources.getDimension(R.dimen.panel_grid_view_vertical_spacing);
        int outerSpacing = (int) resources.getDimension(R.dimen.panel_grid_view_outer_spacing);

        addItemDecoration(new SpacingDecoration(horizontalSpacing, verticalSpacing));

        setPadding(outerSpacing, outerSpacing, outerSpacing, outerSpacing);
        setClipToPadding(false);

        RecyclerViewClickSupport.addTo(this)
            .setOnItemClickListener(this)
            .setOnItemLongClickListener(this);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        super.onMeasure(widthSpec, heightSpec);

        if (autoFit) {
            // Adjust span based on space available (What GridView does when you say numColumns="auto_fit")
            final int spanCount = (int) Math.max(1, getMeasuredWidth() / columnWidth);
            layoutManager.setSpanCount(spanCount);
        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        itemHandler.setOnItemOpenListener(itemOpenListener);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        itemHandler.setOnItemOpenListener(null);
    }

    @Override
    public void setDataset(Cursor cursor) {
        adapter.swapCursor(cursor);
    }

    @Override
    public void setFilterManager(PanelLayout.FilterManager manager) {
        adapter.setFilterManager(manager);
        itemHandler.setFilterManager(manager);
    }

    @Override
    public void setOnItemOpenListener(PanelLayout.OnItemOpenListener listener) {
        itemOpenListener = listener;
        itemHandler.setOnItemOpenListener(listener);
    }

    @Override
    public HomeContextMenuInfo getContextMenuInfo() {
        return contextMenuInfo;
    }

    @Override
    public void setContextMenuInfoFactory(HomeContextMenuInfo.Factory factory) {
        contextMenuInfoFactory = factory;
    }

    @Override
    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
        if (viewConfig.hasHeaderConfig()) {
            if (position == 0) {
                itemOpenListener.onItemOpen(viewConfig.getHeaderConfig().getUrl(), null);
                return;
            }

            position--;
        }

        itemHandler.openItemAtPosition(adapter.getCursor(), position);
    }

    @Override
    public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
        if (viewConfig.hasHeaderConfig()) {
            if (position == 0) {
                final HomeConfig.HeaderConfig headerConfig = viewConfig.getHeaderConfig();

                final HomeContextMenuInfo info = new HomeContextMenuInfo(v, position, -1);
                info.url = headerConfig.getUrl();
                info.title = headerConfig.getUrl();

                contextMenuInfo = info;
                return showContextMenuForChild(this);
            }

            position--;
        }

        Cursor cursor = adapter.getCursor();
        cursor.moveToPosition(position);

        contextMenuInfo = contextMenuInfoFactory.makeInfoForCursor(recyclerView, position, -1, cursor);
        return showContextMenuForChild(this);
    }

    private class PanelSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
        @Override
        public int getSpanSize(int position) {
            if (position == 0 && viewConfig.hasHeaderConfig()) {
                return layoutManager.getSpanCount();
            }

            return 1;
        }
    }
}