Search in sources :

Example 26 with SMS

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

the class SMSReceivedReceiver method smsReceived.

/**
 * This method is called if event is SMS_RECEIVED
 * @param context
 * @param intent
 */
private void smsReceived(Context context, Intent intent) {
    // Start Log
    final String methodName = "smsReceived()";
    log.justEntered(methodName);
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        Object[] pdus = (Object[]) bundle.get("pdus");
        int slot = bundle.getInt("slot", -1);
        int subscription = bundle.getInt("subscription", -1);
        log.info(methodName, "Slot: " + slot);
        log.info(methodName, "Subscription: " + subscription);
        final SmsMessage[] messages = new SmsMessage[pdus.length];
        log.debug(methodName, String.format("message count = %s", messages.length));
        // Join Multi parts
        StringBuilder sbMessage = new StringBuilder();
        for (int i = 0; i < pdus.length; i++) {
            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], MSG_FORMAT);
            String msg = messages[i].getMessageBody();
            sbMessage.append(msg);
        }
        // Create an SMS body
        String msgBody = sbMessage.toString();
        SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0], MSG_FORMAT);
        String address = smsMessage.getOriginatingAddress();
        boolean isReplySupported = PhoneUtilsSingleton.getInstance().isReplySupported(address);
        SMS sms = new SMS();
        sms.setAddress(address);
        sms.setDateTime(smsMessage.getTimestampMillis());
        sms.setBody(msgBody);
        sms.setRead(false);
        sms.setType(SMS.TYPE_RECEIVED);
        sms.setSubscription(subscription);
        sms.setReplySupported(isReplySupported);
        log.debug(methodName, "Service Center: " + smsMessage.getServiceCenterAddress());
        log.info(methodName, "Received Message From: " + smsMessage.getDisplayOriginatingAddress());
        boolean isAppDefault = PermissionUtilSingleton.getInstance().isAppDefault(context);
        if (!isAppDefault)
            log.error(methodName, "App is not default");
        if (isAppDefault) {
            // Save SMS in DataProvider
            try {
                log.info(methodName, "Saving SMS in DataProvider");
                InboxUtil inboxUtil = new InboxUtil(context);
                String id = inboxUtil.insertSMS(sms);
                log.info(methodName, "Received Created id: " + id);
                sms.setId(id);
            } catch (Exception e) {
                log.info(methodName, "Logging Error..");
                log.error(methodName, e);
                Toast.makeText(context, "Failed in Saving", Toast.LENGTH_SHORT).show();
            }
            try {
                // Broadcast SMS Locally
                log.info(methodName, "Broadcasting SMS");
                broadcastLocalSMS(context, sms);
            } catch (Exception e) {
                log.info(methodName, "Logging Error..");
                log.error(methodName, e);
                Toast.makeText(context, "Failed in Broadcasting", Toast.LENGTH_SHORT).show();
            }
            // Create Notification
            try {
                notifUtil.createSMSNotification(context, sms);
            } catch (Exception e) {
                log.info(methodName, "Logging Error..");
                log.error(methodName, e);
                Toast.makeText(context, "Failed in creating notification", Toast.LENGTH_SHORT).show();
            }
        }
    }
    log.returning(methodName);
}
Also used : SmsMessage(android.telephony.SmsMessage) Bundle(android.os.Bundle) SMS(dev.sagar.smsblocker.tech.beans.SMS) InboxUtil(dev.sagar.smsblocker.tech.utils.InboxUtil)

Example 27 with SMS

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

the class SMSSentReceiver method resultOk.

private void resultOk(Context context, Intent intent) {
    final String methodName = "resultOk(Context, Intent)";
    log.justEntered(methodName);
    try {
        Bundle basket = intent.getExtras();
        log.debug(methodName, "Sgr Receiving key: " + KEY_SMS);
        String strSMS = basket.getString(KEY_SMS);
        SMS sms = gson.fromJson(strSMS, SMS.class);
        String id = sms.getId();
        String selection = _id + " = ?";
        String[] selectionArgs = { id };
        ContentValues values = new ContentValues();
        values.put(type, SMS.TYPE_SENT);
        int updateCount = context.getContentResolver().update(SMS_URI, values, selection, selectionArgs);
        log.info(methodName, "Update Count: " + updateCount);
        log.info(methodName, "Broadcasting Locally");
        broadcastLocalSMS(context, sms, KEY_SMS_SENT);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    log.returning(methodName);
}
Also used : ContentValues(android.content.ContentValues) 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