1 /* 2 * Copyright 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.directboot.alarms; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 import android.support.annotation.NonNull; 22 23 import org.json.JSONException; 24 import org.json.JSONObject; 25 26 import java.util.Calendar; 27 import java.util.Objects; 28 29 /** 30 * Class represents a single alarm. 31 */ 32 public class Alarm implements Comparable<Alarm>, Parcelable { 33 34 public int id; 35 public int month; 36 public int date; 37 /** Integer as a 24-hour format */ 38 public int hour; 39 public int minute; 40 Alarm()41 public Alarm() {} 42 Alarm(Parcel in)43 protected Alarm(Parcel in) { 44 id = in.readInt(); 45 month = in.readInt(); 46 date = in.readInt(); 47 hour = in.readInt(); 48 minute = in.readInt(); 49 } 50 51 public static final Creator<Alarm> CREATOR = new Creator<Alarm>() { 52 @Override 53 public Alarm createFromParcel(Parcel in) { 54 return new Alarm(in); 55 } 56 57 @Override 58 public Alarm[] newArray(int size) { 59 return new Alarm[size]; 60 } 61 }; 62 63 @Override describeContents()64 public int describeContents() { 65 return 0; 66 } 67 68 @Override writeToParcel(Parcel parcel, int i)69 public void writeToParcel(Parcel parcel, int i) { 70 parcel.writeInt(id); 71 parcel.writeInt(month); 72 parcel.writeInt(date); 73 parcel.writeInt(hour); 74 parcel.writeInt(minute); 75 } 76 77 /** 78 * Serialize the instance as a JSON String. 79 * @return serialized JSON String. 80 */ toJson()81 public String toJson() { 82 JSONObject jsonObject = new JSONObject(); 83 try { 84 jsonObject.put("id", id); 85 jsonObject.put("month", month); 86 jsonObject.put("date", date); 87 jsonObject.put("hour", hour); 88 jsonObject.put("minute", minute); 89 } catch (JSONException e) { 90 throw new IllegalStateException("Failed to convert the object to JSON"); 91 } 92 return jsonObject.toString(); 93 } 94 95 /** 96 * Parses a Json string to an {@link Alarm} instance. 97 * 98 * @param string The String representation of an alarm 99 * @return an instance of {@link Alarm} 100 */ fromJson(String string)101 public static Alarm fromJson(String string) { 102 JSONObject jsonObject; 103 Alarm alarm = new Alarm(); 104 try { 105 jsonObject = new JSONObject(string); 106 alarm.id = jsonObject.getInt("id"); 107 alarm.month = jsonObject.getInt("month"); 108 alarm.date = jsonObject.getInt("date"); 109 alarm.hour = jsonObject.getInt("hour"); 110 alarm.minute = jsonObject.getInt("minute"); 111 } catch (JSONException e) { 112 throw new IllegalArgumentException("Failed to parse the String: " + string); 113 } 114 115 return alarm; 116 } 117 118 @Override toString()119 public String toString() { 120 return "Alarm{" + 121 "id=" + id + 122 ", month=" + month + 123 ", date=" + date + 124 ", hour=" + hour + 125 ", minute=" + minute + 126 '}'; 127 } 128 129 @Override equals(Object o)130 public boolean equals(Object o) { 131 if (this == o) { 132 return true; 133 } 134 if (!(o instanceof Alarm)) { 135 return false; 136 } 137 Alarm alarm = (Alarm) o; 138 return id == alarm.id && 139 month == alarm.month && 140 date == alarm.date && 141 hour == alarm.hour && 142 minute == alarm.minute; 143 } 144 145 @Override hashCode()146 public int hashCode() { 147 return Objects.hash(id, month, date, hour, minute); 148 } 149 150 @Override compareTo(@onNull Alarm other)151 public int compareTo(@NonNull Alarm other) { 152 Calendar calendar = Calendar.getInstance(); 153 calendar.set(Calendar.MONTH, month); 154 calendar.set(Calendar.DATE, date); 155 calendar.set(Calendar.HOUR_OF_DAY, hour); 156 calendar.set(Calendar.MINUTE, minute); 157 158 Calendar otherCal = Calendar.getInstance(); 159 otherCal.set(Calendar.MONTH, other.month); 160 otherCal.set(Calendar.DATE, other.date); 161 otherCal.set(Calendar.HOUR_OF_DAY, other.hour); 162 otherCal.set(Calendar.MINUTE, other.minute); 163 return calendar.compareTo(otherCal); 164 } 165 } 166