package com.ldci.t56.mobile.safe;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.OperationApplicationException;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.Contacts.Phones;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TabHost.OnTabChangeListener;
import com.ldci.t56.mobile.db.DbAdapter;
import com.ldci.t56.mobile.info.Message_Forbid_Info;
import com.ldci.t56.mobile.tool.BroadCastTool;
public class MessageActivity extends TabActivity implements OnTabChangeListener {
private DbAdapter mDbAdapter;
private Cursor mCursorTab1;
private Cursor mCursorTab2;
private ListView mListViewTab1;
private ListView mListViewTab2;
private static final int MENU_RUBBISH_BACK = Menu.FIRST;
private static final int MENU_RUBBISH_REPLY = Menu.FIRST+1;
private static final int MENU_RUBBISH_CALL = Menu.FIRST+2;
private static final int MENU_RUBBISH_DELETE = Menu.FIRST+3;
private static final int MENU_RUBBISH_MULTIPLE = Menu.FIRST+4;
private static final int MENU_FORBID_REPLY = Menu.FIRST+5;
private static final int MENU_FORBID_CALL = Menu.FIRST+6;
private static final int MENU_FORBID_DELETE = Menu.FIRST+7;
private static final int MENU_FORBID_MULTIPLE = Menu.FIRST+8;
private static final int MENU_FORBID_ADD = Menu.FIRST+9;
private static final int MENU_FORBID_ADD_FIRST = Menu.FIRST+10;
private static final int MENU_ADD_CANTACT = Menu.FIRST+11;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost_public);
initFindViewById();
TabHost mTabHost=getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("拦截记录",getResources().getDrawable(R.drawable.tab_icon_1)).setContent(R.id.tabhost_public_listview));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("黑名单",getResources().getDrawable(R.drawable.tab_icon_2)).setContent(R.id.tabhost_public_listview_tab2));
mTabHost.setOnTabChangedListener(this);
mTabHost.setCurrentTab(0);
mDbAdapter = new DbAdapter(MessageActivity.this);
mDbAdapter.open();
initTab1Data();
initTabListViewListener(mListViewTab1);
}
private MyBroadCastReceiver mMBCR;
private IntentFilter mIF;
@Override
protected void onResume(){
super.onResume();
mIF = new IntentFilter();
mIF.addAction(BroadCastTool.SMS_RECEIVED_ACTION);
mMBCR = new MyBroadCastReceiver();
this.registerReceiver(mMBCR, mIF);
}
@Override
protected void onDestroy(){
super.onDestroy();
this.unregisterReceiver(mMBCR);
mDbAdapter.close();
}
/** 打電話*/
Intent mIntent;
private void actionCALL(int position, Cursor c,String column){
c.moveToPosition(position);
Uri uri = Uri.parse("tel:"+c.getString(c.getColumnIndex(column)));
mIntent = new Intent(Intent.ACTION_CALL, uri);
startActivity(mIntent);
}
/**發短信 */
private void actionSMS(int position, Cursor c,String column){
c.moveToPosition(position);
mIntent = new Intent(Intent.ACTION_VIEW);
mIntent.putExtra("address", c.getString(c.getColumnIndex(column)));
mIntent.setType("vnd.android-dir/mms-sms");
startActivity(mIntent);
}
/** 刪除標記*/
private void delMarkItemData(ListView lv,Cursor c,String table){
int mContant = lv.getCount();
for(int i=0;i!=mContant;i++){
if(lv.getCheckedItemPositions().get(i)){
c.moveToPosition(i);
long rowId = c.getLong(c.getColumnIndex(DbAdapter.TABLE_ID));
mDbAdapter.getDel(rowId, table);
}
}
}
private void returnMessage(){
for(int i=0;i!=mListViewTab1.getCount();i++){
if(mListViewTab1.getCheckedItemPositions().get(i)){
ContentValues cv = new ContentValues();
cv.put("type", "1");
mCursorTab1.moveToPosition(i);
cv.put("address", mCursorTab1.getString(mCursorTab1.getColumnIndex(DbAdapter.MESSAGE_RUBBISH_PHONE)));
String rowContent = mCursorTab1.getString(mCursorTab1.getColumnIndex(DbAdapter.MESSAGE_RUBBISH_CONTENT));
cv.put("body", rowContent);
getContentResolver().insert(Uri.parse("content://sms/inbox"), cv);
long rowId = mCursorTab1.getLong(mCursorTab1.getColumnIndex(DbAdapter.TABLE_ID));
mDbAdapter.getDel(rowId, DbAdapter.MESSAGE_RUBBISH_TABLE_NAME);
}
}
}
private String getPhoneNumber(int position,Cursor c){
c.moveToPosition(position);
return c.getString(c.getColumnIndex(DbAdapter.MESSAGE_RUBBISH_PHONE));
}
EditText mCallName;
EditText mCallNumber;
private void addContact(String phone){
LayoutInflater mLI = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout mLL = (LinearLayout)mLI.inflate(R.layout.set_call_record_name, null);
mCallName = (EditText)mLL.findViewById(R.id.EditText01);
mCallNumber = (EditText)mLL.findViewById(R.id.EditText02);
mCallNumber.setText(phone);
new AlertDialog.Builder(this).setTitle("加入联系人").
setView(mLL).
setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(mCallName.getText().toString().length() != 0&&mCallNumber.getText().toString().length() != 0){
ContentValues cv = new ContentValues();
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
builder.withValues(cv);
operationList.add(builder.build());
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(StructuredName.RAW_CONTACT_ID, 0);
builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
builder.withValue(StructuredName.DISPLAY_NAME, mCallName.getText().toString());
operationList.add(builder.build());
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(Phone.RAW_CONTACT_ID, 0);
builder.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
builder.withValue(Phone.NUMBER, mCallNumber.getText().toString());
builder.withValue(Data.IS_PRIMARY, 1);
operationList.add(builder.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "成功添加!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "姓名或号码不能为空!", Toa