Android Open Source - texting Text Message Adaptor






From Project

Back to project page texting.

License

The source code is released under:

Free to use, distribute, do anything.

If you think the Android project texting 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.skk.texting.adaptor;
//w w w. jav a2  s. c  om
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
import com.skk.texting.async.AsyncCursorUpdate;
import com.skk.texting.async.BackgroundTask;
import com.skk.texting.R;
import com.skk.texting.constants.ApplicationConstants;
import com.skk.texting.constants.TextMessageConstants;
import com.skk.texting.domain.TextMessage;
import com.skk.texting.eventdata.EmptyEventData;
import com.skk.texting.evented.Event;
import com.skk.texting.evented.EventHandler;
import com.skk.texting.evented.EventRepository;
import com.skk.texting.evented.HandleEvent;
import com.skk.texting.factory.PersonFactory;
import com.skk.texting.eventdata.IncomingSmsData;

public class TextMessageAdaptor extends CursorAdapter implements EventHandler, BackgroundTask {

    private PersonFactory personFactory;
    private ContentResolver contentResolver;

    public TextMessageAdaptor(Context context, Cursor c, PersonFactory personFactory, EventRepository eventRepository, ContentResolver contentResolver) {
        super(context, c, false);
        this.personFactory = personFactory;
        this.contentResolver = contentResolver;
        eventRepository.register(this, Event.SMSReceived);
        eventRepository.register(this, Event.ConversationUpdated);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        return layoutInflater.inflate(R.layout.msg_row, null);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        if(view == null) return;
        TextView contactName = (TextView)view.findViewById(R.id.contact_name);

        TextMessage textMessage = TextMessage.fromCursor(cursor, personFactory);

        contactName.setText(textMessage.getDisplayName());

        Drawable drawable = view.getResources().getDrawable(R.drawable.touch_text_view);
        if(textMessage.isUnread())
            drawable = view.getResources().getDrawable(R.drawable.unread_text_view);

        view.setBackground(drawable);

    }

    @HandleEvent(eventType = Event.SMSReceived)
    public boolean handleSmsReceived(IncomingSmsData eventData) {
        String originatingAddress = eventData.getSmsMessage().getOriginatingAddress();

        if(!conversationExists(originatingAddress)) {
            new AsyncCursorUpdate(this).execute();
            return true;
        }
        taskComplete(); //force reload the cursor, so that the order get reflected.
        return false;
    }


    @HandleEvent(eventType = Event.ConversationUpdated)
    public boolean handleConversationsUpdated(EmptyEventData eventData) {

        taskComplete();
        return true;
    }

    private boolean conversationExists(String originatingAddress) {
        Cursor query = contentResolver.query(Uri.parse("content://mms-sms/conversations"), new String[]{TextMessageConstants.ADDRESS}, null, null, null);
        while (query.moveToNext()){
            String string = query.getString(query.getColumnIndex(TextMessageConstants.ADDRESS));
            if(PhoneNumberUtils.compare(string, originatingAddress)){
                return true;
            }
        }

        return false;
    }

    @Override
    public void taskComplete() {
        Cursor smsContent = contentResolver.query(Uri.parse("content://mms-sms/conversations"), null, null, null, "date DESC");

        changeCursor(smsContent);
    }

    @Override
    public void task() {
        int initialCount = getCursor().getCount();
        int retryCount;
        Cursor newCursor;
        try {
            do {
                Thread.sleep(5);
                newCursor = contentResolver.query(Uri.parse("content://mms-sms/conversations"), null, null, null, "date DESC");
                retryCount =+ 1;
                if(retryCount > ApplicationConstants.MAX_RETRY_COUNT) break;
            } while (!(newCursor.getCount() > initialCount));
        } catch (InterruptedException e) {
            Log.e("TEXTING:", "Exception occurred while trying to reload conversation: ", e);
        }
    }
}




Java Source Code List

com.skk.texting.ApplicationStart.java
com.skk.texting.TextingApplication.java
com.skk.texting.adaptor.ContactsAdaptor.java
com.skk.texting.adaptor.ConversationAdaptor.java
com.skk.texting.adaptor.TextMessageAdaptor.java
com.skk.texting.adaptor.view.ViewHolder.java
com.skk.texting.adaptor.view.ViewType.java
com.skk.texting.async.AsyncCursorUpdate.java
com.skk.texting.async.BackgroundTask.java
com.skk.texting.constants.ApplicationConstants.java
com.skk.texting.constants.TextMessageConstants.java
com.skk.texting.customview.CustomRelativeLayout.java
com.skk.texting.di.RoboSmallApplication.java
com.skk.texting.domain.ConversationRepository.java
com.skk.texting.domain.Conversation.java
com.skk.texting.domain.Person.java
com.skk.texting.domain.TextMessage.java
com.skk.texting.eventdata.EmptyEventData.java
com.skk.texting.eventdata.IncomingSmsData.java
com.skk.texting.eventdata.RepliedSms.java
com.skk.texting.evented.EventData.java
com.skk.texting.evented.EventHandler.java
com.skk.texting.evented.EventRepository.java
com.skk.texting.evented.Event.java
com.skk.texting.evented.HandleEvent.java
com.skk.texting.factory.PersonFactory.java
com.skk.texting.gesture.MotionEventRecorder.java
com.skk.texting.gesture.OnSwipeGestureHandler.java
com.skk.texting.gesture.SwipeGestureHandler.java
com.skk.texting.gesture.SwipeGestureListener.java
com.skk.texting.listener.IncomingSmsListener.java
com.skk.texting.listener.ListItemClickListener.java
com.skk.texting.viewwrapper.AllContactsWrapper.java
com.skk.texting.viewwrapper.HeaderWrapper.java
com.skk.texting.viewwrapper.MessageConsoleWrapper.java
com.skk.texting.viewwrapper.TextMessagesView.java