Android Open Source - LheidoSMS Main Service 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 a  v a2s . c  om
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.telephony.SmsMessage;
import android.text.format.Time;
import android.util.Log;
import android.widget.Toast;

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

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lheido on 01/11/14.
 */
public abstract class MainServiceReceiver extends LheidoBaseReceiver {
    private boolean activ_notif;
    private boolean vibrate;
    private Vibrator v;

    @Override
    protected ArrayList<String> initActions() {
        ArrayList<String> actions = new ArrayList<String>();
        actions.add(LheidoUtils.ACTION_RECEIVE_SMS);
        actions.add(LheidoUtils.ACTION_RECEIVE_MMS);
        actions.add(LheidoUtils.ACTION_SENT_SMS);
        actions.add(LheidoUtils.ACTION_DELIVERED_SMS);
        actions.add(LheidoUtils.ACTION_NEW_MESSAGE_READ);
        actions.add(LheidoUtils.ACTION_USER_NEW_MESSAGE);
        actions.add(LheidoUtils.ACTION_CANCEL_VIBRATOR);
        return actions;
    }

    @Override
    protected void receive(Intent intent) {
        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]);
                }
                String body;
                if(messages.length > -1){
                    body = "";
                    for(SmsMessage x : messages){
                        body += x.getMessageBody();
                    }
                    long date = messages[0].getTimestampMillis();
                    String phone = messages[0].getDisplayOriginatingAddress();
                    String sender = LheidoContact.getContactName(mContext, phone);
                    customReceiveSms(sender, phone, body, date);
                }
            }
        }else if(iAction.equals(LheidoUtils.ACTION_RECEIVE_MMS)){
            Bundle bundle = intent.getExtras();
            if(bundle != null){
                try {
                    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]);
                    }
                    String body;
                    if (messages.length > -1) {
                        body = "";
                        for (SmsMessage x : messages) {
                            body += x.getMessageBody();
                        }
                        long date = messages[0].getTimestampMillis();
                        String phone = messages[0].getDisplayOriginatingAddress();
                        String sender = LheidoContact.getContactName(mContext, phone);
//                        customReceiveMms(sender, phone, body, date);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    Time t = new Time();
                    t.setToNow();
//                    customReceiveMms("quelqu'un", "1234", "", t.normalize(false));
                }
            }
        }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(mContext, "Erreur, le message n'a pas tait envoy", Toast.LENGTH_SHORT).show();
                    try {
                        if (vibrate) v.vibrate(2000);
                    }catch (Exception e){
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                    break;
            }
            customSentSms();
        }else if(iAction.equals(LheidoUtils.ACTION_DELIVERED_SMS)){
            switch(getResultCode()){
                case Activity.RESULT_OK:
                    Bundle extras = intent.getExtras();
                    long id = extras.getLong(LheidoUtils.ARG_SMS_DELIVERED, -1);
                    if(id != -1){
                        ContentValues values = new ContentValues();
                        values.put("status", 0);
                        try{
                            mContext.getContentResolver().update(Uri.parse("content://sms/" + id), values, null, null);
                            customDeliveredSms(id);
                        }catch (Exception ex){
                            Toast.makeText(mContext, ex.toString(), Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
                default:
                    Toast.makeText(mContext, "Erreur, message non remis", Toast.LENGTH_SHORT).show();
                    try {
                        if (vibrate) v.vibrate(2000);
                    }catch (Exception e){
                        Toast.makeText(mContext, 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_USER_NEW_MESSAGE)){
            String phone = intent.getStringExtra("phone");
            customUserNewMessage(phone);
        }else if(iAction.equals(LheidoUtils.ACTION_CANCEL_VIBRATOR)){
            customCancelVibrator();
        }
    }

    protected abstract void customReceiveSms(String new_name, String phone, String body, long date);

    protected abstract void customDeliveredSms(long id);

    protected abstract void customNewMessageRead(int position, String phone);

    protected abstract void customSentSms();

    protected abstract void customReceiveMms(String new_name, String phone, String body, long date);

    protected abstract void customUserNewMessage(String phone);

    protected abstract void customCancelVibrator();
}




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