-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarmListAdapter.java
186 lines (152 loc) · 4.79 KB
/
AlarmListAdapter.java
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
/**************************************************************************
*
* Copyright (C) 2012-2015 Alex Taradov <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************/
package com.application.brainfit;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
class AlarmListAdapter extends BaseAdapter
{
private final String TAG = "AlarmMe";
private Context mContext;
private DataSource mDataSource;
private LayoutInflater mInflater;
private DateTime mDateTime;
private int mColorOutdated;
private int mColorActive;
private AlarmManager mAlarmManager;
public AlarmListAdapter(Context context)
{
mContext = context;
mDataSource = DataSource.getInstance(context);
Log.i(TAG, "AlarmListAdapter.create()");
mInflater = LayoutInflater.from(context);
mDateTime = new DateTime(context);
mColorOutdated = mContext.getResources().getColor(R.color.alarm_title_outdated);
mColorActive = mContext.getResources().getColor(R.color.alarm_title_active);
mAlarmManager = (AlarmManager)context.getSystemService(mContext.ALARM_SERVICE);
dataSetChanged();
}
public void save()
{
mDataSource.save();
}
public void update(Alarm alarm)
{
mDataSource.update(alarm);
dataSetChanged();
}
public void updateAlarms()
{
Log.i(TAG, "AlarmListAdapter.updateAlarms()");
for (int i = 0; i < mDataSource.size(); i++)
mDataSource.update(mDataSource.get(i));
dataSetChanged();
}
public void add(Alarm alarm)
{
mDataSource.add(alarm);
dataSetChanged();
}
public void delete(int index)
{
cancelAlarm(mDataSource.get(index));
mDataSource.remove(index);
dataSetChanged();
}
public void onSettingsUpdated()
{
mDateTime.update();
dataSetChanged();
}
public int getCount()
{
return mDataSource.size();
}
public Alarm getItem(int position)
{
return mDataSource.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
Alarm alarm = mDataSource.get(position);
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.title = (TextView)convertView.findViewById(R.id.item_title);
holder.details = (TextView)convertView.findViewById(R.id.item_details);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.title.setText(alarm.getTitle());
holder.details.setText(mDateTime.formatDetails(alarm) + (alarm.getEnabled() ? "" : " [disabled]"));
if (alarm.getOutdated())
holder.title.setTextColor(mColorOutdated);
else
holder.title.setTextColor(mColorActive);
return convertView;
}
private void dataSetChanged()
{
for (int i = 0; i < mDataSource.size(); i++)
setAlarm(mDataSource.get(i));
notifyDataSetChanged();
}
private void setAlarm(Alarm alarm)
{
PendingIntent sender;
Intent intent;
if (alarm.getEnabled() && !alarm.getOutdated())
{
intent = new Intent(mContext, AlarmReceiver.class);
alarm.toIntent(intent);
sender = PendingIntent.getBroadcast(mContext, (int)alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, alarm.getDate(), sender);
Log.i(TAG, "AlarmListAdapter.setAlarm(" + alarm.getId() + ", '" + alarm.getTitle() + "', " + alarm.getDate()+")");
}
}
private void cancelAlarm(Alarm alarm)
{
PendingIntent sender;
Intent intent;
intent = new Intent(mContext, AlarmReceiver.class);
sender = PendingIntent.getBroadcast(mContext, (int)alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.cancel(sender);
}
static class ViewHolder
{
TextView title;
TextView details;
}
}