package com.voice.secretary;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.TimeZone;
import kankan.wheel.widget.ArrayWheelAdapter;
import kankan.wheel.widget.DateWheelAdapter;
import kankan.wheel.widget.NumericWheelAdapter;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SetDetailsActivity extends Activity {
// date transfer format
private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
// format
private String format = "%1$tm/%1$td";
private String dateResult;
public static final String FINAL_TIME = "finalTime";
public static final String FINAL_REPEAT_VALUE = "finalRepeatValue";
public static final String EXACTLY_SETTING = "exactdate";
public static final String QUICK_SETTING = "quickdate";
private static HashMap<Integer, String> weekDayMap = new HashMap<Integer, String>();
private static String currentYear = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
private static HashMap<String, Integer> quickDateValues = new HashMap<String,Integer>();
private static HashMap<String, String> repeatValues = new HashMap<String,String>();
private static ArrayWheelAdapter<String> quickDateWheelValues;
private static ArrayWheelAdapter<String> repeatWheelValues;
private static String[] quickDateArray = new String[]{"5 mins later",
"10 mins later",
"15 mins later",
"30 mins later",
"45 mins later",
"1 hour later",
"2 hours later",
"3 hours later",
"4 hours later",
"5 hours later",
"6 hours later",
"same time tomorrow"
};
private static String[] repeatArray = new String[]{"No Repeat",
//"Every Hour",
"Every Day",
"Every Week",
"Every Month",
"Every Year"
};
private WheelView exactDate;
private WheelView exactHours;
private WheelView exactMins;
private WheelView exactWeekDay;
private WheelView quickDate;
private WheelView repeat;
private LinearLayout quickDateLayout;
private LinearLayout exactDateLayout;
private LinearLayout repeatLayout;
private Button quickButton;
private Button exactButton;
private Button repeatButton;
private Button saveButton;
private TextView showTime;
static {
// put week days
weekDayMap.put(1, "Mon");
weekDayMap.put(2, "Tue");
weekDayMap.put(3, "Wed");
weekDayMap.put(4, "Thu");
weekDayMap.put(5, "Fri");
weekDayMap.put(6, "Sat");
weekDayMap.put(0, "Sun");
// put quick values
quickDateValues.put("5 mins later", 5);
quickDateValues.put("10 mins later", 10);
quickDateValues.put("15 mins later", 15);
quickDateValues.put("30 mins later", 30);
quickDateValues.put("45 mins later", 45);
quickDateValues.put("1 hour later", 60);
quickDateValues.put("2 hours later", 120);
quickDateValues.put("3 hours later", 180);
quickDateValues.put("4 hours later", 240);
quickDateValues.put("5 hours later", 300);
quickDateValues.put("6 hours later", 360);
quickDateValues.put("same time tomorrow", 1440);
// put repeat values
repeatValues.put("No Repeat", null);
//repeatValues.put("Every Hour", "FREQ=HOURLY;INTERVAL=2");
repeatValues.put("Every Day", "FREQ=DAILY;WKST=SU");
repeatValues.put("Every Week", "FREQ=WEEKLY;WKST=SU");
repeatValues.put("Every Month", "FREQ=MONTHLY;WKST=SU");
repeatValues.put("Every Year", "FREQ=YEARLY;WKST=SU");
quickDateWheelValues = new ArrayWheelAdapter<String>(quickDateArray);
repeatWheelValues = new ArrayWheelAdapter<String>(repeatArray);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setdetail);
quickButton = (Button)findViewById(R.id.quickbutton);
exactButton = (Button)findViewById(R.id.exactbutton);
repeatButton = (Button)findViewById(R.id.repeatbutton);
saveButton = (Button)findViewById(R.id.finishsetdetail);
showTime = (TextView)findViewById(R.id.showtime);
quickDateLayout = (LinearLayout)findViewById(R.id.quickdatelayout);
exactDateLayout = (LinearLayout)findViewById(R.id.exactdatelayout);
repeatLayout = (LinearLayout)findViewById(R.id.repeatlayout);
// set default screen
dateResult = QUICK_SETTING;
displayExactDateView();
showTime.setText(TimeZone.getDefault().getID());
displayQuickTime();
displayRepeat();
// have quick set as default setting
quickDateLayout.setVisibility(View.VISIBLE);
repeatLayout.setVisibility(View.INVISIBLE);
exactDateLayout.setVisibility(View.INVISIBLE);
// quick date listener
quickButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
dateResult = QUICK_SETTING;
quickDateLayout.setVisibility(View.VISIBLE);
repeatLayout.setVisibility(View.INVISIBLE);
exactDateLayout.setVisibility(View.INVISIBLE);
showTime.setText(getQuickSelectDate().toLocaleString());
}});
// exactly date listener
exactButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
dateResult = EXACTLY_SETTING;
quickDateLayout.setVisibility(View.INVISIBLE);
repeatLayout.setVisibility(View.INVISIBLE);
exactDateLayout.setVisibility(View.VISIBLE);
showTime.setText(getSelectExactDate().toLocaleString());
}});
// repeat listener
repeatButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
quickDateLayout.setVisibility(View.INVISIBLE);
repeatLayout.setVisibility(View.VISIBLE);
exactDateLayout.setVisibility(View.INVISIBLE);
}});
// Save listener
saveButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(SetDetailsActivity.this, CalendarSettingActivity.class);
// put final time to bundle
if (dateResult.equals(QUICK_SETTING)) {
intent.putExtra(FINAL_TIME, getQuickSelectDate());
} else if(dateResult.equals(EXACTLY_SETTING)) {
intent.putExtra(FINAL_TIME, getSelectExactDate());
} else {
// This shouldn't happen
intent.putExtra(FINAL_TIME, new Date());
}
// put repeat
intent.putExtra(FINAL_REPEAT_VALUE, getRepeat());
startActivity(intent);
}});
}
/**
* Display exact date time
*/
private void displayExactDateView() {
Date default_min_value = null;
Date default_max_value = null;
try {
default_min_value = dateFormat.parse("01/01/"+currentYear);
default_max_value = dateFormat.parse("12/31/"+currentYear);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
exactDate = (WheelView) findViewById(R.id.date);
exactDate.setAdapter(new DateWheelAdapter(default_min_value, default_max_value, format));
exactDate.setCyclic(true);
exactHours = (WheelView) findViewById(R.id.hour);
exactHours.setAdapter(new NumericWheelAdapter(0, 23, "%02d"));
exactHours.setCyclic(true);
exactHours.setLabel(":");
exactMins = (WheelView) findViewById(R.id.min1);
exactMins.setAdapter(new NumericWheelAdapter(0, 59, "%02d"));
exactMins.setCyclic(true);
exactWeekDay = (WheelView) findViewById(R.id.week1);
// set current time
TimeZone time = TimeZone.getDefault();
TimeZone.setDefault(time);
Calendar c = Calendar.getInstance();
int curDate = c.get(Calendar.DAY_OF_YEAR) -1 ;
int curHours = c.get(Calendar.HOUR_OF_DAY);
int curMins = c.get(Calendar.MINUTE);
int curWeekDay = c.get(Calendar.DAY_OF_WEEK) - 1;
exactDate.setCurrentItem(curDate);
exactHours.setCurrentItem(curHours);
exactMins.setCurrentItem(curMins);
exactWeekDay.setAdapter(new ArrayWheelAdapter<String>(new String[]{weekDayMap.get(curWeekDay)}));
OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
public void onScrollingFinished(WheelView wheel) {
try {
// Get current date string according the newValue(index)
String newDate = exactDate.getAdapter().getItem(exactDate.getCurrentItem());
Log.v("sssssssssssssssssssssssssss", newDate);
exactWeekDay.getAdapter().getItem(0);
if (!newDate.equalsIgnoreCase("Today")) {
exactWeekDay.setAdapter(new ArrayWheelAdapter<String>(new String[]{weekDayMap.get(dateFormat.parse(newDate+"/" + currentYear).getDay())}));
} else {
exactWeekDay.setAdapter(new ArrayWheelAdapter<String>(new String[]{weekDayMap.get(new Date().getDay())}));
}
showTime.setText(getSelectExactDate().toLocaleString());
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("###########Error parse date", e.getMessage());
}
}
public void onScrollingStarted(WheelView wheel) {
// TODO Auto-generated method stub
}
};
OnWheelScrollListener timeScrollListener = new OnWheelScrollListener() {
public void onScrollingFinished(WheelView wheel) {
showTime.setText(getSelectExactDate().toLocaleString());
}
public void onScrollingStarted(WheelView wheel) {
// TODO Auto-generated method stub
}
};
exactHours.addScrollingListener(timeScrollListener);
exactMins.addScrollingListener(timeScrollListener);
exactDate.addScrollingListener(scrollListener);
}
/**
* Display quick time picker
*/
private void displayQuickTime() {
quickDate = (WheelView)findViewById(R.id.quickdateview);
quickDate.setAdapter(quickDateWheelValues);
OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
public void onScrollingFinished(WheelView wheel) {
showTime.setText(getQuickSelectDate().toLocaleString());
}
public void onScrollingStarted(WheelView wheel) {
// TODO Auto-generated method stub
}
};
quickDate.addScrollingListener(scrollListener);
}
/**
* Display repeat picker
*/
private void displayRepeat() {
repeat = (WheelView)findViewById(R.id.repeatview);
repeat.setAdapter(repeatWheelValues);
}
/**
* Get the exact selected date
* @return
*/
public Date getSelectExactDate() {
String selectDate = exactDate.getAdapter().getItem(exactDate.getCurrentItem());
String selectHour = exactHours.getAdapter().getItem(exactHours.getCurrentItem());
String selectMins = exactMins.getAdapter().getItem(exactMins.getCurrentItem());
Calendar myCalendar = Calendar.getInstance();
myCalendar.set(Calendar.YEAR, Integer.valueOf(currentYear));
if (null != selectDate && !"".equals(selectDate)) {
if (!selectDate.equalsIgnoreCase("Today")) {
String[] monDate = selectDate.split("/");
myCalendar.set(Calendar.MONTH, Integer.valueOf(monDate[0]) - 1);
myCalendar.set(Calendar.DATE, Integer.valueOf(monDate[1]));
}
}
myCalendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(selectHour));
myCalendar.set(Calendar.MINUTE, Integer.valueOf(selectMins));
return myCalendar.getTime();
}
/**
* Get quick selected date time
* @return
*/
public Date getQuickSelectDate() {
String quickDateKey = quickDate.getAdapter().getItem(quickDate.getCurrentItem());
Calendar currentTime = Calendar.getInstance();
currentTime.add(Calendar.MINUTE, quickDateValues.get(quickDateKey));
return currentTime.getTime();
}
/**
* Get repeat value
* @return the value of current repeat key
*/
public String getRepeat() {
String currentRepeat = repeat.getAdapter().getItem(repeat.getCurrentItem());
return repeatValues.get(currentRepeat);
}
}
backup - calendar
最新推荐文章于 2024-09-09 18:52:34 发布