1.分析思路
<1>在定时开关机功能上限制开关机时间差在10分钟以内需要考虑到一下情况:
1.开机或关机只有一个被打开,则允许设置
2.开机和关机功能都打开并且均为一次性操作,且时间差大于10,则允许设置
3.开机(关机)一次性,而关机(开机)操作,且时间差大于10,则允许设置
4.重复周天操作,且时间差大于10,则允许设置
<2>在保存设置时间时限制,和打开关闭界面进行限制
2.功能实现
AlarmClock.java
private boolean verifyConflict(Alarm mAlarm){
Alarm verAlarm;
if(mAlarm.mId==1){
verAlarm= Alarms.getAlarm(getContentResolver(), 2);
}else{
verAlarm= Alarms.getAlarm(getContentResolver(), 1);
}
if(verAlarm == null){
return false;
}
if(isRepeat(verAlarm,mAlarm)){
return true;
}else{
return false;
}
}
private boolean isRepeat(Alarm verAlarm,Alarm mAlarm){
boolean[] mDays = verAlarm.mDaysOfWeek.getBooleanArray();
boolean[] days =mAlarm.mDaysOfWeek.getBooleanArray();
boolean common=false;
Calendar c=Calendar.getInstance();
int today = (c.get(Calendar.DAY_OF_WEEK) + 5) % 7;
boolean tenMore=(Math.abs((verAlarm.mHour*60+verAlarm.mMinutes)-(mAlarm.mHour*60+mAlarm.mMinutes))>=10);
for(int i=0;i<days.length;i++){
for(int j=0;j<mDays.length;j++){
if(i==j&&days[i]==true&&mDays[j]==true){
common=true;
}
}
}
if(!verAlarm.mEnabled){
return false;
}
if(verAlarm.mDaysOfWeek.getCoded()==0&&mAlarm.mDaysOfWeek.getCoded()==0){
if(tenMore)
return false;
else
return true;
}
if((mAlarm.mDaysOfWeek.getCoded()==0&&mDays[today])||(verAlarm.mDaysOfWeek.getCoded()==0&&days[today])){
if(tenMore)
return false;
else
return true;
}
if(common){
if(tenMore)
return false;
else
return true;
}
return false;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Xlog.d(TAG, "bindView");
final Alarm alarm = new Alarm(cursor);
final Context cont = context;
CheckBox onButton = (CheckBox) view.findViewById(R.id.alarmButton);
if (onButton != null) {
onButton.setChecked(alarm.mEnabled);
onButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = ((CheckBox) v).isChecked();
Alarms.enableAlarm(cont, alarm.mId, isChecked);
if (isChecked) {
//+Bug285393,huangyabin.wt,ADD,20140901,The interval between power on/off schedule should be at least 10 minutes
if(verifyConflict(alarm)){
Toast.makeText(cont,R.string.interval,Toast.LENGTH_SHORT).show();
Alarms.enableAlarm(cont, alarm.mId, !isChecked);
((CheckBox) v).setChecked(!isChecked);
}else{
SetAlarm.popAlarmSetToast(cont, alarm.mHour, alarm.mMinutes, alarm.mDaysOfWeek, alarm.mId);
}
//-Bug285393,huangyabin.wt,ADD,20140901,The interval between power on/off schedule should be at least 10 minutes
}
}
});
}
ImageView onOffView = (ImageView) view.findViewById(R.id.power_on_off);
if (onOffView != null) {
onOffView.setImageDrawable(getResources().getDrawable(
(alarm.mId == 1) ? R.drawable.ic_settings_schpwron : R.drawable.ic_settings_schpwroff));
}
DigitalClock digitalClock = (DigitalClock) view.findViewById(R.id.digitalClock);
// set the alarm text
final Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, alarm.mHour);
c.set(Calendar.MINUTE, alarm.mMinutes);
if (digitalClock != null) {
digitalClock.updateTime(c);
}
// Set the repeat text or leave it blank if it does not repeat.
TextView daysOfWeekView = (TextView) digitalClock.findViewById(R.id.daysOfWeek);
final String daysOfWeekStr = alarm.mDaysOfWeek.toString(context, false);
if (daysOfWeekView != null) {
if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) {
daysOfWeekView.setText(daysOfWeekStr);
daysOfWeekView.setVisibility(View.VISIBLE);
} else {
daysOfWeekView.setVisibility(View.GONE);
}
}
}
};
SetAlarm.java
private boolean verifyConflict(){
Alarm verAlarm;
if(mId==1){
verAlarm= Alarms.getAlarm(getContentResolver(), 2);
}else{
verAlarm= Alarms.getAlarm(getContentResolver(), 1);
}
if(verAlarm == null){
return false;
}
if(isRepeat(verAlarm)){
return true;
}else{
return false;
}
}
private boolean isRepeat(Alarm verAlarm){
boolean[] verDays = verAlarm.mDaysOfWeek.getBooleanArray();
boolean[] mDays =mRepeatPref.getDaysOfWeek().getBooleanArray();
int mCoded=mRepeatPref.getDaysOfWeek().getCoded();
int verCoded=verAlarm.mDaysOfWeek.getCoded();
boolean common=false;
Calendar c=Calendar.getInstance();
int today = (c.get(Calendar.DAY_OF_WEEK) + 5) % 7;
boolean tenMore=(Math.abs((verAlarm.mHour*60+verAlarm.mMinutes)-(mHour*60+mMinutes))>=10);
for(int i=0;i<mDays.length;i++){
for(int j=0;j<verDays.length;j++){
if(i==j&&mDays[i]==true&&verDays[j]==true){
common=true;
}
}
}
if(!verAlarm.mEnabled){
return false;
}
if(verCoded==0&&mCoded==0){
if(tenMore)
return false;
else
return true;
}
if((mCoded==0&&verDays[today])||(verCoded==0&&mDays[today])){
if(tenMore)
return false;
else
return true;
}
if(common){
if(tenMore)
return false;
else
return true;
}
return false;
}
private void updateTime() {
Xlog.d(TAG, "updateTime " + mId);
mTimePref.setSummary(Alarms.formatTime(this, mHour, mMinutes, mRepeatPref.getDaysOfWeek()));
}
private void saveAlarm() {
final String alert = Alarms.ALARM_ALERT_SILENT;
//mEnabled |= mRepeatPref.mIsPressedPositive;
if(verifyConflict()){
mEnabled=false;
}else{
mEnabled = true;
}
Alarms.setAlarm(this, mId, mEnabled, mHour, mMinutes, mRepeatPref.getDaysOfWeek(), true, "", alert);
if (mEnabled) {
popAlarmSetToast(this.getApplicationContext(), mHour, mMinutes, mRepeatPref.getDaysOfWeek(), mId);
}else{
Toast.makeText(this.getApplicationContext(),R.string.interval,Toast.LENGTH_SHORT).show();
}
}
注!红色部分为功能实现的代码
3.测试建议
设置打开定时开关机功能