package com.android.contactsmanager;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import com.android.contactsmanager.bean.MyContacts;
import com.android.contactsmanager.dao.ContactsManagerDbAdater;
import com.android.contactsmanager.ui.PhotoEditorView;
public class EditContact extends Activity {
ContactsManagerDbAdater contactsManagerDbAdapter;
public static final String TAG = "EditContact";
// 用来标识请求照相功能的activity
private static final int CAMERA_WITH_DATA = 3023;
// 用来标识请求gallery的activity
private static final int PHOTO_PICKED_WITH_DATA = 3021;
// 拍照的照片存储位置
private static final File PHOTO_DIR = new File(Environment
.getExternalStorageDirectory()
+ "/DCIM/Camera");
private File mCurrentPhotoFile;//照相机拍照得到的图片
private PhotoEditorView mEditor;//头像
Cursor contactInfoCursor;//联系人信息
MyContacts contactAllInfoCache=null;//缓存联系人所有信息
ArrayAdapter<String> adapter;//缓存GroupSpinner数据
//各个组件
private EditText name;//姓名
private EditText phoneNumber;//号码
private Spinner groupSpinner;//组
private Button birthdayButton;//生日
private EditText address;//住址
private EditText email;//邮箱
private EditText information;//好友描述
private Button ok;//确定
private Button cancel;//取消
//用户所有信息
String _name;
byte[] img;//头像数据
String _phoneNumber;
String _groupSpinner;
String _birthdayButton;
String _address;
String _email;
String _information;
//联系人信息的索引值
int index_name=1;
int index_contactIcon=2;
int index_telePhone=3;
int index_groupName=4;
int index_birthday=5;
int index_address=6;
int index_email=7;
int index_description=8;
//int selectedGroupNameIndex;//记录用户选择的组的索引,从新的activity返回时用到
//activity处于两种状态,插入状态或者编辑状态
private static final int STATE_INSERT=0;//插入状态
private static final int STATE_EDIT=1;//编辑状态
private int state;//记录当前的状态
// 生日,年,月,日
private int mYear;
private int mMonth;
private int mDay;
String editContactName;//缓存要编辑的联系人
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.v(TAG,"onCreate");
setContentView(R.layout.editcontact);
contactsManagerDbAdapter=new ContactsManagerDbAdater(this);
contactsManagerDbAdapter.open();
initAllComponents();//初始化所有组件
initGroupSpinnerData();
Intent intent=getIntent();
String action=intent.getAction();
if(action.equals(Intent.ACTION_INSERT)){
state=STATE_INSERT;
}else if(action.equals(Intent.ACTION_EDIT)){
state=STATE_EDIT;
//下面就该取数据库里面的数据了
editContactName=intent.getStringExtra("name");
String sql="select * from contacts where name=?";
String selectionArgs[]={editContactName};
System.out.println(editContactName);
contactInfoCursor=contactsManagerDbAdapter.getCursorBySql(sql, selectionArgs);
startManagingCursor(contactInfoCursor);
//不知道为何只有加了下面的if语句到resume方法后才会正常执行
if(contactInfoCursor!=null && contactInfoCursor.getCount()>0){
System.out.println("<<<<<<<<<<<<<<<< test >>>>>>>>>>>>>>>>>>>");
}
}else{
Log.e(TAG, "Unknown action,program will exit...");
finish();
return;
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(state == STATE_INSERT){
setTitle("新建联系人");
getNowTime();
String birthday=mYear+"-"+mMonth+"-"+mDay;
birthdayButton.setText(birthday);
}else if(state == STATE_EDIT){
setTitle("编辑联系人");
if(contactInfoCursor!=null && contactInfoCursor.getCount()>0){
System.out.println(">>>>>>>>>>>>>>>>>> gogogogo >>>>>>>>>>>>>>>>>>");
if(contactAllInfoCache==null){
contactInfoCursor.moveToFirst();
//得到数据库中的联系人信息
_name=contactInfoCursor.getString(1);
img=contactInfoCursor.getBlob(2);
_phoneNumber=contactInfoCursor.getString(3);
_groupSpinner=contactInfoCursor.getString(4);
_birthdayButton=contactInfoCursor.getString(5);
System.out.println(_birthdayButton);
initDateFromDb(_birthdayButton);
_address=contactInfoCursor.getString(6);
_email=contactInfoCursor.getString(7);
_information=contactInfoCursor.getString(8);
}else{
_name=contactAllInfoCache.getName();
//img已经在onActivityResult方法内赋了新值,这里再调用contactAllInfoCache的getContactIcon
//方法会把img给覆盖掉,所以就不再调用了
//img=contactAllInfoCache.getContactIcon();
_phoneNumber=contactAllInfoCache.getTelPhone();
_groupSpinner=contactAllInfoCache.getGroupName();
_birthdayButton=contactAllInfoCache.getBirthday();
_address=contactAllInfoCache.getAddress();
_email=contactAllInfoCache.getEmail();
_information=contactAllInfoCache.getDescription();
}
name.setText(_name);
mEditor.setPhotoBitmap(getBitmapFromByte(img));
phoneNumber.setText(_phoneNumber);
int groupIndex=adapter.getPosition(_groupSpinner);
groupSpinner.setSelection(groupIndex);
birthdayButton.setText(_birthdayButton);
address.setText(_address);
email.setText(_email);
information.setTextKeepState(_information);
System.out.println(">>>>>>>>>>>>>>>>>> end >>>>>>>>>>>>>>>>>>");
}
}
}
private void initDateFromDb(String birthdayButton2) {
System.out.println(birthdayButton2);
String args[]=birthdayButton2.split("-");
System.out.println(args.length);
mYear = Integer.valueOf(args[0]);
mMonth = Integer.valueOf(args[1]);
mDay = Integer.valueOf(args[2]);
}
//当从当前的Activity切换到另一个Activity时调用,切换过去时当前Activity并未结束(Destory)
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
Log.v(TAG, "record original data");
//缓存联系人信息
contactAllInfoCache=new MyContacts();
//得到最终用户信息
contactAllInfoCache.setName(name.getText().toString());
BitmapDrawable bd=(BitmapDrawable)mEditor.getDrawable();
Bitmap bitMap=bd.getBitmap();
contactAllInfoCache.setContactIcon(getBitmapByte(bitMap));
contactAllInfoCache.setTelPhone(phoneNumber.getText().toString());
contactAllInfoCache.setGroupName(groupSpinner.getSelectedItem().toString());
contactAllInfoCache.setBirthday(birthdayButton.getText().toString());
contactAllInfoCache.setAddress(address.getText().toString());
contactAllInfoCach