use of android.telephony.SmsMessage in project android-sms-relay by nyaruka.
the class SMSModem method onReceive.
@Override
public void onReceive(Context c, Intent intent) {
final String action = intent.getAction();
if (action.equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
final Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i < pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
final String address = messages[i].getDisplayOriginatingAddress();
final String message = messages[i].getDisplayMessageBody();
listener.onNewSMS(address, message);
}
}
} else if (action.equalsIgnoreCase(SMS_DELIVER_REPORT_ACTION)) {
final int resultCode = getResultCode();
final String token = intent.getStringExtra(SMS_DELIVER_REPORT_TOKEN_EXTRA);
Log.d(TAG, "Deliver report, result code '" + resultCode + "', token '" + token + "' URI: " + intent.getData());
if (resultCode == Activity.RESULT_OK) {
if (pendingSMS.containsKey(token)) {
pendingSMS.put(token, pendingSMS.get(token).intValue() - 1);
if (pendingSMS.get(token).intValue() == 0) {
pendingSMS.remove(token);
listener.onSMSSent(token);
}
}
} else {
if (pendingSMS.containsKey(token)) {
pendingSMS.remove(token);
listener.onSMSSendError(token, extractError(resultCode, intent));
}
}
}
}
use of android.telephony.SmsMessage in project XobotOS by xamarin.
the class RIL method processUnsolicited.
private void processUnsolicited(Parcel p) {
int response;
Object ret;
response = p.readInt();
try {
switch(response) {
case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED:
ret = responseVoid(p);
break;
case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED:
ret = responseVoid(p);
break;
case RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED:
ret = responseVoid(p);
break;
case RIL_UNSOL_RESPONSE_NEW_SMS:
ret = responseString(p);
break;
case RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT:
ret = responseString(p);
break;
case RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM:
ret = responseInts(p);
break;
case RIL_UNSOL_ON_USSD:
ret = responseStrings(p);
break;
case RIL_UNSOL_NITZ_TIME_RECEIVED:
ret = responseString(p);
break;
case RIL_UNSOL_SIGNAL_STRENGTH:
ret = responseSignalStrength(p);
break;
case RIL_UNSOL_DATA_CALL_LIST_CHANGED:
ret = responseDataCallList(p);
break;
case RIL_UNSOL_SUPP_SVC_NOTIFICATION:
ret = responseSuppServiceNotification(p);
break;
case RIL_UNSOL_STK_SESSION_END:
ret = responseVoid(p);
break;
case RIL_UNSOL_STK_PROACTIVE_COMMAND:
ret = responseString(p);
break;
case RIL_UNSOL_STK_EVENT_NOTIFY:
ret = responseString(p);
break;
case RIL_UNSOL_STK_CALL_SETUP:
ret = responseInts(p);
break;
case RIL_UNSOL_SIM_SMS_STORAGE_FULL:
ret = responseVoid(p);
break;
case RIL_UNSOL_SIM_REFRESH:
ret = responseInts(p);
break;
case RIL_UNSOL_CALL_RING:
ret = responseCallRing(p);
break;
case RIL_UNSOL_RESTRICTED_STATE_CHANGED:
ret = responseInts(p);
break;
case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED:
ret = responseVoid(p);
break;
case RIL_UNSOL_RESPONSE_CDMA_NEW_SMS:
ret = responseCdmaSms(p);
break;
case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS:
ret = responseRaw(p);
break;
case RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL:
ret = responseVoid(p);
break;
case RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE:
ret = responseVoid(p);
break;
case RIL_UNSOL_CDMA_CALL_WAITING:
ret = responseCdmaCallWaiting(p);
break;
case RIL_UNSOL_CDMA_OTA_PROVISION_STATUS:
ret = responseInts(p);
break;
case RIL_UNSOL_CDMA_INFO_REC:
ret = responseCdmaInformationRecord(p);
break;
case RIL_UNSOL_OEM_HOOK_RAW:
ret = responseRaw(p);
break;
case RIL_UNSOL_RINGBACK_TONE:
ret = responseInts(p);
break;
case RIL_UNSOL_RESEND_INCALL_MUTE:
ret = responseVoid(p);
break;
case RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED:
ret = responseInts(p);
break;
case RIL_UNSOl_CDMA_PRL_CHANGED:
ret = responseInts(p);
break;
case RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE:
ret = responseVoid(p);
break;
case RIL_UNSOL_RIL_CONNECTED:
ret = responseInts(p);
break;
default:
throw new RuntimeException("Unrecognized unsol response: " + response);
}
} catch (Throwable tr) {
Log.e(LOG_TAG, "Exception processing unsol response: " + response + "Exception:" + tr.toString());
return;
}
switch(response) {
case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED:
/* has bonus radio state int */
RadioState newState = getRadioStateFromInt(p.readInt());
if (RILJ_LOGD)
unsljLogMore(response, newState.toString());
switchToRadioState(newState);
break;
case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED:
if (RILJ_LOGD)
unsljLog(response);
mCallStateRegistrants.notifyRegistrants(new AsyncResult(null, null, null));
break;
case RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED:
if (RILJ_LOGD)
unsljLog(response);
mVoiceNetworkStateRegistrants.notifyRegistrants(new AsyncResult(null, null, null));
break;
case RIL_UNSOL_RESPONSE_NEW_SMS:
{
if (RILJ_LOGD)
unsljLog(response);
// FIXME this should move up a layer
String[] a = new String[2];
a[1] = (String) ret;
SmsMessage sms;
sms = SmsMessage.newFromCMT(a);
if (mGsmSmsRegistrant != null) {
mGsmSmsRegistrant.notifyRegistrant(new AsyncResult(null, sms, null));
}
break;
}
case RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mSmsStatusRegistrant != null) {
mSmsStatusRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM:
if (RILJ_LOGD)
unsljLogRet(response, ret);
int[] smsIndex = (int[]) ret;
if (smsIndex.length == 1) {
if (mSmsOnSimRegistrant != null) {
mSmsOnSimRegistrant.notifyRegistrant(new AsyncResult(null, smsIndex, null));
}
} else {
if (RILJ_LOGD)
riljLog(" NEW_SMS_ON_SIM ERROR with wrong length " + smsIndex.length);
}
break;
case RIL_UNSOL_ON_USSD:
String[] resp = (String[]) ret;
if (resp.length < 2) {
resp = new String[2];
resp[0] = ((String[]) ret)[0];
resp[1] = null;
}
if (RILJ_LOGD)
unsljLogMore(response, resp[0]);
if (mUSSDRegistrant != null) {
mUSSDRegistrant.notifyRegistrant(new AsyncResult(null, resp, null));
}
break;
case RIL_UNSOL_NITZ_TIME_RECEIVED:
if (RILJ_LOGD)
unsljLogRet(response, ret);
// has bonus long containing milliseconds since boot that the NITZ
// time was received
long nitzReceiveTime = p.readLong();
Object[] result = new Object[2];
result[0] = ret;
result[1] = Long.valueOf(nitzReceiveTime);
if (mNITZTimeRegistrant != null) {
mNITZTimeRegistrant.notifyRegistrant(new AsyncResult(null, result, null));
} else {
// in case NITZ time registrant isnt registered yet
mLastNITZTimeInfo = result;
}
break;
case RIL_UNSOL_SIGNAL_STRENGTH:
// frequently
if (RILJ_LOGV)
unsljLogvRet(response, ret);
if (mSignalStrengthRegistrant != null) {
mSignalStrengthRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_DATA_CALL_LIST_CHANGED:
if (RILJ_LOGD)
unsljLogRet(response, ret);
mDataNetworkStateRegistrants.notifyRegistrants(new AsyncResult(null, ret, null));
break;
case RIL_UNSOL_SUPP_SVC_NOTIFICATION:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mSsnRegistrant != null) {
mSsnRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_STK_SESSION_END:
if (RILJ_LOGD)
unsljLog(response);
if (mCatSessionEndRegistrant != null) {
mCatSessionEndRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_STK_PROACTIVE_COMMAND:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mCatProCmdRegistrant != null) {
mCatProCmdRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_STK_EVENT_NOTIFY:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mCatEventRegistrant != null) {
mCatEventRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_STK_CALL_SETUP:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mCatCallSetUpRegistrant != null) {
mCatCallSetUpRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_SIM_SMS_STORAGE_FULL:
if (RILJ_LOGD)
unsljLog(response);
if (mIccSmsFullRegistrant != null) {
mIccSmsFullRegistrant.notifyRegistrant();
}
break;
case RIL_UNSOL_SIM_REFRESH:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mIccRefreshRegistrants != null) {
mIccRefreshRegistrants.notifyRegistrants(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_CALL_RING:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mRingRegistrant != null) {
mRingRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_RESTRICTED_STATE_CHANGED:
if (RILJ_LOGD)
unsljLogvRet(response, ret);
if (mRestrictedStateRegistrant != null) {
mRestrictedStateRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED:
if (RILJ_LOGD)
unsljLog(response);
if (mIccStatusChangedRegistrants != null) {
mIccStatusChangedRegistrants.notifyRegistrants();
}
break;
case RIL_UNSOL_RESPONSE_CDMA_NEW_SMS:
if (RILJ_LOGD)
unsljLog(response);
SmsMessage sms = (SmsMessage) ret;
if (mCdmaSmsRegistrant != null) {
mCdmaSmsRegistrant.notifyRegistrant(new AsyncResult(null, sms, null));
}
break;
case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS:
if (RILJ_LOGD)
unsljLog(response);
if (mGsmBroadcastSmsRegistrant != null) {
mGsmBroadcastSmsRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL:
if (RILJ_LOGD)
unsljLog(response);
if (mIccSmsFullRegistrant != null) {
mIccSmsFullRegistrant.notifyRegistrant();
}
break;
case RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE:
if (RILJ_LOGD)
unsljLog(response);
if (mEmergencyCallbackModeRegistrant != null) {
mEmergencyCallbackModeRegistrant.notifyRegistrant();
}
break;
case RIL_UNSOL_CDMA_CALL_WAITING:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mCallWaitingInfoRegistrants != null) {
mCallWaitingInfoRegistrants.notifyRegistrants(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_CDMA_OTA_PROVISION_STATUS:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mOtaProvisionRegistrants != null) {
mOtaProvisionRegistrants.notifyRegistrants(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_CDMA_INFO_REC:
ArrayList<CdmaInformationRecords> listInfoRecs;
try {
listInfoRecs = (ArrayList<CdmaInformationRecords>) ret;
} catch (ClassCastException e) {
Log.e(LOG_TAG, "Unexpected exception casting to listInfoRecs", e);
break;
}
for (CdmaInformationRecords rec : listInfoRecs) {
if (RILJ_LOGD)
unsljLogRet(response, rec);
notifyRegistrantsCdmaInfoRec(rec);
}
break;
case RIL_UNSOL_OEM_HOOK_RAW:
if (RILJ_LOGD)
unsljLogvRet(response, IccUtils.bytesToHexString((byte[]) ret));
if (mUnsolOemHookRawRegistrant != null) {
mUnsolOemHookRawRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_RINGBACK_TONE:
if (RILJ_LOGD)
unsljLogvRet(response, ret);
if (mRingbackToneRegistrants != null) {
boolean playtone = (((int[]) ret)[0] == 1);
mRingbackToneRegistrants.notifyRegistrants(new AsyncResult(null, playtone, null));
}
break;
case RIL_UNSOL_RESEND_INCALL_MUTE:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mResendIncallMuteRegistrants != null) {
mResendIncallMuteRegistrants.notifyRegistrants(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mCdmaSubscriptionChangedRegistrants != null) {
mCdmaSubscriptionChangedRegistrants.notifyRegistrants(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOl_CDMA_PRL_CHANGED:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mCdmaPrlChangedRegistrants != null) {
mCdmaPrlChangedRegistrants.notifyRegistrants(new AsyncResult(null, ret, null));
}
break;
case RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE:
if (RILJ_LOGD)
unsljLogRet(response, ret);
if (mExitEmergencyCallbackModeRegistrants != null) {
mExitEmergencyCallbackModeRegistrants.notifyRegistrants(new AsyncResult(null, null, null));
}
break;
case RIL_UNSOL_RIL_CONNECTED:
{
if (RILJ_LOGD)
unsljLogRet(response, ret);
// Initial conditions
setRadioPower(false, null);
setPreferredNetworkType(mPreferredNetworkType, null);
setCdmaSubscriptionSource(mCdmaSubscription, null);
notifyRegistrantsRilConnectionChanged(((int[]) ret)[0]);
break;
}
}
}
use of android.telephony.SmsMessage in project XobotOS by xamarin.
the class SMSDispatcher method processMessagePart.
/**
* If this is the last part send the parts out to the application, otherwise
* the part is stored for later processing. Handles both 3GPP concatenated messages
* as well as 3GPP2 format WAP push messages processed by
* {@link com.android.internal.telephony.cdma.CdmaSMSDispatcher#processCdmaWapPdu}.
*
* @param pdu the message PDU, or the datagram portion of a CDMA WDP datagram segment
* @param address the originating address
* @param referenceNumber distinguishes concatenated messages from the same sender
* @param sequenceNumber the order of this segment in the message
* (starting at 0 for CDMA WDP datagrams and 1 for concatenated messages).
* @param messageCount the number of segments in the message
* @param timestamp the service center timestamp in millis
* @param destPort the destination port for the message, or -1 for no destination port
* @param isCdmaWapPush true if pdu is a CDMA WDP datagram segment and not an SM PDU
*
* @return a result code from {@link Telephony.Sms.Intents}, or
* {@link Activity#RESULT_OK} if the message has been broadcast
* to applications
*/
protected int processMessagePart(byte[] pdu, String address, int referenceNumber, int sequenceNumber, int messageCount, long timestamp, int destPort, boolean isCdmaWapPush) {
byte[][] pdus = null;
Cursor cursor = null;
try {
// used by several query selection arguments
String refNumber = Integer.toString(referenceNumber);
String seqNumber = Integer.toString(sequenceNumber);
// Check for duplicate message segment
cursor = mResolver.query(mRawUri, PDU_PROJECTION, "address=? AND reference_number=? AND sequence=?", new String[] { address, refNumber, seqNumber }, null);
// moveToNext() returns false if no duplicates were found
if (cursor.moveToNext()) {
Log.w(TAG, "Discarding duplicate message segment from address=" + address + " refNumber=" + refNumber + " seqNumber=" + seqNumber);
String oldPduString = cursor.getString(PDU_COLUMN);
byte[] oldPdu = HexDump.hexStringToByteArray(oldPduString);
if (!Arrays.equals(oldPdu, pdu)) {
Log.e(TAG, "Warning: dup message segment PDU of length " + pdu.length + " is different from existing PDU of length " + oldPdu.length);
}
return Intents.RESULT_SMS_HANDLED;
}
cursor.close();
// not a dup, query for all other segments of this concatenated message
String where = "address=? AND reference_number=?";
String[] whereArgs = new String[] { address, refNumber };
cursor = mResolver.query(mRawUri, PDU_SEQUENCE_PORT_PROJECTION, where, whereArgs, null);
int cursorCount = cursor.getCount();
if (cursorCount != messageCount - 1) {
// We don't have all the parts yet, store this one away
ContentValues values = new ContentValues();
values.put("date", timestamp);
values.put("pdu", HexDump.toHexString(pdu));
values.put("address", address);
values.put("reference_number", referenceNumber);
values.put("count", messageCount);
values.put("sequence", sequenceNumber);
if (destPort != -1) {
values.put("destination_port", destPort);
}
mResolver.insert(mRawUri, values);
return Intents.RESULT_SMS_HANDLED;
}
// All the parts are in place, deal with them
pdus = new byte[messageCount][];
for (int i = 0; i < cursorCount; i++) {
cursor.moveToNext();
int cursorSequence = cursor.getInt(SEQUENCE_COLUMN);
// GSM sequence numbers start at 1; CDMA WDP datagram sequence numbers start at 0
if (!isCdmaWapPush) {
cursorSequence--;
}
pdus[cursorSequence] = HexDump.hexStringToByteArray(cursor.getString(PDU_COLUMN));
// It's not a bad idea to prefer the port from the first segment for 3GPP as well.
if (cursorSequence == 0 && !cursor.isNull(DESTINATION_PORT_COLUMN)) {
destPort = cursor.getInt(DESTINATION_PORT_COLUMN);
}
}
// GSM sequence numbers start at 1; CDMA WDP datagram sequence numbers start at 0
if (isCdmaWapPush) {
pdus[sequenceNumber] = pdu;
} else {
pdus[sequenceNumber - 1] = pdu;
}
// Remove the parts from the database
mResolver.delete(mRawUri, where, whereArgs);
} catch (SQLException e) {
Log.e(TAG, "Can't access multipart SMS database", e);
return Intents.RESULT_SMS_GENERIC_ERROR;
} finally {
if (cursor != null)
cursor.close();
}
// Special handling for CDMA WDP datagrams
if (isCdmaWapPush) {
// Build up the data stream
ByteArrayOutputStream output = new ByteArrayOutputStream();
for (int i = 0; i < messageCount; i++) {
// reassemble the (WSP-)pdu
output.write(pdus[i], 0, pdus[i].length);
}
byte[] datagram = output.toByteArray();
// Dispatch the PDU to applications
if (destPort == SmsHeader.PORT_WAP_PUSH) {
// Handle the PUSH
return mWapPush.dispatchWapPdu(datagram);
} else {
pdus = new byte[1][];
pdus[0] = datagram;
// The messages were sent to any other WAP port
dispatchPortAddressedPdus(pdus, destPort);
return Activity.RESULT_OK;
}
}
// Dispatch the PDUs to applications
if (destPort != -1) {
if (destPort == SmsHeader.PORT_WAP_PUSH) {
// Build up the data stream
ByteArrayOutputStream output = new ByteArrayOutputStream();
for (int i = 0; i < messageCount; i++) {
SmsMessage msg = SmsMessage.createFromPdu(pdus[i], getFormat());
byte[] data = msg.getUserData();
output.write(data, 0, data.length);
}
// Handle the PUSH
return mWapPush.dispatchWapPdu(output.toByteArray());
} else {
// The messages were sent to a port, so concoct a URI for it
dispatchPortAddressedPdus(pdus, destPort);
}
} else {
// The messages were not sent to a port
dispatchPdus(pdus);
}
return Activity.RESULT_OK;
}
use of android.telephony.SmsMessage in project KISS by Neamar.
the class IncomingSmsHandler method onReceive.
@Override
public void onReceive(Context context, Intent intent) {
// Only handle SMS received
if (!intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
return;
}
// Stop if contacts are not enabled
DataHandler dataHandler = KissApplication.getDataHandler(context);
ContactsProvider contactsProvider = dataHandler.getContactsProvider();
if (contactsProvider == null) {
// Contacts have been disabled from settings
return;
}
// Get the SMS message passed in, if any
Bundle bundle = intent.getExtras();
if (bundle == null) {
return;
}
// Retrieve the SMS message received.
// Since we're not interested in content, we can safely discard
// all records but the first one
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus == null) {
return;
}
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
// Now, retrieve the contact by its lookup key on our contactsProvider
ContactsPojo contactPojo = contactsProvider.findByPhone(msg.getOriginatingAddress());
if (contactPojo != null) {
// We have a match!
dataHandler.addToHistory(contactPojo.id);
}
}
use of android.telephony.SmsMessage in project Synthese_2BIN by TheYoungSensei.
the class SMSReceiver method onReceive.
@Override
public void onReceive(Context context, Intent intent) {
MyModel mainModel = ((Builder) context.getApplicationContext()).getModel();
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
String message = "";
String tel = "";
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
message += messages[i].getMessageBody();
tel = messages[i].getOriginatingAddress();
}
String[] decoded = message.split(";");
if (decoded[0].equalsIgnoreCase("#laFete")) {
if (decoded.length != 4) {
Toast.makeText(context, "Petit Coquin", Toast.LENGTH_LONG).show();
} else {
mainModel.ajouterUtil(decoded[1], decoded[2], decoded[3], tel);
}
} else {
Toast.makeText(context, "Petit Coquin", Toast.LENGTH_LONG).show();
}
}
Aggregations