Skip to content

Instantly share code, notes, and snippets.

@kshivang
Last active May 11, 2020 19:22
Show Gist options
  • Save kshivang/76a4962020ef910bfec45a6ddccdefc4 to your computer and use it in GitHub Desktop.
Save kshivang/76a4962020ef910bfec45a6ddccdefc4 to your computer and use it in GitHub Desktop.
Magic souse to read sms from specific date..

Must be implemented through services

private long parseAllSmsFromProvider(long lastRead, ShortSms ignoreSms) {
            Uri uri = Uri.parse("content://sms/inbox");
            String[] columns = new String[]{"_id", "body", "address", "date_sent", "date", "case when date_sent IS NOT 0 then date_sent else date END as dateSent"};
            // If last read is 0 then assume to read from 6 months back sms
            if (lastRead == 0) {
                Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(System.currentTimeMillis());
                cal.set(Calendar.DATE, 0);
                cal.add(Calendar.MONTH, -6);
                Log.d(this.TAG, "Fresh read from provider : reading only last 3 months data : " + cal.getTime() + " : " + new Date(System.currentTimeMillis()));
                lastRead = cal.getTimeInMillis();
            }
            String[] selectionArgs = new String[]{String.valueOf(lastRead)};
            Cursor c = null;
            boolean dateSentNotPresent = false;
            try {
                c = getContentResolver().query(uri, columns, "dateSent > " + lastRead, null, "dateSent ASC");
            } catch (SQLiteException e) {
                try {
                    c = getContentResolver().query(uri, new String[]{"_id", "body", "address", "date"}, "date > ? ", selectionArgs, "date ASC");
                    dateSentNotPresent = true;
                } catch (SecurityException e2) {
//                    RiverApp.Companion.broadcastReadSmsPermissionRequest(localBroadcastManager);
                }
            } catch (SecurityException e3) {
//                RiverApp.Companion.broadcastReadSmsPermissionRequest(localBroadcastManager);
            }
            if (c == null) {
                return lastRead;
            }
            int totalCount = c.getCount();
            int count = 0;
 //           if (c.getCount() > 0) {
 //               Log.i(this.TAG, "Parsing " + totalCount + " SMSs");
 //               if (riverApp != null) {
 //                   riverApp.setupRules();
 //               }
            }
            c.moveToFirst();
            long lastReadIgnoreCurrSms = lastRead;
            boolean foundIgnoreSmsStopParsing = false;
            while (!c.isAfterLast() && !RiverService.mParseCancelled && !foundIgnoreSmsStopParsing) {
                String body = c.getString(c.getColumnIndexOrThrow("body"));
                String number = c.getString(c.getColumnIndexOrThrow("address"));
                if (!dateSentNotPresent) {
                    lastRead = c.getLong(c.getColumnIndex("date_sent"));
                    Log.i(this.TAG, "Reading DATE_SENT " + c.getLong(c.getColumnIndex("date_sent")) + " DATE " + c.getLong(c.getColumnIndexOrThrow("date")));
                }
                if (dateSentNotPresent || lastRead <= 0) {
                    Log.i(this.TAG, "Reading DATE Field either DATE_SENT not present or <= 0 lastRead = " + lastRead);
                    lastRead = c.getLong(c.getColumnIndexOrThrow("date"));
                }
                long smsId = (long) c.getInt(c.getColumnIndexOrThrow("_id"));
                Date date = new Date(lastRead);
                int nonPersonalSmsCnt = 0;
                if (number == null) {
                } else if (body != null) {
                    Log.i(this.TAG, "Processing *********** " + number);
                    if (ignoreSms == null || !TextUtils.equals(body.trim(), ignoreSms.getBody().trim())) {
                        lastReadIgnoreCurrSms = lastRead;
                        long delta = 0;
                        if (lastRead > 0) {
                            delta = 86400000;
                        }
                        if (smsTable.isDuplicate(number, body, date, delta)) {
                            Log.i(this.TAG, "Duplicate SMS: " + number + " / " + body + " / " + date);
                        } else {
                            try {
          //                      List<ShortSms> shortSmsList = parseAndStoreToDB(number, body, date, smsId, null, false);
                                }
                            }
                            catch (Throwable e3) {
                                Log.e(this.TAG, "*** Exception while Parsing SMS from provider: " + date + " : " + number + " " + body, e3);
                            }
                        }
                    } else {
                        Log.i(this.TAG, "Ignoring current SMS time from broadcast [" + ignoreSms.getDate().getTime() + "] from db [" + date.getTime() + "]");
                        foundIgnoreSmsStopParsing = true;
                    }
                }
                c.moveToNext();
                count++;
                if (!foundIgnoreSmsStopParsing) {
                    localDatabase.setAnalysing(true);
                    RiverApp.Companion.broadcastProgress(localBroadcastManager, count + "/" + totalCount);
                }
            }
            c.close();
            if (lastRead != 0 || totalCount == 0) {
                return lastReadIgnoreCurrSms;
            }
//            RiverApp.broadcastToast(localBroadcastManager, "SMS Parser read " + totalCount + " records and  found " + count + " transactional SMSs");
            return lastReadIgnoreCurrSms;
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment