Search in sources :

Example 1 with IndexedHashMap

use of dev.sagar.smsblocker.tech.datastructures.IndexedHashMap in project SMSBlocker by sagarpawardev.

the class ConversationMapFilter method performFiltering.

@Override
protected FilterResults performFiltering(CharSequence charSequence) {
    String searchStr = charSequence.toString().toLowerCase();
    IndexedHashMap<String, Conversation> filteredConvMap = new IndexedHashMap<>();
    if (searchStr.isEmpty()) {
        filteredConvMap.update(conversationMap);
    } else {
        for (int i = 0; i < conversationMap.size(); i++) {
            Conversation conv = conversationMap.get(i);
            String addr = conv.getAddress().toLowerCase();
            String displayName = conv.getContactName();
            String body = conv.getBody().toLowerCase();
            if (displayName != null)
                displayName = displayName.toLowerCase();
            if (addr.contains(searchStr) || (displayName != null && displayName.contains(searchStr)) || body.contains(searchStr)) {
                filteredConvMap.put(addr, conv);
            }
        }
    }
    FilterResults filterResults = new FilterResults();
    filterResults.values = filteredConvMap;
    return filterResults;
}
Also used : IndexedHashMap(dev.sagar.smsblocker.tech.datastructures.IndexedHashMap) Conversation(dev.sagar.smsblocker.tech.beans.Conversation)

Example 2 with IndexedHashMap

use of dev.sagar.smsblocker.tech.datastructures.IndexedHashMap in project SMSBlocker by sagarpawardev.

the class InboxUtil method getLatestMsgs.

/**
 * Helps in getting most recent SMSes from all contacts
 * @return key-value pair of contact_number and most_recent_message <Contact Number, Most Recent Message>
 */
public IndexedHashMap<String, SMS> getLatestMsgs() {
    final String methodName = "getLatestMsgs()";
    log.justEntered(methodName);
    final String m_threadid = Telephony.Sms.Conversations.THREAD_ID;
    final String mSnippet = Telephony.Sms.Conversations.SNIPPET;
    final String mMessageCount = Telephony.Sms.Conversations.MESSAGE_COUNT;
    final String mDate = Telephony.Sms.Conversations.DATE;
    ContentResolver mContentResolver = context.getContentResolver();
    Uri mConversationUri = Telephony.Sms.Conversations.CONTENT_URI;
    IndexedHashMap<String, SMS> smsMap = new IndexedHashMap<>();
    String[] mProjection = { m_threadid, mSnippet, mMessageCount };
    String mSelection = "";
    String[] mSelectionArgs = {};
    String mSortOrder = mDate + " DESC";
    Cursor mConversation = dbService.query(mContentResolver, mConversationUri, mProjection, mSelection, mSelectionArgs, mSortOrder);
    log.info(methodName, "Reading Messages..");
    if (mConversation == null) {
        log.info(methodName, "Conversation Query returned null cursor");
        return smsMap;
    }
    log.info(methodName, "Conversation Count: " + mConversation.getCount());
    // Collecting ThreadId from Conversation Starts
    String[] conversations = new String[mConversation.getCount()];
    int count = 0;
    while (mConversation.moveToNext()) {
        conversations[count++] = mConversation.getString(mConversation.getColumnIndexOrThrow(m_threadid));
    }
    Uri mLatestSmsUri = Telephony.Sms.CONTENT_URI;
    String[] mLatestSmsProjection = { _id, address, body, read, date, subscriptionId, type };
    StringBuilder sb = new StringBuilder(threadId + " IN (");
    for (int i = 0; i < conversations.length; i++) {
        sb.append("?");
        if (i != conversations.length - 1) {
            // If not last index
            sb.append(",");
        }
    }
    sb.append(")");
    String mLatestSmsSelection = sb.toString();
    String[] mLatestSmsSelectionArgs = conversations;
    String mLatestSmsSortOrder = this.date + " desc";
    // -- Collecting ThreadId from Conversation Ends
    Cursor mLatestSmsCursor = mContentResolver.query(mLatestSmsUri, mLatestSmsProjection, mLatestSmsSelection, mLatestSmsSelectionArgs, mLatestSmsSortOrder);
    if (mLatestSmsCursor == null) {
        log.info(methodName, "Latest SMS Query returned null cursor");
        return smsMap;
    }
    while (mLatestSmsCursor.moveToNext()) {
        String id = mLatestSmsCursor.getString(mLatestSmsCursor.getColumnIndexOrThrow(this._id));
        String from = mLatestSmsCursor.getString(mLatestSmsCursor.getColumnIndexOrThrow(this.address));
        String body = mLatestSmsCursor.getString(mLatestSmsCursor.getColumnIndexOrThrow(this.body));
        int serviceCenter = mLatestSmsCursor.getInt(mLatestSmsCursor.getColumnIndexOrThrow(this.subscriptionId));
        boolean readState = mLatestSmsCursor.getInt(mLatestSmsCursor.getColumnIndex(this.read)) == 1;
        long time = mLatestSmsCursor.getLong(mLatestSmsCursor.getColumnIndexOrThrow(this.date));
        long type = mLatestSmsCursor.getLong(mLatestSmsCursor.getColumnIndexOrThrow(this.type));
        if (smsMap.containsKey(from)) {
            continue;
        }
        SMS sms = new SMS();
        sms.setId(id);
        sms.setAddress(from);
        sms.setBody(body);
        sms.setRead(readState);
        sms.setDateTime(time);
        sms.setType(type);
        sms.setSubscription(serviceCenter);
        log.debug(methodName, from + "\t" + body + "\t" + id);
        smsMap.put(from, sms);
    }
    mLatestSmsCursor.close();
    mConversation.close();
    log.returning(methodName);
    return smsMap;
}
Also used : IndexedHashMap(dev.sagar.smsblocker.tech.datastructures.IndexedHashMap) SMS(dev.sagar.smsblocker.tech.beans.SMS) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Example 3 with IndexedHashMap

use of dev.sagar.smsblocker.tech.datastructures.IndexedHashMap 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)

Example 4 with IndexedHashMap

use of dev.sagar.smsblocker.tech.datastructures.IndexedHashMap in project SMSBlocker by sagarpawardev.

the class ConversationUnreadFilter method performFiltering.

@Override
protected FilterResults performFiltering(CharSequence charSequence) {
    IndexedHashMap<String, Conversation> filteredConvMap = new IndexedHashMap<>();
    if (charSequence == null) {
        filteredConvMap.update(conversationMap);
    } else {
        for (int i = 0; i < conversationMap.size(); i++) {
            Conversation conv = conversationMap.get(i);
            boolean isRead = conv.isRead();
            String addr = conv.getAddress();
            if (!isRead) {
                filteredConvMap.put(addr, conv);
            }
        }
    }
    FilterResults filterResults = new FilterResults();
    filterResults.values = filteredConvMap;
    return filterResults;
}
Also used : IndexedHashMap(dev.sagar.smsblocker.tech.datastructures.IndexedHashMap) Conversation(dev.sagar.smsblocker.tech.beans.Conversation)

Aggregations

IndexedHashMap (dev.sagar.smsblocker.tech.datastructures.IndexedHashMap)4 Conversation (dev.sagar.smsblocker.tech.beans.Conversation)3 Cursor (android.database.Cursor)2 Uri (android.net.Uri)2 ContentResolver (android.content.ContentResolver)1 Context (android.content.Context)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 SMS (dev.sagar.smsblocker.tech.beans.SMS)1 DBHelper (dev.sagar.smsblocker.tech.service.helper.DBHelper)1