Search in sources :

Example 61 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project zxingfragmentlib by mitoyarzun.

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>
 *
 * <ul>
 *  <li>Raw text</li>
 *  <li>Display text</li>
 *  <li>Format (e.g. QR_CODE)</li>
 *  <li>Timestamp</li>
 *  <li>Formatted version of timestamp</li>
 * </ul>
 */
CharSequence buildHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        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(new Date(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;
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DateFormat(java.text.DateFormat) Cursor(android.database.Cursor) Date(java.util.Date)

Example 62 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project zxingfragmentlib by mitoyarzun.

the class HistoryManager method clearHistory.

void clearHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    try {
        db = helper.getWritableDatabase();
        db.delete(DBHelper.TABLE_NAME, null, null);
    } finally {
        close(null, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 63 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project zxingfragmentlib by mitoyarzun.

the class HistoryManager method addHistoryItemDetails.

public void addHistoryItemDetails(String itemID, String itemDetails) {
    // As we're going to do an update only we don't need need to worry
    // about the preferences; if the item wasn't saved it won't be udpated
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, ID_DETAIL_COL_PROJECTION, DBHelper.TEXT_COL + "=?", new String[] { itemID }, null, null, DBHelper.TIMESTAMP_COL + " DESC", "1");
        String oldID = null;
        String oldDetails = null;
        if (cursor.moveToNext()) {
            oldID = cursor.getString(0);
            oldDetails = cursor.getString(1);
        }
        if (oldID != null) {
            String newDetails;
            if (oldDetails == null) {
                newDetails = itemDetails;
            } else if (oldDetails.contains(itemDetails)) {
                newDetails = null;
            } else {
                newDetails = oldDetails + " : " + itemDetails;
            }
            if (newDetails != null) {
                ContentValues values = new ContentValues();
                values.put(DBHelper.DETAILS_COL, newDetails);
                db.update(DBHelper.TABLE_NAME, values, DBHelper.ID_COL + "=?", new String[] { oldID });
            }
        }
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor)

Example 64 with SQLiteOpenHelper

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

the class InterceptorTest method setUp.

@Before
public void setUp() throws Exception {
    final SQLiteOpenHelper sqLiteOpenHelper = new TestSQLiteOpenHelper(RuntimeEnvironment.application);
    callCount = new AtomicInteger(0);
    interceptor1 = createInterceptor();
    interceptor2 = createInterceptor();
    storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).addTypeMapping(Tweet.class, SQLiteTypeMapping.<Tweet>builder().putResolver(TweetTableMeta.PUT_RESOLVER).getResolver(TweetTableMeta.GET_RESOLVER).deleteResolver(TweetTableMeta.DELETE_RESOLVER).build()).addInterceptor(interceptor1).addInterceptor(interceptor2).build();
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Before(org.junit.Before)

Example 65 with SQLiteOpenHelper

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

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);
    try (SQLiteDatabase db = helper.getWritableDatabase()) {
        // Insert the new entry into the DB.
        db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SharedPreferences(android.content.SharedPreferences) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

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