2015-02-16 20:31:44 -08:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
import android.app.Dialog;
|
2015-02-16 20:31:44 -08:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.os.Bundle;
|
2020-08-19 10:06:26 +10:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.preference.DialogPreference;
|
|
|
|
import androidx.preference.PreferenceDialogFragmentCompat;
|
2015-02-16 20:31:44 -08:00
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.util.AttributeSet;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2015-02-16 20:31:44 -08:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.Spinner;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2019-07-24 12:30:23 +10:00
|
|
|
import network.loki.messenger.R;
|
2017-09-20 18:10:44 -07:00
|
|
|
import org.thoughtcrime.securesms.components.CustomDefaultPreference.CustomDefaultPreferenceDialogFragmentCompat.CustomPreferenceValidator;
|
2015-02-16 20:31:44 -08:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
|
|
|
|
|
|
|
public class CustomDefaultPreference extends DialogPreference {
|
|
|
|
|
|
|
|
private static final String TAG = CustomDefaultPreference.class.getSimpleName();
|
|
|
|
|
|
|
|
private final int inputType;
|
|
|
|
private final String customPreference;
|
|
|
|
private final String customToggle;
|
|
|
|
|
|
|
|
private CustomPreferenceValidator validator;
|
|
|
|
private String defaultValue;
|
|
|
|
|
|
|
|
public CustomDefaultPreference(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
|
|
|
|
int[] attributeNames = new int[]{android.R.attr.inputType, R.attr.custom_pref_toggle};
|
|
|
|
TypedArray attributes = context.obtainStyledAttributes(attrs, attributeNames);
|
|
|
|
|
|
|
|
this.inputType = attributes.getInt(0, 0);
|
|
|
|
this.customPreference = getKey();
|
|
|
|
this.customToggle = attributes.getString(1);
|
2017-09-20 18:10:44 -07:00
|
|
|
this.validator = new CustomDefaultPreferenceDialogFragmentCompat.NullValidator();
|
2015-02-16 20:31:44 -08:00
|
|
|
|
|
|
|
attributes.recycle();
|
|
|
|
|
|
|
|
setPersistent(false);
|
|
|
|
setDialogLayoutResource(R.layout.custom_default_preference_dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
public CustomDefaultPreference setValidator(CustomPreferenceValidator validator) {
|
|
|
|
this.validator = validator;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CustomDefaultPreference setDefaultValue(String defaultValue) {
|
|
|
|
this.defaultValue = defaultValue;
|
|
|
|
this.setSummary(getSummary());
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getSummary() {
|
|
|
|
if (isCustom()) {
|
|
|
|
return getContext().getString(R.string.CustomDefaultPreference_using_custom,
|
|
|
|
getPrettyPrintValue(getCustomValue()));
|
|
|
|
} else {
|
|
|
|
return getContext().getString(R.string.CustomDefaultPreference_using_default,
|
|
|
|
getPrettyPrintValue(getDefaultValue()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getPrettyPrintValue(String value) {
|
|
|
|
if (TextUtils.isEmpty(value)) return getContext().getString(R.string.CustomDefaultPreference_none);
|
|
|
|
else return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isCustom() {
|
|
|
|
return TextSecurePreferences.getBooleanPreference(getContext(), customToggle, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setCustom(boolean custom) {
|
|
|
|
TextSecurePreferences.setBooleanPreference(getContext(), customToggle, custom);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getCustomValue() {
|
|
|
|
return TextSecurePreferences.getStringPreference(getContext(), customPreference, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setCustomValue(String value) {
|
|
|
|
TextSecurePreferences.setStringPreference(getContext(), customPreference, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getDefaultValue() {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
public static class CustomDefaultPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat {
|
|
|
|
|
|
|
|
private static final String INPUT_TYPE = "input_type";
|
|
|
|
|
|
|
|
private Spinner spinner;
|
|
|
|
private EditText customText;
|
|
|
|
private TextView defaultLabel;
|
|
|
|
|
|
|
|
public static CustomDefaultPreferenceDialogFragmentCompat newInstance(String key) {
|
|
|
|
CustomDefaultPreferenceDialogFragmentCompat fragment = new CustomDefaultPreferenceDialogFragmentCompat();
|
|
|
|
Bundle b = new Bundle(1);
|
|
|
|
b.putString(PreferenceDialogFragmentCompat.ARG_KEY, key);
|
|
|
|
fragment.setArguments(b);
|
|
|
|
return fragment;
|
2015-02-16 20:31:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-20 18:10:44 -07:00
|
|
|
protected void onBindDialogView(@NonNull View view) {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "onBindDialogView");
|
2017-09-20 18:10:44 -07:00
|
|
|
super.onBindDialogView(view);
|
|
|
|
|
|
|
|
CustomDefaultPreference preference = (CustomDefaultPreference)getPreference();
|
|
|
|
|
|
|
|
this.spinner = (Spinner) view.findViewById(R.id.default_or_custom);
|
|
|
|
this.defaultLabel = (TextView) view.findViewById(R.id.default_label);
|
|
|
|
this.customText = (EditText) view.findViewById(R.id.custom_edit);
|
|
|
|
|
|
|
|
this.customText.setInputType(preference.inputType);
|
|
|
|
this.customText.addTextChangedListener(new TextValidator());
|
|
|
|
this.customText.setText(preference.getCustomValue());
|
|
|
|
this.spinner.setOnItemSelectedListener(new SelectionLister());
|
|
|
|
this.defaultLabel.setText(preference.getPrettyPrintValue(preference.defaultValue));
|
2015-02-16 20:31:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public @NonNull Dialog onCreateDialog(Bundle instanceState) {
|
2017-09-20 18:10:44 -07:00
|
|
|
Dialog dialog = super.onCreateDialog(instanceState);
|
2015-02-16 20:31:44 -08:00
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
CustomDefaultPreference preference = (CustomDefaultPreference)getPreference();
|
|
|
|
|
|
|
|
if (preference.isCustom()) spinner.setSelection(1, true);
|
|
|
|
else spinner.setSelection(0, true);
|
|
|
|
|
|
|
|
return dialog;
|
|
|
|
}
|
2015-02-16 20:31:44 -08:00
|
|
|
|
|
|
|
@Override
|
2017-09-20 18:10:44 -07:00
|
|
|
public void onDialogClosed(boolean positiveResult) {
|
|
|
|
CustomDefaultPreference preference = (CustomDefaultPreference)getPreference();
|
|
|
|
|
|
|
|
if (positiveResult) {
|
|
|
|
if (spinner != null) preference.setCustom(spinner.getSelectedItemPosition() == 1);
|
|
|
|
if (customText != null) preference.setCustomValue(customText.getText().toString());
|
|
|
|
|
|
|
|
preference.setSummary(preference.getSummary());
|
2015-02-16 20:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
interface CustomPreferenceValidator {
|
|
|
|
public boolean isValid(String value);
|
|
|
|
}
|
2015-02-16 20:31:44 -08:00
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
private static class NullValidator implements CustomPreferenceValidator {
|
|
|
|
@Override
|
|
|
|
public boolean isValid(String value) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-16 20:31:44 -08:00
|
|
|
}
|
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
private class TextValidator implements TextWatcher {
|
2015-02-16 20:31:44 -08:00
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
CustomDefaultPreference preference = (CustomDefaultPreference)getPreference();
|
|
|
|
|
|
|
|
if (spinner.getSelectedItemPosition() == 1) {
|
|
|
|
Button positiveButton = ((AlertDialog)getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
|
|
|
|
positiveButton.setEnabled(preference.validator.isValid(s.toString()));
|
|
|
|
}
|
2015-02-16 20:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
public static class UriValidator implements CustomPreferenceValidator {
|
|
|
|
@Override
|
|
|
|
public boolean isValid(String value) {
|
|
|
|
if (TextUtils.isEmpty(value)) return true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
new URI(value);
|
|
|
|
return true;
|
|
|
|
} catch (URISyntaxException mue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-16 20:31:44 -08:00
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
public static class HostnameValidator implements CustomPreferenceValidator {
|
|
|
|
@Override
|
|
|
|
public boolean isValid(String value) {
|
|
|
|
if (TextUtils.isEmpty(value)) return true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
URI uri = new URI(null, value, null, null);
|
|
|
|
return true;
|
|
|
|
} catch (URISyntaxException mue) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-16 20:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
public static class PortValidator implements CustomPreferenceValidator {
|
|
|
|
@Override
|
|
|
|
public boolean isValid(String value) {
|
|
|
|
try {
|
|
|
|
Integer.parseInt(value);
|
|
|
|
return true;
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-16 20:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
2017-09-20 18:10:44 -07:00
|
|
|
|
|
|
|
private class SelectionLister implements AdapterView.OnItemSelectedListener {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
CustomDefaultPreference preference = (CustomDefaultPreference)getPreference();
|
|
|
|
Button positiveButton = ((AlertDialog)getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
|
|
|
|
|
|
|
|
defaultLabel.setVisibility(position == 0 ? View.VISIBLE : View.GONE);
|
|
|
|
customText.setVisibility(position == 0 ? View.GONE : View.VISIBLE);
|
|
|
|
positiveButton.setEnabled(position == 0 || preference.validator.isValid(customText.getText().toString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNothingSelected(AdapterView<?> parent) {
|
|
|
|
defaultLabel.setVisibility(View.VISIBLE);
|
|
|
|
customText.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-16 20:31:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|