Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SettingsFragment.java « fragments « passman « app « wolfi « es « java « main « src « app - github.com/nextcloud/passman-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e91bd0843c001b1fee81cc7eb1670049f0838a61 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 * Passman Android App
 *
 * @copyright Copyright (c) 2016, Sander Brand (brantje@gmail.com)
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel (wolfi@wolfi.es)
 * @license GNU AGPL version 3 or any later version
 * <p>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * <p>
 * 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 Affero General Public License for more details.
 * <p>
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.wolfi.app.passman.fragments;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.material.checkbox.MaterialCheckBox;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.koushikdutta.async.future.FutureCallback;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import butterknife.ButterKnife;
import es.wolfi.app.passman.R;
import es.wolfi.app.passman.SettingValues;
import es.wolfi.app.passman.SingleTon;
import es.wolfi.app.passman.activities.PasswordListActivity;
import es.wolfi.passman.API.Vault;
import es.wolfi.utils.PasswordGenerator;


public class SettingsFragment extends Fragment {

    EditText settings_nextcloud_url;
    EditText settings_nextcloud_user;
    EditText settings_nextcloud_password;

    MaterialCheckBox settings_app_start_password_switch;

    MaterialCheckBox settings_password_generator_shortcut_switch;
    MaterialCheckBox settings_password_generator_use_uppercase_switch;
    MaterialCheckBox settings_password_generator_use_lowercase_switch;
    MaterialCheckBox settings_password_generator_use_digits_switch;
    MaterialCheckBox settings_password_generator_use_special_chars_switch;
    MaterialCheckBox settings_password_generator_avoid_ambiguous_chars_switch;
    MaterialCheckBox settings_password_generator_require_every_char_type_switch;
    EditText settings_password_generator_length_value;

    MaterialCheckBox enable_credential_list_icons_switch;

    TextView default_autofill_vault_title;
    Spinner default_autofill_vault;
    EditText clear_clipboard_delay_value;

    EditText request_connect_timeout_value;
    EditText request_response_timeout_value;

    SharedPreferences settings;
    PasswordGenerator passwordGenerator;

    public SettingsFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of this fragment.
     *
     * @return A new instance of fragment SettingsFragment.
     */
    public static SettingsFragment newInstance() {
        SettingsFragment fragment = new SettingsFragment();

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_settings, container, false);

        FloatingActionButton settingsSaveButton = view.findViewById(R.id.settings_save_button);
        settingsSaveButton.setOnClickListener(this.getSaveButtonListener());

        settings_nextcloud_url = view.findViewById(R.id.settings_nextcloud_url);
        settings_nextcloud_user = view.findViewById(R.id.settings_nextcloud_user);
        settings_nextcloud_password = view.findViewById(R.id.settings_nextcloud_password);

        settings_app_start_password_switch = view.findViewById(R.id.settings_app_start_password_switch);

        settings_password_generator_shortcut_switch = view.findViewById(R.id.settings_password_generator_shortcut_switch);
        settings_password_generator_use_uppercase_switch = view.findViewById(R.id.settings_password_generator_use_uppercase_switch);
        settings_password_generator_use_lowercase_switch = view.findViewById(R.id.settings_password_generator_use_lowercase_switch);
        settings_password_generator_use_digits_switch = view.findViewById(R.id.settings_password_generator_use_digits_switch);
        settings_password_generator_use_special_chars_switch = view.findViewById(R.id.settings_password_generator_use_special_chars_switch);
        settings_password_generator_avoid_ambiguous_chars_switch = view.findViewById(R.id.settings_password_generator_avoid_ambiguous_chars_switch);
        settings_password_generator_require_every_char_type_switch = view.findViewById(R.id.settings_password_generator_require_every_char_type_switch);
        settings_password_generator_length_value = view.findViewById(R.id.settings_password_generator_length_value);

        enable_credential_list_icons_switch = view.findViewById(R.id.enable_credential_list_icons_switch);

        default_autofill_vault_title = view.findViewById(R.id.default_autofill_vault_title);
        default_autofill_vault = view.findViewById(R.id.default_autofill_vault);
        clear_clipboard_delay_value = view.findViewById(R.id.clear_clipboard_delay_value);

        request_connect_timeout_value = view.findViewById(R.id.request_connect_timeout_value);
        request_response_timeout_value = view.findViewById(R.id.request_response_timeout_value);

        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

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

