Search in sources :

Example 1 with DBHelper

use of dev.sagar.smsblocker.tech.service.helper.DBHelper 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));
                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);
                SMS sms = new SMS();
                sms.setId(id);
                sms.setAddress(from);
                sms.setBody(body);
                sms.setRead(readState);
                sms.setDateTime(time);
                sms.setType(type);
                sms.setSubscription(subscriptionId);
                sms.setReplySupported(replySupported);
                sms.setSaved(true);
                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 2 with DBHelper

use of dev.sagar.smsblocker.tech.service.helper.DBHelper in project SMSBlocker by sagarpawardev.

the class InboxUtil method bgGetAllSMSFromTo.

private List<SMS> bgGetAllSMSFromTo(String contactNo, int sortingOrder) {
    final String methodName = "bgGetAllSMSFromTo(String, int)";
    log.justEntered(methodName);
    // Reading Saved SMSes
    String selection = address + " LIKE ?";
    String[] projection = { starredsms_id };
    String tableName = DBConstants.TABLE_SAVEDSMS;
    String encodedAddress = phoneUtils.encode(contactNo);
    String[] selectionArgs = { encodedAddress };
    DBHelper helper = new DBHelper(context);
    SQLiteDatabase db = helper.getReadableDatabase();
    DBServiceSingleton dbService = DBServiceSingleton.getInstance();
    Cursor cursor = dbService.query(db, tableName, projection, selection, selectionArgs, null);
    log.debug(methodName, "Saved SMS Returned Row Count: " + cursor.getCount() + " Selection: " + selection + " Args: " + selectionArgs[0]);
    HashSet<String> set = new HashSet<>();
    try {
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndexOrThrow(this.starredsms_id));
            set.add(id);
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
        db.close();
        helper.close();
    }
    log.debug(methodName, "Saved SMS Set size: " + set.size());
    // Reading SMSes from database
    Uri uriSmsURI = Telephony.Sms.CONTENT_URI;
    selection = address + " = ?";
    projection = new String[] { "*" };
    selectionArgs = new String[] { contactNo };
    String mSortOrder = null;
    switch(sortingOrder) {
        case SORT_DESC:
            mSortOrder = date + " DESC";
            break;
        case SORT_ASC:
            mSortOrder = date + " ASC";
            break;
        default:
            mSortOrder = "";
    }
    ContentResolver mContentResolver = context.getContentResolver();
    Cursor c = dbService.query(mContentResolver, uriSmsURI, projection, selection, selectionArgs, mSortOrder);
    ArrayList<SMS> smses = new ArrayList<>();
    log.info(methodName, "Reading SMSes... ");
    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));
            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);
            SMS sms = new SMS();
            sms.setId(id);
            sms.setAddress(from);
            sms.setBody(body);
            sms.setRead(readState);
            sms.setDateTime(time);
            sms.setType(type);
            sms.setSubscription(subscriptionId);
            sms.setReplySupported(replySupported);
            if (set.contains(id))
                sms.setSaved(true);
            smses.add(sms);
            log.debug(methodName, "Address: " + from + " ReplySupported: " + c.getString(c.getColumnIndex(this.replySupported)));
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    } finally {
        if (c != null)
            c.close();
    }
    return smses;
}
Also used : DBServiceSingleton(dev.sagar.smsblocker.tech.service.DBServiceSingleton) DBHelper(dev.sagar.smsblocker.tech.service.helper.DBHelper) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SMS(dev.sagar.smsblocker.tech.beans.SMS) HashSet(java.util.HashSet)

Example 3 with DBHelper

use of dev.sagar.smsblocker.tech.service.helper.DBHelper 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 4 with DBHelper

use of dev.sagar.smsblocker.tech.service.helper.DBHelper 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 5 with DBHelper

use of dev.sagar.smsblocker.tech.service.helper.DBHelper in project SMSBlocker by sagarpawardev.

the class LatestMsgHandler method doInBackground.

@Override
protected IndexedHashMap<String, Conversation> doInBackground(Context... contexts) {
    final String methodName = "doInBackground()";
    log.justEntered(methodName);
    Context context = contexts[0];
    IndexedHashMap<String, Conversation> convMap = new IndexedHashMap<>();
    DBHelper dbHelper = new DBHelper(context);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    String[] projection = { "*" };
    String selection = null;
    String[] selectionArgs = null;
    String mSortOrder = date + " DESC";
    Cursor c = db.query(Converesation.TABLE_NAME, projection, selection, selectionArgs, null, null, mSortOrder);
    if (c == null) {
        log.info(methodName, "Cursor for conversation Query Returned null");
        return convMap;
    }
    log.info(methodName, "Latest Messages Cursor returned rows count: " + c.getCount());
    while (c.moveToNext()) {
        String address = c.getString(c.getColumnIndexOrThrow(this.address));
        String id = c.getString(c.getColumnIndexOrThrow(this._id));
        String body = c.getString(c.getColumnIndexOrThrow(this.body));
        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));
        String strPhotoUri = c.getString(c.getColumnIndexOrThrow(this.photo));
        String strPhotoThumbUri = c.getString(c.getColumnIndexOrThrow(this.photothumb));
        String contactName = c.getString(c.getColumnIndexOrThrow(this.contactName));
        int unreadCount = c.getInt(c.getColumnIndexOrThrow(this.unreadCount));
        Uri uriPhoto = null;
        if (strPhotoUri != null) {
            uriPhoto = Uri.parse(strPhotoUri);
        }
        Uri uriPhotoThumb = null;
        if (strPhotoThumbUri != null) {
            uriPhotoThumb = Uri.parse(strPhotoThumbUri);
        }
        Conversation conversation = new Conversation();
        conversation.setAddress(address);
        conversation.setSmsId(id);
        conversation.setBody(body);
        conversation.setSubscriptionId(subscriptionId);
        conversation.setReadState(readState);
        conversation.setDateTime(time);
        conversation.setType(type);
        conversation.setPhotoUri(uriPhoto);
        conversation.setPhotoThumbnailUri(uriPhotoThumb);
        conversation.setContactName(contactName);
        conversation.setUnreadCount(unreadCount);
        convMap.put(address, conversation);
    }
    c.close();
    dbHelper.close();
    log.returning(methodName);
    return convMap;
}
Also used : Context(android.content.Context) IndexedHashMap(dev.sagar.smsblocker.tech.datastructures.IndexedHashMap) DBHelper(dev.sagar.smsblocker.tech.service.helper.DBHelper) Conversation(dev.sagar.smsblocker.tech.beans.Conversation) Cursor(android.database.Cursor) Uri(android.net.Uri) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Aggregations

SQLiteDatabase (android.database.sqlite.SQLiteDatabase)7 DBHelper (dev.sagar.smsblocker.tech.service.helper.DBHelper)7 ContentResolver (android.content.ContentResolver)4 Cursor (android.database.Cursor)4 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)3 Uri (android.net.Uri)3 SMS (dev.sagar.smsblocker.tech.beans.SMS)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 ContentValues (android.content.ContentValues)1 Context (android.content.Context)1 Conversation (dev.sagar.smsblocker.tech.beans.Conversation)1 IndexedHashMap (dev.sagar.smsblocker.tech.datastructures.IndexedHashMap)1 DBServiceSingleton (dev.sagar.smsblocker.tech.service.DBServiceSingleton)1 HashMap (java.util.HashMap)1