/**
   * Reloads server data and updates the ListView
   *
   * <p>Reads servers from both available_servers and Preferences.custom_servers. The list will have
   * available_servers first, the custom_servers. Remember that the custom_servers are editable,
   * while the available_servers are not.
   */
  private void refresh() {
    try {
      mServers = new JSONArray(Util.file_get_contents(getActivity(), R.raw.available_servers));
      mNumAvailableServers = mServers.length();

      mCustomServers = Preferences.getCustomServers(getActivity());
      int len = mCustomServers.length();
      for (int i = 0; i < len; i++) {
        mServers.put(mCustomServers.getJSONObject(i));
      }
    } catch (JSONException e) {
      Util.displayCrashDialog(getActivity(), "Could not load endpoints from json");
    }
    mListView.setAdapter(new ServersAdapter(mServers, getActivity()));
  }
  @Override
  public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    switch (item.getItemId()) {
      case R.id.menu_delete:
        int position = info.position - mNumAvailableServers;
        mCustomServers.remove(position);
        Preferences.setCustomServers(mCustomServers, getActivity());
        refresh();
        return true;

      default:
        return super.onContextItemSelected(item);
    }
  }
  /**
   * Sets the current server
   *
   * <p>When the user touches one of the servers, set that one to be the current_server and send
   * them to MainActivity
   */
  @Override
  public void onItemClick(AdapterView<?> l, View v, int position, long id) {
    JSONObject current_server = null;
    try {
      current_server = mServers.getJSONObject(position);
    } catch (JSONException e) {
      // We'll just pass null to Preferences, which will wipe current_server
      // Once they get sent to Home, home will realize there isn't
      // a current_server and send them back here
    }
    Preferences.setCurrentServer(current_server, getActivity());

    Intent i = new Intent(getActivity(), MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
  }
  /**
   * Adds a new server JSONObject to Preferences
   *
   * <p>This only updates the Preferences data. You still need make sure that the screen actually
   * reflects this change.
   *
   * @param dialog
   */
  private void saveNewServer(View dialog) {
    final EditText name = (EditText) dialog.findViewById(R.id.name);
    final EditText url = (EditText) dialog.findViewById(R.id.url);
    final EditText jurisdiction = (EditText) dialog.findViewById(R.id.jurisdiction);
    final EditText api_key = (EditText) dialog.findViewById(R.id.api_key);
    final ToggleButton supports_media = (ToggleButton) dialog.findViewById(R.id.supports_media);
    final Spinner format = (Spinner) dialog.findViewById(R.id.format);

    JSONObject server = new JSONObject();
    try {
      server.put(Open311.NAME, name.getText().toString());
      server.put(Open311.URL, url.getText().toString());
      server.put(Open311.JURISDICTION, jurisdiction.getText().toString());
      server.put(Open311.API_KEY, api_key.getText().toString());
      server.put(Open311.SUPPORTS_MEDIA, supports_media.isChecked());
      server.put(Open311.FORMAT, format.getSelectedItem());

      mCustomServers.put(server);
      Preferences.setCustomServers(mCustomServers, getActivity());
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }