aboutsummaryrefslogtreecommitdiff
path: root/security/DirectBoot/Application/src/main/java/com/example/android/directboot/alarms/Alarm.java
blob: 27ca5c4534400ad823a499180fb0e366b73c066d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.directboot.alarms;

import org.json.JSONException;
import org.json.JSONObject;

import android.support.annotation.NonNull;

import java.util.Calendar;
import java.util.Objects;

/**
 * Class represents a single alarm.
 */
public class Alarm implements Comparable<Alarm> {

    public int id;

    public int month;

    public int date;

    /** Integer as a 24-hour format */
    public int hour;

    public int minute;

    public Alarm(int id, int month, int date, int hour, int minute) {
        this.id = id;
        this.month = month;
        this.date = date;
        this.hour = hour;
        this.minute = minute;
    }

    public Alarm() {
    }

    /**
     * Serialize the instance as a JSON String.
     *
     * @return serialized JSON String.
     */
    public String toJson() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", id);
            jsonObject.put("month", month);
            jsonObject.put("date", date);
            jsonObject.put("hour", hour);
            jsonObject.put("minute", minute);
        } catch (JSONException e) {
            throw new IllegalStateException("Failed to convert the object to JSON");
        }
        return jsonObject.toString();
    }

    /**
     * Parses a Json string to an {@link Alarm} instance.
     *
     * @param string The String representation of an alarm
     * @return an instance of {@link Alarm}
     */
    public static Alarm fromJson(String string) {
        JSONObject jsonObject;
        Alarm alarm = new Alarm();
        try {
            jsonObject = new JSONObject(string);
            alarm.id = jsonObject.getInt("id");
            alarm.month = jsonObject.getInt("month");
            alarm.date = jsonObject.getInt("date");
            alarm.hour = jsonObject.getInt("hour");
            alarm.minute = jsonObject.getInt("minute");
        } catch (JSONException e) {
            throw new IllegalArgumentException("Failed to parse the String: " + string);
        }

        return alarm;
    }

    @Override
    public String toString() {
        return "Alarm{" +
                "id=" + id +
                ", month=" + month +
                ", date=" + date +
                ", hour=" + hour +
                ", minute=" + minute +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Alarm)) {
            return false;
        }
        Alarm alarm = (Alarm) o;
        return id == alarm.id &&
                month == alarm.month &&
                date == alarm.date &&
                hour == alarm.hour &&
                minute == alarm.minute;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, month, date, hour, minute);
    }

    @Override
    public int compareTo(@NonNull Alarm other) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, date);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);

        Calendar otherCal = Calendar.getInstance();
        otherCal.set(Calendar.MONTH, other.month);
        otherCal.set(Calendar.DATE, other.date);
        otherCal.set(Calendar.HOUR_OF_DAY, other.hour);
        otherCal.set(Calendar.MINUTE, other.minute);
        return calendar.compareTo(otherCal);
    }
}