<commit_before>package org.owntracks.android.ui.preferences.connection.dialog;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.OpenableColumns;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.appcompat.widget.PopupMenu;

import org.owntracks.android.R;
import org.owntracks.android.support.Preferences;
import org.owntracks.android.ui.base.navigator.Navigator;

import java.io.FileOutputStream;
import java.io.InputStream;

import timber.log.Timber;

import static android.app.Activity.RESULT_OK;

public class ConnectionSecurityViewModel extends BaseDialogViewModel {
    private static final int REQUEST_CODE_FILE_CA_CRT = 1;
    private static final int REQUEST_CODE_FILE_CLIENT_CRT = 2;

    private final Navigator navigator;
    private final Context context;
    private boolean tls;
    private String tlsCaCrtName;
    private String tlsClientCrtName;
    private String tlsClientCrtPassword;
    private boolean tlsClientCrtNameDirty;
    private boolean tlsCaCrtNameDirty;
    private boolean tlsClientCrtPasswortDirty;

    public ConnectionSecurityViewModel(Preferences preferences, Navigator navigator, Context context) {
        super(preferences);
        this.navigator = navigator;
        this.context = context;
    }

    @Override
    public void load() {
        this.tls = preferences.getTls();
        this.tlsCaCrtName = preferences.getTlsCaCrt();
        this.tlsClientCrtName = preferences.getTlsClientCrt();
        this.tlsClientCrtPassword = preferences.getTlsClientCrtPassword();
    }

    @Override
    public void save() {
        preferences.setTls(tls);

        if(tlsCaCrtNameDirty)
            preferences.setTlsCaCrt(tlsCaCrtName);

        if(tlsClientCrtNameDirty)
            preferences.setTlsClientCrt(tlsClientCrtName);

        if(tlsClientCrtPasswortDirty)
            preferences.setTlsClientCrtPassword(tlsClientCrtPassword);
    }

    public boolean isTls() {
        return tls;
    }

    private void setTls(boolean tls) {
        this.tls = tls;
        notifyChange();
    }

    public String getTlsCaCrtName() {
        return tlsCaCrtName;
    }

    private void setTlsCaCrtName(String tlsCaCrtName) {
        this.tlsCaCrtName = tlsCaCrtName;
        this.tlsCaCrtNameDirty = true;
        notifyChange();
    }
<commit_msg>Fix for #865 to prevent NPE on clearing Security Cert fields<commit_after>package org.owntracks.android.ui.preferences.connection.dialog;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.OpenableColumns;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.appcompat.widget.PopupMenu;

import org.owntracks.android.R;
import org.owntracks.android.support.Preferences;
import org.owntracks.android.ui.base.navigator.Navigator;

import java.io.FileOutputStream;
import java.io.InputStream;

import timber.log.Timber;

import static android.app.Activity.RESULT_OK;

public class ConnectionSecurityViewModel extends BaseDialogViewModel {
    private static final int REQUEST_CODE_FILE_CA_CRT = 1;
    private static final int REQUEST_CODE_FILE_CLIENT_CRT = 2;

    private final Navigator navigator;
    private final Context context;
    private boolean tls;
    private String tlsCaCrtName;
    private String tlsClientCrtName;
    private String tlsClientCrtPassword;
    private boolean tlsClientCrtNameDirty;
    private boolean tlsCaCrtNameDirty;
    private boolean tlsClientCrtPasswortDirty;

    public ConnectionSecurityViewModel(Preferences preferences, Navigator navigator, Context context) {
        super(preferences);
        this.navigator = navigator;
        this.context = context;
    }

    @Override
    public void load() {
        this.tls = preferences.getTls();
        this.tlsCaCrtName = preferences.getTlsCaCrt();
        this.tlsClientCrtName = preferences.getTlsClientCrt();
        this.tlsClientCrtPassword = preferences.getTlsClientCrtPassword();
    }

    @Override
    public void save() {
        preferences.setTls(tls);

        if(tlsCaCrtNameDirty)
            preferences.setTlsCaCrt(tlsCaCrtName == null ? "" : tlsCaCrtName);

        if(tlsClientCrtNameDirty)
            preferences.setTlsClientCrt(tlsClientCrtName == null ? "" : tlsClientCrtName);

        if(tlsClientCrtPasswortDirty)
            preferences.setTlsClientCrtPassword(tlsClientCrtPassword == null ? "" : tlsClientCrtPassword);
    }

    public boolean isTls() {
        return tls;
    }

    private void setTls(boolean tls) {
        this.tls = tls;
        notifyChange();
    }

    public String getTlsCaCrtName() {
        return tlsCaCrtName;
    }

    private void setTlsCaCrtName(String tlsCaCrtName) {
        this.tlsCaCrtName = tlsCaCrtName;
        this.tlsCaCrtNameDirty = true;
        notifyChange();
    }

{'fraction_non_alphanumeric': 0.07758790021606757, 'fraction_numerical': 0.0013749754468670202, 'mean_word_length': 3.5750224618149145, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1379918', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1516, 'n_words': 304}