admin
2021-03-13 7becc97c5bfdd827b9a999c26746bb8e8bc3e25c
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.tencent.xinge;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Vector;
 
import org.json.JSONArray;
import org.json.JSONObject;
 
public class Message {
    public static final int TYPE_NOTIFICATION  = 1;
    public static final int TYPE_MESSAGE = 2;
    
    public Message()
    {
        this.m_title = "";
        this.m_content = "";
        this.m_sendTime = "2013-12-20 18:31:00";
        this.m_acceptTimes = new Vector<TimeInterval>();
        this.m_multiPkg = 0;
        this.m_raw = "";
        this.m_loopInterval = -1;
        this.m_loopTimes = -1;
        this.m_action = new ClickAction();
        this.m_style = new Style(0);
    }
    
    public void setTitle(String title)
    {
        this.m_title = title;
    }
    public void setContent(String content)
    {
        this.m_content = content;
    }
    public void setExpireTime(int expireTime)
    {
        this.m_expireTime = expireTime;
    }
    public int getExpireTime()
    {
        return this.m_expireTime;
    }
    public void setSendTime(String sendTime)
    {
        this.m_sendTime = sendTime;
    }
    public String getSendTime()
    {
        return this.m_sendTime;
    }
    public void addAcceptTime(TimeInterval acceptTime)
    {
        this.m_acceptTimes.add(acceptTime);
    }
    public String acceptTimeToJson()
    {
        JSONArray json_arr = new JSONArray();
        for (TimeInterval ti : m_acceptTimes) 
        {
            JSONObject jtmp = ti.toJsonObject();
            json_arr.put(jtmp);
        }
        return json_arr.toString();
    }
    public JSONArray acceptTimeToJsonArray()
    {
        JSONArray json_arr = new JSONArray();
        for (TimeInterval ti : m_acceptTimes) 
        {
            JSONObject jtmp = ti.toJsonObject();
            json_arr.put(jtmp);
        }
        return json_arr;
    }
    public void setType(int type)
    {
        this.m_type = type;
    }
    public int getType()
    {
        return m_type;
    }
    public void setMultiPkg(int multiPkg)
    {
        this.m_multiPkg = multiPkg;
    }
    public int getMultiPkg()
    {
        return m_multiPkg;
    }
    public void setStyle(Style style)
    {
        this.m_style = style;
    }
    public void setAction(ClickAction action)
    {
        this.m_action = action;
    }
    public void setCustom(Map<String, Object> custom)
    {
        this.m_custom = custom;
    }
    public void setRaw(String raw)
    {
        this.m_raw = raw;
    }
    public int getLoopInterval()
    {
        return m_loopInterval;
    }
    public void setLoopInterval(int loopInterval)
    {
        m_loopInterval = loopInterval;
    }
    public int getLoopTimes()
    {
        return m_loopTimes;
    }
    public void setLoopTimes(int loopTimes)
    {
        m_loopTimes = loopTimes;
    }
    
    public boolean isValid()
    {
        if (!m_raw.isEmpty()) return true;
        if (m_type < TYPE_NOTIFICATION || m_type > TYPE_MESSAGE) 
            return false;
        if (m_multiPkg < 0 || m_multiPkg > 1)
            return false;
        if (m_type == TYPE_NOTIFICATION)
        {
            if(!m_style.isValid()) return false;
            if(!m_action.isValid()) return false;
        }
        if (m_expireTime<0 || m_expireTime>3*24*60*60)
            return false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try
        {
            sdf.parse(m_sendTime);
        }catch(ParseException e){
            return false;
        }
        for (TimeInterval ti : m_acceptTimes) {
            if(!ti.isValid()) return false;
        }
        if (m_loopInterval > 0 && m_loopTimes > 0 
                && ((m_loopTimes - 1) * m_loopInterval + 1) > 15) {
            return false;
        }
        
        return true;
    }
    
    public String toJson()
    {
        if (!m_raw.isEmpty()) return m_raw;
        JSONObject json = new JSONObject();
        if(m_type == TYPE_NOTIFICATION)
        {
            json.put("title", m_title);
            json.put("content", m_content);
            json.put("accept_time", acceptTimeToJsonArray());
            json.put("builder_id",m_style.getBuilderId());
            json.put("ring", m_style.getRing());
            json.put("vibrate", m_style.getVibrate());
            json.put("clearable", m_style.getClearable());
            json.put("n_id", m_style.getNId());
            json.put("ring_raw", m_style.getRingRaw());
            json.put("lights", m_style.getLights());
            json.put("icon_type", m_style.getIconType());
            json.put("icon_res", m_style.getIconRes());
            json.put("style_id", m_style.getStyleId());
            json.put("small_icon", m_style.getSmallIcon());
            json.put("action", m_action.toJsonObject());
        }
        else if(m_type == TYPE_MESSAGE)
        {
            json.put("title", m_title);
            json.put("content", m_content);
            json.put("accept_time", acceptTimeToJsonArray());
        }
        json.put("custom_content", m_custom);
        return json.toString();
    }
    
    private String m_title;
    private String m_content;
    private int m_expireTime;
    private String m_sendTime;
    private Vector<TimeInterval> m_acceptTimes;
    private int m_type;
    private int m_multiPkg;
    private Style m_style;
    private ClickAction m_action;
    private Map<String, Object> m_custom;
    private String m_raw;
    private int m_loopInterval;
    private int m_loopTimes;
}