Android Open Source - LheidoSMS Sms Receiver






From Project

Back to project page LheidoSMS.

License

The source code is released under:

GNU General Public License

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

package com.lheidosms.receiver;
//  ww  w  .  j  av a 2  s . c  o  m
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsMessage;
import android.widget.Toast;

import com.lheidosms.utils.LheidoContact;
import com.lheidosms.utils.LheidoUtils;
import com.lheidosms.app.R;

import java.util.HashMap;
import java.util.Map;

public abstract class SmsReceiver extends BroadcastReceiver {
    protected Context context;
    protected boolean activ_notif;
    protected boolean vibrate;
    protected Vibrator v;
    protected String body;
    protected String phone;
    protected String new_name;
    protected long date;
    protected Map<String, Integer> notificationsId = new HashMap<String, Integer>();
    protected SharedPreferences userPref;

    public void showNotification(String body, String name, String phone, PendingIntent pIntent, PendingIntent openConversationIntent){
        // Create Notification using NotificationCompat.Builder
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.lheido_sms_icon)
            .setTicker(body)
            .setContentTitle("" + name)
            .setContentText(body)
            .setPriority(2)
            // Add an Action Button below Notification
            .addAction(R.drawable.send_sms, "Ouvrir", openConversationIntent)
            // Set PendingIntent into Notification
            .setContentIntent(pIntent)
            // Dismiss Notification
            .setAutoCancel(true)
        ;

        // Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // Build Notification with Notification Manager
        int id = 0;
        if(notificationsId.containsKey(phone))
            id = notificationsId.get(phone);
        notificationmanager.notify(id, builder.build());
    }

    public void playNotificationSound(){
        AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        SharedPreferences userPref = PreferenceManager.getDefaultSharedPreferences(context);
        switch (am.getRingerMode()) {
            case AudioManager.RINGER_MODE_SILENT:
                break;
            case AudioManager.RINGER_MODE_VIBRATE:
                try {
                    if (vibrate) v.vibrate(1000);
                }catch (Exception e){
                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                }
                break;
            case AudioManager.RINGER_MODE_NORMAL:

                if(userPref.getBoolean(LheidoUtils.receiver_ringtone_key, true)) {
                    try {
                        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                        Ringtone r = RingtoneManager.getRingtone(context, notification);
                        r.play();
                    } catch (Exception e) {
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
                break;
        }
    }

    public void cancelNotif(String phone){
//        Log.v("LHEIDO SMS LOG", notificationsId.keySet().toString());
        try {
            NotificationManager notificationmanager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            for(String p : notificationsId.keySet()){
                if(PhoneNumberUtils.compare(p, phone))
                    notificationmanager.cancel(notificationsId.get(p));
            }
        }catch (Exception ex){ex.printStackTrace();}
    }

    @Override
    public void onReceive(Context c, Intent intent) {
        context = c;
        userPref = PreferenceManager.getDefaultSharedPreferences(context);
        activ_notif = userPref.getBoolean(LheidoUtils.receiver_notification_key, true);
        vibrate = userPref.getBoolean(LheidoUtils.vibration_key, true);
        v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        String iAction = intent.getAction();
        if(iAction.equals(LheidoUtils.ACTION_CANCEL_VIBRATOR)){
            v.cancel();
        }else if(iAction.equals(LheidoUtils.ACTION_RECEIVE_SMS)){
            Bundle bundle = intent.getExtras();
            if(bundle != null){
                Object[] pdus = (Object[]) bundle.get("pdus");
                final SmsMessage[] messages = new SmsMessage[pdus.length];
                for(int i = 0; i<pdus.length; i++){
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }
                if(messages.length > -1){
                    body = "";
                    for(SmsMessage x : messages){
                        body += x.getMessageBody();
                    }
                    date = messages[0].getTimestampMillis();
                    phone = messages[0].getDisplayOriginatingAddress();
                    new_name = LheidoContact.getContactName(context, phone);
                    if(!notificationsId.containsKey(phone))
                        notificationsId.put(phone, notificationsId.size());
                    customReceivedSMS();
                }
            }
        } else if(iAction.equals(LheidoUtils.ACTION_DELIVERED_SMS)){
            switch(getResultCode()){
                case Activity.RESULT_OK:
                    long _id = intent.getExtras().getLong(LheidoUtils.ARG_SMS_DELIVERED, -1);
                    if(_id != -1){
                        ContentValues values = new ContentValues();
                        values.put("status", 0);
                        try{
                            context.getContentResolver().update(Uri.parse("content://sms/" + _id), values, null, null);
                            customDelivered(_id);
                        }catch (Exception ex){
                            Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
                default:
                    Toast.makeText(context, "Erreur, message non remis", Toast.LENGTH_SHORT).show();
                    try {
                        if (vibrate) v.vibrate(2000);
                    }catch (Exception e){
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                    break;
            }
        } else if(iAction.equals(LheidoUtils.ACTION_SENT_SMS)){
            switch(getResultCode()){
                case Activity.RESULT_OK:
                    //Toast.makeText(context, "Le message a certainement d tre envoy  quelqu'un..." , Toast.LENGTH_SHORT).show();
                    //v.vibrate(1000);
                    break;
                default:
                    Toast.makeText(context, "Erreur, le message n'a pas tait envoy", Toast.LENGTH_SHORT).show();
                    try {
                        if (vibrate) v.vibrate(2000);
                    }catch (Exception e){
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                    break;
            }
        } else if(iAction.equals(LheidoUtils.ACTION_NEW_MESSAGE_READ)){
            int position = intent.getIntExtra("position", 0);
            String phone = intent.getStringExtra("phone");
            customNewMessageRead(position, phone);
        } else if(iAction.equals(LheidoUtils.ACTION_RECEIVE_MMS)){
//            try {
//                Bundle bundle = intent.getExtras();
//                if (bundle != null) {
//                    Object[] pdus = (Object[]) bundle.get("pdus");
//                    final SmsMessage[] messages = new SmsMessage[pdus.length];
//                    for (int i = 0; i < pdus.length; i++) {
//                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
//                    }
//                    if (messages.length > -1) {
//                        body = "";
//                        for (SmsMessage x : messages) {
//                            body += x.getMessageBody();
//                        }
//                        date = messages[0].getTimestampMillis();
//                        phone = messages[0].getDisplayOriginatingAddress();
//                        new_name = LheidoContact.getContactName(context, phone);
//                        if (!notificationsId.containsKey(phone))
//                            notificationsId.put(phone, notificationsId.size());
//                        customReceivedMMS();
//                    }
//                }
//            }catch (Exception e){e.printStackTrace();}
        }
    }

    public abstract void customReceivedSMS();
    public abstract void customReceivedMMS();
    public abstract void customNewMessageRead(int position, String phone);
    public abstract void customDelivered(long id);


}




Java Source Code List

com.lheidosms.adapter.ContactsListAdapter.java
com.lheidosms.adapter.ConversationAdapter.java
com.lheidosms.adapter.ListeConversationsAdapter.java
com.lheidosms.adapter.MMSAdapter.java
com.lheidosms.adapter.SmsAdapter.java
com.lheidosms.adapter.SmsBaseAdapter.java
com.lheidosms.adapter.ViewPagerAdapter.java
com.lheidosms.app.AutoComplete.java
com.lheidosms.app.Global.java
com.lheidosms.app.MainLheidoSMS.java
com.lheidosms.fragment.MMSFragment.java
com.lheidosms.fragment.NavigationDrawerFragment.java
com.lheidosms.fragment.SMSFragment.java
com.lheidosms.fragment.SmsBaseFragment.java
com.lheidosms.preference.LheidoSMSPreferenceOldApi.java
com.lheidosms.preference.LheidoSMSPreference.java
com.lheidosms.preference.PrefConversationFragment.java
com.lheidosms.preference.PrefGeneralFragment.java
com.lheidosms.preference.PrefListConversationsFragment.java
com.lheidosms.preference.PrefReceiveFragment.java
com.lheidosms.receiver.BootReceiver.java
com.lheidosms.receiver.LheidoBaseReceiver.java
com.lheidosms.receiver.MainServiceReceiver.java
com.lheidosms.receiver.MmsFragmentReceiver.java
com.lheidosms.receiver.SmsFragmentReceiver.java
com.lheidosms.receiver.SmsReceiver.java
com.lheidosms.service.DeleteOldSMSService.java
com.lheidosms.service.LheidoSMSService.java
com.lheidosms.service.MainService.java
com.lheidosms.service.RemoveConversationService.java
com.lheidosms.utils.BuildFragment.java
com.lheidosms.utils.LheidoContact.java
com.lheidosms.utils.LheidoUtils.java
com.lheidosms.utils.Message.java