2015-08-03 23:09:49 +02:00
package nodomain.freeyourgadget.gadgetbridge.impl ;
2015-06-24 20:14:08 +02:00
2015-06-25 14:34:21 +02:00
import android.os.Parcel ;
2015-10-18 23:52:59 +02:00
import android.support.annotation.NonNull ;
2015-06-24 20:14:08 +02:00
import java.util.Calendar ;
2015-06-26 17:22:42 +02:00
import java.util.HashSet ;
import java.util.Iterator ;
import java.util.Set ;
2015-06-24 20:14:08 +02:00
2015-08-03 23:09:49 +02:00
import nodomain.freeyourgadget.gadgetbridge.GBApplication ;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm ;
2016-04-25 23:39:03 +02:00
import nodomain.freeyourgadget.gadgetbridge.util.Prefs ;
2015-06-24 20:14:08 +02:00
2015-08-03 23:09:49 +02:00
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_ALARMS ;
2015-06-26 17:22:42 +02:00
2015-08-03 23:09:49 +02:00
public class GBAlarm implements Alarm {
2015-06-25 14:34:21 +02:00
2015-11-23 23:04:46 +01:00
private final int index ;
2015-06-24 20:14:08 +02:00
private boolean enabled ;
private boolean smartWakeup ;
private int repetition ;
private int hour ;
private int minute ;
2015-06-30 12:09:29 +02:00
public static final String [ ] DEFAULT_ALARMS = { " 2,false,false,0,15,30 " , " 1,false,false,96,8,0 " , " 0,false,true,31,7,30 " } ;
2015-06-26 17:22:42 +02:00
2015-06-24 20:14:08 +02:00
public GBAlarm ( int index , boolean enabled , boolean smartWakeup , byte repetition , int hour , int minute ) {
this . index = index ;
this . enabled = enabled ;
this . smartWakeup = smartWakeup ;
this . repetition = repetition ;
this . hour = hour ;
this . minute = minute ;
}
2015-06-30 12:09:29 +02:00
public GBAlarm ( String fromPreferences ) {
2015-06-24 20:14:08 +02:00
String [ ] tokens = fromPreferences . split ( " , " ) ;
//TODO: sanify the string!
this . index = Integer . parseInt ( tokens [ 0 ] ) ;
this . enabled = Boolean . parseBoolean ( tokens [ 1 ] ) ;
this . smartWakeup = Boolean . parseBoolean ( tokens [ 2 ] ) ;
2015-06-30 12:09:29 +02:00
this . repetition = Integer . parseInt ( tokens [ 3 ] ) ;
2015-06-24 20:14:08 +02:00
this . hour = Integer . parseInt ( tokens [ 4 ] ) ;
this . minute = Integer . parseInt ( tokens [ 5 ] ) ;
}
2015-06-25 14:34:21 +02:00
private static GBAlarm readFromParcel ( Parcel pc ) {
int index = pc . readInt ( ) ;
boolean enabled = Boolean . parseBoolean ( pc . readString ( ) ) ;
boolean smartWakeup = Boolean . parseBoolean ( pc . readString ( ) ) ;
int repetition = pc . readInt ( ) ;
int hour = pc . readInt ( ) ;
int minute = pc . readInt ( ) ;
2015-06-30 12:09:29 +02:00
return new GBAlarm ( index , enabled , smartWakeup , ( byte ) repetition , hour , minute ) ;
2015-06-25 14:34:21 +02:00
}
@Override
public boolean equals ( Object o ) {
if ( o instanceof GBAlarm ) {
2015-06-30 12:09:29 +02:00
GBAlarm comp = ( GBAlarm ) o ;
2015-06-25 14:34:21 +02:00
return comp . getIndex ( ) = = getIndex ( ) ;
} else {
return false ;
}
}
2015-06-26 17:22:42 +02:00
@Override
public int hashCode ( ) {
return getIndex ( ) ;
}
2015-06-25 14:34:21 +02:00
@Override
public int describeContents ( ) {
return 0 ;
}
@Override
public void writeToParcel ( Parcel dest , int flags ) {
dest . writeInt ( this . index ) ;
dest . writeString ( String . valueOf ( this . enabled ) ) ;
dest . writeString ( String . valueOf ( this . smartWakeup ) ) ;
dest . writeInt ( this . repetition ) ;
dest . writeInt ( this . hour ) ;
dest . writeInt ( this . minute ) ;
}
2015-06-26 17:22:42 +02:00
@Override
2015-10-18 23:52:59 +02:00
public int compareTo ( @NonNull Alarm another ) {
2015-08-03 23:09:49 +02:00
if ( this . getIndex ( ) < another . getIndex ( ) ) {
2015-06-26 17:22:42 +02:00
return - 1 ;
2015-08-03 23:09:49 +02:00
} else if ( this . getIndex ( ) > another . getIndex ( ) ) {
2015-06-26 17:22:42 +02:00
return 1 ;
}
return 0 ;
}
2015-08-03 23:09:49 +02:00
@Override
2015-06-24 20:14:08 +02:00
public int getIndex ( ) {
return this . index ;
}
2015-08-03 23:09:49 +02:00
@Override
2015-06-24 20:14:08 +02:00
public String getTime ( ) {
2015-06-30 12:09:29 +02:00
return String . format ( " %02d " , this . hour ) + " : " + String . format ( " %02d " , this . minute ) ;
2015-06-24 20:14:08 +02:00
}
2015-06-30 12:09:29 +02:00
public int getHour ( ) {
2015-06-24 20:14:08 +02:00
return this . hour ;
}
2015-06-30 12:09:29 +02:00
public int getMinute ( ) {
2015-06-24 20:14:08 +02:00
return this . minute ;
}
2015-06-30 12:09:29 +02:00
2015-08-03 23:09:49 +02:00
@Override
2015-06-24 20:14:08 +02:00
public Calendar getAlarmCal ( ) {
2015-06-27 18:50:21 +02:00
2015-06-24 20:14:08 +02:00
Calendar alarm = Calendar . getInstance ( ) ;
2015-06-27 18:50:21 +02:00
Calendar now = Calendar . getInstance ( ) ;
2015-06-24 20:14:08 +02:00
alarm . set ( Calendar . HOUR_OF_DAY , this . hour ) ;
alarm . set ( Calendar . MINUTE , this . minute ) ;
2015-06-27 18:50:21 +02:00
if ( now . after ( alarm ) & & repetition = = ALARM_ONCE ) {
//if the alarm is in the past set it to tomorrow
alarm . add ( Calendar . DATE , 1 ) ;
}
2015-06-24 20:14:08 +02:00
return alarm ;
}
2015-08-03 23:09:49 +02:00
@Override
2015-06-24 20:14:08 +02:00
public boolean isEnabled ( ) {
return this . enabled ;
}
2015-08-03 23:09:49 +02:00
@Override
2015-06-24 20:14:08 +02:00
public boolean isSmartWakeup ( ) {
return this . smartWakeup ;
}
2015-08-03 23:09:49 +02:00
@Override
2015-06-24 20:14:08 +02:00
public boolean getRepetition ( int dow ) {
return ( this . repetition & dow ) > 0 ;
}
2015-08-03 23:09:49 +02:00
@Override
2015-06-24 20:14:08 +02:00
public int getRepetitionMask ( ) {
return this . repetition ;
}
public String toPreferences ( ) {
2015-06-30 12:09:29 +02:00
return String . valueOf ( this . index ) + ',' +
String . valueOf ( this . enabled ) + ',' +
String . valueOf ( this . smartWakeup ) + ',' +
String . valueOf ( this . repetition ) + ',' +
String . valueOf ( this . hour ) + ',' +
2015-06-24 20:14:08 +02:00
String . valueOf ( this . minute ) ;
}
public void setSmartWakeup ( boolean smartWakeup ) {
this . smartWakeup = smartWakeup ;
}
public void setRepetition ( boolean mon , boolean tue , boolean wed , boolean thu , boolean fri , boolean sat , boolean sun ) {
this . repetition = ALARM_ONCE |
( mon ? ALARM_MON : 0 ) |
( tue ? ALARM_TUE : 0 ) |
( wed ? ALARM_WED : 0 ) |
( thu ? ALARM_THU : 0 ) |
( fri ? ALARM_FRI : 0 ) |
( sat ? ALARM_SAT : 0 ) |
( sun ? ALARM_SUN : 0 ) ;
}
public void setHour ( int hour ) {
this . hour = hour ;
}
public void setMinute ( int minute ) {
this . minute = minute ;
}
public void setEnabled ( boolean enabled ) {
this . enabled = enabled ;
}
2015-06-26 17:22:42 +02:00
public void store ( ) {
2016-04-25 23:39:03 +02:00
Prefs prefs = GBApplication . getPrefs ( ) ;
Set < String > preferencesAlarmListSet = prefs . getStringSet ( PREF_MIBAND_ALARMS , new HashSet < String > ( ) ) ;
2015-06-26 17:22:42 +02:00
//the old Set cannot be updated in place see http://developer.android.com/reference/android/content/SharedPreferences.html#getStringSet%28java.lang.String,%20java.util.Set%3Cjava.lang.String%3E%29
2015-11-23 22:46:12 +01:00
Set < String > newPrefs = new HashSet < > ( preferencesAlarmListSet ) ;
2015-06-26 17:22:42 +02:00
Iterator < String > iterator = newPrefs . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
String alarmString = iterator . next ( ) ;
2015-06-30 12:09:29 +02:00
if ( this . equals ( new GBAlarm ( alarmString ) ) ) {
2015-06-26 17:22:42 +02:00
iterator . remove ( ) ;
2015-06-27 18:56:05 +02:00
break ;
2015-06-26 17:22:42 +02:00
}
}
newPrefs . add ( this . toPreferences ( ) ) ;
2016-04-25 23:39:03 +02:00
prefs . getPreferences ( ) . edit ( ) . putStringSet ( PREF_MIBAND_ALARMS , newPrefs ) . apply ( ) ;
2015-06-24 20:14:08 +02:00
}
2015-06-25 14:34:21 +02:00
public static final Creator CREATOR = new Creator ( ) {
public GBAlarm createFromParcel ( Parcel in ) {
return readFromParcel ( in ) ;
}
public GBAlarm [ ] newArray ( int size ) {
return new GBAlarm [ size ] ;
}
} ;
2015-06-24 20:14:08 +02:00
}