Search in sources :

Example 51 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project SMSBlocker by sagarpawardev.

the class InboxUtil method getSavedSMSes.

/**
 * This method return all the Saves SMS in SAVEDSMS database
 * @param sortOrder SORT_ASC or SORT_DEC
 * @return list of starred sms
 */
public List<SMS> getSavedSMSes(int sortOrder) {
    final String methodName = "getSavedSMSes(int)";
    log.justEntered(methodName);
    List<SMS> smses = new ArrayList<>();
    dbService = DBServiceSingleton.getInstance();
    SQLiteOpenHelper dbHelper = new DBHelper(context);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    String[] mProjection = { starredsms_address, starredsms_id };
    String mSelection = "";
    String[] mSelectionArgs = {};
    String mSortOrder = starredsms_dateadded;
    switch(sortOrder) {
        case SORT_ASC:
            mSortOrder = mSortOrder + " ASC";
            break;
        case SORT_DESC:
            mSortOrder = mSortOrder + " DESC";
            break;
        default:
            log.error(methodName, "Unknown Sorting order");
    }
    // Create Query for Saved SMS Database
    Cursor c = dbService.query(db, DBConstants.TABLE_SAVEDSMS, mProjection, mSelection, mSelectionArgs, mSortOrder);
    ArrayList<String> values = new ArrayList<>();
    StringBuilder sbQuery = new StringBuilder();
    log.debug(methodName, "Saved Message Count: " + c.getCount());
    while (c.moveToNext()) {
        String address = c.getString(c.getColumnIndexOrThrow(this.starredsms_address));
        String id = c.getString(c.getColumnIndexOrThrow(this.starredsms_id));
        sbQuery.append("(");
        /*sbQuery.append(this.address+"=?");
            sbQuery.append(" AND ");*/
        sbQuery.append(this._id + "=?");
        sbQuery.append(" ) ");
        if (!c.isLast())
            sbQuery.append(" OR ");
        // values.add(address);
        values.add(id);
    }
    c.close();
    db.close();
    dbHelper.close();
    if (values.size() > 0) {
        // Querying SMS Content Provider
        mProjection = new String[] { "*" };
        mSelection = sbQuery.toString();
        mSelectionArgs = values.toArray(new String[values.size()]);
        mSortOrder = null;
        ContentResolver resolver = context.getContentResolver();
        c = dbService.query(resolver, DBConstants.URI_INBOX, mProjection, mSelection, mSelectionArgs, mSortOrder);
        log.debug(methodName, "Saved Reading SMS Count: " + c.getCount());
        try {
            while (c.moveToNext()) {
                String from = c.getString(c.getColumnIndexOrThrow(this.address));
                String id = c.getString(c.getColumnIndexOrThrow(this._id));
                String body = c.getString(c.getColumnIndexOrThrow(this.body));
                String threadId = c.getString(c.getColumnIndexOrThrow(this.threadId));
                int subscriptionId = c.getInt(c.getColumnIndexOrThrow(this.subscriptionId));
                boolean readState = c.getInt(c.getColumnIndex(this.read)) == 1;
                long time = c.getLong(c.getColumnIndexOrThrow(this.date));
                long type = c.getLong(c.getColumnIndexOrThrow(this.type));
                boolean replySupported = PhoneUtilsSingleton.getInstance().isReplySupported(from);
                boolean isSeen = type == SMS.TYPE_SENT && (c.getLong(c.getColumnIndexOrThrow(this.seen)) == 1);
                SMS sms = new SMS();
                sms.setId(id);
                sms.setAddress(from);
                sms.setBody(body);
                sms.setThreadId(threadId);
                sms.setRead(readState);
                sms.setDateTime(time);
                sms.setType(type);
                sms.setSubscription(subscriptionId);
                sms.setReplySupported(replySupported);
                sms.setSaved(true);
                sms.setSeen(isSeen);
                smses.add(sms);
                log.debug(methodName, "Address: " + from + " ReplySupported: " + sms.isReplySupported());
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        } finally {
            c.close();
        }
    } else {
        log.info(methodName, "There are no Saved SMS in Database :)");
    }
    log.returning(methodName);
    return smses;
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) DBHelper(dev.sagar.smsblocker.tech.service.helper.DBHelper) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SMS(dev.sagar.smsblocker.tech.beans.SMS)

Example 52 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project SMSBlocker by sagarpawardev.

the class DBServiceSingleton method delete.

/**
 * Providers Database service for deleting
 * @param context Context
 * @param tableName name of the table
 * @param whereClause Where Clause parameter
 * @param whereArgs Where Clause Arguments
 * @return Count of deleted rows
 */
public int delete(Context context, String tableName, String whereClause, String[] whereArgs) {
    String methodName = "delete(Context, String, String, String[])";
    log.justEntered(methodName);
    SQLiteOpenHelper dbHelper = new DBHelper(context);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    int count = db.delete(tableName, whereClause, whereArgs);
    db.close();
    dbHelper.close();
    log.returning(methodName);
    return count;
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DBHelper(dev.sagar.smsblocker.tech.service.helper.DBHelper)

Example 53 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project SMSBlocker by sagarpawardev.

the class DBServiceSingleton method insert.

/**
 * Provides Database service related to insert
 * @param context Context
 * @param tableName Table to insert
 * @param mContentValues Query Content Values
 * @return the row ID of the newly inserted row, or -1 if an error occurred
 */
public boolean insert(Context context, String tableName, ContentValues mContentValues) {
    String methodName = "insert()";
    log.justEntered(methodName);
    boolean result = false;
    SQLiteOpenHelper dbHelper = new DBHelper(context);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    long rowId = db.insert(tableName, null, mContentValues);
    db.close();
    dbHelper.close();
    log.debug(methodName, "Got Received row_id: " + rowId);
    if (rowId != -1)
        result = true;
    log.returning(methodName);
    return result;
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DBHelper(dev.sagar.smsblocker.tech.service.helper.DBHelper)

Example 54 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project Neo-Launcher by NeoApplications.

the class DbDowngradeHelperTest method testDowngrade_success_v22.

@Test
public void testDowngrade_success_v22() throws Exception {
    setupTestDb();
    SQLiteOpenHelper helper = new TestOpenHelper(22);
    assertEquals(22, helper.getWritableDatabase().getVersion());
    // Check column does not exist
    try (Cursor c = helper.getWritableDatabase().query(Favorites.TABLE_NAME, null, null, null, null, null, null)) {
        assertEquals(-1, c.getColumnIndex(Favorites.OPTIONS));
        // Check data is present
        assertEquals(10, c.getCount());
    }
    helper.close();
    helper = new DatabaseHelper(mContext, null, DB_FILE) {

        @Override
        public void onOpen(SQLiteDatabase db) {
        }
    };
    assertEquals(LauncherProvider.SCHEMA_VERSION, helper.getWritableDatabase().getVersion());
    try (Cursor c = helper.getWritableDatabase().query(Favorites.TABLE_NAME, null, null, null, null, null, null)) {
        // Check column exists
        assertNotSame(-1, c.getColumnIndex(Favorites.OPTIONS));
        // Check data is present
        assertEquals(10, c.getCount());
    }
    helper.close();
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) DatabaseHelper(com.android.launcher3.LauncherProvider.DatabaseHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 55 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project mobile-center-sdk-android by Microsoft.

the class DatabaseManagerTest method getDatabaseFailedThenCleanupFailedThenRetryFailed.

@Test
public void getDatabaseFailedThenCleanupFailedThenRetryFailed() {
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    SQLiteOpenHelper helperMock = mock(SQLiteOpenHelper.class);
    when(helperMock.getWritableDatabase()).thenThrow(new RuntimeException()).thenReturn(mock(SQLiteDatabase.class));
    when(contextMock.deleteDatabase("database")).thenReturn(true);
    /* Instantiate real instance for DatabaseManager. */
    DatabaseManager databaseManager = new DatabaseManager(contextMock, "database", "table", 1, null, 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)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