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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*
* Southampton University Map App
* Copyright (C) 2011 Christopher Baines
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package net.cbaines.suma;
import java.sql.SQLException;
import java.util.ArrayList;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import com.j256.ormlite.android.apptools.OpenHelperManager;
class ViewDialog extends Dialog implements OnChildClickListener {
private final ExpandableListView epView;
private static final String TAG = "ViewDialog";
private final MyExpandableListAdapter mAdapter;
private OnChildClickListener listener;
private DatabaseHelper helper;
private String[] busRoutes;
private String[] buildingTypes;
private String[] other;
private String[] groupHeadings;
private String[] siteNames;
protected MapActivity context;
public ViewDialog(MapActivity context) {
super(context);
this.context = context;
helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
int size;
try {
size = (int) helper.getSiteDao().countOf();
ArrayList<Site> sites = new ArrayList<Site>(size);
try {
sites.addAll(helper.getSiteDao().queryForAll());
} catch (SQLException e) {
e.printStackTrace();
}
siteNames = new String[size];
for (int i = 0; i < size; i++) {
siteNames[i] = sites.get(i).name;
}
} catch (SQLException e1) {
e1.printStackTrace();
}
busRoutes = context.getResources().getStringArray(R.array.uniLinkBusRoutes);
buildingTypes = context.getResources().getStringArray(R.array.buildingTypes);
other = context.getResources().getStringArray(R.array.utilityOverlays);
groupHeadings = context.getResources().getStringArray(R.array.preferencesHeadings);
setContentView(R.layout.view_dialog);
setTitle("Select the map elements to display");
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(this.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.FILL_PARENT;
this.getWindow().setAttributes(lp);
epView = (ExpandableListView) findViewById(R.id.view_list);
mAdapter = new MyExpandableListAdapter(context);
epView.setAdapter(mAdapter);
epView.setOnChildClickListener(this);
}
public void setOnItemClickListener(OnChildClickListener onChildClickListener) {
Log.i(TAG, "Listener set for dialog");
listener = onChildClickListener;
}
class MyExpandableListAdapter extends BaseExpandableListAdapter implements Preferences {
private LayoutInflater inflater;
private static final String TAG = "MyExpandableListAdapter";
// Bus Stops (0)
// |_ U1 (0:0)
// |_ U1N (0:1)
// |_ U2 (0:2)
// |_ U6 (0:3)
// |_ U9 (0:4)
// Bus Routes (1)
// |_ U1 (1:0)
// |_ U1N (1:1)
// |_ U2 (1:2)
// |_ U6 (1:3)
// |_ U9 (1:4)
// Buildings (2)
// |_ Residential (2:0)
// |_ Non-Residential (2:1)
// Site Outlines (3)
// |_ Highfield Campus (3:0)
// |_ Boldrewood Campus (3:1)
// |_ Avenue Campus (3:2)
// |_ Winchester School of Art (3:3)
// |_ The University of Southampton Science Park (3:4)
// |_ National Oceanography Centre Campus (3:5)
// |_ Boat House (3:6)
// |_ Southampton General Hospital (3:0)
// |_ Royal South Hants Hospital (3:0)
// |_ Belgrave Industrial Site (3:0)
// |_ Highfield Hall (3:0)
// |_ Glen Eyre Hall (3:0)
// |_ South Hill Hall (3:0)
// |_ Chamberlain Hall (3:0)
// |_ Hartley Grove1 (3:0)
// |_ Bencraft Hall (3:0)
// |_ Connaught Hall (3:0)
// |_ Montefiore Hall (3:0)
// |_ Stoneham Hall (3:0)
// |_ Erasmus Park (3:0)
// Other (4)
// |_ Scale Bar (4:0)
// |_ Compass (4:1)
// |_ My Location (4:2)
MyExpandableListAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public Object getChild(int groupPosition, int childPosition) {
if (groupPosition == 0 || groupPosition == 1) {
return busRoutes[childPosition];
} else if (groupPosition == 2) {
return buildingTypes[childPosition];
} else if (groupPosition == 3) {
return siteNames[childPosition];
} else if (groupPosition == 4) {
return other[childPosition];
} else {
Log.e(TAG, "Unrecognised groupPosition " + groupPosition);
return null;
}
}
public long getChildId(int groupPosition, int childPosition) {
return groupPosition * 50 + childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = null;
if (convertView != null)
v = convertView;
else
v = inflater.inflate(R.layout.view_child_row, parent, false);
String c = (String) getChild(groupPosition, childPosition);
TextView childName = (TextView) v.findViewById(R.id.childname);
if (childName != null)
childName.setText(c);
CheckBox cb = (CheckBox) v.findViewById(R.id.check1);
cb.setClickable(false);
cb.setFocusable(false);
if (context == null) {
Log.e(TAG, "context == null");
}
SharedPreferences activityPrefs = context.getPreferences(0);
String str = groupPosition + ":" + childPosition;
if (groupPosition == 0) {
cb.setChecked(activityPrefs.getBoolean(str, MapActivity.UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT));
} else if (groupPosition == 1) {
cb.setChecked(activityPrefs.getBoolean(str, MapActivity.ROUTE_OVERLAYS_ENABLED_BY_DEFAULT));
} else if (groupPosition == 2) {
if (childPosition == 0) {
cb.setChecked(activityPrefs.getBoolean(str, MapActivity.RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT));
} else {
cb.setChecked(activityPrefs.getBoolean(str, MapActivity.NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT));
}
} else if (groupPosition == 3) {
cb.setChecked(activityPrefs.getBoolean(str, MapActivity.SITE_OVERLAYS_ENABLED_BY_DEFAULT));
} else if (groupPosition == 4) {
cb.setChecked(activityPrefs.getBoolean(str, MapActivity.SCALE_BAR_OVERLAY_ENABLED_BY_DEFAULT));
}
return v;
}
public int getChildrenCount(int groupPosition) {
if (groupPosition == 0 || groupPosition == 1) {
return busRoutes.length;
} else if (groupPosition == 2) {
return buildingTypes.length;
} else if (groupPosition == 3) {
return siteNames.length;
} else if (groupPosition == 4) {
return other.length;
}
return 0;
}
public Object getGroup(int groupPosition) {
return groupHeadings[groupPosition];
}
public int getGroupCount() {
return groupHeadings.length;
}
public long getGroupId(int groupPosition) {
return groupPosition * 5;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View v = null;
if (convertView != null)
v = convertView;
else
v = inflater.inflate(R.layout.view_group_row, parent, false);
String gt = (String) getGroup(groupPosition);
TextView colorGroup = (TextView) v.findViewById(R.id.childname);
if (gt != null)
colorGroup.setText(gt);
return v;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Log.i(TAG, "Got view dialog click at " + groupPosition + ":" + childPosition);
SharedPreferences activityPrefs = context.getPreferences(0);
Editor editor = activityPrefs.edit();
CheckBox cb = (CheckBox) v.findViewById(R.id.check1);
String str = groupPosition + ":" + childPosition;
editor.putBoolean(str, !cb.isChecked());
editor.commit();
mAdapter.notifyDataSetInvalidated();
if (listener != null) {
listener.onChildClick(parent, v, groupPosition, childPosition, id);
}
return true;
}
}
|