Home | History | Annotate | Download | only in basicsmsreceiver
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.basicsmsreceiver;
     18 
     19 import android.app.Notification;
     20 import android.app.NotificationManager;
     21 import android.app.PendingIntent;
     22 import android.content.BroadcastReceiver;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.os.Bundle;
     26 import android.telephony.SmsMessage;
     27 import android.util.Log;
     28 
     29 public class SmsMessageReceiver extends BroadcastReceiver {
     30     /** Tag string for our debug logs */
     31     private static final String LOG_TAG = "SmsMessageReceiver";
     32 
     33     @Override
     34     public void onReceive(Context context, Intent intent) {
     35         Bundle extras = intent.getExtras();
     36         Log.i(LOG_TAG, "onReceive");
     37         if (extras == null)
     38             return;
     39 
     40         Object[] pdus = (Object[]) extras.get("pdus");
     41 
     42         for (int i = 0; i < pdus.length; i++) {
     43             SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
     44             String fromAddress = message.getOriginatingAddress();
     45             String messageBody = message.getMessageBody().toString();
     46 
     47             Log.i(LOG_TAG, "From: " + fromAddress + " message: " + messageBody);
     48 
     49             addNotification(context, fromAddress, messageBody);
     50         }
     51     }
     52 
     53     private void addNotification(Context context, String fromAddress, String message) {
     54         int notificationId = BasicSmsReceiverApp.getBasicSmsReceiverApp().getNextNotificationId();
     55 
     56         Notification.Builder notification = new Notification.Builder(context)
     57             .setTicker(message)
     58             .setWhen(System.currentTimeMillis())
     59             .setContentTitle(fromAddress)
     60             .setContentText(message)
     61             .setSmallIcon(R.drawable.stat_notify_sms)
     62             .setContentIntent(createDisplayMessageIntent(context, fromAddress, message,
     63                     notificationId));
     64 
     65         Log.i(LOG_TAG, "addNotification notificationId: " + notificationId);
     66 
     67         NotificationManager notificationManager =
     68             (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
     69 
     70         notificationManager.notify(notificationId, notification.getNotification());
     71     }
     72 
     73     private PendingIntent createDisplayMessageIntent(Context context, String fromAddress,
     74             String message, int notificationId) {
     75         // Trigger the main activity to fire up a dialog that shows the received messages
     76         Intent di = new Intent();
     77         di.setClass(context, DialogSmsDisplay.class);
     78         di.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |
     79                 Intent.FLAG_ACTIVITY_CLEAR_TOP);
     80         di.putExtra(DialogSmsDisplay.SMS_FROM_ADDRESS_EXTRA, fromAddress);
     81         di.putExtra(DialogSmsDisplay.SMS_MESSAGE_EXTRA, message);
     82         di.putExtra(DialogSmsDisplay.SMS_NOTIFICATION_ID_EXTRA, notificationId);
     83 
     84         // This line is needed to make this intent compare differently than the other intents
     85         // created here for other messages. Without this line, the PendingIntent always gets the
     86         // intent of a previous message and notification.
     87         di.setType(Integer.toString(notificationId));
     88 
     89         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, di, 0);
     90         return pendingIntent;
     91     }
     92 
     93 }
     94