Search in sources :

Example 1 with SQLiteOpenHelper

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

the class HistoryManager method addHistoryItem.

public void addHistoryItem(Result result, ResultHandler handler) {
    // considered secure.
    if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true) || handler.areContentsSecure() || !enableHistory) {
        return;
    }
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    if (!prefs.getBoolean(PreferencesActivity.KEY_REMEMBER_DUPLICATES, false)) {
        deletePrevious(result.getText());
    }
    ContentValues values = new ContentValues();
    values.put(DBHelper.TEXT_COL, result.getText());
    values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
    values.put(DBHelper.DISPLAY_COL, handler.getDisplayContents().toString());
    values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    try {
        db = helper.getWritableDatabase();
        // Insert the new entry into the DB.
        db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
    } finally {
        close(null, db);
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SharedPreferences(android.content.SharedPreferences) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 2 with SQLiteOpenHelper

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

the class HistoryManager method deletePrevious.

private void deletePrevious(String text) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    try {
        db = helper.getWritableDatabase();
        db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "=?", new String[] { text });
    } catch (SQLException sqle) {
        Log.w(TAG, sqle);
    } finally {
        close(null, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException)

Example 3 with SQLiteOpenHelper

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

the class HistoryManager method hasHistoryItems.

public boolean hasHistoryItems() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COUNT_COLUMN, null, null, null, null, null);
        cursor.moveToFirst();
        return cursor.getInt(0) > 0;
    } catch (SQLException sqle) {
        Log.w(TAG, sqle);
        return false;
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) Cursor(android.database.Cursor)

Example 4 with SQLiteOpenHelper

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

the class HistoryManager method trimHistory.

public void trimHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        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) {
        // We're seeing an error here when called in CaptureActivity.onCreate() in rare cases
        // and don't understand it. First theory is that it's transient so can be safely ignored.
        Log.w(TAG, sqle);
    // continue
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) Cursor(android.database.Cursor)

Example 5 with SQLiteOpenHelper

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

the class HistoryManager method buildHistoryItems.

public List<HistoryItem> buildHistoryItems() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    List<HistoryItem> items = new ArrayList<>();
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
        while (cursor.moveToNext()) {
            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);
            items.add(new HistoryItem(result, display, details));
        }
    } finally {
        close(cursor, db);
    }
    return items;
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Result(com.google.zxing.Result)

Aggregations

SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)85 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)70 Cursor (android.database.Cursor)41 Test (org.junit.Test)27 ContentValues (android.content.ContentValues)17 Context (android.content.Context)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 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 SQLiteException (android.database.sqlite.SQLiteException)3 DBHelper (dev.sagar.smsblocker.tech.service.helper.DBHelper)3 Date (java.util.Date)3 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)2 SQLiteDiskIOException (android.database.sqlite.SQLiteDiskIOException)2 SQLiteFullException (android.database.sqlite.SQLiteFullException)2 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)2