package com.cn.daming;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class AlarmClockView extends View{
private MainActivity activity;
private LinearLayout mLinearLayout;
private Context context;
private DataBaseHelper dbHelper;
private Cursor cursor;
private ListView mAlarmListView;
private List<String> ids;
private List<String> times;
private List<String> repeats;
private List<String> isopens;
private List<String> kinds;
private AlarmClockAdapter alarmAdapter;
Calendar c=Calendar.getInstance();
TextView time1TextView = null;
TextView time2TextView = null;
Button setTimeButton1 = null;
Button setTimeButton2 = null;
TextView repeat1TextView = null;
TextView repeat2TextView = null;
Button repeatButton1 = null;
Button repeatButton2 = null;
CheckBox time1CheckBox = null;
CheckBox time2CheckBox = null;
String tmpS1 = "目前无设置";
String repeatString1 = "目前无设置";
String tmpS2 = "目前无设置";
String repeatString2 = "目前无设置";
boolean isOpenInt1 = false;
boolean isOpenInt2 = false;
String isOpentime1 = null;
String isOpentime2 = null;
boolean isOpenAlarm = false;
CheckBox isOpenCheckBox;
int[] repeatArray1 = {0,0,0,0,0,0,0};
int[] repeatArray2 = {0,0,0,0,0,0,0};
boolean repeat_1_1 = false;
boolean repeat_1_2 = false;
boolean repeat_1_3 = false;
boolean repeat_1_4 = false;
boolean repeat_1_5 = false;
boolean repeat_1_6 = false;
boolean repeat_1_7 = false;
boolean repeat_2_1 = false;
boolean repeat_2_2 = false;
boolean repeat_2_3 = false;
boolean repeat_2_4 = false;
boolean repeat_2_5 = false;
boolean repeat_2_6 = false;
boolean repeat_2_7 = false;
public AlarmClockView(final Context context,MainActivity activity, LinearLayout linearLayout) {
super(context);
this.context = context;
this.activity = activity;
this.mLinearLayout = linearLayout;
View alarmView = View.inflate(context, R.layout.alarm_listview, null);
mLinearLayout.addView(alarmView);
mAlarmListView = (ListView)alarmView.findViewById(R.id.alarm_list);
mAlarmListView.setCacheColorHint(0);
dbHelper = new DataBaseHelper(context);
refreshDBHelper();
mAlarmListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
showAlarmDialog(position); //点击闹钟出现设置页面
}
});
}
public void refreshDBHelper()
{
cursor = dbHelper.selectAlarmColock();
ids = new ArrayList<String>();
times = new ArrayList<String>();
repeats = new ArrayList<String>();
isopens = new ArrayList<String>();
kinds = new ArrayList<String>();
int count = cursor.getCount();
if(count==0){
String[] tempStr = new String[4];
for(int i=1;i<=2;i++){
dbHelper.insertAlarmColock(tempStr);
}
int count2 = cursor.getCount();
if(count2>0){
for(int i=0;i<count2;i++){
cursor.moveToPosition(i);
ids.add(cursor.getString(0));
times.add(cursor.getString(1));
repeats.add(cursor.getString(2));
isopens.add(cursor.getString(3));
kinds.add(cursor.getString(4));
}
}
cursor.close();
dbHelper.close();
refreshDBHelper();
Toast.makeText(context, R.string.not_dbcursor_values, Toast.LENGTH_SHORT).show();
}
else if(count>0){
for(int i=0;i<count;i++){
cursor.moveToPosition(i);
ids.add(cursor.getString(0));
times.add(cursor.getString(1));
repeats.add(cursor.getString(2));
isopens.add(cursor.getString(3));
kinds.add(cursor.getString(4));
}
}
alarmAdapter = new AlarmClockAdapter(context,ids,times,repeats,isopens,kinds);
mAlarmListView.setAdapter(alarmAdapter);
cursor.close();
dbHelper.close();
}
public void showAlarmDialog(int position){
//设置闹钟弹窗
dbHelper = new DataBaseHelper(context);
cursor = dbHelper.selectAlarmColock();
int count = cursor.getCount();
if(position == 0){
LayoutInflater factory = LayoutInflater.from(context);
final View alarm1View = factory.inflate(R.layout.time_repeat_dialog, null);
time1TextView = (TextView)alarm1View.findViewById(R.id.text); //标题设置闹钟
setTimeButton1 = (Button)alarm1View.findViewById(R.id.mButton);//设置时间
repeat1TextView = (TextView)alarm1View.findViewById(R.id.repeattext);
repeatButton1 = (Button)alarm1View.findViewById(R.id.repeatButton); //设置重复
time1CheckBox = (CheckBox)alarm1View.findViewById(R.id.isopen_check);
String isOpen1 = "关";
if(count==2){
for(int i=0;i<count;i++){
if(i == 0){
cursor.moveToPosition(i);
if(cursor.getString(1)==null){
time1TextView.setText(tmpS1);
}else{
time1TextView.setText(cursor.getString(1));
tmpS1 = cursor.getString(1);
}
if((cursor.getString(2))==null){
repeat1TextView.setText(repeatString1);
}else{
repeat1TextView.setText(cursor.getString(2));
repeatString1 = cursor.getString(2);
}
isOpen1 = cursor.getString(3);
if(isOpen1!=null){
if(isOpen1.equals("开")){
time1CheckBox.setChecked(true);
isOpenInt1 = true;
}else{
time1CheckBox.setChecked(false);
isOpenInt1 = false;
}
}
}
}
}
time1CheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){//是否开启闹钟
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(arg0.isChecked()){
isOpenInt1 = true;
}else{
isOpenInt1 = false;
}
}
});
setTimeButton1.setOnClickListener(new OnClickListener(){//设置时间
public void onClick(View arg0) {
c.setTimeInMillis(System.currentTimeMillis());//设置当前时间
int mHour=c.get(Calendar.HOUR_OF_DAY);
int mMinute=c.get(Calendar.MINUTE);
int mDay=c.get(Calendar.DAY_OF_WEEK);
new TimePickerDialog(context,
new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet(TimePicker view,int hourOfDay,
int minute)
{
c.setTimeInMillis(System.currentTimeMillis