158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor/* 258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * Copyright (C) 2011 The Android Open Source Project 358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * 458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * Licensed under the Apache License, Version 2.0 (the "License"); 558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * you may not use this file except in compliance with the License. 658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * You may obtain a copy of the License at 758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * 858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * http://www.apache.org/licenses/LICENSE-2.0 958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * 1058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * Unless required by applicable law or agreed to in writing, software 1158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * distributed under the License is distributed on an "AS IS" BASIS, 1258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * See the License for the specific language governing permissions and 1458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor * limitations under the License. 1558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor */ 1658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 1758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorpackage com.android.basicsmsreceiver; 1858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 1958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.app.Notification; 2058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.app.NotificationManager; 2158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.app.PendingIntent; 2258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.content.BroadcastReceiver; 2358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.content.Context; 2458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.content.Intent; 2558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.os.Bundle; 2658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.telephony.SmsMessage; 2758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorimport android.util.Log; 2858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 2958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylorpublic class SmsMessageReceiver extends BroadcastReceiver { 3058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor /** Tag string for our debug logs */ 3158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor private static final String LOG_TAG = "SmsMessageReceiver"; 3258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 3358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor @Override 3458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor public void onReceive(Context context, Intent intent) { 3558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor Bundle extras = intent.getExtras(); 3658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor Log.i(LOG_TAG, "onReceive"); 3758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor if (extras == null) 3858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor return; 3958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 4058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor Object[] pdus = (Object[]) extras.get("pdus"); 4158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 4258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor for (int i = 0; i < pdus.length; i++) { 4358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]); 4458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor String fromAddress = message.getOriginatingAddress(); 4558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor String messageBody = message.getMessageBody().toString(); 4658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 4758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor Log.i(LOG_TAG, "From: " + fromAddress + " message: " + messageBody); 4858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 4958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor addNotification(context, fromAddress, messageBody); 5058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor } 5158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor } 5258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 5358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor private void addNotification(Context context, String fromAddress, String message) { 5458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor int notificationId = BasicSmsReceiverApp.getBasicSmsReceiverApp().getNextNotificationId(); 5558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 5658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor Notification.Builder notification = new Notification.Builder(context) 5758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor .setTicker(message) 5858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor .setWhen(System.currentTimeMillis()) 5958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor .setContentTitle(fromAddress) 6058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor .setContentText(message) 6158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor .setSmallIcon(R.drawable.stat_notify_sms) 6258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor .setContentIntent(createDisplayMessageIntent(context, fromAddress, message, 6358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor notificationId)); 6458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 6558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor Log.i(LOG_TAG, "addNotification notificationId: " + notificationId); 6658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 6758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor NotificationManager notificationManager = 6858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 6958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 7058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor notificationManager.notify(notificationId, notification.getNotification()); 7158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor } 7258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 7358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor private PendingIntent createDisplayMessageIntent(Context context, String fromAddress, 7458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor String message, int notificationId) { 7558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor // Trigger the main activity to fire up a dialog that shows the received messages 7658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor Intent di = new Intent(); 7758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor di.setClass(context, DialogSmsDisplay.class); 7858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor di.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | 7958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor Intent.FLAG_ACTIVITY_CLEAR_TOP); 8058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor di.putExtra(DialogSmsDisplay.SMS_FROM_ADDRESS_EXTRA, fromAddress); 8158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor di.putExtra(DialogSmsDisplay.SMS_MESSAGE_EXTRA, message); 8258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor di.putExtra(DialogSmsDisplay.SMS_NOTIFICATION_ID_EXTRA, notificationId); 8358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 8458a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor // This line is needed to make this intent compare differently than the other intents 8558a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor // created here for other messages. Without this line, the PendingIntent always gets the 8658a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor // intent of a previous message and notification. 8758a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor di.setType(Integer.toString(notificationId)); 8858a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 8958a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, di, 0); 9058a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor return pendingIntent; 9158a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor } 9258a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor 9358a104a69871bd7e93afd768ceca5df1eac32ff9Tom Taylor} 94