com.oodles.smslistener.SmsListenerModule.java Source code

Java tutorial

Introduction

Here is the source code for com.oodles.smslistener.SmsListenerModule.java

Source

/**
 * This file was auto-generated by the Titanium Module SDK helper for Android
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 *
 */
package com.oodles.smslistener;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import org.appcelerator.titanium.*;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.titanium.TiC;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;

import android.os.Bundle;
import android.telephony.SmsMessage;

import java.lang.String;

import android.support.v4.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.Fragment;
import android.os.Build;

@Kroll.module(name = "SmsListener", id = "com.oodles.smslistener")
public class SmsListenerModule extends KrollModule {

    // Standard Debugging variables
    private static final String LCAT = "SmsListenerModule";
    private static final boolean DBG = TiConfig.LOGD;

    private static final String MESSAGE_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    String message, phoneNumber, senderNum;
    String[] permissions = { "android.permission.RECEIVE_SMS" };

    // You can define constants with @Kroll.constant, for example:
    // @Kroll.constant public static final String EXTERNAL_NAME = value;
    @Kroll.constant
    public static final int RECEIVED = 0;
    @Kroll.constant
    public static final int FAILED = -2;

    public SmsListenerModule() {
        super();
        setupIntentReceivers();
    }

    @Kroll.onAppCreate
    public static void onAppCreate(TiApplication app) {
        Log.d(LCAT, "inside onAppCreate");
        // put module init code that needs to run when the application is created
    }

    void setupIntentReceivers() {
        Log.d(LCAT, "inside setupIntentReceivers");
        Activity currentActivity = TiApplication.getInstance().getCurrentActivity();

        //let's register broadcast receivers
        BroadcastReceiver smsReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d(LCAT, "inside onReceive");
                KrollDict event;
                switch (getResultCode()) {
                case Activity.RESULT_OK:

                    final Bundle bundle = intent.getExtras();
                    try {
                        if (bundle != null) {
                            final Object[] pdusObj = (Object[]) bundle.get("pdus");
                            for (int i = 0; i < pdusObj.length; i++) {
                                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                                String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                                senderNum = phoneNumber;
                                message = currentMessage.getDisplayMessageBody();
                            }
                        }
                    } catch (Exception e) {
                        Log.e("SmsReceiver", "Exception smsReceiver" + e);
                    }
                    event = createEventObject(true, RECEIVED, message, senderNum);
                    Log.d(LCAT, "message Received");
                    fireEvent("complete", event);
                    break;
                default:
                    event = createEventObject(false, FAILED, "Message delivery failed", "");
                    Log.d(LCAT, "message receiving failure");
                    fireEvent("complete", event);
                    break;
                }
            }
        };

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.setPriority(1000);
        intentFilter.addAction(MESSAGE_RECEIVED);
        //---when the SMS has been Received---
        currentActivity.registerReceiver(smsReceiver, intentFilter);
    }

    public KrollDict createEventObject(boolean success, int result, String resultMessage, String senderNum) {
        KrollDict event = new KrollDict();
        event.put("success", success);
        event.put("result", result);
        event.put("resultMessage", resultMessage);
        event.put("senderNum", senderNum);

        return event;
    }

    // Methods
    @Kroll.method
    private boolean hasSMSReceivePermission() {
        if (Build.VERSION.SDK_INT < 23) {
            return true;
        }
        Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
        if (currentActivity
                .checkSelfPermission(Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        return false;
    }

    /*@Kroll.method
    public void requestSMSReceivePermissions(@Kroll.argument(optional=true)KrollFunction permissionCallback)
    {
       if (hasSMSReceivePermission()) {
     return;
       }
        
       if (TiBaseActivity.smsCallbackContext == null) {
     TiBaseActivity.smsCallbackContext = getKrollObject();
       }
       TiBaseActivity.smsPermissionCallback = permissionCallback;
       String[] permissions = null;
       permissions = new String[] {Manifest.permission.RECEIVE_SMS};
        
        
       Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
       currentActivity.requestPermissions(permissions, 10);
        
    }*/
}