package jiaping.android.study;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony.Sms;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class DataFactoryActivity extends Activity {
public final static int ENGLISH = 0;
public final static int CHINESE = 1;
public final int ID_SET = 2;
public final int ID_EXIT = 3;
public final int ID_CUSTOM_MODE = 4;
public final int ID_AUTO_MODE = 5;
private EditText msgNumber,addressNumber,wordsNumber,phoneNumber,msgContent;
private TextView menuIntroduce;
private CheckBox customContent;
private RadioGroup LanguageChoice;
private RadioButton En,Ch;
private Button msgCreate;
private ProgressDialog progress;
private int MsgNumber,AddressNumber,WordsNumber,Language = ENGLISH;
private String PhoneNumber = null;
private String MsgContent = null;
//初始化状态
private int StatusTag = Sms.STATUS_NONE,ReadTag = 1,TypeTag = Sms.MESSAGE_TYPE_INBOX;
private int ModeTag = ID_AUTO_MODE;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("onCreate1111", "onCreate");
setContentView(R.layout.main);
init();
msgNumber.setKeyListener(DigitsKeyListener.getInstance());
addressNumber.setKeyListener(DigitsKeyListener.getInstance());
phoneNumber.setKeyListener(DigitsKeyListener.getInstance());
wordsNumber.setKeyListener(DigitsKeyListener.getInstance());
customContent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(customContent.isChecked()){
wordsNumber.setVisibility(View.GONE);
msgContent.setVisibility(View.VISIBLE);
LanguageChoice.setVisibility(View.GONE);
}else{
wordsNumber.setVisibility(View.VISIBLE);
msgContent.setVisibility(View.GONE);
LanguageChoice.setVisibility(View.VISIBLE);
}
}
});
LanguageChoice.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
if(En.isChecked()){
Language=ENGLISH;
}else if(Ch.isChecked()){
Language=CHINESE;
}
}
});
msgCreate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(ModeTag == ID_AUTO_MODE)
{
if(msgNumber.getText().length()>0){
MsgNumber = Integer.parseInt(msgNumber.getText().toString());
}else{
MsgNumber = -1;
}
if(addressNumber.getText().length()>0){
AddressNumber = Integer.parseInt(addressNumber.getText().toString());
}else{
AddressNumber = -1;
}
if(MsgNumber>0&&AddressNumber>0&&MsgNumber>=AddressNumber){
progress.setCancelable(false);
progress.setMax(MsgNumber);
progress.setProgress(0);
progress.show();
new Thread(new RunAutoMsgInstall()).start();
}else{
AlertDialog.Builder warning = new AlertDialog.Builder(DataFactoryActivity.this);
warning.setTitle("warning");
warning.setMessage(R.string.warning);
warning.setPositiveButton("close", null);
warning.show();
}
}
else if(ModeTag == ID_CUSTOM_MODE){
if(msgNumber.getText().length()>0){
MsgNumber = Integer.parseInt(msgNumber.getText().toString());
}else{
MsgNumber = -1;
}
if(phoneNumber.getText().length()>0){
PhoneNumber = phoneNumber.getText().toString();
}else{
PhoneNumber = null;
}
if(customContent.isChecked()){
if(msgContent.getText().length()>0){
MsgContent = msgContent.getText().toString();
}else{
MsgContent = null;
}
}else{
if(wordsNumber.getText().length()>0){
WordsNumber = Integer.parseInt(wordsNumber.getText().toString());
}else{
WordsNumber = -1;
}
}
if(customContent.isChecked()&&MsgNumber>0&&PhoneNumber != null&&MsgContent != null){
progress.setCancelable(false);
progress.setMax(MsgNumber);
progress.setProgress(0);
progress.show();
new Thread(new RunCustomContentMsgInstall()).start();
}else if (!customContent.isChecked()&&MsgNumber>0&&PhoneNumber != null&&WordsNumber>0) {
progress.setCancelable(false);
progress.setMax(MsgNumber);
progress.setProgress(0);
progress.show();
new Thread(new RunAutoContentMsgInstall()).start();
}else{
AlertDialog.Builder warning = new AlertDialog.Builder(DataFactoryActivity.this);
warning.setTitle("warning");
warning.setMessage(R.string.warning);
warning.setPositiveButton("close", null);
warning.show();
}
}
}
});
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
if(ModeTag == ID_AUTO_MODE){
menu.removeItem(ID_CUSTOM_MODE);
menu.removeItem(ID_AUTO_MODE);
menu.removeItem(ID_SET);
menu.removeItem(ID_EXIT);
menu.add(0,ID_CUSTOM_MODE,0,"CustomMode");
menu.add(0,ID_EXIT,0,"Exit");
}
else if(ModeTag == ID_CUSTOM_MODE){
menu.removeItem(ID_AUTO_MODE);
menu.removeItem(ID_CUSTOM_MODE);
menu.removeItem(ID_SET);
menu.removeItem(ID_EXIT);
menu.add(0,ID_AUTO_MODE,0,"AutoMode");
menu.add(0,ID_SET,0,"Setting");
menu.add(0,ID_EXIT,0,"Exit");
}
return super.onPrepareOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()==ID_SET){
Intent intent = new Intent("android.intent.action.SETTING");
startActivityForResult(intent, 4);
}
else if(item.getItemId()==ID_EXIT){
finish();
}
else if(item.getItemId()==ID_AUTO_MODE){
addressNumber.setVisibility(View.VISIBLE);
customContent.setVisibility(View.GONE);
customContent.setChecked(false);
wordsNumber.setVisibility(View.GONE);
phoneNumber.setVisibility(View.GONE);
msgContent.setVisibility(View.GONE);
LanguageChoice.setVisibility(View.VISIBLE);
menuIntroduce.setVisibility(View.GONE);
ModeTag = ID_AUTO_MODE;
AutoModeClean();
}
else if(item.getItemId()==ID_CUSTOM_MODE){
addressNumber.setVisibility(View.GONE);
customContent.setVisibility(View.VISIBLE);
customContent.setChecked(false);
wordsNumber.setVisibility(View.VISIBLE);
phoneNumber.setVisibility(View.VISIBLE);
msgContent.setVisibility(View.GONE);
LanguageChoice.setVisibility(View.VISIBLE);
menuIntroduce.setVisibility(View.VISIBLE);
ModeTag = ID_CUSTOM_MODE;
CustomModeClean();
//重新初始化状态
TypeTag = Sms.MESSAGE_TYPE_INBOX;
StatusTag = Sms.STATUS_NONE;
ReadTag = 1;
}
return super.onOptionsItemSelected(item);
}
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case 44:
Bundle bundle = data.getExtras();
StatusTag = bundle.getInt("STATUS",Sms.STATUS_NONE);
ReadTag = bundle.getInt("READ",1);
TypeTag = bundle.getInt("TYPE",Sms.MESSA