Android Open Source - Lessaging Sms Task






From Project

Back to project page Lessaging.

License

The source code is released under:

GNU General Public License

If you think the Android project Lessaging 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 fr.lessaging.message;
//from   ww w  . ja  v  a 2 s. c  o m
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.text.format.Time;
import android.util.Log;
import android.widget.Toast;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import fr.lessaging.utils.UserPref;

/**
 * Created by lheido on 07/12/14.
 */
public class SmsTask extends AsyncTask<Void, Message, Boolean> {

    private MessageTaskCallback callback;
    private long last_sms = -1;
    protected WeakReference<FragmentActivity> act = null;
    private Context context = null;
    private int conversationId;
    private static final String sms_uri = "content://sms";
    private static final String[] projection = {"*"};
    private String selection = "thread_id = ?";
    private ArrayList<String> selectionArgs = new ArrayList<String>();


    public SmsTask(FragmentActivity activity, int id, MessageTaskCallback callback){
        link(activity);
        conversationId = id;
        this.callback = callback;
        selectionArgs.add("" + conversationId);
    }

    public SmsTask(FragmentActivity activity, int id, long last_id_sms, MessageTaskCallback callback){
        link(activity);
        conversationId = id;
        this.callback = callback;
        last_sms = last_id_sms;
        selection = "thread_id = ? AND _id < ?";
        selectionArgs.add("" + conversationId);
        selectionArgs.add("" + last_sms);
    }

    @Override
    protected void onPreExecute () {
        if(act.get() != null){
            context = act.get().getApplicationContext();
        }
    }

    @Override
    protected void onPostExecute (Boolean result) {
        if (act.get() != null) {
            if(!result)
                Toast.makeText(context, "Problme gnration conversation", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        if(act.get() != null){
            try{
                Uri uri = Uri.parse(sms_uri);
                Cursor query = context.getContentResolver().query(
                        uri,
                        projection,
                        selection,
                        selectionArgs.toArray(new String[selectionArgs.size()]),
                        "date DESC");
                int count = 0;
                if(query != null){
                    long _id; String body; String type; int status; long date; String sender;
                    while(count < UserPref.getMaxSms(context) && query.moveToNext()){
                        _id = query.getLong(query.getColumnIndexOrThrow("_id"));
                        body = query.getString(query.getColumnIndexOrThrow("body"));
                        type = query.getString(query.getColumnIndexOrThrow("type"));
//                            Log.v("LOG type", "type = "+type+", body = "+body);
                        sender = query.getString(query.getColumnIndexOrThrow("address"));
//                        if(sender == null) sender = MessageManager.getUserPhone(context);
//                        Log.e("address_"+_id, ""+sender);
                        status = query.getInt(query.getColumnIndexOrThrow("status"));
                        date = query.getLong(query.getColumnIndexOrThrow("date"));
                        Time t = new Time();
                        t.set(date);
                        Message sms = new Message(_id, body, sender, status, type, t);
                        publishProgress(sms);
                        count += 1;
                    }
                    query.close();
                    if(count == 0 && last_sms == -1){
                        Time now = new Time();
                        now.setToNow();
                        Message sms = new Message(-1L, "Pas de sms", "1", 0,"2", now);
                        publishProgress(sms);
                    }
                    publishProgress();
                }
            }catch(Exception ex){
                ex.printStackTrace();
            }

            return true;
        }
        return false;
    }

    @Override
    protected void onProgressUpdate (Message... prog){
        if (act.get() != null) {
            if (callback != null) {
                if (prog.length > 0) {
                    callback.onMessageLoaded(prog[0]);
                } else {
                    callback.onLoaded();
                }
            }
        }
    }

    public void link (FragmentActivity pActivity) {
        act = new WeakReference<FragmentActivity>(pActivity);
    }

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




Java Source Code List

fr.lessaging.ApplicationTest.java
fr.lessaging.MainActivity.java
fr.lessaging.adapters.ConversationsListAdapter.java
fr.lessaging.adapters.MmsAdapter.java
fr.lessaging.adapters.SmsAdapter.java
fr.lessaging.adapters.SmsBaseAdapter.java
fr.lessaging.adapters.ViewPagerAdapter.java
fr.lessaging.conversation.Conversation.java
fr.lessaging.conversation.ConversationsList.java
fr.lessaging.fragments.CurrentConversationFragment.java
fr.lessaging.fragments.MmsFragment.java
fr.lessaging.fragments.NavigationDrawerFragment.java
fr.lessaging.fragments.SmsBaseFragment.java
fr.lessaging.fragments.SmsFragment.java
fr.lessaging.message.MessageManager.java
fr.lessaging.message.MessageTaskCallback.java
fr.lessaging.message.Message.java
fr.lessaging.message.MmsTask.java
fr.lessaging.message.SmsTask.java
fr.lessaging.preferences.LessagingPreference.java
fr.lessaging.preferences.PrefConversationFragment.java
fr.lessaging.preferences.PrefGeneralFragment.java
fr.lessaging.preferences.PrefListConversationsFragment.java
fr.lessaging.preferences.PrefReceiveFragment.java
fr.lessaging.receiver.MessageReceiver.java
fr.lessaging.utils.AppConfig.java
fr.lessaging.utils.BuildFragment.java
fr.lessaging.utils.LessagingNotification.java
fr.lessaging.utils.LessagingVibrator.java
fr.lessaging.utils.UserPref.java