@Override
  public void onDismiss(DialogInterface dialog) {
    android.util.Log.e(TAG, "dismiss: sending back data to Activity");
    // Depending on the value of caPurpose, assign the certs in currentConnection.
    if (caPurpose == ManageCustomCaFragment.TYPE_OVIRT) {
      android.util.Log.e(TAG, "Setting custom oVirt CA");
      currentConnection.setOvirtCaData(caCert.getText().toString());
    }

    dismissalListener.onFragmentDismissed(currentConnection);
  }
  /** Saves the preferences which are selected on-screen by the user into shared preferences. */
  private void saveSelectedPreferences(boolean saveInList) {
    android.util.Log.i(TAG, "Saving current settings to file: " + currentSelectedConnection);

    String u = user.getText().toString();
    String h = hostname.getText().toString();

    // Only if a username and a hostname were entered, save the connection to list of connections.
    if (saveInList && !(u.equals("") || h.equals(""))) {
      saveConnections();
    }

    // Then, save the connection to a separate SharedPreferences file.
    currentConnection.setUser(u);
    currentConnection.setHostname(h);
    currentConnection.setVmname(vmname.getText().toString());
    currentConnection.setPassword(password.getText().toString());
    TextView selection = null;
    if (layoutMapSpinner != null) {
      selection = (TextView) layoutMapSpinner.getSelectedView();
    }
    if (selection != null) {
      currentConnection.setLayoutMap(selection.getText().toString());
    }
    currentConnection.saveToSharedPreferences(appContext);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    appContext = getApplicationContext();
    setContentView(R.layout.connection_setup_activity);

    hostname = (EditText) findViewById(R.id.hostname);
    vmname = (EditText) findViewById(R.id.vmname);
    user = (EditText) findViewById(R.id.user);
    password = (EditText) findViewById(R.id.password);

    // Define what happens when one taps the Advanced Settings button.
    advancedSettingsButton = (Button) findViewById(R.id.advancedSettingsButton);
    advancedSettingsButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            saveSelectedPreferences(false);

            Intent intent =
                new Intent(ConnectionSetupActivity.this, AdvancedSettingsActivity.class);
            intent.putExtra("com.undatech.opaque.ConnectionSettings", currentConnection);
            startActivityForResult(intent, Constants.ADVANCED_SETTINGS);
          }
        });

    // Define what happens when one taps the Connect button.
    saveButton = (Button) findViewById(R.id.saveButton);
    saveButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            String u = user.getText().toString();
            String h = hostname.getText().toString();

            // Only if a username and a hostname were entered, save the connection and try to
            // connect.
            if (!(u.equals("") || h.equals(""))) {
              saveSelectedPreferences(true);
              finish();
              // Intent intent = new Intent(ConnectionSetupActivity.this,
              // RemoteCanvasActivity.class);
              // intent.putExtra("com.undatech.opaque.ConnectionSettings", currentConnection);
              // startActivity(intent);
              // Otherwise, let the user know that at least a user and hostname are required.
            } else {
              Toast toast =
                  Toast.makeText(appContext, R.string.error_no_user_hostname, Toast.LENGTH_LONG);
              toast.show();
            }
          }
        });

    // Load any existing list of connection preferences.
    loadConnections();

    Intent i = getIntent();
    currentSelectedConnection = (String) i.getStringExtra("com.undatech.opaque.connectionToEdit");
    android.util.Log.e(TAG, "currentSelectedConnection SET TO: " + currentSelectedConnection);

    // If no currentSelectedConnection was passed in, then generate one.
    if (currentSelectedConnection == null) {
      currentSelectedConnection = nextLargestNumber(connectionsArray);
      newConnection = true;
    }

    currentConnection = new ConnectionSettings(currentSelectedConnection);
    if (newConnection) {
      // Save the empty connection preferences to override any values of a previously
      // deleted connection.
      saveSelectedPreferences(false);
    }

    // Finally, load the preferences for the currentSelectedConnection.
    loadSelectedPreferences();

    // Load list of items from asset folder and populate this:
    List<String> spinnerArray = null;
    try {
      spinnerArray = listFiles("layouts");
    } catch (IOException e) {
      e.printStackTrace();
    }
    layoutMapSpinner = (Spinner) findViewById(R.id.layoutMaps);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<String> adapter =
        new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    layoutMapSpinner.setAdapter(adapter);
    int selection = spinnerArray.indexOf(currentConnection.getLayoutMap());
    if (selection < 0) {
      selection = spinnerArray.indexOf(Constants.DEFAULT_LAYOUT_MAP);
    }
    layoutMapSpinner.setSelection(selection);
  }
 private void updateViewsFromPreferences() {
   hostname.setText(currentConnection.getHostname());
   vmname.setText(currentConnection.getVmname());
   user.setText(currentConnection.getUser());
   password.setText(currentConnection.getPassword());
 }
 /** Loads the preferences from shared preferences and populates the on-screen Views. */
 private void loadSelectedPreferences() {
   // We use the index as the file name to which to save the connection.
   android.util.Log.i(TAG, "Loading current settings from file: " + currentSelectedConnection);
   currentConnection.loadFromSharedPreferences(appContext);
 }
 private void setWidgetStateAppropriately() {
   if (caPurpose == ManageCustomCaFragment.TYPE_OVIRT) {
     caCert.setText(currentConnection.getOvirtCaData());
     caCertPath.setText(getExternalSDCardDirectory());
   }
 }