Android Open Source - android_sdk Qubecell S M S Manager






From Project

Back to project page android_sdk.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android_sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2013  Spunk Media Pvt Ltd (www.qubecell.com)
 *//*from  ww w . j  a v  a2 s.  c o m*/

package com.qubecell.smsmgr;

import java.util.ArrayList;

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 com.qubecell.elogger.ELogger;

/**
 * The QubecellSMSManager class is used to provide the functionality for sending and receiving messages
 * to and from the android device.
 * @author Eninov
 *
 */
public class QubecellSMSManager {
  
  private ELogger log = null;
  private String logTag = "QubecellSMSManager";
  private Context appContext = null;
  private static QubecellSMSManager smsObj = null;
  private static final int MAX_SMS_MESSAGE_LENGTH = 160;
    private static final int SMS_PORT = 5555;
    private static final String SMS_DELIVERED = "SMS_DELIVERED";
    private static final String SMS_SENT = "SMS_SENT";

  /**
   * This method is used to initialize the QubecellSMSManager class.
   * @param context 
   * @return
   */
  public boolean init(Context context)
  {
    if(context == null)
      return false;
    initLog();
    smsObj = getInstance();
    appContext = context;
    appContext.registerReceiver(sendreceiver, new IntentFilter(SMS_SENT));
    appContext.registerReceiver(deliveredreceiver, new IntentFilter(SMS_DELIVERED));
    return true;
  }

  /**
   * This method is used to unregister the receivers.
   * @param context
   * @return
   */
  public boolean unRegisterReceivers(Context context)
  {
    if(context == null)
      return false;
    context.unregisterReceiver(sendreceiver);
    context.unregisterReceiver(deliveredreceiver);
    return true;
  }
  
  /**
   * This method is used to get the QubecellSMSManager instance.
   * @return
   */
  public static QubecellSMSManager getInstance() //pass
  {
    if(smsObj == null)
    {
      smsObj = new QubecellSMSManager();
    }
    return smsObj;
  }

  /**
   * This method is used to initialize the ELogger object.
   */
  private boolean initLog() 
  {
    if(log == null)
      log = new ELogger();
    log.setTag(logTag);
    return true;
  }

  /**
   * This method is used to send message to the given address.
   * @param appContext
   * @param message
   * @param address
   * @return
   */
  public boolean sendMessage(Context appContext, String message, String address, boolean isBinary)
  {
    SmsManager manager = SmsManager.getDefault();
        PendingIntent piSend = PendingIntent.getBroadcast(appContext, 0, new Intent(SMS_SENT), 0);
        PendingIntent piDelivered = PendingIntent.getBroadcast(appContext, 0, new Intent(SMS_DELIVERED), 0);
        if((message == null) || (address == null))
        {
          return false;
        }
        log.info("detectMsisdnForOperator() : Address : "+address +", Message : "+message);
        if(isBinary)
        {
                byte[] data = new byte[message.length()];
               
                for(int index=0; index<message.length() && index < MAX_SMS_MESSAGE_LENGTH; ++index)
                {
                        data[index] = (byte)message.charAt(index);
                }
                log.info("detectMsisdnForOperator() : Sending binary message"); 
                manager.sendDataMessage(address, null, (short) SMS_PORT, data,piSend, piDelivered);
        }
        else
        {
          int length = message.length();
          try
          {   
            if(length > MAX_SMS_MESSAGE_LENGTH)
            {
              ArrayList<String> messagelist = manager.divideMessage(message);
              log.info("detectMsisdnForOperator() : Sending text message :" + message+"  Address is : "+ address);
              manager.sendMultipartTextMessage(address, null, messagelist, null, null);
            }
            else
            {
              log.info("detectMsisdnForOperator() : Sending text message :" + message+"  Address is : "+ address);
              manager.sendTextMessage(address, null, message, piSend, piDelivered);
            }
          }
          catch(IllegalArgumentException ex)
          {
            log.error("sendMessage() : IllegalArgumentException : "+ ex);
          }
        }

    return true;
  }
  
  /**
   * This receiver is used to handle the send information status of the message.
   */
  private BroadcastReceiver sendreceiver = new BroadcastReceiver()
    {
            @Override
            public void onReceive(Context context, Intent intent)
            {
              log.debug("Message received.");
            }
    };

    /**
   * This receiver is used to handle the delivered information status of the message.
   */
  private BroadcastReceiver deliveredreceiver = new BroadcastReceiver()
    {
            @Override
            public void onReceive(Context context, Intent intent)
            {
              log.debug("Message delivered.");
            }
    };
}




Java Source Code List

com.qubecell.beans.BaseBean.java
com.qubecell.beans.CheckStatusRespBean.java
com.qubecell.beans.EventChargeRespBean.java
com.qubecell.beans.MsisdnRespBean.java
com.qubecell.beans.OperatorDetails.java
com.qubecell.beans.OperatorsRespBean.java
com.qubecell.beans.ResponseBaseBean.java
com.qubecell.beans.SendOTPRespBean.java
com.qubecell.beans.ValidateOTPRespBean.java
com.qubecell.constants.ApplicationActivities.java
com.qubecell.constants.CheckstatusServerRespCode.java
com.qubecell.constants.ConstantStrings.java
com.qubecell.constants.EventChargeServerRespCode.java
com.qubecell.constants.HttpConstant.java
com.qubecell.constants.IntentConstant.java
com.qubecell.constants.MerchantData.java
com.qubecell.constants.MessageResponseCode.java
com.qubecell.constants.MobileOperators.java
com.qubecell.constants.MsisdnServerRespCode.java
com.qubecell.constants.NetworkResponseCode.java
com.qubecell.constants.NetworkResponse.java
com.qubecell.constants.NetworkURL.java
com.qubecell.constants.PaymentResult.java
com.qubecell.constants.ProductIds.java
com.qubecell.constants.QubecellResult.java
com.qubecell.constants.SendOTPServerRespCode.java
com.qubecell.constants.ServerCommand.java
com.qubecell.constants.ThemeConfigurationVariables.java
com.qubecell.constants.ValidateOTPServerRespCode.java
com.qubecell.constants.WidgetsTagName.java
com.qubecell.elogger.ELogger.java
com.qubecell.network.AsyncClient.java
com.qubecell.network.NetworkController.java
com.qubecell.receivers.SMSReceiver.java
com.qubecell.smsmgr.QubecellSMSManager.java
com.qubecell.ui.BaseActivity.java
com.qubecell.ui.QubecellActivity.java
com.qubecell.ui.ResultActivity.java
com.qubecell.ui.SelectOperatorActivity.java
com.qubecell.ui.ValidateOTPActivity.java
com.qubecell.utility.CommonUtility.java
com.qubecell.utility.ImageBase64.java
com.qubecell.xmlparser.XMLParser.java
com.qubecell.xmlparser.XMLfunctions.java
com.qubecelltestapp.ui.MerchantActivity.java