private void init()

in app/src/main/java/com/google/reviewit/ServerSettingsFragment.java [123:274]


  private void init() {
    final AutoCompleteTextView urlInput =
        (AutoCompleteTextView) v(R.id.urlInput);
    ArrayAdapter<String> adapter =
        new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1,
            getResources().getStringArray(R.array.urls));
    urlInput.setAdapter(adapter);

    v(R.id.pasteCredentialsButton).setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            ClipboardManager clipboard = (ClipboardManager) getActivity()
                .getSystemService(Context.CLIPBOARD_SERVICE);
            if (clipboard.hasPrimaryClip()
                && clipboard.getPrimaryClipDescription().hasMimeType(
                ClipDescription.MIMETYPE_TEXT_PLAIN)) {
              ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
              String pasteData = item.getText().toString();
              if (!pasteData.contains("/.gitcookies")) {
                return;
              }

              pasteData = pasteData.substring(pasteData.indexOf("/.gitcookies"));
              pasteData = pasteData.substring(pasteData.lastIndexOf(",") + 1);
              int pos = pasteData.indexOf("=");
              String user = pasteData.substring(0, pos);
              pasteData = pasteData.substring(pos + 1);
              String password = pasteData.substring(0, pasteData.indexOf("\n"));
              WidgetUtil.setText(v(R.id.userInput), user);
              WidgetUtil.setText(v(R.id.passwordInput), password);

              // hide keyboard if it is open
              View view = getActivity().getCurrentFocus();
              if (view != null) {
                ((InputMethodManager) getActivity().getSystemService(
                    Context.INPUT_METHOD_SERVICE))
                        .hideSoftInputFromWindow(view.getWindowToken(), 0);
              }
            }
          }
        });

    ((EditText) v(R.id.urlInput)).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) {
        if (Strings.isNullOrEmpty(textOf(R.id.nameInput))) {
          try {
            String host = new URL(s.toString()).getHost();
            int pos = host.indexOf(".");
            WidgetUtil.setText(v(R.id.nameInput),
                pos > 0
                    ? host.substring(0, pos)
                    : host);
          } catch (MalformedURLException e) {
            // ignore
          }
        }
        displayCredentialsInfo(s.toString());
      }
    });

    v(R.id.saveServerSettings).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        enabledForm(false);

        if (!isServerInputComplete()) {
          widgetUtil.showError(R.string.incompleteInput);
          enabledForm(true);
          return;
        }

        if (!isUrlValid()) {
          widgetUtil.showError(R.string.invalidUrl);
          enabledForm(true);
          return;
        }

        if (!hasUniqueName()) {
          widgetUtil.showError(
              getString(R.string.duplicate_server_name, textOf(R.id.nameInput)));
          enabledForm(true);
          return;
        }

        setVisible(
            v(R.id.statusTestConnection, R.id.statusTestConnectionProgress));
        WidgetUtil.setText(v(R.id.statusTestConnectionText), null);
        new AsyncTask<Void, Void, String>() {
          private TextView status;
          private View statusTestConnectionProgress;
          private View statusTestConnection;

          @Override
          protected void onPreExecute() {
            super.onPreExecute();
            status = tv(R.id.statusTestConnectionText);
            statusTestConnectionProgress = v(R.id.statusTestConnectionProgress);
            statusTestConnection = v(R.id.statusTestConnection);
          }

          @Override
          protected String doInBackground(Void... v) {
            return testConnection();
          }

          protected void onPostExecute(String errorMsg) {
            if (errorMsg != null) {
              enabledForm(true);
              status.setTextColor(widgetUtil.color(R.color.statusFailed));
              status.setText(getString(R.string.test_server_connection_failed));
              setInvisible(statusTestConnectionProgress);
              new AlertDialog.Builder(getContext())
                  .setTitle(getString(R.string.error_title))
                  .setMessage(getString(R.string.connection_failed, errorMsg))
                  .setPositiveButton(android.R.string.ok,
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                          // do nothing
                        }
                      }).setNegativeButton(getString(R.string.save_anyway),
                  new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                      setGone(statusTestConnection);
                      onServerSave(saveServerSettings());
                    }
                  }).setIcon(android.R.drawable.ic_dialog_alert).show();
            } else {
              status.setTextColor(widgetUtil.color(R.color.statusOk));
              status.setText(getString(R.string.test_server_connection_ok));
              setGone(statusTestConnection);
              onServerSave(saveServerSettings());
            }
          }
        }.execute();
      }
    });

    enabledForm(true);
    setGone(v(R.id.statusTestConnection));
  }