Search in sources :

Example 66 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project android-zxing by PearceXu.

the class HistoryManager method buildHistory.

/**
 * <p>Builds a text representation of the scanning history. Each scan is encoded on one
 * line, terminated by a line break (\r\n). The values in each line are comma-separated,
 * and double-quoted. Double-quotes within values are escaped with a sequence of two
 * double-quotes. The fields output are:</p>
 *
 * <ol>
 *  <li>Raw text</li>
 *  <li>Display text</li>
 *  <li>Format (e.g. QR_CODE)</li>
 *  <li>Unix timestamp (milliseconds since the epoch)</li>
 *  <li>Formatted version of timestamp</li>
 *  <li>Supplemental info (e.g. price info for a product barcode)</li>
 * </ol>
 */
CharSequence buildHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    try (SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC")) {
        DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        StringBuilder historyText = new StringBuilder(1000);
        while (cursor.moveToNext()) {
            historyText.append('"').append(massageHistoryField(cursor.getString(0))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(1))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(2))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(3))).append("\",");
            // Add timestamp again, formatted
            long timestamp = cursor.getLong(3);
            historyText.append('"').append(massageHistoryField(format.format(timestamp))).append("\",");
            // Above we're preserving the old ordering of columns which had formatted data in position 5
            historyText.append('"').append(massageHistoryField(cursor.getString(4))).append("\"\r\n");
        }
        return historyText;
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DateFormat(java.text.DateFormat) Cursor(android.database.Cursor)

Example 67 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project android-zxing by PearceXu.

the class HistoryManager method buildHistoryItem.

public HistoryItem buildHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    try (SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC")) {
        cursor.move(number + 1);
        String text = cursor.getString(0);
        String display = cursor.getString(1);
        String format = cursor.getString(2);
        long timestamp = cursor.getLong(3);
        String details = cursor.getString(4);
        Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
        return new HistoryItem(result, display, details);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) Result(com.google.zxing.Result)

Example 68 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project android-zxing by PearceXu.

the class HistoryManager method trimHistory.

public void trimHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    try (SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_NAME, ID_COL_PROJECTION, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC")) {
        cursor.move(MAX_ITEMS);
        while (cursor.moveToNext()) {
            String id = cursor.getString(0);
            Log.i(TAG, "Deleting scan history ID " + id);
            db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + id, null);
        }
    } catch (SQLException sqle) {
        Log.w(TAG, sqle);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) Cursor(android.database.Cursor)

Example 69 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method shouldPassSQLWithArgsToExecSQL.

@Test
public void shouldPassSQLWithArgsToExecSQL() {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
    when(sqLiteOpenHelper.getWritableDatabase()).thenReturn(sqLiteDatabase);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    RawQuery rawQuery = RawQuery.builder().query("DROP TABLE users").args("arg1", "arg2").build();
    storIOSQLite.lowLevel().executeSQL(rawQuery);
    verify(sqLiteOpenHelper).getWritableDatabase();
    verify(sqLiteDatabase).execSQL(eq(rawQuery.query()), eq(new String[] { "arg1", "arg2" }));
    verifyNoMoreInteractions(sqLiteOpenHelper, sqLiteDatabase);
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) RawQuery(com.pushtorefresh.storio.sqlite.queries.RawQuery) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 70 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method notifyAboutChangesShouldNotAcceptNullAsChanges.

@Test
public void notifyAboutChangesShouldNotAcceptNullAsChanges() {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    assertThat(lowLevel).isNotNull();
    try {
        //noinspection ConstantConditions
        lowLevel.notifyAboutChanges(null);
        failBecauseExceptionWasNotThrown(NullPointerException.class);
    } catch (NullPointerException expected) {
        assertThat(expected).hasMessage("Changes can not be null");
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Aggregations

SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)95 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)79 Cursor (android.database.Cursor)47 Test (org.junit.Test)35 ContentValues (android.content.ContentValues)19 Context (android.content.Context)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)16 Result (com.google.zxing.Result)10 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)9 SQLException (android.database.SQLException)7 ArrayList (java.util.ArrayList)7 SharedPreferences (android.content.SharedPreferences)5 DateFormat (java.text.DateFormat)5 SQLiteDiskIOException (android.database.sqlite.SQLiteDiskIOException)4 SQLiteFullException (android.database.sqlite.SQLiteFullException)4 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)4 Returns (org.mockito.internal.stubbing.answers.Returns)4 SQLiteException (android.database.sqlite.SQLiteException)3 DatabaseHelper (com.android.launcher3.LauncherProvider.DatabaseHelper)3 DBHelper (dev.sagar.smsblocker.tech.service.helper.DBHelper)3