aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLFragment.java
blob: b383082edcd923edb3ffb58b37b7511a1eb42e75 (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
/*
 * Copyright (C) 2015 Vincent Breitmoser <v.breitmoser@my.amazin.horse>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain.ui;


import java.util.ArrayList;
import java.util.Collections;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;

import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
import org.sufficientlysecure.keychain.ui.util.recyclerview.DividerItemDecoration;
import org.sufficientlysecure.keychain.util.Preferences.CacheTTLPrefs;


public class SettingsCacheTTLFragment extends Fragment {

    public static final String ARG_TTL_PREFS = "ttl_prefs";

    private CacheTTLListAdapter mAdapter;

    public static SettingsCacheTTLFragment newInstance(CacheTTLPrefs ttlPrefs) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_TTL_PREFS, ttlPrefs);

        SettingsCacheTTLFragment fragment = new SettingsCacheTTLFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {

        return inflater.inflate(R.layout.settings_cache_ttl_fragment, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        CacheTTLPrefs prefs = (CacheTTLPrefs) getArguments().getSerializable(ARG_TTL_PREFS);

        mAdapter = new CacheTTLListAdapter(prefs);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.cache_ttl_recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(mAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));


    }

    private void saveKeyserverList() {
        // Preferences.getPreferences(getActivity()).setKeyServers(servers);
    }

    public class CacheTTLListAdapter extends RecyclerView.Adapter<CacheTTLListAdapter.ViewHolder> {

        private final ArrayList<Boolean> mPositionIsChecked;
        private int mDefaultPosition;

        public CacheTTLListAdapter(CacheTTLPrefs prefs) {
            this.mPositionIsChecked = new ArrayList<>();
            for (int ttlTime : CacheTTLPrefs.CACHE_TTLS) {
                mPositionIsChecked.add(prefs.ttlTimes.contains(ttlTime));
                if (ttlTime == prefs.defaultTtl) {
                    mDefaultPosition = mPositionIsChecked.size() -1;
                }
            }

        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.settings_cache_ttl_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            holder.bind(position);
        }

        @Override
        public int getItemCount() {
            return mPositionIsChecked.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            CheckBox mChecked;
            TextView mTitle;
            RadioButton mIsDefault;

            public ViewHolder(View itemView) {
                super(itemView);
                mChecked = (CheckBox) itemView.findViewById(R.id.ttl_selected);
                mTitle = (TextView) itemView.findViewById(R.id.ttl_title);
                mIsDefault = (RadioButton) itemView.findViewById(R.id.ttl_default);

                itemView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mChecked.performClick();
                    }
                });
            }

            public void bind(final int position) {

                int ttl = CacheTTLPrefs.CACHE_TTLS.get(position);
                boolean isChecked = mPositionIsChecked.get(position);
                boolean isDefault = position == mDefaultPosition;

                mTitle.setText(CacheTTLPrefs.CACHE_TTL_NAMES.get(ttl));
                mChecked.setChecked(isChecked);
                mIsDefault.setEnabled(isChecked);
                mIsDefault.setChecked(isDefault);

                mChecked.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setTtlChecked(position);
                    }
                });

                mIsDefault.setOnClickListener(!isChecked ? null : new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setDefault(position);
                    }
                });

            }

            private void setTtlChecked(int position) {
                boolean isChecked = mPositionIsChecked.get(position);
                int checkedItems = countCheckedItems();

                boolean isLastChecked = isChecked && checkedItems == 1;
                boolean isOneTooMany = !isChecked && checkedItems >= 3;
                if (isLastChecked) {
                    Notify.create(getActivity(), R.string.settings_cache_ttl_at_least_one, Style.ERROR).show();
                } else if (isOneTooMany) {
                    Notify.create(getActivity(), R.string.settings_cache_ttl_max_three, Style.ERROR).show();
                } else {
                    mPositionIsChecked.set(position, !isChecked);
                    repositionDefault();
                }
                notifyItemChanged(position);
            }

            private void repositionDefault() {
                boolean defaultPositionIsChecked = mPositionIsChecked.get(mDefaultPosition);
                if (defaultPositionIsChecked) {
                    return;
                }

                // prefer moving default up
                int i = mDefaultPosition;
                while (--i >= 0) {
                    if (mPositionIsChecked.get(i)) {
                        setDefault(i);
                        return;
                    }
                }

                // if that didn't work, move it down
                i = mDefaultPosition;
                while (++i < mPositionIsChecked.size()) {
                    if (mPositionIsChecked.get(i)) {
                        setDefault(i);
                        return;
                    }
                }

                // we should never get here - if we do, leave default as is (there is a sanity check in the
                // set preference method, so no biggie)

            }

            private void setDefault(int position) {
                int previousDefaultPosition = mDefaultPosition;
                mDefaultPosition = position;
                notifyItemChanged(previousDefaultPosition);
                notifyItemChanged(mDefaultPosition);
            }

            private int countCheckedItems() {
                int result = 0;
                for (boolean isChecked : mPositionIsChecked) {
                    if (isChecked) {
                        result += 1;
                    }
                }
                return result;
            }

        }

    }
}