public void saveAndWriteRecent(boolean saveEmpty) {
    Database database = new Database(c);

    // We need server address or SSH server to be filled out to save. Otherwise,
    // we keep adding empty connections.
    // However, if there is partial data from a URI, we can present the edit screen.
    // Alternately, perhaps we could process some extra data
    if ((getConnectionType() == Constants.CONN_TYPE_SSH && getSshServer().equals("")
            || getAddress().equals(""))
        && !saveEmpty) {
      return;
    }

    SQLiteDatabase db = database.getWritableDatabase();
    db.beginTransaction();
    try {
      save(db);
      MostRecentBean mostRecent = getMostRecent(db);
      if (mostRecent == null) {
        mostRecent = new MostRecentBean();
        mostRecent.setConnectionId(get_Id());
        mostRecent.Gen_insert(db);
      } else {
        mostRecent.setConnectionId(get_Id());
        mostRecent.Gen_update(db);
      }
      db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
      db.close();
    }
    if (db.isOpen()) {
      db.close();
    }
  }
Example #2
0
  protected void saveAndWriteRecent() {
    // We need server address or SSH server to be filled out to save. Otherwise,
    // we keep adding empty connections.
    if (selected.getConnectionType() == Constants.CONN_TYPE_SSH
            && selected.getSshServer().equals("")
        || selected.getAddress().equals("")) return;

    SQLiteDatabase db = database.getWritableDatabase();
    db.beginTransaction();
    try {
      selected.save(db);
      MostRecentBean mostRecent = getMostRecent(db);
      if (mostRecent == null) {
        mostRecent = new MostRecentBean();
        mostRecent.setConnectionId(selected.get_Id());
        mostRecent.Gen_insert(db);
      } else {
        mostRecent.setConnectionId(selected.get_Id());
        mostRecent.Gen_update(db);
      }
      db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
      db.close();
    }
  }
  static ConnectionBean createLoadFromUri(Uri dataUri, Context ctx) {
    ConnectionBean connection = new ConnectionBean(ctx);
    if (dataUri == null) return connection;
    Database database = new Database(ctx);
    String host = dataUri.getHost();

    // Intent generated by connection shortcut widget
    if (host != null && host.startsWith(Constants.CONNECTION)) {
      int port = 0;
      int idx = host.indexOf(':');

      if (idx != -1) {
        try {
          port = Integer.parseInt(host.substring(idx + 1));
        } catch (NumberFormatException nfe) {
        }
        host = host.substring(0, idx);
      }

      if (connection.Gen_read(database.getReadableDatabase(), port)) {
        MostRecentBean bean = getMostRecent(database.getReadableDatabase());
        if (bean != null) {
          bean.setConnectionId(connection.get_Id());
          bean.Gen_update(database.getWritableDatabase());
          database.close();
        }
      }
      return connection;
    }

    // search based on nickname
    SQLiteDatabase queryDb = database.getReadableDatabase();
    String connectionName = dataUri.getQueryParameter(Constants.PARAM_CONN_NAME);
    Cursor nickCursor = null;
    if (connectionName != null)
      nickCursor =
          queryDb.query(
              GEN_TABLE_NAME,
              new String[] {GEN_FIELD__ID},
              GEN_FIELD_NICKNAME + " = ?",
              new String[] {connectionName},
              null,
              null,
              null);
    if (nickCursor != null && nickCursor.moveToFirst()) {
      // there could be many values, so we will just pick one
      Log.i(
          TAG,
          String.format(Locale.US, "Loding connection info from nickname: %s", connectionName));
      connection.Gen_populate(nickCursor, connection.Gen_columnIndices(nickCursor));
      nickCursor.close();
      database.close();
      return connection;
    }
    if (nickCursor != null) nickCursor.close();

    // search based on hostname
    Cursor hostCursor = null;
    if (host != null)
      hostCursor =
          queryDb.query(
              GEN_TABLE_NAME,
              new String[] {GEN_FIELD__ID},
              GEN_FIELD_ADDRESS + " = ?",
              new String[] {host},
              null,
              null,
              null);
    if (hostCursor != null && hostCursor.moveToFirst()) {
      Log.i(TAG, String.format(Locale.US, "Loding connection info from hostname: %s", host));
      connection.Gen_populate(hostCursor, connection.Gen_columnIndices(hostCursor));
      hostCursor.close();
      database.close();
      return connection;
    }
    if (hostCursor != null) hostCursor.close();
    database.close();
    return connection;
  }