summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java
blob: 4a34caa0141f64cbaa087be8b7fdf7e7dd868a52 (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
package se.leap.bitmaskclient;

import android.view.*;

import com.pedrogomez.renderers.*;

import java.util.*;

public class ProviderListAdapter extends RendererAdapter<Provider> {
    private static boolean[] hidden = null;

    public void hide(int position) {
        hidden[getRealPosition(position)] = true;
        notifyDataSetChanged();
        notifyDataSetInvalidated();
    }

    public void unHide(int position) {
        hidden[getRealPosition(position)] = false;
        notifyDataSetChanged();
        notifyDataSetInvalidated();
    }

    public void showAllProviders() {
        for (int i = 0; i < hidden.length; i++)
            hidden[i] = false;
        notifyDataSetChanged();
        notifyDataSetInvalidated();
    }

    public void hideAllBut(int position) {
        for (int i = 0; i < hidden.length; i++) {
            if (i != position)
                hidden[i] = true;
            else
                hidden[i] = false;
        }
        notifyDataSetChanged();
        notifyDataSetInvalidated();
    }

    private int getRealPosition(int position) {
        int hElements = getHiddenCountUpTo(position);
        int diff = 0;
        for (int i = 0; i < hElements; i++) {
            diff++;
            if (hidden[position + diff])
                i--;
        }
        return (position + diff);
    }

    private int getHiddenCount() {
        int count = 0;
        for (int i = 0; i < hidden.length; i++)
            if (hidden[i])
                count++;
        return count;
    }

    private int getHiddenCountUpTo(int location) {
        int count = 0;
        for (int i = 0; i <= location; i++) {
            if (hidden[i])
                count++;
        }
        return count;
    }

    @Override
    public int getCount() {
        return (hidden.length - getHiddenCount());
    }

    public ProviderListAdapter(LayoutInflater layoutInflater, RendererBuilder rendererBuilder,
                               AdapteeCollection<Provider> collection) {
        super(layoutInflater, rendererBuilder, collection);
        hidden = new boolean[collection.size()];
        for (int i = 0; i < collection.size(); i++)
            hidden[i] = false;
    }

    @Override
    public void add(Provider item) {
        super.add(item);
        if (getCollection().size() > hidden.length) {
            boolean[] new_hidden = new boolean[hidden.length + 1];
            System.arraycopy(hidden, 0, new_hidden, 0, hidden.length);
            new_hidden[hidden.length] = false;
            hidden = new_hidden;
        }
    }

    @Override
    public void remove(Provider item) {
        super.remove(item);
        boolean[] new_hidden = new boolean[hidden.length - 1];
        System.arraycopy(hidden, 0, new_hidden, 0, hidden.length - 1);
        hidden = new_hidden;
    }

    protected int indexOf(Provider item) {
        int index = 0;
        ProviderManager provider_manager = (ProviderManager) getCollection();
        Set<Provider> providers = provider_manager.providers();
        for (Provider provider : providers) {
            if (provider.equals(item)) {
                break;
            } else index++;
        }
        return index;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return super.getView(getRealPosition(position), convertView, parent);
    }

    public void saveProviders() {
        ProviderManager provider_manager = (ProviderManager) getCollection();
        provider_manager.saveCustomProvidersToFile();
    }
}