-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarmMe.java
214 lines (183 loc) · 6.05 KB
/
AlarmMe.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**************************************************************************
*
* 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.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
// Toast.makeText(getApplicationContext(), "Delete" + index, Toast.LENGTH_SHORT).show();
public class AlarmMe extends Activity
{
private final String TAG = "AlarmMe";
private ListView mAlarmList;
private AlarmListAdapter mAlarmListAdapter;
private Alarm mCurrentAlarm;
private final int NEW_ALARM_ACTIVITY = 0;
private final int EDIT_ALARM_ACTIVITY = 1;
private final int PREFERENCES_ACTIVITY = 2;
private final int ABOUT_ACTIVITY = 3;
private final int CONTEXT_MENU_EDIT = 0;
private final int CONTEXT_MENU_DELETE = 1;
private final int CONTEXT_MENU_DUPLICATE = 2;
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.main);
Log.i(TAG, "AlarmMe.onCreate()");
mAlarmList = (ListView)findViewById(R.id.alarm_list);
mAlarmListAdapter = new AlarmListAdapter(this);
mAlarmList.setAdapter(mAlarmListAdapter);
mAlarmList.setOnItemClickListener(mListOnItemClickListener);
registerForContextMenu(mAlarmList);
mCurrentAlarm = null;
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.i(TAG, "AlarmMe.onDestroy()");
// mAlarmListAdapter.save();
}
@Override
public void onResume()
{
super.onResume();
Log.i(TAG, "AlarmMe.onResume()");
mAlarmListAdapter.updateAlarms();
}
public void onAddAlarmClick(View view)
{
Intent intent = new Intent(getBaseContext(), EditAlarm.class);
mCurrentAlarm = new Alarm(this);
mCurrentAlarm.toIntent(intent);
AlarmMe.this.startActivityForResult(intent, NEW_ALARM_ACTIVITY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == NEW_ALARM_ACTIVITY)
{
if (resultCode == RESULT_OK)
{
mCurrentAlarm.fromIntent(data);
mAlarmListAdapter.add(mCurrentAlarm);
}
mCurrentAlarm = null;
}
else if (requestCode == EDIT_ALARM_ACTIVITY)
{
if (resultCode == RESULT_OK)
{
mCurrentAlarm.fromIntent(data);
mAlarmListAdapter.update(mCurrentAlarm);
}
mCurrentAlarm = null;
}
else if (requestCode == PREFERENCES_ACTIVITY)
{
mAlarmListAdapter.onSettingsUpdated();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (R.id.menu_settings == item.getItemId())
{
Intent intent = new Intent(getBaseContext(), Preferences.class);
startActivityForResult(intent, PREFERENCES_ACTIVITY);
return true;
}
else if (R.id.menu_zim == item.getItemId())
{
Intent intent = new Intent(getBaseContext(), Zim.class);
startActivity(intent);
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
if (v.getId() == R.id.alarm_list)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle(mAlarmListAdapter.getItem(info.position).getTitle());
menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Edit");
menu.add(Menu.NONE, CONTEXT_MENU_DELETE, Menu.NONE, "Delete");
menu.add(Menu.NONE, CONTEXT_MENU_DUPLICATE, Menu.NONE, "Duplicate");
}
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int index = item.getItemId();
if (index == CONTEXT_MENU_EDIT)
{
Intent intent = new Intent(getBaseContext(), EditAlarm.class);
mCurrentAlarm = mAlarmListAdapter.getItem(info.position);
mCurrentAlarm.toIntent(intent);
startActivityForResult(intent, EDIT_ALARM_ACTIVITY);
}
else if (index == CONTEXT_MENU_DELETE)
{
mAlarmListAdapter.delete(info.position);
}
else if (index == CONTEXT_MENU_DUPLICATE)
{
Alarm alarm = mAlarmListAdapter.getItem(info.position);
Alarm newAlarm = new Alarm(this);
Intent intent = new Intent();
alarm.toIntent(intent);
newAlarm.fromIntent(intent);
newAlarm.setTitle(alarm.getTitle() + " (copy)");
mAlarmListAdapter.add(newAlarm);
}
return true;
}
private AdapterView.OnItemClickListener mListOnItemClickListener = new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = new Intent(getBaseContext(), EditAlarm.class);
mCurrentAlarm = mAlarmListAdapter.getItem(position);
mCurrentAlarm.toIntent(intent);
AlarmMe.this.startActivityForResult(intent, EDIT_ALARM_ACTIVITY);
}
};
}