package com.sogouchat.os;
import java.util.LinkedList;
import com.sogouchat.bean.MsgNode;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
public class SMSContentObserver extends ContentObserver {
private static String TAG = "SMSContentObserver";
private Context mContext ;
private Handler mHandler ; //更新UI线程
MsgService mChatSrv;
public SMSContentObserver(Context context,Handler handler, MsgService chatSrv) {
super(handler);
mChatSrv = chatSrv;
mContext = context ;
mHandler = handler ;
}
/**
* @param selfChange 此值意义不大 一般情况下该回调值false
*/
@Override
public void onChange(boolean selfChange){
Log.i(TAG, "the sms table has changed");
//查询短信箱里的内容
Uri outSMSUri = Uri.parse("content://sms/inbox") ;
String[] projection= new String[] { "_id", "thread_id", "protocol", "read", "status", "type", "date", "address", "subject", "body"};
String selection = "_id>"+mChatSrv.mMaxMsgId;
Cursor cur = mContext.getContentResolver().query(outSMSUri,
projection, selection, null, "date desc");
if(cur != null){
boolean send = false;
Log.i(TAG, "the number of send is"+cur.getCount()) ;
//循环遍历
while(cur.moveToNext()){
int read = cur.getInt(cur.getColumnIndex("read"));
Log.i(TAG, "id="+cur.getString(cur.getColumnIndex("_id"))+"read="+read);
if (0 == cur.getInt(cur.getColumnIndex("read"))){
if (!send){
send = true;
mChatSrv.mMsgList.clear();
}
addNode(cur);
}
}
cur.close();
if (send){
mHandler.sendEmptyMessage(ChatAppConstant.SRV_BackMsg_New_Sms);
}
}
}
private void addNode(Cursor cur){
MsgNode node = new MsgNode();
node.mMsgId = cur.getInt(cur.getColumnIndex("_id"));
if ( node.mMsgId > mChatSrv.mMaxMsgId) {
mChatSrv.mMaxMsgId = node.mMsgId;
}
else{
return;
}
node.mThreadId = cur.getInt(cur.getColumnIndex("thread_id"));
node.mProtocol = cur.getInt(cur.getColumnIndex("protocol"));
node.mRead = cur.getInt(cur.getColumnIndex("read"));
node.mStatus = cur.getInt(cur.getColumnIndex("status"));
node.mType = cur.getInt(cur.getColumnIndex("type"));
node.mDate = cur.getLong(cur.getColumnIndex("date"));
node.mAdress = cur.getString(cur.getColumnIndex("address"));
node.mBody = cur.getString(cur.getColumnIndex("body"));
mChatSrv.mMsgList.add(node);
}
}