/
OrgAgenda.java
148 lines (113 loc) · 4.1 KB
/
OrgAgenda.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
package com.matburt.mobileorg.Gui.Agenda;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import android.content.Context;
import com.matburt.mobileorg.util.OrgUtils;
public class OrgAgenda implements Serializable {
private static final long serialVersionUID = 2;
private static final String AGENDA_CONFIG_FILE = "agendas.conf";
public String title = "";
public ArrayList<OrgQueryBuilder> queries = new ArrayList<OrgQueryBuilder>();
public static ArrayList<OrgAgenda> readAgendas(Context context) throws IOException {
FileInputStream fis = context.openFileInput(AGENDA_CONFIG_FILE);
byte[] serializedObject = new byte[fis.available()];
fis.read(serializedObject);
fis.close();
@SuppressWarnings("unchecked")
ArrayList<OrgAgenda> result = (ArrayList<OrgAgenda>) OrgUtils
.deserializeObject(serializedObject);
return result;
}
public static void writeAgendas(ArrayList<OrgAgenda> agendas, Context context) throws IOException {
byte[] serializeObject = OrgUtils.serializeObject(agendas);
FileOutputStream fos = context.openFileOutput(AGENDA_CONFIG_FILE, Context.MODE_PRIVATE);
fos.write(serializeObject);
fos.close();
}
public static ArrayList<String> getAgendasTitles(Context context) {
ArrayList<String> result = new ArrayList<String>();
try {
ArrayList<OrgAgenda> agendas = readAgendas(context);
if(agendas == null)
return result;
for(OrgAgenda agenda: agendas)
result.add(agenda.title);
return result;
} catch (IOException e) {
return result;
}
}
public static void removeAgenda(int position, Context context) {
try {
ArrayList<OrgAgenda> agendas = readAgendas(context);
agendas.remove(position);
writeAgendas(agendas, context);
} catch (IOException e) {}
}
public static void replaceAgenda(OrgAgenda agenda, int position, Context context) {
try {
ArrayList<OrgAgenda> agendas = readAgendas(context);
agendas.remove(position);
agendas.add(position, agenda);
writeAgendas(agendas, context);
} catch (IOException e) {}
}
public static OrgAgenda getAgenda(int agendaPos, Context context) {
try {
ArrayList<OrgAgenda> agendas = readAgendas(context);
if(agendaPos >= 0 && agendaPos < agendas.size())
return agendas.get(agendaPos);
} catch (IOException e) {}
return new OrgAgenda();
}
public static int addAgenda(Context context) {
try {
ArrayList<OrgAgenda> agendas = readAgendas(context);
OrgAgenda agenda = new OrgAgenda();
agendas.add(agenda);
writeAgendas(agendas, context);
return agendas.size() - 1;
} catch (IOException e) {}
return -1;
}
public static ArrayList<String> getAgendaQueryTitles(int agendaPos, Context context) {
ArrayList<String> result = new ArrayList<String>();
OrgAgenda agenda = getAgenda(agendaPos, context);
for (OrgQueryBuilder query : agenda.queries)
result.add(query.title);
return result;
}
/** Agenda Query **/
public static OrgQueryBuilder getAgendaEntry(int agendaPos, int entryPos, Context context) {
OrgAgenda agenda = getAgenda(agendaPos, context);
if(entryPos >= 0 && entryPos < agenda.queries.size())
return agenda.queries.get(entryPos);
else
return new OrgQueryBuilder("");
}
public static void removeAgendaEntry(int agendaPos, int entryPos, Context context) {
try {
ArrayList<OrgAgenda> agendas = readAgendas(context);
OrgAgenda agenda = agendas.get(agendaPos);
agenda.queries.remove(entryPos);
writeAgendas(agendas, context);
} catch (IOException e) {}
}
public static void writeAgendaEntry(OrgQueryBuilder query, int agendaPos, int entryPos, Context context) {
try {
ArrayList<OrgAgenda> agendas = readAgendas(context);
if(agendaPos < 0 || agendaPos >= agendas.size())
return;
OrgAgenda agenda = agendas.get(agendaPos);
if(entryPos >= 0 && entryPos < agenda.queries.size()) {
agenda.queries.remove(entryPos);
agenda.queries.add(entryPos, query);
} else
agenda.queries.add(query);
writeAgendas(agendas, context);
} catch (IOException e) {}
}
}