2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.profiles;
20 import android.app.Activity;
21 import android.app.AlertDialog;
22 import android.os.Bundle;
23 import android.text.Editable;
24 import android.text.TextWatcher;
25 import android.view.LayoutInflater;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.LinearLayout;
32 import android.widget.PopupMenu;
33 import android.widget.Switch;
34 import android.widget.TextView;
36 import androidx.annotation.NonNull;
37 import androidx.annotation.Nullable;
38 import androidx.fragment.app.Fragment;
39 import androidx.fragment.app.FragmentActivity;
41 import com.google.android.material.appbar.CollapsingToolbarLayout;
42 import com.google.android.material.floatingactionbutton.FloatingActionButton;
43 import com.google.android.material.textfield.TextInputLayout;
45 import net.ktnx.mobileledger.BuildConfig;
46 import net.ktnx.mobileledger.R;
47 import net.ktnx.mobileledger.async.SendTransactionTask;
48 import net.ktnx.mobileledger.model.Data;
49 import net.ktnx.mobileledger.model.MobileLedgerProfile;
50 import net.ktnx.mobileledger.ui.HueRingDialog;
51 import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
52 import net.ktnx.mobileledger.utils.Colors;
54 import org.jetbrains.annotations.NonNls;
55 import org.jetbrains.annotations.NotNull;
57 import java.net.MalformedURLException;
59 import java.util.ArrayList;
60 import java.util.Objects;
62 import static net.ktnx.mobileledger.utils.Logger.debug;
65 * A fragment representing a single Profile detail screen.
66 * a {@link ProfileDetailActivity}
69 public class ProfileDetailFragment extends Fragment implements HueRingDialog.HueSelectedListener {
71 * The fragment argument representing the item ID that this fragment
74 public static final String ARG_ITEM_ID = "item_id";
76 private static final String HTTPS_URL_START = "https://";
79 * The dummy content this fragment is presenting.
81 private MobileLedgerProfile mProfile;
83 private Switch postingPermitted;
84 private TextInputLayout urlLayout;
85 private LinearLayout authParams;
86 private Switch useAuthentication;
87 private TextView userName;
88 private TextInputLayout userNameLayout;
89 private TextView password;
90 private TextInputLayout passwordLayout;
91 private TextView profileName;
92 private TextInputLayout profileNameLayout;
93 private TextView preferredAccountsFilter;
94 private TextInputLayout preferredAccountsFilterLayout;
95 private View huePickerView;
96 private View insecureWarningText;
97 private TextView futureDatesText;
98 private MobileLedgerProfile.FutureDates futureDates;
99 private View futureDatesLayout;
100 private TextView apiVersionText;
101 private View apiVersionLayout;
102 private SendTransactionTask.API apiVersion;
105 * Mandatory empty constructor for the fragment manager to instantiate the
106 * fragment (e.g. upon screen orientation changes).
108 public ProfileDetailFragment() {
111 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
112 debug("profiles", "[fragment] Creating profile details options menu");
113 super.onCreateOptionsMenu(menu, inflater);
114 inflater.inflate(R.menu.profile_details, menu);
115 final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
116 menuDeleteProfile.setOnMenuItemClickListener(item -> {
117 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
118 builder.setTitle(mProfile.getName());
119 builder.setMessage(R.string.remove_profile_dialog_message);
120 builder.setPositiveButton(R.string.Remove, (dialog, which) -> {
122 String.format("[fragment] removing profile %s", mProfile.getUuid()));
123 mProfile.removeFromDB();
124 ArrayList<MobileLedgerProfile> oldList = Data.profiles.getValue();
126 throw new AssertionError();
127 ArrayList<MobileLedgerProfile> newList = new ArrayList<>(oldList);
128 newList.remove(mProfile);
129 Data.profiles.setValue(newList);
130 if (mProfile.equals(Data.profile.getValue())) {
131 debug("profiles", "[fragment] setting current profile to 0");
132 Data.setCurrentProfile(newList.get(0));
135 final FragmentActivity activity = getActivity();
136 if (activity != null)
142 final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
143 menuDeleteProfile.setVisible(
144 (mProfile != null) && (profiles != null) && (profiles.size() > 1));
146 if (BuildConfig.DEBUG) {
147 final MenuItem menuWipeProfileData = menu.findItem(R.id.menuWipeData);
148 menuWipeProfileData.setOnMenuItemClickListener(ignored -> onWipeDataMenuClicked());
149 menuWipeProfileData.setVisible(mProfile != null);
152 private boolean onWipeDataMenuClicked() {
153 // this is a development option, so no confirmation
154 mProfile.wipeAllData();
155 if (mProfile.equals(Data.profile.getValue()))
156 triggerProfileChange();
159 private void triggerProfileChange() {
160 int index = Data.getProfileIndex(mProfile);
161 MobileLedgerProfile newProfile = new MobileLedgerProfile(mProfile);
162 final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
163 if (profiles == null)
164 throw new AssertionError();
165 profiles.set(index, newProfile);
167 ProfilesRecyclerViewAdapter prva = ProfilesRecyclerViewAdapter.getInstance();
169 prva.notifyItemChanged(index);
171 if (mProfile.equals(Data.profile.getValue()))
172 Data.profile.setValue(newProfile);
175 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
176 super.onActivityCreated(savedInstanceState);
177 Activity context = getActivity();
181 if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
182 int index = getArguments().getInt(ARG_ITEM_ID, -1);
183 ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
184 if ((profiles != null) && (index != -1) && (index < profiles.size()))
185 mProfile = profiles.get(index);
187 Activity activity = this.getActivity();
188 if (activity == null)
189 throw new AssertionError();
190 CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
191 if (appBarLayout != null) {
192 if (mProfile != null)
193 appBarLayout.setTitle(mProfile.getName());
195 appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
199 FloatingActionButton fab = context.findViewById(R.id.fab);
200 fab.setOnClickListener(v -> onSaveFabClicked());
201 profileName = context.findViewById(R.id.profile_name);
202 profileNameLayout = context.findViewById(R.id.profile_name_layout);
203 url = context.findViewById(R.id.url);
204 urlLayout = context.findViewById(R.id.url_layout);
205 postingPermitted = context.findViewById(R.id.profile_permit_posting);
206 futureDatesLayout = context.findViewById(R.id.future_dates_layout);
207 futureDatesText = context.findViewById(R.id.future_dates_text);
208 context.findViewById(R.id.future_dates_layout)
209 .setOnClickListener(v -> {
210 MenuInflater mi = new MenuInflater(context);
211 PopupMenu menu = new PopupMenu(context, v);
212 menu.inflate(R.menu.future_dates);
213 menu.setOnMenuItemClickListener(item -> {
214 switch (item.getItemId()) {
215 case R.id.menu_future_dates_30:
216 futureDates = MobileLedgerProfile.FutureDates.OneMonth;
218 case R.id.menu_future_dates_60:
219 futureDates = MobileLedgerProfile.FutureDates.TwoMonths;
221 case R.id.menu_future_dates_90:
222 futureDates = MobileLedgerProfile.FutureDates.ThreeMonths;
224 case R.id.menu_future_dates_180:
225 futureDates = MobileLedgerProfile.FutureDates.SixMonths;
227 case R.id.menu_future_dates_365:
228 futureDates = MobileLedgerProfile.FutureDates.OneYear;
230 case R.id.menu_future_dates_all:
231 futureDates = MobileLedgerProfile.FutureDates.All;
234 futureDates = MobileLedgerProfile.FutureDates.None;
236 futureDatesText.setText(futureDates.getText(getResources()));
241 apiVersionLayout = context.findViewById(R.id.api_version_layout);
242 apiVersionText = context.findViewById(R.id.api_version_text);
243 apiVersionLayout.setOnClickListener(v -> {
244 MenuInflater mi = new MenuInflater(context);
245 PopupMenu menu = new PopupMenu(context, v);
246 menu.inflate(R.menu.api_version);
247 menu.setOnMenuItemClickListener(item -> {
248 switch (item.getItemId()) {
249 case R.id.api_version_menu_html:
250 apiVersion = SendTransactionTask.API.html;
252 case R.id.api_version_menu_post_1_14:
253 apiVersion = SendTransactionTask.API.post_1_14;
255 case R.id.api_version_menu_pre_1_15:
256 apiVersion = SendTransactionTask.API.pre_1_15;
258 case R.id.api_version_menu_auto:
260 apiVersion = SendTransactionTask.API.auto;
262 apiVersionText.setText(apiVersion.getDescription(getResources()));
267 authParams = context.findViewById(R.id.auth_params);
268 useAuthentication = context.findViewById(R.id.enable_http_auth);
269 userName = context.findViewById(R.id.auth_user_name);
270 userNameLayout = context.findViewById(R.id.auth_user_name_layout);
271 password = context.findViewById(R.id.password);
272 passwordLayout = context.findViewById(R.id.password_layout);
273 huePickerView = context.findViewById(R.id.btn_pick_ring_color);
274 preferredAccountsFilter = context.findViewById(R.id.preferred_accounts_filter_filter);
275 preferredAccountsFilterLayout =
276 context.findViewById(R.id.preferred_accounts_accounts_filter_layout);
277 insecureWarningText = context.findViewById(R.id.insecure_scheme_text);
279 useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
280 debug("profiles", isChecked ? "auth enabled " : "auth disabled");
281 authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
283 userName.requestFocus();
284 checkInsecureSchemeWithAuth();
287 postingPermitted.setOnCheckedChangeListener(((buttonView, isChecked) -> {
288 preferredAccountsFilterLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
289 futureDatesLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
292 hookClearErrorOnFocusListener(profileName, profileNameLayout);
293 hookClearErrorOnFocusListener(url, urlLayout);
294 hookClearErrorOnFocusListener(userName, userNameLayout);
295 hookClearErrorOnFocusListener(password, passwordLayout);
298 if (mProfile != null) {
299 profileName.setText(mProfile.getName());
300 postingPermitted.setChecked(mProfile.isPostingPermitted());
301 futureDates = mProfile.getFutureDates();
302 futureDatesText.setText(futureDates.getText(getResources()));
303 apiVersion = mProfile.getApiVersion();
304 apiVersionText.setText(apiVersion.getDescription(getResources()));
305 url.setText(mProfile.getUrl());
306 useAuthentication.setChecked(mProfile.isAuthEnabled());
307 authParams.setVisibility(mProfile.isAuthEnabled() ? View.VISIBLE : View.GONE);
308 userName.setText(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
309 password.setText(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
310 preferredAccountsFilter.setText(mProfile.getPreferredAccountsFilter());
311 profileThemeId = mProfile.getThemeId();
314 profileName.setText("");
315 url.setText(HTTPS_URL_START);
316 postingPermitted.setChecked(true);
317 futureDates = MobileLedgerProfile.FutureDates.None;
318 futureDatesText.setText(futureDates.getText(getResources()));
319 apiVersion = SendTransactionTask.API.auto;
320 apiVersionText.setText(apiVersion.getDescription(getResources()));
321 useAuthentication.setChecked(false);
322 authParams.setVisibility(View.GONE);
323 userName.setText("");
324 password.setText("");
325 preferredAccountsFilter.setText(null);
329 checkInsecureSchemeWithAuth();
331 url.addTextChangedListener(new TextWatcher() {
333 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
337 public void onTextChanged(CharSequence s, int start, int before, int count) {
341 public void afterTextChanged(Editable s) {
342 checkInsecureSchemeWithAuth();
346 final int hue = (profileThemeId == -1) ? Colors.DEFAULT_HUE_DEG : profileThemeId;
347 final int profileColor = Colors.getPrimaryColorForHue(hue);
349 huePickerView.setBackgroundColor(profileColor);
350 huePickerView.setTag(profileThemeId);
351 huePickerView.setOnClickListener(v -> {
352 HueRingDialog d = new HueRingDialog(
353 Objects.requireNonNull(ProfileDetailFragment.this.getContext()), profileThemeId,
354 (Integer) v.getTag());
356 d.setColorSelectedListener(this);
359 profileName.requestFocus();
361 private void onSaveFabClicked() {
362 if (!checkValidity())
365 if (mProfile != null) {
366 updateProfileFromUI();
367 // debug("profiles", String.format("Selected item is %d", mProfile.getThemeId()));
368 mProfile.storeInDB();
369 debug("profiles", "profile stored in DB");
370 triggerProfileChange();
373 mProfile = new MobileLedgerProfile();
374 updateProfileFromUI();
375 mProfile.storeInDB();
376 final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
377 if (profiles == null)
378 throw new AssertionError();
379 ArrayList<MobileLedgerProfile> newList = new ArrayList<>(profiles);
380 newList.add(mProfile);
381 Data.profiles.setValue(newList);
382 MobileLedgerProfile.storeProfilesOrder();
384 // first profile ever?
385 if (newList.size() == 1)
386 Data.profile.setValue(mProfile);
389 Activity activity = getActivity();
390 if (activity != null)
393 private void updateProfileFromUI() {
394 mProfile.setName(profileName.getText());
395 mProfile.setUrl(url.getText());
396 mProfile.setPostingPermitted(postingPermitted.isChecked());
397 mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getText());
398 mProfile.setAuthEnabled(useAuthentication.isChecked());
399 mProfile.setAuthUserName(userName.getText());
400 mProfile.setAuthPassword(password.getText());
401 mProfile.setThemeId(huePickerView.getTag());
402 mProfile.setFutureDates(futureDates);
403 mProfile.setApiVersion(apiVersion);
406 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
407 Bundle savedInstanceState) {
409 return inflater.inflate(R.layout.profile_detail, container, false);
411 private boolean checkUrlValidity() {
412 boolean valid = true;
414 String val = String.valueOf(url.getText())
418 urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
421 URL url = new URL(val);
422 String host = url.getHost();
423 if (host == null || host.isEmpty())
424 throw new MalformedURLException("Missing host");
425 String protocol = url.getProtocol()
427 if (!protocol.equals("HTTP") && !protocol.equals("HTTPS")) {
429 urlLayout.setError(getResources().getText(R.string.err_invalid_url));
432 catch (MalformedURLException e) {
434 urlLayout.setError(getResources().getText(R.string.err_invalid_url));
439 private void checkInsecureSchemeWithAuth() {
440 boolean showWarning = false;
442 if (useAuthentication.isChecked()) {
443 String urlText = url.getText()
445 if (urlText.startsWith("http") && !urlText.startsWith("https"))
450 insecureWarningText.setVisibility(View.VISIBLE);
452 insecureWarningText.setVisibility(View.GONE);
454 private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
455 view.setOnFocusChangeListener((v, hasFocus) -> {
457 layout.setError(null);
459 view.addTextChangedListener(new TextWatcher() {
461 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
464 public void onTextChanged(CharSequence s, int start, int before, int count) {
465 layout.setError(null);
468 public void afterTextChanged(Editable s) {
472 private boolean checkValidity() {
473 boolean valid = true;
475 String val = String.valueOf(profileName.getText());
480 profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
483 if (!checkUrlValidity())
486 if (useAuthentication.isChecked()) {
487 val = String.valueOf(userName.getText());
492 userNameLayout.setError(
493 getResources().getText(R.string.err_profile_user_name_empty));
496 val = String.valueOf(password.getText());
501 passwordLayout.setError(
502 getResources().getText(R.string.err_profile_password_empty));
509 public void onHueSelected(int hue) {
510 huePickerView.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
511 huePickerView.setTag(hue);