Search in sources :

Example 11 with SMS

use of dev.sagar.smsblocker.tech.beans.SMS 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 12 with SMS

use of dev.sagar.smsblocker.tech.beans.SMS in project SMSBlocker by sagarpawardev.

the class InboxUtil method deleteThread.

/**
 * This method will delete all SMS from (or) SMS sent to, phoneNo
 * @param phoneNo Phne Number as Thread Id to deletes
 * @return Number of SMS deleted
 */
public int deleteThread(String phoneNo) {
    final String methodName = "deleteThread()";
    log.justEntered(methodName);
    // TODO: Instead od delete messages one by one delete all message at once. Also provide methods to delete from multipleThreads
    log.error(methodName, "Performance issues Temporarily adding. Need to remove later");
    ContentResolver contentResolver = context.getContentResolver();
    int count = 0;
    List<SMS> smses = bgGetAllSMSFromTo(phoneNo, SORT_DESC);
    for (SMS sms : smses) {
        String id = sms.getId();
        Uri tempUri = Uri.withAppendedPath(SMS_URI, Uri.encode(id));
        int tempCount = contentResolver.delete(tempUri, null, null);
        log.debug(methodName, "Delete Count: " + tempCount);
        count += tempCount;
    }
    /*String selection = address+" = ?";
        String[] selectionArgs = {phoneNo};
        log.debug(methodName, "Deleting message from: "+phoneNo);
        int deleteCount = context.getContentResolver()
                .delete(uriSmsURI, selection, selectionArgs);
        log.debug(methodName, "Delete Count: "+deleteCount);

        try{
            throw new NotImplementedException(this.getClass().getSimpleName(), methodName);
        }
        catch (NotImplementedException e){
            e.printStackTrace();
        }*/
    log.returning(methodName);
    return count;
}
Also used : SMS(dev.sagar.smsblocker.tech.beans.SMS) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Example 13 with SMS

use of dev.sagar.smsblocker.tech.beans.SMS in project SMSBlocker by sagarpawardev.

the class InboxUtil method unstarSMSes.

/**
 * This method will unstar SMSes in DB
 * @param smses List of SMSes to unstar
 * @return number of smses unstared in DB
 */
public int unstarSMSes(List<SMS> smses) {
    final String methodName = "unstarSMSes(List<SMS>)";
    log.justEntered(methodName);
    int count = -1;
    if (smses.size() > 0) {
        String whereClause = "";
        StringBuilder sbWhereClause = new StringBuilder(whereClause);
        // (address, id) pair
        String[] whereArgs = new String[2 * smses.size()];
        /*sbWhereClause.append("(");
            sbWhereClause.append(starredsms_address);
            sbWhereClause.append(",");
            sbWhereClause.append(starredsms_id);
            sbWhereClause.append(")");

            sbWhereClause.append(" IN (");*/
        for (int i = 0; i < smses.size(); i++) {
            SMS sms = smses.get(i);
            sbWhereClause.append("(");
            sbWhereClause.append(starredsms_address + " = ? AND ");
            sbWhereClause.append(starredsms_id + " = ? ");
            sbWhereClause.append(")");
            if (i < smses.size() - 1) {
                // Last ? does not contain ,
                sbWhereClause.append(" OR ");
            }
            whereArgs[2 * i] = sms.getAddress();
            whereArgs[2 * i + 1] = sms.getId();
            log.debug(methodName, "Unstarring SMS address: " + whereArgs[2 * i] + " id: " + whereArgs[2 * i + 1]);
        }
        /*sbWhereClause.append(")");*/
        whereClause = sbWhereClause.toString();
        log.info(methodName, "Query built for deletion: " + whereClause);
        count = dbService.delete(context, DBConstants.TABLE_SAVEDSMS, whereClause, whereArgs);
    } else {
    // Nothing
    }
    log.returning(methodName);
    return count;
}
Also used : SMS(dev.sagar.smsblocker.tech.beans.SMS)

Example 14 with SMS

use of dev.sagar.smsblocker.tech.beans.SMS 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 15 with SMS

use of dev.sagar.smsblocker.tech.beans.SMS in project SMSBlocker by sagarpawardev.

the class ThreadActivity method highlightSMS.

public void highlightSMS() {
    final String methodName = "highlightSMS()";
    log.justEntered(methodName);
    Bundle basket = getIntent().getExtras();
    String id = basket.getString(KEY_SMS_ID);
    if (id != null) {
        // Find Position
        log.info(methodName, "Looking for position for SMS id: " + id);
        for (int i = 0; i < smses.size(); i++) {
            SMS sms = smses.get(i);
            if (sms.getId().equals(id)) {
                // Scroll to position
                log.info(methodName, "Found Position: " + i);
                recyclerView.smoothScrollToPosition(i);
                adapter.highlightItem(i);
                break;
            }
        }
    } else {
        log.info(methodName, "No special SMS to show");
    }
    log.returning(methodName);
}
Also used : Bundle(android.os.Bundle) SMS(dev.sagar.smsblocker.tech.beans.SMS)

Aggregations

SMS (dev.sagar.smsblocker.tech.beans.SMS)27 Bundle (android.os.Bundle)11 ContentResolver (android.content.ContentResolver)5 Uri (android.net.Uri)5 ArrayList (java.util.ArrayList)5 Cursor (android.database.Cursor)4 ContentValues (android.content.ContentValues)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 DBHelper (dev.sagar.smsblocker.tech.service.helper.DBHelper)3 HashSet (java.util.HashSet)2 Animator (android.animation.Animator)1 ArgbEvaluator (android.animation.ArgbEvaluator)1 ValueAnimator (android.animation.ValueAnimator)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)1 Typeface (android.graphics.Typeface)1 Handler (android.os.Handler)1 SmsManager (android.telephony.SmsManager)1 SmsMessage (android.telephony.SmsMessage)1