Android Open Source - AndrOTP Accounts Data Source






From Project

Back to project page AndrOTP.

License

The source code is released under:

MIT License

If you think the Android project AndrOTP listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package lol.otp;
// w w  w. j av  a2  s  . com
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;

public class AccountsDataSource {
  // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
      MySQLiteHelper.COLUMN_MAIL, MySQLiteHelper.COLUMN_TOKEN };

  public AccountsDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public Account createAccount(String account, String token) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_MAIL, account);
    values.put(MySQLiteHelper.COLUMN_TOKEN, token);
    long insertId = database.insert(MySQLiteHelper.TABLE_ACCOUNTS, null,
        values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_ACCOUNTS,
        allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
        null, null, null);
    cursor.moveToFirst();
    Account newAccount = cursorToAccount(cursor);
    cursor.close();
    return newAccount;
  }

  public void deleteAccount(Account account) {
    long id = account.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_ACCOUNTS, MySQLiteHelper.COLUMN_ID
        + " = " + id, null);
  }

  public List<Account> getAllAccounts() {
    List<Account> comments = new ArrayList<Account>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_ACCOUNTS,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Account comment = cursorToAccount(cursor);
      comments.add(comment);
      cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return comments;
  }
  
  private Account cursorToAccount(Cursor cursor) {
    Account account = new Account();
    account.setId(cursor.getLong(0));
    account.setAccountName(cursor.getString(1));
    account.setOriginalSecret(cursor.getString(2));
    return account;
  }
}




Java Source Code List

lol.otp.AccountListAdapter.java
lol.otp.Account.java
lol.otp.AccountsDataSource.java
lol.otp.MainActivity.java
lol.otp.MySQLiteHelper.java
lol.otp.TOTPUtility.java