summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/tabs/TabStripAdapter.java
blob: 8778aac3126bc2080fded2a99e307e1d534378c1 (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
/* -*- 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.tabs;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

import org.mozilla.gecko.R;
import org.mozilla.gecko.Tab;
import org.mozilla.gecko.Tabs;

class TabStripAdapter extends BaseAdapter {
    private static final String LOGTAG = "GeckoTabStripAdapter";

    private final Context context;
    private List<Tab> tabs;

    public TabStripAdapter(Context context) {
        this.context = context;
    }

    @Override
    public Tab getItem(int position) {
        return (tabs != null &&
                position >= 0 &&
                position < tabs.size() ? tabs.get(position) : null);
    }

    @Override
    public long getItemId(int position) {
        final Tab tab = getItem(position);
        return (tab != null ? tab.getId() : -1);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final TabStripItemView item;
        if (convertView == null) {
            item = (TabStripItemView)
                    LayoutInflater.from(context).inflate(R.layout.tab_strip_item, parent, false);
        } else {
            item = (TabStripItemView) convertView;
        }

        final Tab tab = tabs.get(position);
        item.updateFromTab(tab);

        return item;
    }

    @Override
    public int getCount() {
        return (tabs != null ? tabs.size() : 0);
    }

    int getPositionForTab(Tab tab) {
        if (tabs == null || tab == null) {
            return -1;
        }

        return tabs.indexOf(tab);
    }

    void removeTab(Tab tab) {
        if (tabs == null) {
            return;
        }

        tabs.remove(tab);
        notifyDataSetChanged();
    }

    void refresh(List<Tab> tabs) {
        // The list of tabs is guaranteed to be non-null.
        // See TabStripView.refreshTabs().
        this.tabs = tabs;
        notifyDataSetChanged();
    }

    void clear() {
        tabs = null;
        notifyDataSetInvalidated();
    }
}