Search in sources :

Example 1 with ProxyConfig

use of de.danoeh.antennapod.core.service.download.ProxyConfig in project AntennaPod by AntennaPod.

the class ProxyDialog method createDialog.

public Dialog createDialog() {
    dialog = new MaterialDialog.Builder(context).title(R.string.pref_proxy_title).customView(R.layout.proxy_settings, true).positiveText(R.string.proxy_test_label).negativeText(R.string.cancel_label).onPositive((dialog1, which) -> {
        if (!testSuccessful) {
            dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
            test();
            return;
        }
        String type = (String) ((Spinner) dialog1.findViewById(R.id.spType)).getSelectedItem();
        ProxyConfig proxy;
        if (Proxy.Type.valueOf(type) == Proxy.Type.DIRECT) {
            proxy = ProxyConfig.direct();
        } else {
            String host = etHost.getText().toString();
            String port = etPort.getText().toString();
            String username = etUsername.getText().toString();
            if (TextUtils.isEmpty(username)) {
                username = null;
            }
            String password = etPassword.getText().toString();
            if (TextUtils.isEmpty(password)) {
                password = null;
            }
            int portValue = 0;
            if (!TextUtils.isEmpty(port)) {
                portValue = Integer.valueOf(port);
            }
            proxy = ProxyConfig.http(host, portValue, username, password);
        }
        UserPreferences.setProxyConfig(proxy);
        AntennapodHttpClient.reinit();
        dialog.dismiss();
    }).onNegative((dialog1, which) -> dialog1.dismiss()).autoDismiss(false).build();
    View view = dialog.getCustomView();
    spType = (Spinner) view.findViewById(R.id.spType);
    String[] types = { Proxy.Type.DIRECT.name(), Proxy.Type.HTTP.name() };
    ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, types);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spType.setAdapter(adapter);
    ProxyConfig proxyConfig = UserPreferences.getProxyConfig();
    spType.setSelection(adapter.getPosition(proxyConfig.type.name()));
    etHost = (EditText) view.findViewById(R.id.etHost);
    if (!TextUtils.isEmpty(proxyConfig.host)) {
        etHost.setText(proxyConfig.host);
    }
    etHost.addTextChangedListener(requireTestOnChange);
    etPort = (EditText) view.findViewById(R.id.etPort);
    if (proxyConfig.port > 0) {
        etPort.setText(String.valueOf(proxyConfig.port));
    }
    etPort.addTextChangedListener(requireTestOnChange);
    etUsername = (EditText) view.findViewById(R.id.etUsername);
    if (!TextUtils.isEmpty(proxyConfig.username)) {
        etUsername.setText(proxyConfig.username);
    }
    etUsername.addTextChangedListener(requireTestOnChange);
    etPassword = (EditText) view.findViewById(R.id.etPassword);
    if (!TextUtils.isEmpty(proxyConfig.password)) {
        etPassword.setText(proxyConfig.username);
    }
    etPassword.addTextChangedListener(requireTestOnChange);
    if (proxyConfig.type == Proxy.Type.DIRECT) {
        enableSettings(false);
        setTestRequired(false);
    }
    spType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            enableSettings(position > 0);
            setTestRequired(position > 0);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            enableSettings(false);
        }
    });
    txtvMessage = (TextView) view.findViewById(R.id.txtvMessage);
    checkValidity();
    return dialog;
}
Also used : Context(android.content.Context) SocketAddress(java.net.SocketAddress) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Dialog(android.app.Dialog) Editable(android.text.Editable) TypedArray(android.content.res.TypedArray) Observable(rx.Observable) UserPreferences(de.danoeh.antennapod.core.preferences.UserPreferences) MDButton(com.afollestad.materialdialogs.internal.MDButton) Patterns(android.util.Patterns) Proxy(java.net.Proxy) Schedulers(rx.schedulers.Schedulers) View(android.view.View) Response(okhttp3.Response) AdapterView(android.widget.AdapterView) AntennapodHttpClient(de.danoeh.antennapod.core.service.download.AntennapodHttpClient) Request(okhttp3.Request) Subscriber(rx.Subscriber) R(de.danoeh.antennapod.R) ContextCompat(android.support.v4.content.ContextCompat) TextUtils(android.text.TextUtils) DialogAction(com.afollestad.materialdialogs.DialogAction) IOException(java.io.IOException) Credentials(okhttp3.Credentials) InetSocketAddress(java.net.InetSocketAddress) ProxyConfig(de.danoeh.antennapod.core.service.download.ProxyConfig) Spinner(android.widget.Spinner) TimeUnit(java.util.concurrent.TimeUnit) ArrayAdapter(android.widget.ArrayAdapter) TextView(android.widget.TextView) OkHttpClient(okhttp3.OkHttpClient) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Subscription(rx.Subscription) EditText(android.widget.EditText) TextWatcher(android.text.TextWatcher) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ProxyConfig(de.danoeh.antennapod.core.service.download.ProxyConfig) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) AdapterView(android.widget.AdapterView) ArrayAdapter(android.widget.ArrayAdapter)

Aggregations

Dialog (android.app.Dialog)1 Context (android.content.Context)1 TypedArray (android.content.res.TypedArray)1 ContextCompat (android.support.v4.content.ContextCompat)1 Editable (android.text.Editable)1 TextUtils (android.text.TextUtils)1 TextWatcher (android.text.TextWatcher)1 Patterns (android.util.Patterns)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ArrayAdapter (android.widget.ArrayAdapter)1 EditText (android.widget.EditText)1 Spinner (android.widget.Spinner)1 TextView (android.widget.TextView)1 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 MDButton (com.afollestad.materialdialogs.internal.MDButton)1 R (de.danoeh.antennapod.R)1 UserPreferences (de.danoeh.antennapod.core.preferences.UserPreferences)1 AntennapodHttpClient (de.danoeh.antennapod.core.service.download.AntennapodHttpClient)1