Search in sources :

Example 81 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project Shuttle by timusus.

the class InclExclDbOpenHelper method onCreate.

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE_WHITELIST);
    // It's OK to block here, we're already on a background thread. Not sure if this is a given
    // with SQLiteOpenHelper, but certainly since we're using SqlBrite, we're safe. We need to block
    // so we can complete the SQL transaction in the onCreate() block, allowing the framework to commit
    // the transaction (which happens after this method is called).
    // Blacklist
    List<BlacklistedSong> blacklistedSongs = LegacyDatabaseUtils.getBlacklistSongsObservable().blockingGet();
    if (!blacklistedSongs.isEmpty()) {
        List<Song> allSongs = DataManager.getInstance().getAllSongsRelay().first(Collections.emptyList()).blockingGet();
        Stream.of(allSongs).filter(song -> Stream.of(blacklistedSongs).anyMatch(blacklistedSong -> blacklistedSong.songId == song.id)).forEach(song -> {
            ContentValues contentValues = new ContentValues(2);
            contentValues.put(InclExclDbOpenHelper.COLUMN_PATH, song.path);
            contentValues.put(InclExclDbOpenHelper.COLUMN_TYPE, InclExclItem.Type.EXCLUDE);
            database.insert(TABLE_NAME, null, contentValues);
        });
    }
    // Whitelist
    List<WhitelistFolder> whitelistFolders = LegacyDatabaseUtils.getWhitelistFolders().blockingGet();
    Stream.of(whitelistFolders).forEach(whitelistFolder -> {
        ContentValues contentValues = new ContentValues(2);
        contentValues.put(InclExclDbOpenHelper.COLUMN_PATH, whitelistFolder.folder);
        contentValues.put(InclExclDbOpenHelper.COLUMN_TYPE, InclExclItem.Type.INCLUDE);
        database.insert(TABLE_NAME, null, contentValues);
    });
    ShuttleApplication.getInstance().deleteDatabase(BlacklistDbOpenHelper.DATABASE_NAME);
    ShuttleApplication.getInstance().deleteDatabase(WhitelistDbOpenHelper.DATABASE_NAME);
}
Also used : Context(android.content.Context) LegacyDatabaseUtils(com.simplecity.amp_library.sql.legacy.LegacyDatabaseUtils) Stream(com.annimon.stream.Stream) WhitelistFolder(com.simplecity.amp_library.sql.legacy.WhitelistFolder) BlacklistedSong(com.simplecity.amp_library.sql.legacy.BlacklistedSong) WhitelistDbOpenHelper(com.simplecity.amp_library.sql.legacy.WhitelistDbOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Song(com.simplecity.amp_library.model.Song) List(java.util.List) SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) DataManager(com.simplecity.amp_library.utils.DataManager) BlacklistDbOpenHelper(com.simplecity.amp_library.sql.legacy.BlacklistDbOpenHelper) ContentValues(android.content.ContentValues) InclExclItem(com.simplecity.amp_library.model.InclExclItem) Collections(java.util.Collections) ContentValues(android.content.ContentValues) BlacklistedSong(com.simplecity.amp_library.sql.legacy.BlacklistedSong) Song(com.simplecity.amp_library.model.Song) BlacklistedSong(com.simplecity.amp_library.sql.legacy.BlacklistedSong) WhitelistFolder(com.simplecity.amp_library.sql.legacy.WhitelistFolder)

Example 82 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project incubator-weex by apache.

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 83 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project incubator-weex by apache.

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);
    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 84 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project incubator-weex by apache.

the class HistoryManager method buildHistoryItem.

public HistoryItem buildHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    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");
        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);
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) Result(com.google.zxing.Result)

Example 85 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project AppCenter-SDK-Android by Microsoft.

the class DatabaseManagerTest method getDatabaseFailedOnce.

@Test
public void getDatabaseFailedOnce() {
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    SQLiteOpenHelper helperMock = mock(SQLiteOpenHelper.class);
    when(helperMock.getWritableDatabase()).thenThrow(new RuntimeException()).thenReturn(mock(SQLiteDatabase.class));
    /* Instantiate real instance for DatabaseManager. */
    DatabaseManager databaseManager = new DatabaseManager(contextMock, "database", "table", 1, null, null);
    databaseManager.setSQLiteOpenHelper(helperMock);
    /* Get database. */
    SQLiteDatabase database = databaseManager.getDatabase();
    /* Verify. */
    assertNotNull(database);
    verify(contextMock).deleteDatabase("database");
}
Also used : Context(android.content.Context) SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)92 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)76 Cursor (android.database.Cursor)44 Test (org.junit.Test)32 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 DBHelper (dev.sagar.smsblocker.tech.service.helper.DBHelper)3 File (java.io.File)3