Android Open Source - Android-SMSDetector S M S Receiver






From Project

Back to project page Android-SMSDetector.

License

The source code is released under:

Apache License

If you think the Android project Android-SMSDetector 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

//
//  Licensed to the Apache Software Foundation (ASF) under one
//  or more contributor license agreements.  See the NOTICE file
//  distributed with this work for additional information
//  regarding copyright ownership.  The ASF licenses this file
//  to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
//  with the License.  You may obtain a copy of the License at
////from  w  ww .j a  v  a 2 s  .  c  om
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing,
//  software distributed under the License is distributed on an
//  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//  KIND, either express or implied.  See the License for the
//  specific language governing permissions and limitations
//  under the License.
//

package fiesta.smsreceiver;

import messages.detector.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.telephony.SmsMessage;

/*
 * SMSReceiver
 * Is the BroadcastReceiver 
 */
public class SMSReceiver extends BroadcastReceiver {
  static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION)) {

      Bundle bundle = intent.getExtras();

      if (bundle != null) {
        SharedPreferences settings = context.getSharedPreferences(
            context.getString(R.string.PREFERENCES_FILENAME), 0);

        // check if app is enable
        boolean activeApp = settings.getBoolean(
            context.getString(R.string.PREFERENCES_ENABLE_APP),
            true);

        if (activeApp) {

          // read the configured code to detect and compare with
          // message
          // content
          String configuredCode = settings
              .getString(
                  context.getString(R.string.PREFERENCES_CODE),
                  context.getString(R.string.DEFAULT_PREFERENCE_CODE));

          Object[] pdusObj = (Object[]) bundle.get("pdus");

          if (configuredCode != null) {
            String[] importantWords = configuredCode.split(",");

            // examine every received message
            for (int i = 0; i < pdusObj.length; i++) {
              SmsMessage message = SmsMessage
                  .createFromPdu((byte[]) pdusObj[i]);

              // if received message contains some of the
              // important
              // words just
              // create a notification
              if (this.isAnyImportantWordsIncludedInMessage(
                  message.getMessageBody(), importantWords)) {

                PowerManager pm = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);
                PowerManager.WakeLock wl = pm.newWakeLock(
                    PowerManager.SCREEN_DIM_WAKE_LOCK,
                    "FiestaDetectorWakeLock");

                wl.acquire();
                this.configureNotification(context);

                wl.release();

              }
            }
          }
        }

      }
    }

  }

  private boolean isAnyImportantWordsIncludedInMessage(String message,
      String[] importantWords) {

    String[] messageWords = message.split(" ");

    for (int index = 0; index < importantWords.length; index++) {
      String lowerCaseWord = importantWords[index].toLowerCase().replace(
          " ", "");
      lowerCaseWord = lowerCaseWord.replace("\n", "");

      if (lowerCaseWord.length() != 0) {
        for (int messageIndex = 0; messageIndex < messageWords.length; messageIndex++) {
          if (lowerCaseWord
              .equalsIgnoreCase(messageWords[messageIndex])) {
            return true;
          }
        }
      }
    }
    return false;
  }

  private void enableSoundsAndVibration(Context context) {
    AudioManager audioManager = (AudioManager) context
        .getSystemService(Context.AUDIO_SERVICE);
    audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
        AudioManager.VIBRATE_SETTING_ON);

    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,
        audioManager
            .getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),
        0);

  }

  private void configureNotification(Context context) {
    this.enableSoundsAndVibration(context);

    SharedPreferences settings = context.getSharedPreferences(
        context.getString(R.string.PREFERENCES_FILENAME), 0);

    // read the raw identifier for the selected sound
    int configuredSoundId = settings.getInt(
        context.getString(R.string.PREFERENCES_SOUND_RAW_ID), 0);

    // build the notification
    Notification notification = new Notification();

    int icon = R.drawable.notification_icon;
    notification.icon = icon;

    notification.tickerText = context
        .getString(R.string.TEXT_NOTIFICATION_YOU_HAVE_A_PARTY_TICKLE);

    Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
    notificationIntent.setClassName("com.android.mms",
        "com.android.mms.ui.ConversationList");
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
        notificationIntent, 0);

    notification.setLatestEventInfo(context, context
        .getString(R.string.TEXT_NOTIFICATION_YOU_HAVE_A_PARTY_TITLE),
        context.getString(R.string.TEXT_NOTIFICATION_YOU_HAVE_A_PARTY),
        pendingIntent);

    // add the sound to the notification
    notification.sound = Uri.parse("android.resource://messages.detector/"
        + configuredSoundId);

    // add a vibrate pattern
    long[] vibrate = { 0, 100, 200, 300 };
    notification.vibrate = vibrate;

    // add a custom flashlights set
    notification.ledARGB = 0xff00ff00;
    notification.ledOnMS = 300;
    notification.ledOffMS = 1000;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;

    // set the notification as very insistent
    notification.flags |= Notification.FLAG_INSISTENT;

    // set auto-cancel flag to clear the notification
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1000, notification);

  }
}




Java Source Code List

exceptions.EmptyArgumentException.java
exceptions.InvalidResourceIdException.java
exceptions.PreviouslyInitializedVariableException.java
fiesta.share.ShareActivity.java
fiesta.smsreceiver.SMSReceiver.java
helpers.admob.AdMobHelper.java
helpers.audio.AudioHelper.java
helpers.facebook.BaseDialogListener.java
helpers.facebook.BaseRequestListener.java
helpers.facebook.LoginButton.java
helpers.facebook.SessionEvents.java
helpers.facebook.SessionStore.java
helpers.fonts.FontsHelper.java
messages.detector.ImportantMessagesDetectorActivity.java