        settings_nextcloud_url.setText(settings.getString(SettingValues.HOST.toString(), null));
        settings_nextcloud_user.setText(settings.getString(SettingValues.USER.toString(), null));
        settings_nextcloud_password.setText(settings.getString(SettingValues.PASSWORD.toString(), null));

        settings_app_start_password_switch.setChecked(settings.getBoolean(SettingValues.ENABLE_APP_START_DEVICE_PASSWORD.toString(), false));

        passwordGenerator = new PasswordGenerator(getContext());

        settings_password_generator_shortcut_switch.setChecked(settings.getBoolean(SettingValues.ENABLE_PASSWORD_GENERATOR_SHORTCUT.toString(), true));
        settings_password_generator_use_uppercase_switch.setChecked(passwordGenerator.isUseUppercase());
        settings_password_generator_use_lowercase_switch.setChecked(passwordGenerator.isUseLowercase());
        settings_password_generator_use_digits_switch.setChecked(passwordGenerator.isUseDigits());
        settings_password_generator_use_special_chars_switch.setChecked(passwordGenerator.isUseSpecialChars());
        settings_password_generator_avoid_ambiguous_chars_switch.setChecked(passwordGenerator.isAvoidAmbiguousCharacters());
        settings_password_generator_require_every_char_type_switch.setChecked(passwordGenerator.isRequireEveryCharType());
        settings_password_generator_length_value.setText(String.valueOf(passwordGenerator.getLength()));

        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
            ((ViewManager) settings_password_generator_shortcut_switch.getParent()).removeView(settings_password_generator_shortcut_switch);
        }

        enable_credential_list_icons_switch.setChecked(settings.getBoolean(SettingValues.ENABLE_CREDENTIAL_LIST_ICONS.toString(), true));

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String last_selected_guid = "";
            if (settings.getString(SettingValues.AUTOFILL_VAULT_GUID.toString(), null) != null) {
                last_selected_guid = settings.getString(SettingValues.AUTOFILL_VAULT_GUID.toString(), null);
            }
            Set<Map.Entry<String, Vault>> vaults = getVaultsEntrySet();
            String[] vault_names = new String[vaults.size() + 1];
            vault_names[0] = getContext().getString(R.string.automatically);
            int i = 1;
            int selection_id = 0;
            for (Map.Entry<String, Vault> vault_entry : vaults) {
                if (last_selected_guid.equals(vault_entry.getValue().guid)) {
                    selection_id = i;
                }
                vault_names[i] = vault_entry.getValue().name;
                i++;
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, vault_names);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            default_autofill_vault.setAdapter(adapter);
            default_autofill_vault.setSelection(selection_id);
        } else {
            ((ViewManager) default_autofill_vault.getParent()).removeView(default_autofill_vault);
            ((ViewManager) default_autofill_vault_title.getParent()).removeView(default_autofill_vault_title);
        }

        clear_clipboard_delay_value.setText(String.valueOf(settings.getInt(SettingValues.CLEAR_CLIPBOARD_DELAY.toString(), 0)));

        request_connect_timeout_value.setText(String.valueOf(settings.getInt(SettingValues.REQUEST_CONNECT_TIMEOUT.toString(), 15)));
        request_response_timeout_value.setText(String.valueOf(settings.getInt(SettingValues.REQUEST_RESPONSE_TIMEOUT.toString(), 120)));
    }

    private Set<Map.Entry<String, Vault>> getVaultsEntrySet() {
        HashMap<String, Vault> vaults = (HashMap<String, Vault>) SingleTon.getTon().getExtra(SettingValues.VAULTS.toString());
        return vaults.entrySet();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        settings = PreferenceManager.getDefaultSharedPreferences(getContext());
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public View.OnClickListener getSaveButtonListener() {
        return new View.OnClickListener() {
            @SuppressLint("ApplySharedPref")
            @Override
            public void onClick(View view) {
                SingleTon ton = SingleTon.getTon();

                settings.edit().putBoolean(SettingValues.ENABLE_APP_START_DEVICE_PASSWORD.toString(), settings_app_start_password_switch.isChecked()).commit();

                settings.edit().putBoolean(SettingValues.ENABLE_PASSWORD_GENERATOR_SHORTCUT.toString(), settings_password_generator_shortcut_switch.isChecked()).commit();

                int length = Integer.parseInt(settings_password_generator_length_value.getText().toString());
                passwordGenerator.setLength(length > 0 ? length : 12);
                passwordGenerator.setUseUppercase(settings_password_generator_use_uppercase_switch.isChecked());
                passwordGenerator.setUseLowercase(settings_password_generator_use_lowercase_switch.isChecked());
                passwordGenerator.setUseDigits(settings_password_generator_use_digits_switch.isChecked());
                passwordGenerator.setUseSpecialChars(settings_password_generator_use_special_chars_switch.isChecked());
                passwordGenerator.setAvoidAmbiguousCharacters(settings_password_generator_avoid_ambiguous_chars_switch.isChecked());
                passwordGenerator.setRequireEveryCharType(settings_password_generator_require_every_char_type_switch.isChecked());
                passwordGenerator.applyChanges();

                settings.edit().putBoolean(SettingValues.ENABLE_CREDENTIAL_LIST_ICONS.toString(), enable_credential_list_icons_switch.isChecked()).commit();

                settings.edit().putInt(SettingValues.CLEAR_CLIPBOARD_DELAY.toString(), Integer.parseInt(clear_clipboard_delay_value.getText().toString())).commit();
                Objects.requireNonNull(((PasswordListActivity) getActivity())).attachClipboardListener();

                settings.edit().putInt(SettingValues.REQUEST_CONNECT_TIMEOUT.toString(), Integer.parseInt(request_connect_timeout_value.getText().toString())).commit();
                settings.edit().putInt(SettingValues.REQUEST_RESPONSE_TIMEOUT.toString(), Integer.parseInt(request_response_timeout_value.getText().toString())).commit();

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    if (default_autofill_vault.getSelectedItem().toString().equals(getContext().getString(R.string.automatically))) {
                        ton.removeExtra(SettingValues.AUTOFILL_VAULT_GUID.toString());
                        settings.edit().putString(SettingValues.AUTOFILL_VAULT_GUID.toString(), "").commit();
                    } else {
                        Set<Map.Entry<String, Vault>> vaults = getVaultsEntrySet();
                        for (Map.Entry<String, Vault> vault_entry : vaults) {
                            if (vault_entry.getValue().name.equals(default_autofill_vault.getSelectedItem().toString())) {
                                ton.addExtra(SettingValues.AUTOFILL_VAULT_GUID.toString(), vault_entry.getValue().guid);
                                settings.edit().putString(SettingValues.AUTOFILL_VAULT_GUID.toString(), vault_entry.getValue().guid).commit();

                                Vault.getVault(getContext(), vault_entry.getValue().guid, new FutureCallback<Vault>() {
                                    @Override
                                    public void onCompleted(Exception e, Vault result) {
                                        if (e != null) {
                                            return;
                                        }
                                        Vault.updateAutofillVault(result, settings);
                                    }
                                });

                                break;
                            }
                        }
                    }
                }

                if (!settings.getString(SettingValues.HOST.toString(), null).equals(settings_nextcloud_url.getText().toString()) ||
                        !settings.getString(SettingValues.USER.toString(), null).equals(settings_nextcloud_user.getText().toString()) ||
                        !settings.getString(SettingValues.PASSWORD.toString(), null).equals(settings_nextcloud_password.getText().toString())) {
                    ton.removeString(SettingValues.HOST.toString());
                    ton.removeString(SettingValues.USER.toString());
                    ton.removeString(SettingValues.PASSWORD.toString());

                    ton.addString(SettingValues.HOST.toString(), settings_nextcloud_url.getText().toString());
                    ton.addString(SettingValues.USER.toString(), settings_nextcloud_user.getText().toString());
                    ton.addString(SettingValues.PASSWORD.toString(), settings_nextcloud_password.getText().toString());

                    settings.edit().putString(SettingValues.HOST.toString(), settings_nextcloud_url.getText().toString()).commit();
                    settings.edit().putString(SettingValues.USER.toString(), settings_nextcloud_user.getText().toString()).commit();
                    settings.edit().putString(SettingValues.PASSWORD.toString(), settings_nextcloud_password.getText().toString()).commit();

                    Objects.requireNonNull(((PasswordListActivity) getActivity())).applyNewSettings(true);
                } else {
                    Objects.requireNonNull(((PasswordListActivity) getActivity())).applyNewSettings(false);
                }
            }
        };
    }
}