Android Open Source - LheidoSMS Lheido S M S Service






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.service;
/*from   w  w  w.ja v  a 2s  .  c  o m*/
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.IBinder;
import android.telephony.PhoneNumberUtils;
import android.widget.Toast;

import com.lheidosms.app.Global;
import com.lheidosms.utils.LheidoContact;
import com.lheidosms.utils.LheidoUtils;
import com.lheidosms.app.MainLheidoSMS;
import com.lheidosms.receiver.SmsReceiver;

/**
 * Manage conversation list, notifications and sms/mms receiver.
 */
public class LheidoSMSService extends Service {
    private static final String SERVICE_TAG = "LHEIDOSMS SERVICE LOG";
    SmsReceiver smsReceiver;
    protected Context context;
    private BroadcastReceiver mBroadcast;

    @Override
    public void onCreate(){
        super.onCreate();
//        Log.v(SERVICE_TAG, "=====> Service start! <=====");
        context = getApplicationContext();
        // load conversations
        Global.conversationsList.clear();
//        getConversationsList(); // with asyncTask
        getConversationsList2(); // without asyncTask
        // init receiver
        smsReceiver = new SmsReceiver(){
            @Override
            public void customReceivedSMS() {
                Toast.makeText(context, "Sms reu de " + new_name, Toast.LENGTH_LONG).show();
                if(activ_notif){
                    Intent notificationIntent = new Intent(context, MainLheidoSMS.class);
                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                    Intent openAction = new Intent(context, MainLheidoSMS.class);
                    openAction.putExtra("name", new_name);
                    PendingIntent openPending = PendingIntent.getActivity(context, 0, openAction, 0);
                    showNotification(body, new_name, phone, pIntent, openPending);
                }
                playNotificationSound();
                moveConversationOnTop(phone, true);
                LheidoUtils.Send.receiveNewMessage(context);
            }

            @Override
            public void customReceivedMMS() {
                Toast.makeText(context, "Mms reu de " + new_name, Toast.LENGTH_LONG).show();
                if(activ_notif){
                    Intent notificationIntent = new Intent(context, MainLheidoSMS.class);
                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                    Intent openAction = new Intent(context, MainLheidoSMS.class);
                    openAction.putExtra("name", new_name);
                    PendingIntent openPending = PendingIntent.getActivity(context, 0, openAction, 0);
                    showNotification("MMS", new_name, phone, pIntent, openPending);
                }
                playNotificationSound();
                moveConversationOnTop(phone, true);
                LheidoUtils.Send.receiveNewMessage(context);
            }

            @Override
            public void customNewMessageRead(int position, String phone) {
                cancelNotif(phone);
                int i = 0;
                int size = Global.conversationsList.size();
                while(i < size && !PhoneNumberUtils.compare(Global.conversationsList.get(i).getPhone(), phone)) {i++;}
                if(i < size && PhoneNumberUtils.compare(Global.conversationsList.get(i).getPhone(), phone)) {
                    // retrieved position in conversationsList
                    Global.conversationsList.get(i).markNewMessage(false);
                }
                LheidoUtils.Send.notifyDataChanged(context);
            }

            @Override
            public void customDelivered(long id) {
                Toast.makeText(context, "Message remis" , Toast.LENGTH_SHORT).show();
                if(userPref.getBoolean("delivered_vibration", true)){
                    try {
                        long[] pattern = {
                                0, // Start immediately
                                100, 100, 100, 100, 100, 100, 100, 100
                        };
                        v.vibrate(pattern, -1);
                    }catch (Exception e){
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(LheidoUtils.ACTION_RECEIVE_SMS);
        filter.addAction(LheidoUtils.ACTION_RECEIVE_MMS);
        filter.addAction(LheidoUtils.ACTION_SENT_SMS);
        filter.addAction(LheidoUtils.ACTION_DELIVERED_SMS);
        filter.addAction(LheidoUtils.ACTION_NEW_MESSAGE_READ);
        filter.setPriority(2000);
        getApplication().registerReceiver(smsReceiver, filter);
        mBroadcast = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String iAction = intent.getAction();
                if(iAction.equals(LheidoUtils.ACTION_USER_NEW_MESSAGE)){
                    String phone = intent.getStringExtra("phone");
                    moveConversationOnTop(phone, false);
                    LheidoUtils.Send.receiveNewMessage(context);
                }
            }
        };
        IntentFilter filter2 = new IntentFilter();
        filter2.addAction(LheidoUtils.ACTION_USER_NEW_MESSAGE);
        filter2.addAction(LheidoUtils.ACTION_CANCEL_VIBRATOR);
        filter2.setPriority(2000);
        getApplication().registerReceiver(mBroadcast, filter2);
        Toast.makeText(context, "LheidoSMS Service started", Toast.LENGTH_SHORT).show();
    }

    private static void moveConversationOnTop(String phone, boolean mark) {
        // get contact position in conversationList
        int i = 0;
        int size = Global.conversationsList.size();
        while(i < size && !PhoneNumberUtils.compare(Global.conversationsList.get(i).getPhone(), phone)) {i++;}
        if(i < size && PhoneNumberUtils.compare(Global.conversationsList.get(i).getPhone(), phone)) {
            // retrieved position in conversationsList
            LheidoContact c = Global.conversationsList.remove(i);
            c.Nb_sms_Plus();
            if(mark) c.markNewMessage(true);
            Global.conversationsList.add(0, c);
        } else{
            // not in conversationsList
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
//        Log.v(SERVICE_TAG, "=====> onStartCommand <=====");
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
//        Log.v(SERVICE_TAG, "=====> onBind <=====");
        return null;
    }

    @Override
    public void onDestroy(){
//        Log.v(SERVICE_TAG, "=====> Service done! <=====");
        getApplication().unregisterReceiver(smsReceiver);
        getApplication().unregisterReceiver(mBroadcast);
        super.onDestroy();
    }

    public void getConversationsList(){
        ConversationsListTask c = new ConversationsListTask();
        c.execTask();
    }

    public void getConversationsList2(){
        final String[] projection = new String[] {"_id", "date", "message_count", "recipient_ids", "read", "type"};
        Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
        Cursor query = context.getContentResolver().query(uri, projection, null, null, "date DESC");
        if(query != null) {
            if (query.moveToFirst()) {
                do {
                    Global.conversationsList.add(LheidoUtils.getLConversationInfo(context, query));
                    if(Global.conversationsList.size() == 1)
                        LheidoUtils.Send.first(context);
                } while (query.moveToNext());
            }
            query.close();
        }
    }

    private final class ConversationsListTask extends AsyncTask<Void, LheidoContact, Boolean>{

        @Override
        protected Boolean doInBackground(Void... voids) {
            final String[] projection = new String[] {"_id", "date", "message_count", "recipient_ids", "read", "type"};
            Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
            Cursor query = context.getContentResolver().query(uri, projection, null, null, "date DESC");
            if(query != null) {
                if (query.moveToFirst()) {
                    do {
                        publishProgress(LheidoUtils.getLConversationInfo(context, query));
                    } while (query.moveToNext());
                }
                query.close();
            }
            return true;
        }

        @Override
        protected void onProgressUpdate (LheidoContact... prog){
            Global.conversationsList.add(prog[0]);
            if(Global.conversationsList.size() == 1)
                LheidoUtils.Send.first(context);
        }

        public void execTask(){
            if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
                executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            } else {
                execute();
            }
        }
    }
}




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