1 |
|
|
2 |
|
package pl.magot.vetch.ancal.reminder; |
3 |
|
|
4 |
|
|
5 |
|
import pl.magot.vetch.ancal.*; |
6 |
|
import pl.magot.vetch.ancal.views.*; |
7 |
|
import java.util.ArrayList; |
8 |
|
import java.util.Calendar; |
9 |
|
import android.content.Context; |
10 |
|
import android.content.Intent; |
11 |
|
import android.widget.Toast; |
12 |
|
import android.app.NotificationManager; |
13 |
|
import android.app.Notification; |
14 |
|
import android.app.PendingIntent; |
15 |
|
import android.os.Bundle; |
16 |
|
|
17 |
|
|
|
|
| 99,2% |
Uncovered Elements: 1 (123) |
Complexity: 32 |
Complexity Density: 0,39 |
|
18 |
|
public class AlarmReminder |
19 |
|
{ |
20 |
|
|
21 |
|
private Context context = null; |
22 |
|
protected AlarmService.AlarmPrefs prefs = null; |
23 |
|
protected Utils utils = null; |
24 |
|
private ArrayList<AlarmDataViewItem> alarmItems = new ArrayList<AlarmDataViewItem>(); |
25 |
|
private NotificationManager mNM = null; |
26 |
|
private final int iNotifyID1 = 1; |
27 |
|
private int iLastAlarmsHash = 0; |
28 |
|
private Calendar calHashToday = Calendar.getInstance(); |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
private String msgYouHaveASTS = ""; |
33 |
|
private String msgYouHaveTS = ""; |
34 |
|
private String msgYouHaveAS = ""; |
35 |
|
private String msgASTS = ""; |
36 |
|
private String msgTS = ""; |
37 |
|
private String msgAS = ""; |
38 |
|
private String strReminder = ""; |
39 |
|
|
40 |
|
|
41 |
|
private String sAppName = ""; |
42 |
|
|
43 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0,08 |
|
44 |
1
|
public AlarmReminder(Context ctx, AlarmService.AlarmPrefs prefs, Utils utils)... |
45 |
|
{ |
46 |
1
|
this.context = ctx; |
47 |
1
|
this.prefs = prefs; |
48 |
1
|
this.utils = utils; |
49 |
|
|
50 |
1
|
sAppName = this.context.getString(R.string.app_name); |
51 |
1
|
mNM = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE); |
52 |
1
|
RemoveNotify(); |
53 |
|
|
54 |
|
|
55 |
1
|
msgYouHaveASTS = utils.GetResStr(R.string.msgYouHaveASTS); |
56 |
1
|
msgYouHaveTS = utils.GetResStr(R.string.msgYouHaveTS); |
57 |
1
|
msgYouHaveAS = utils.GetResStr(R.string.msgYouHaveAS); |
58 |
1
|
msgASTS = utils.GetResStr(R.string.msgASTS); |
59 |
1
|
msgTS = utils.GetResStr(R.string.msgTS); |
60 |
1
|
msgAS = utils.GetResStr(R.string.msgAS); |
61 |
1
|
strReminder = utils.GetResStr(R.string.strReminder); |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
} |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
67 |
30
|
public void Clear()... |
68 |
|
{ |
69 |
30
|
alarmItems.clear(); |
70 |
|
} |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
27
|
public void RemoveNotify()... |
73 |
|
{ |
74 |
27
|
mNM.cancel(iNotifyID1); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
77 |
60
|
public void AddAlarmData(ArrayList<AlarmDataViewItem> items)... |
78 |
|
{ |
79 |
60
|
this.alarmItems.addAll(items); |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
30
|
public boolean IsAlarm()... |
83 |
|
{ |
84 |
30
|
return (alarmItems.size() > 0); |
85 |
|
} |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0,5 |
|
87 |
22
|
public int GetItemsCount(int iOrderFilter)... |
88 |
|
{ |
89 |
22
|
int iCount = 0; |
90 |
52
|
for (int index = 0; index < alarmItems.size(); index++) |
91 |
|
{ |
92 |
30
|
AlarmDataViewItem item = alarmItems.get(index); |
93 |
30
|
if (item.GetOrder() == iOrderFilter) |
94 |
15
|
iCount++; |
95 |
|
} |
96 |
22
|
return iCount; |
97 |
|
} |
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 5 |
Complexity Density: 0,83 |
|
99 |
7
|
public String GetPopupMsg(int iCountAppts, int iCountTasks)... |
100 |
|
{ |
101 |
7
|
String msg = String.format(msgYouHaveASTS, iCountAppts, iCountTasks); |
102 |
7
|
if ((iCountAppts == 0) && (iCountTasks > 0)) |
103 |
3
|
msg = String.format(msgYouHaveTS, iCountTasks); |
104 |
7
|
if ((iCountTasks == 0) && (iCountAppts > 0)) |
105 |
4
|
msg = String.format(msgYouHaveAS, iCountAppts); |
106 |
7
|
return msg; |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 5 |
Complexity Density: 0,83 |
|
109 |
7
|
public String GetStatusMsg(int iCountAppts, int iCountTasks)... |
110 |
|
{ |
111 |
7
|
String msg = String.format(msgASTS, iCountAppts, iCountTasks); |
112 |
7
|
if ((iCountAppts == 0) && (iCountTasks > 0)) |
113 |
3
|
msg = String.format(msgTS, iCountTasks); |
114 |
7
|
if ((iCountTasks == 0) && (iCountAppts > 0)) |
115 |
4
|
msg = String.format(msgAS, iCountAppts); |
116 |
7
|
return msg; |
117 |
|
} |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
119 |
11
|
public String GetTodayHashString()... |
120 |
|
{ |
121 |
11
|
calHashToday.setTimeInMillis(System.currentTimeMillis()); |
122 |
11
|
return Integer.toString(calHashToday.get(Calendar.YEAR)) + |
123 |
|
Integer.toString(calHashToday.get(Calendar.MONTH)) + |
124 |
|
Integer.toString(calHashToday.get(Calendar.DAY_OF_MONTH)); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
127 |
11
|
public String GetAlarmsHashString()... |
128 |
|
{ |
129 |
11
|
String sMsg = ""; |
130 |
26
|
for (int index = 0; index < alarmItems.size(); index++) |
131 |
|
{ |
132 |
15
|
AlarmDataViewItem item = alarmItems.get(index); |
133 |
15
|
sMsg += item.GetHashString(); |
134 |
|
} |
135 |
|
|
136 |
11
|
sMsg += GetTodayHashString(); |
137 |
11
|
return sMsg; |
138 |
|
} |
139 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0,4 |
|
140 |
11
|
public boolean AlarmsChanged()... |
141 |
|
{ |
142 |
11
|
String sAlarmsHashString = GetAlarmsHashString(); |
143 |
11
|
if (sAlarmsHashString.hashCode() == iLastAlarmsHash) |
144 |
4
|
return false; |
145 |
7
|
iLastAlarmsHash = sAlarmsHashString.hashCode(); |
146 |
7
|
return true; |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0,33 |
|
149 |
14
|
public void PutDataItemsToBundle(Bundle data, int iOrderFilter)... |
150 |
|
{ |
151 |
14
|
int iKeyIndex = 0; |
152 |
32
|
for (int i = 0; i < alarmItems.size(); i++) |
153 |
|
{ |
154 |
18
|
AlarmDataViewItem item = alarmItems.get(i); |
155 |
18
|
if (item.GetOrder() == iOrderFilter) |
156 |
|
{ |
157 |
9
|
String sKey = "_" + Integer.toString(iOrderFilter) + "_" + Integer.toString(iKeyIndex); |
158 |
|
|
159 |
9
|
data.putLong("rowid" + sKey, item.GetID()); |
160 |
9
|
data.putString("text" + sKey, item.GetText(utils, prefs.b24HourMode)); |
161 |
|
|
162 |
9
|
iKeyIndex++; |
163 |
|
} |
164 |
|
} |
165 |
14
|
data.putInt("typetotal_" + Integer.toString(iOrderFilter), iKeyIndex); |
166 |
|
} |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
|
168 |
7
|
public PendingIntent getAlarmDialogIntent()... |
169 |
|
{ |
170 |
7
|
Intent intentAlarmDialog = new Intent("android.intent.action.AnCal.ACTION_MODE_VIEW_ALARMDIALOG"); |
171 |
7
|
Bundle data = new Bundle(); |
172 |
7
|
PutDataItemsToBundle(data, AlarmDataViewItem.iOrderAppts); |
173 |
7
|
PutDataItemsToBundle(data, AlarmDataViewItem.iOrderTasks); |
174 |
7
|
intentAlarmDialog.putExtras(data); |
175 |
|
|
176 |
7
|
return PendingIntent.getActivity(this.context, 0, intentAlarmDialog, 0); |
177 |
|
} |
178 |
|
|
|
|
| 96,2% |
Uncovered Elements: 1 (26) |
Complexity: 5 |
Complexity Density: 0,25 |
|
179 |
30
|
public void ShowNotification()... |
180 |
|
{ |
181 |
30
|
if (IsAlarm()) |
182 |
|
{ |
183 |
11
|
int iCountAppts = GetItemsCount(AlarmDataViewItem.iOrderAppts); |
184 |
11
|
int iCountTasks = GetItemsCount(AlarmDataViewItem.iOrderTasks); |
185 |
|
|
186 |
|
|
187 |
11
|
if ((iCountAppts > 0) || (iCountTasks > 0)) |
188 |
|
{ |
189 |
|
|
190 |
11
|
if (AlarmsChanged()) |
191 |
|
{ |
192 |
|
|
193 |
7
|
RemoveNotify(); |
194 |
|
|
195 |
|
|
196 |
7
|
final String sTitle = sAppName + ": " + strReminder; |
197 |
7
|
String sMessage = GetPopupMsg(iCountAppts, iCountTasks); |
198 |
|
|
199 |
|
|
200 |
7
|
String sStatus = GetStatusMsg(iCountAppts, iCountTasks); |
201 |
|
|
202 |
|
|
203 |
7
|
ViewToastMsg viewToastMsg = new ViewToastMsg(this.context, sTitle, sMessage); |
204 |
7
|
Toast toast = new Toast(this.context); |
205 |
7
|
toast.setDuration(Toast.LENGTH_LONG); |
206 |
7
|
toast.setView(viewToastMsg); |
207 |
7
|
toast.show(); |
208 |
|
|
209 |
|
|
210 |
7
|
Notification notifyItem = new Notification(R.drawable.stat_notify_alarm, sStatus, System.currentTimeMillis()); |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
7
|
notifyItem.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS; |
225 |
7
|
notifyItem.setLatestEventInfo(this.context, strReminder, sStatus, getAlarmDialogIntent()); |
226 |
|
|
227 |
|
|
228 |
7
|
mNM.notify(iNotifyID1, notifyItem); |
229 |
|
} |
230 |
|
} |
231 |
|
} else { |
232 |
19
|
RemoveNotify(); |
233 |
19
|
iLastAlarmsHash = 0; |
234 |
|
} |
235 |
|
} |
236 |
|
|
237 |
|
} |
238 |
|
|