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 org.json.JSONException;
20 import org.json.JSONObject;
21 
22 import android.support.annotation.NonNull;
23 
24 import java.util.Calendar;
25 import java.util.Objects;
26 
27 /**
28  * Class represents a single alarm.
29  */
30 public class Alarm implements Comparable<Alarm> {
31 
32     public int id;
33 
34     public int month;
35 
36     public int date;
37 
38     /** Integer as a 24-hour format */
39     public int hour;
40 
41     public int minute;
42 
Alarm(int id, int month, int date, int hour, int minute)43     public Alarm(int id, int month, int date, int hour, int minute) {
44         this.id = id;
45         this.month = month;
46         this.date = date;
47         this.hour = hour;
48         this.minute = minute;
49     }
50 
Alarm()51     public Alarm() {
52     }
53 
54     /**
55      * Serialize the instance as a JSON String.
56      *
57      * @return serialized JSON String.
58      */
toJson()59     public String toJson() {
60         JSONObject jsonObject = new JSONObject();
61         try {
62             jsonObject.put("id", id);
63             jsonObject.put("month", month);
64             jsonObject.put("date", date);
65             jsonObject.put("hour", hour);
66             jsonObject.put("minute", minute);
67         } catch (JSONException e) {
68             throw new IllegalStateException("Failed to convert the object to JSON");
69         }
70         return jsonObject.toString();
71     }
72 
73     /**
74      * Parses a Json string to an {@link Alarm} instance.
75      *
76      * @param string The String representation of an alarm
77      * @return an instance of {@link Alarm}
78      */
fromJson(String string)79     public static Alarm fromJson(String string) {
80         JSONObject jsonObject;
81         Alarm alarm = new Alarm();
82         try {
83             jsonObject = new JSONObject(string);
84             alarm.id = jsonObject.getInt("id");
85             alarm.month = jsonObject.getInt("month");
86             alarm.date = jsonObject.getInt("date");
87             alarm.hour = jsonObject.getInt("hour");
88             alarm.minute = jsonObject.getInt("minute");
89         } catch (JSONException e) {
90             throw new IllegalArgumentException("Failed to parse the String: " + string);
91         }
92 
93         return alarm;
94     }
95 
96     @Override
toString()97     public String toString() {
98         return "Alarm{" +
99                 "id=" + id +
100                 ", month=" + month +
101                 ", date=" + date +
102                 ", hour=" + hour +
103                 ", minute=" + minute +
104                 '}';
105     }
106 
107     @Override
equals(Object o)108     public boolean equals(Object o) {
109         if (this == o) {
110             return true;
111         }
112         if (!(o instanceof Alarm)) {
113             return false;
114         }
115         Alarm alarm = (Alarm) o;
116         return id == alarm.id &&
117                 month == alarm.month &&
118                 date == alarm.date &&
119                 hour == alarm.hour &&
120                 minute == alarm.minute;
121     }
122 
123     @Override
hashCode()124     public int hashCode() {
125         return Objects.hash(id, month, date, hour, minute);
126     }
127 
128     @Override
compareTo(@onNull Alarm other)129     public int compareTo(@NonNull Alarm other) {
130         Calendar calendar = Calendar.getInstance();
131         calendar.set(Calendar.MONTH, month);
132         calendar.set(Calendar.DATE, date);
133         calendar.set(Calendar.HOUR_OF_DAY, hour);
134         calendar.set(Calendar.MINUTE, minute);
135 
136         Calendar otherCal = Calendar.getInstance();
137         otherCal.set(Calendar.MONTH, other.month);
138         otherCal.set(Calendar.DATE, other.date);
139         otherCal.set(Calendar.HOUR_OF_DAY, other.hour);
140         otherCal.set(Calendar.MINUTE, other.minute);
141         return calendar.compareTo(otherCal);
142     }
143 }
144