private void loadSecurityPreferences(final Activity a) {

      Preference.OnPreferenceClickListener securityListener =
          new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
              securityDialog =
                  new MaterialDialog.Builder(a)
                      .customView(R.layout.preferences_security, true)
                      .title(R.string.preferencesSecurity)
                      .positiveText(R.string.accept)
                      .negativeText(R.string.cancel)
                      .showListener(
                          new DialogInterface.OnShowListener() {
                            @Override
                            public void onShow(DialogInterface dialog) {
                              tlsCaCrtFullPath = Preferences.getTlsCaCrtPath();
                              tlsClientCrtFullPath = Preferences.getTlsClientCrtPath();

                              MaterialDialog d = MaterialDialog.class.cast(dialog);
                              Switch tls = (Switch) d.findViewById(R.id.tls);

                              final MaterialEditText tlsCaCrt =
                                  (MaterialEditText) d.findViewById(R.id.tlsCaCrt);
                              final MaterialEditText tlsClientCrt =
                                  (MaterialEditText) d.findViewById(R.id.tlsClientCrt);
                              final MaterialEditText tlsClientCrtPassword =
                                  (MaterialEditText) d.findViewById(R.id.tlsClientCrtPassword);

                              setTlsCaCrtPath(tlsCaCrtFullPath, tlsCaCrt);
                              setTlsClientCrtPath(tlsClientCrtFullPath, tlsClientCrt);

                              tls.setChecked(tlsVal);
                              tls.setOnCheckedChangeListener(
                                  new CompoundButton.OnCheckedChangeListener() {
                                    @Override
                                    public void onCheckedChanged(
                                        CompoundButton buttonView, boolean isChecked) {
                                      tlsVal = isChecked;
                                      tlsCaCrt.setVisibility(tlsVal ? View.VISIBLE : View.GONE);
                                      tlsClientCrt.setVisibility(tlsVal ? View.VISIBLE : View.GONE);
                                      tlsClientCrtPassword.setVisibility(
                                          tlsVal && !"".equals(tlsClientCrt.getText().toString())
                                              ? View.VISIBLE
                                              : View.GONE);
                                    }
                                  });

                              tlsCaCrt.setVisibility(tlsVal ? View.VISIBLE : View.GONE);
                              tlsClientCrt.setVisibility(tlsVal ? View.VISIBLE : View.GONE);
                              tlsCaCrt.setOnClickListener(
                                  new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                      tlsCaCrt.setFocusable(true);
                                      tlsCaCrt.setFocusableInTouchMode(true);
                                      tlsCaCrt.requestFocus();
                                      PopupMenu popup =
                                          new PopupMenu(
                                              FragmentPreferences.this.getActivity(), tlsCaCrt);
                                      popup
                                          .getMenuInflater()
                                          .inflate(R.menu.picker, popup.getMenu());
                                      popup.setOnMenuItemClickListener(
                                          new PopupMenu.OnMenuItemClickListener() {
                                            public boolean onMenuItemClick(MenuItem item) {
                                              if (item.getItemId() == R.id.clear) {
                                                setTlsCaCrtPath("", tlsCaCrt);
                                              } else if (item.getItemId() == R.id.select)
                                                showFileChooser(
                                                    getActivity(),
                                                    FILE_SELECT_CODE_TLS_CA_CRT_PATH);
                                              tlsCaCrt.setFocusable(false);
                                              tlsCaCrt.setFocusableInTouchMode(false);

                                              return true;
                                            }
                                          });
                                      popup.setOnDismissListener(
                                          new PopupMenu.OnDismissListener() {
                                            @Override
                                            public void onDismiss(PopupMenu menu) {
                                              tlsCaCrt.setFocusable(false);
                                              tlsCaCrt.setFocusableInTouchMode(false);
                                            }
                                          });

                                      popup.show(); // showing popup menu
                                    }
                                  });

                              tlsClientCrt.setVisibility(tlsVal ? View.VISIBLE : View.GONE);
                              tlsClientCrt.setOnClickListener(
                                  new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                      tlsClientCrt.setFocusable(true);
                                      tlsClientCrt.setFocusableInTouchMode(true);
                                      tlsClientCrt.requestFocus();
                                      PopupMenu popup =
                                          new PopupMenu(
                                              FragmentPreferences.this.getActivity(), tlsClientCrt);
                                      popup
                                          .getMenuInflater()
                                          .inflate(R.menu.picker, popup.getMenu());
                                      popup.setOnMenuItemClickListener(
                                          new PopupMenu.OnMenuItemClickListener() {
                                            public boolean onMenuItemClick(MenuItem item) {
                                              if (item.getItemId() == R.id.clear) {
                                                setTlsClientCrtPath("", tlsClientCrt);
                                              } else if (item.getItemId() == R.id.select)
                                                showFileChooser(
                                                    getActivity(),
                                                    FILE_SELECT_CODE_TLS_CLIENT_CRT_PATH);
                                              tlsClientCrt.setFocusable(false);
                                              tlsClientCrt.setFocusableInTouchMode(false);

                                              return true;
                                            }
                                          });
                                      popup.setOnDismissListener(
                                          new PopupMenu.OnDismissListener() {
                                            @Override
                                            public void onDismiss(PopupMenu menu) {
                                              tlsClientCrt.setFocusable(false);
                                              tlsClientCrt.setFocusableInTouchMode(false);
                                            }
                                          });

                                      popup.show(); // showing popup menu
                                    }
                                  });
                              tlsClientCrt.addTextChangedListener(
                                  new TextWatcher() {
                                    @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) {
                                      tlsClientCrtPassword.setVisibility(
                                          "".equals(s.toString()) ? View.GONE : View.VISIBLE);
                                    }
                                  });

                              tlsClientCrtPassword.setVisibility(
                                  tlsVal && !"".equals(tlsClientCrt.getText().toString())
                                      ? View.VISIBLE
                                      : View.GONE);
                              tlsClientCrtPassword.setText(Preferences.getTlsClientCrtPassword());
                            }
                          })
                      .callback(
                          new MaterialDialog.ButtonCallback() {
                            @Override
                            public void onPositive(MaterialDialog dialog) {
                              MaterialDialog d = MaterialDialog.class.cast(dialog);

                              Preferences.setTls(tlsVal);
                              Preferences.setTlsCaCrtPath(tlsCaCrtFullPath);
                              Preferences.setTlsClientCrtPath(tlsClientCrtFullPath);
                              Preferences.setTlsClientCrtPassword(
                                  ((MaterialEditText) d.findViewById(R.id.tlsClientCrtPassword))
                                      .getText()
                                      .toString());
                              updateConnectButton();
                              securityDialog = null;
                            }
                          })
                      .show();

              return true;
            }
          };
      tlsVal = Preferences.getTls();
      securityPreference = findPreference(getString(R.string.keySecurity));
      securityPreference.setOnPreferenceClickListener(securityListener);
    }
    private void loadSecurityPreferences(final Activity a) {
      Preference.OnPreferenceClickListener securityListener =
          new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
              new MaterialDialog.Builder(a)
                  .customView(R.layout.preferences_security, true)
                  .title(R.string.preferencesSecurity)
                  .positiveText(R.string.accept)
                  .negativeText(R.string.cancel)
                  .showListener(
                      new DialogInterface.OnShowListener() {
                        @Override
                        public void onShow(DialogInterface dialog) {
                          MaterialDialog d = MaterialDialog.class.cast(dialog);
                          Switch tls = (Switch) d.findViewById(R.id.tls);
                          final MaterialEditText tlsCaCrt =
                              (MaterialEditText) d.findViewById(R.id.tlsCaCrt);
                          final MaterialEditText tlsClientCrt =
                              (MaterialEditText) d.findViewById(R.id.tlsClientCrt);

                          tls.setChecked(tlsVal);
                          tlsCaCrt.setVisibility(tlsVal ? View.VISIBLE : View.GONE);
                          tlsCaCrt.setText(Preferences.getTlsCaCrtPath());

                          tlsClientCrt.setVisibility(tlsVal ? View.VISIBLE : View.GONE);
                          tlsClientCrt.setText(Preferences.getTlsClientCrtPath());

                          tls.setOnCheckedChangeListener(
                              new CompoundButton.OnCheckedChangeListener() {
                                @Override
                                public void onCheckedChanged(
                                    CompoundButton buttonView, boolean isChecked) {
                                  tlsVal = isChecked;
                                  tlsCaCrt.setVisibility(tlsVal ? View.VISIBLE : View.GONE);
                                  tlsClientCrt.setVisibility(tlsVal ? View.VISIBLE : View.GONE);
                                }
                              });
                        }
                      })
                  .callback(
                      new MaterialDialog.ButtonCallback() {
                        @Override
                        public void onPositive(MaterialDialog dialog) {
                          MaterialDialog d = MaterialDialog.class.cast(dialog);
                          MaterialEditText tlsCaCrt =
                              (MaterialEditText) d.findViewById(R.id.tlsCaCrt);
                          MaterialEditText tlsClientCrt =
                              (MaterialEditText) d.findViewById(R.id.tlsClientCrt);

                          Preferences.setTls(tlsVal);
                          Preferences.setTlsCaCrtPath(tlsCaCrt.getText().toString());
                          Preferences.setTlsClientCrtPath(tlsClientCrt.getText().toString());

                          updateConnectButton();
                        }
                      })
                  .show();

              return true;
            }
          };
      tlsVal = Preferences.getTls();
      securityPreference = findPreference(getString(R.string.keySecurity));
      securityPreference.setOnPreferenceClickListener(securityListener);
    }