package com.sogouchat.os;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.sogouchat.bean.ContactNode;
import com.sogouchat.bean.DownNode;
import com.sogouchat.bean.MsgNode;
import com.sogouchat.bean.TelNode;
import com.sogouchat.bean.ThreadNode;
import com.sogouchat.os.DownloadManagerAsync.OnDownloadCompleteListener;
import com.sogouchat.os.DownloadManagerAsync.OnDownloadConnectListener;
import com.sogouchat.os.DownloadManagerAsync.OnDownloadErrorListener;
import com.sogouchat.os.DownloadManagerAsync.OnDownloadUpdateListener;
import com.sogouchat.util.HanZiToPinYin;
import com.sogouchat.util.ListUtil;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcelable;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.widget.Toast;
public class MsgService extends Service {
private static final String TAG = "MsgService";
/** For showing and hiding our notification. */
NotificationManager mNM;
/** Keeps track of all current registered clients. */
ArrayList<Messenger> mClients = new ArrayList<Messenger>();
int mValue = 0;
// LinkedList<ContactNode> mContactList = new LinkedList<ContactNode>();
LinkedList<TelNode> mTelList = new LinkedList<TelNode>();
ArrayList<ThreadNode> mThreadList = new ArrayList<ThreadNode>();
ArrayList<ThreadNode> mCountList = new ArrayList<ThreadNode>();
LinkedList<MsgNode> mMsgList = new LinkedList<MsgNode>();
LinkedList<MsgNode> mSmsList = new LinkedList<MsgNode>();
LinkedList<MsgNode> mMmsList = new LinkedList<MsgNode>();
ArrayList<DownNode> mDownList = new ArrayList<DownNode>();
int mMaxMsgId=0;
SMSContentObserver mSmsObserver;
private int mMsgType;
private Intent mIntent = new Intent();
private Bundle mBundle = new Bundle();
private DownloadManagerAsync mDownMgr = new DownloadManagerAsync();
private DownListener mDownListener = new DownListener();
private Timer mDownTimer;
private TimerTask mDownTask;
private ContentResolver mCr;
private Handler mSrvHandler = new Handler() {
public void handleMessage(Message msg) {
Log.i(TAG, "mObserverHandler handleMessage") ;
switch (msg.what) {
case ChatAppConstant.SRV_BackMsg_New_Sms:{
sendSrvBroadcast(ChatAppConstant.SRV_BackMsg_New_Sms);
}
break;
case ChatAppConstant.SRV_Fetch_All_Msg_Ok:{
sendSrvBroadcast(ChatAppConstant.SRV_BackMsg_Fetch_All_Msg_Ok);
}
break;
case ChatAppConstant.SRV_Re_Fetch_Contacts_Msg_Ok:{
Log.i(TAG, "receive SRV_Re_Fetch_Contacts_Msg_Ok") ;
sendSrvBroadcast(ChatAppConstant.SRV_BackMsg_Fetch_Contacts_Msg_Ok);
}
break;
default:
break;
}
}
};
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind") ;
return mBinder;
}
private final IMsgService.Stub mBinder = new IMsgService.Stub(){
@Override
public List<TelNode> getTelList() throws RemoteException {
// Log.i("IMsgService", "getContactList") ;
return mTelList;
}
public List<MsgNode> getNewMsgList() throws RemoteException {
// Log.i("IMsgService", "getContactList") ;
return mMsgList;
}
public void RefetchContacts() {
Log.i(TAG, "RefetchContacts123");
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
mTelList.clear();
Log.i("ticket time 1 RefetchContacts begin", "" + System.currentTimeMillis());
doFetchContactBox();
Log.i(TAG, "fetchContactDb --doFetchContactBox Ok");
// setupPinyin();
fetchMsgBox();
appendThread();
mMsgList.clear();
Log.i(TAG, "fetchMsgByThread");
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
doFetchMsgByThread();
mSrvHandler.sendEmptyMessage(ChatAppConstant.SRV_Re_Fetch_Contacts_Msg_Ok);
}});
mSrvHandler.postDelayed(thread, 1000);//5000
}
});
thread.start();
}
public void fetchContactDb() {
Log.i(TAG, "fetchContactDb");
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
doFetchContactDb();
Log.i(TAG, "fetchContactDb ++++Ok");
sendSrvBroadcast(ChatAppConstant.SRV_BackMsg_FetchDb_Ok);
}
});
thread.start();
}
private void doFetchContactDb(){
// android.os.Debug.waitForDebugger();
mTelList.clear();
doFetchContactBox();
// setupPinyin();
fetchMsgBox();
appendThread();
Log.i(TAG, "fetchContactDb --appendMsgList Ok");
//clean
fetchMsgByThread();
Log.i(TAG, "Print list ++");
// Iterator<ContactNode> it = mContactList.iterator();
// while(it.hasNext()){
// ContactNode node = it.next();
// node.logTo();
// }
Log.i(TAG, "Print list --");
}
public void fetchMsgBox(){
Log.i("IMsgService", "fetchMsgBox") ;
doFetchThread();
}
public void handleSrvForeMsg(int msg, String arg1, String arg2){
Log.i("handleSrvMsg", "msg"+msg) ;
mMsgType = msg;
switch(msg){
case ChatAppConstant.SRV_ForeMsg_FetchDb:{
fetchContactDb();
// sendSrvBroadcast(ChatAppConstant.SRV_BackMsg_FetchDb_Ok);
}
break;
case ChatAppConstant.SRV_ForeMsg_Down_Msg:{
Log.i("handleSrvMsg", "msg file ="+arg2) ;
mDownMgr.download(arg1, arg2);
}
break;
case ChatAppConstant.SRV_ForeMsg_Read_Sms:{
// setReadSms(arg1);
}
break;
case ChatAppConstant.SRV_ForeMsg_Fetch_Contacts:{
Log.i("handleSrvMsg: SRV_ForeMsg_Fetch_Contacts:", "msg"+msg) ;
RefetchContacts();//重取联系人数据
}
break;
default:{
}
break;
}
}
public void downMediaList(List<DownNode> msgList){
Iterator<DownNode> it = msgList.iterator();
while(it.hasNext()){
DownNode node = it.next();
addDownList(node);
}
//Start down timer
if (mDownTask == null ) {
mDownTask = new TimerTask(){
@Override
public void run() {
Log.i(TAG, "mDownTask run") ;
// TODO Auto-generated method stub
if (mDownList.size()>0 ){
Log.i(TAG, "mDownTask mRunning="+mDownMgr.mRunning);
if (!mDownMgr.mRunning){
DownNode node = mDownList.remove(0);
if (node.mShortUrl.length()>0){
Log.i(TAG, "mDownTask run start++"+node.mShortUrl) ;
mDownMgr.download(node);
}
}
}
else {
Log.i(TAG, "mDownTask stop TimerTask!!!!!") ;
mDownTask.cancel();
mDownTask = null;
}
}
};
mDownTimer.schedule(mDownTask, 1000, 5000);
}
}
};
@Override
public void onCreate() {
Log.i(TAG, "onCreate") ;
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Display a notification about us starting.
mCr = getContentResolver();
showNotification();
mSmsObserver = new SMSContentObserver(this, mSrvHandler, this);
Uri smsUri = Uri.parse("content://sms");
mCr.registerContentObserver(smsUri, true,mSmsObserver);
Toast.makeText(this, "server create", Toast.LENGTH_SHORT).show();
mDownMgr.setOnDownloadCompleteListener(mDownListener);
mDownMgr.setOnDownloadConnectListener(mDownListener);
mDownMgr.setOnDownloadErrorListener(mDownListener);
mDownMgr.setOnDownloadUpdateListener(mDownListener);
checkSavePath();
// setReadMsg();
mDownTimer = new Timer();
}
protected void setupPinyin() {
// TODO Auto-generated method stub
Iterator<TelNode> telIt = mTelList.iterator();
while (telIt.hasNext()) {
TelNode telNode = telIt.next();
telNode.mPinyin = HanZiToPinYin.StrToPinYin(telNode.mName);
if (telNode.mPinyin == "")
telNode.mPinyin=telNode.mName;
}
}
@Override
public void onDestroy() {
// Cancel the persistent notification.
mNM.cancel("server start", 1000);
// Tell the user we stopped.
Toast.makeText(this, "server stop", Toast.LENGTH_SHORT).show();
}
private void showNotification() {
}
private void sendSrvBroadcast(int value){
Log.i(TAG, "sendSrvBroadcast") ;
mBundle.putInt("CastType", value);
mIntent.putExtras(mBundle);
mIntent.setAction("com.sogouchat.action.CONTENT_CHANGED_ACTION");
sendBroadcast(mIntent);
}
private void sendSrvBroadcast(int value, String body){
Log.i(TAG, "sendSrvBroadcast") ;
mBundle.putInt("CastType", value);
mBundle.putString("File", body);
mIntent.putExtras(mBundle);
mIntent.setAction("com.sogouchat.action.CONTENT_CHANGED_ACTION");
sendBroadcast(mIntent);
}
private void sendSrvBroadcast(int value, Parcelable body){
Log.i(TAG, "sendSrvBroadcast") ;
mBundle.putInt("CastType", value);
mBundle.putParcelable("parcel", body);
mIntent.putExtras(mBundle);
mIntent.setAction("com.sogouchat.action.CONTENT_CHANGED_ACTION");
sendBroadcast(mIntent);
}
public void doFetchThread() {
Log.i(TAG, "doFetchThread");
mThreadList.clear();
/*
select threads._id, canonical_addresses.address, threads.date, threads.message_count, threads.snippet, threads.snippet_cs, threads.read, threads.has_attachment
from threads,canonical_addresses, sms
where threads.recipient_ids=canonical_addresses._id and sms.thread_id=threads._id
order by threads.date desc --
*/
Cursor draftCursor = mCr.query(Uri.parse("content://sms"),
new String[] {"threads._id, canonical_addresses.address, threads.date, threads.message_count, " +
" threads.snippet, threads.snippet_cs, threads.read, threads.has_attachment "+
" from threads,canonical_addresses" +
" where threads.recipient_ids=canonical_addresses._id" +
" order by threads.date desc --"},
null, null, null);
if (draftCursor != null) {
// 循环遍历
while (draftCursor.moveToNext()) {
addThreadNode(draftCursor);
}
draftCursor.close();
}
/*
select threads._id, canonical_addresses.address, count(sms._id) as unread_count
from threads,canonical_addresses, sms
where (threads.recipient_ids=canonical_addresses._id) and (sms.thread_id=threads._id) and (sms.read=0)
group by threads._id
order by threads.date desc --
*/
draftCursor = mCr.query(Uri.parse("content://sms"),
new String[] {"threads._id, canonical_addresses.address, count(sms._id) as unread_count " +
" from threads,canonical_addresses, sms" +
" where (threads.recipient_ids=canonical_addresses._id) and (sms.thread_id=threads._id) and (sms.read=0) " +
" group by threads._id "+
" order by threads.date desc --"},
null, null, null);
if (draftCursor != null) {
// 循环遍历
while (draftCursor.moveToNext()) {
addThreadUnread(draftCursor);
}
draftCursor.close();
}
}
public void fetchMsgByThread(){
Log.i(TAG, "fetchMsgByThread");
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
doFetchMsgByThread();
mSrvHandler.sendEmptyMessage(ChatAppConstant.SRV_Fetch_All_Msg_Ok);
}});
mSrvHandler.postDelayed(thread, 5000);
}
private void doFetchMsgByThread( ){
for(TelNode node :mTelList){
if(node.mMsgCnt >0){
fetchSmsByThreadId(node.mThreadId);
fetchMmsByThreadId(node.mThreadId);
mergeMsgList();
if (node.mMsgList == null){
node.mMsgList = new ArrayList<MsgNode>();
}
node.mMsgList.addAll(mMsgList);
}
}
}
private void mergeMsgList() {
// TODO Auto-generated method stub
mMsgList.clear();
mMsgList.addAll(mSmsList);
mMsgList.addAll(mMmsList);
Collections.sort(mMsgList, new ListUtil.SortByMsgDate() );
}
public void fetchSmsByThreadId(int threadId) {
// Log.i(TAG, "doFetchMsgByThread="+threadId);
mSmsList.clear();
// 查询发件箱里的内容
Uri outSMSUri = Uri.parse("content://sms/");
// String[] projection = new String[] { "_id", "thread_id", "person",
// "protocol", "read", "status", "type", "date", "address",
// "subject", "body" };
String selection = "thread_id" + " == " + threadId;
Cursor cur = mCr.query(outSMSUri, MsgSqlConst.Projection_Sms,
selection, null, "date asc");
if (cur != null) {
// 循环遍历
while (cur.moveToNext()) {
addSmsNode(cur, mSmsList);
}
cur.close();
}
}
public void fetchMmsByThreadId(int threadId) {
// Log.i(TAG, "doFetchMsgByThread="+threadId);
mMmsList.clear();
// 查询发件箱里的内容
/*
select pdu._id, pdu.thread_id, pdu.read, pdu.st, pdu.msg_box, pdu.date, addr.address, pdu.ct_l
from pdu, addr
where (pdu._id=addr.msg_id) and (pdu.thread_id=115)
group by pdu._id
*/
Uri outSMSUri = Uri.parse("content://mms/");
Cursor cur = mCr.query(Uri.parse("content://mms"),
new String[] {"pdu._id, pdu.thread_id, pdu.read, pdu.st, pdu.msg_box, pdu.date, addr.address, pdu.ct_l " +
" from pdu, addr " +
" where (pdu._id=addr.msg_id) and (pdu.thread_id="+threadId+")" +
" group by pdu._id --"}, null, null, null);
if (cur != null) {
// 循环遍历
while (cur.moveToNext()) {
addMmsNode(cur, mMmsList);
}
cur.close();
}
}
private void addSmsNode(Cursor cur , LinkedList<MsgNode> list) {
MsgNode node = new MsgNode();
node.mMsgId = cur.getInt(MsgSqlConst.Index_Sms_Id );
// record max msgid
if (node.mMsgId > mMaxMsgId) mMaxMsgId = node.mMsgId;
node.mThreadId = cur.getInt(MsgSqlConst.Index_Sms_ThreadId );
node.mPerson = cur.getInt(MsgSqlConst.Index_Sms_Person );
node.mProtocol = cur.getInt(MsgSqlConst.Index_Sms_Protocol );
node.mRead = cur.getInt(MsgSqlConst.Index_Sms_Read );
node.mStatus = cur.getInt(MsgSqlConst.Index_Sms_Status );
node.mType = cur.getInt(MsgSqlConst.Index_Sms_Type );
node.mDate = cur.getLong(MsgSqlConst.Index_Sms_Date );
node.mAdress = cur.getString(MsgSqlConst.Index_Sms_Address );
node.mBody = cur.getString(MsgSqlConst.Index_Sms_Body );
if (node.mAdress == null) return;
list.add(node);
// Log.i(TAG, "id="+node.mMsgId+" thread_id="+node.mThreadId+"body="+ node.mBody);
}
private void addMmsNode(Cursor cur , LinkedList<MsgNode> list) {
MsgNode node = new MsgNode();
node.mMsgId = cur.getInt(MsgSqlConst.Index_Mms_Id);
// record max msgid
if (node.mMsgId > mMaxMsgId) mMaxMsgId = node.mMsgId;
node.mThreadId = cur.getInt(MsgSqlConst.Index_Mms_ThreadId );
node.mProtocol = 1;
node.mRead = cur.getInt( MsgSqlConst.Index_Mms_Read );
node.mStatus = cur.getInt(MsgSqlConst.Index_Mms_Status );
node.mType = cur.getInt(MsgSqlConst.Index_Mms_Type );
node.mDate = cur.getLong(MsgSqlConst.Index_Mms_Date ) * 1000;
node.mAdress = cur.getString(MsgSqlConst.Index_Mms_Address );
node.mBody = cur.getString(MsgSqlConst.Index_Mms_Body );
if (node.mAdress == null) return;
list.add(node);
// Log.i(TAG, "id="+node.mMsgId+" thread_id="+node.mThreadId+"body="+ node.mBody);
}
private void addThreadUnread(Cursor cur) {
int threadId = cur.getInt(MsgSqlConst.Index_Threads_Id);
String address = cur.getString(MsgSqlConst.Index_Threads_Address);
int unMsgCnt = cur.getInt(MsgSqlConst.Index_Threads_UnMsg_Cnt);
ThreadNode node = getThreadById(threadId, address);
node.mUnMsgCnt = unMsgCnt;
Log.i(TAG, "addThreadUnread "+threadId+" "+ unMsgCnt);
}
private ThreadNode getThreadById(int threadId, String add){
for(int i=0; i < mThreadList.size(); i++){
ThreadNode nd = mThreadList.get(i);
if( nd.mThreadId == threadId){
return nd;
}
}
return null;
}
private void addThreadNode(Cursor cur) {
ThreadNode node = new ThreadNode();
node.mThreadId = cur.getInt(MsgSqlConst.Index_Threads_Id);
node.mAddress = cur.getString(MsgSqlConst.Index_Threads_Address);
node.mDate = cur.getLong(MsgSqlConst.Index_Threads_Date);
node.mMsgCnt = cur.getInt(MsgSqlConst.Index_Threads_Msg_Cnt);
// node.mUnMsgCnt = cur.getInt(MsgSqlConst.Index_Threads_UnMsg_Cnt);
node.mSnippet = cur.getString(MsgSqlConst.Index_Threads_Snippet);
node.mSinppetCs = cur.getInt(MsgSqlConst.Index_Threads_Snippet_cs);
if (node.mSinppetCs == 0){
if(node.mSnippet!=null)
node.mSnippet = node.mSnippet.trim();
} else if (node.mSinppetCs == 106){
if (node.mSnippet != null){
try {
node.mSnippet = new String(node.mSnippet.getBytes("iso-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
node.mSnippet = "彩信";
}
}
node.mRead = cur.getInt(MsgSqlConst.Index_Threads_Read);
node.mAttach = cur.getInt(MsgSqlConst.Index_Threads_Attach);
Log.i(TAG, "addThreadNode "+node.toString());
mThreadList.add(node);
}
public void doFetchContactBox() {
Log.i(TAG, "doFetchContactBox");
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
//+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))";
Cursor cursor = mCr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
MsgSqlConst.Projection_Contact,
select,
null,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
if (cursor != null) {
while (cursor.moveToNext()) {
TelNode telNode = new TelNode();
// 遍历所有的联系人下面所有的电话号码
telNode.mContactId = cursor.getInt(MsgSqlConst.Index_Contact_Id);
telNode.mPhotoId = cursor.getInt(MsgSqlConst.Index_Photo_Id);
telNode.mName = cursor.getString(MsgSqlConst.Index_Name);
telNode.mTel = cursor.getString(MsgSqlConst.Index_Number);
telNode.mType = cursor.getInt(MsgSqlConst.Index_Type);
Log.i(TAG, "addTelNumNode---"+telNode.mName+"id="+ telNode.mPhotoId);
// Log.i(TAG, "addTelNumNode+"+ telNode.mTel);
// Log.i(TAG, "addTelNumNode+++");
mTelList.add(telNode);
}
cursor.close();
}
else{
Toast.makeText(this, "cursor is null!", Toast.LENGTH_LONG).show();
}
}
void appendThread(){
Log.i(TAG, "appendThread");
Iterator<ThreadNode> ThreadIt = mThreadList.iterator();
while(ThreadIt.hasNext()){
ThreadNode thNode = ThreadIt.next();
//TODO draft
if (thNode.mAddress == null) continue;
// Log.i(TAG, "appendThread th_id="+ thNode.mThreadId);
TelNode telNode = getTelNumNode(thNode.mAddress);
if (telNode == null){
telNode = new TelNode();
telNode.mTel = thNode.mAddress;
telNode.mName = telNode.mTel;
telNode.mContactId=0;
mTelList.add(telNode);
}
telNode.mThreadId = thNode.mThreadId;
telNode.mAddress = thNode.mAddress;
telNode.mUnMsgCnt = thNode.mUnMsgCnt;
telNode.mMsgCnt = thNode.mMsgCnt;
telNode.mLastDate = thNode.mDate;
telNode.mLastBody = thNode.mSnippet;
telNode.mBodyCs = thNode.mSinppetCs;
}
}
private TelNode getTelNode(MsgNode msgNode){
Iterator<TelNode> telIt = mTelList.iterator();
while (telIt.hasNext()) {
TelNode telNode = telIt.next();
if (telNode.mContactId == msgNode.mPerson) {
return telNode;
}
}
return null;
}
TelNode getTelNumNode(String tel) {
// Log.i(TAG, "getTelNumNode"+tel);
Iterator<TelNode> telIt = mTelList.iterator();
while (telIt.hasNext()) {
TelNode telNode = telIt.next();
// if (isSameTel (telNode.mTel, tel)) {
boolean same = PhoneNumberUtils.compare(telNode.mTel, tel);
if (same)
// Log.i(TAG, "getTelNumNode "+telNode.mTel+" "+ tel+" ="+same);
if (PhoneNumberUtils.compare(telNode.mTel, tel)) {
return telNode;
}
}
// Log.i(TAG, "getTelNumNode Not"+tel);
return null;
}
private boolean isSameTel(String tel1, String tel2) {
if (tel1 == null || tel2==null) {
return false;
}
int len1 = tel1.length();
int len2 = tel2.length();
int sameSuf=0;
int minLen = Math.min(len1, len2);
int maxLen = Math.max(len1, len2);
len1--;
len2--;
char v1[] = tel1.toCharArray();
char v2[] = tel2.toCharArray();
while (minLen-- != 0) {
if (v1[len1--] == v2[len2--]) {
sameSuf++;
}
else{
break;
}
}
if (sameSuf > 4 || (sameSuf == minLen&& sameSuf/maxLen>0.5)){
return true;
}
else{
return false;
}
}
class DownListener implements OnDownloadUpdateListener,
OnDownloadConnectListener,
OnDownloadCompleteListener,
OnDownloadErrorListener {
@Override
public void onDownloadError(DownloadManagerAsync manager, Exception e) {
// TODO Auto-generated method stub
Log.i(TAG, "onDownloadError"+e) ;
}
@Override
public void onDownloadComplete(DownloadManagerAsync manager,
Object result, String file) {
// TODO Auto-generated method stub
Log.i(TAG, "onDownloadComplete"+file) ;
// Log.i(TAG, "mDownTask mRunning="+mDownMgr.mRunning);
sendSrvBroadcast( ChatAppConstant.SRV_BackMsg_File_Down_Ok, file);
}
@Override
public void onDownloadConnect(DownloadManagerAsync manager) {
// TODO Auto-generated method stub
Log.i(TAG, "onDownloadConnect") ;
// Log.i(TAG, "mDownTask mRunning="+mDownMgr.mRunning);
}
@Override
public void onDownloadUpdate(DownloadManagerAsync manager, int percent) {
// TODO Auto-generated method stub
Log.i(TAG, "onDownloadUpdate"+percent) ;
// Log.i(TAG, "mDownTask mRunning="+mDownMgr.mRunning);
}
}
public static boolean hasSdcard() {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
private void checkSavePath() {
if (getSDPath() == null) {
Log.i(TAG, "SD is not existing.");
} else {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
File sdcardDir = Environment.getExternalStorageDirectory();
String newPath = sdcardDir.getPath() + "/sogouchat/";
File path = new File(newPath);
if (!path.exists()) {
path.mkdirs();
System.out.println("paht ok,path:" + newPath);
}
newPath = newPath + "/media/";
path = new File(newPath);
if (!path.exists()) {
// 若不存在,创建目录,可以在应用启动的时候创建
path.mkdirs();
System.out.println("paht ok,path:" + newPath);
}
} else {
System.out.println("false");
}
}
}
public String getSDPath() {
File SDdir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (sdCardExist) {
SDdir = Environment.getExternalStorageDirectory();
}
if (SDdir != null) {
return SDdir.toString();
} else {
return null;
}
}
public void setReadSms(String tel){
Log.i(TAG, "setRead tel ="+tel);
ContentValues cv = new ContentValues();
cv.put("read", "1");
Uri smsUri = Uri.parse("content://sms/index");
mCr.update(smsUri, cv,
"address= ? and type = ?",new String[] {tel, "1"});
}
private void addDownList(DownNode node){
Iterator<DownNode> it = mDownList.iterator();
while(it.hasNext()){
if (it.next().mDate == node.mDate){
break;
}
}
if (!it.hasNext()){
mDownList.add(0, node);
}
}
private void setReadMsg() {
ContentValues cv = new ContentValues();
// int msgArray[][]={{123,400},{163,391},{162,377},{159,373},{158,372},
// {45,365},{113,361},{70,354},{147,349},{45,348},
// {136,328},{136,327},{129,317},{128,316},{121,309},
// {120,308},{70,307},{117,304},{115,299},{111,295},};
// cv.put("read", "0");
// for(int i=0;i < 20;i++){
// mCr.update(Uri.parse("content://sms"), cv, " thread_id=? and _id=?",
// new String[] { msgArray[i][0] + "", msgArray[i][1] + "" });
// }
int msgArray[] = {1,2,3,10, 12,15,25,35,64,100, 110, 115, 118, 124,129, 184,186,189, 200,
203,228,230,259,267,269,271,273,274,276,278,279,288,290};
cv.put("read", "0");
for (int i = 0; i < 20; i++) {
mCr.update(Uri.parse("content://sms"), cv," _id=?", new String[] {
msgArray[i]+""});
}
}
}