aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/ViewDialog.java
blob: e97cbdfed6cd08b3624da4c7e1a2f4fa75757da9 (plain)
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
/*
 * 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 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;

class ViewDialog extends Dialog implements OnChildClickListener {

	private final ExpandableListView epView;

	private static final String TAG = "ViewDialog";

	private final MyExpandableListAdapter mAdapter;

	private OnChildClickListener listener;

	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;

		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);
		siteNames = MapActivity.SITE_NAMES; // TODO: Temp hack, should be included in the strings res for translation
		// purposes?

		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
		// |_ U1
		// |_ U1N
		// |_ U2
		// |_ U6
		// |_ U9
		// Bus Routes
		// |_ U1
		// |_ U1N
		// |_ U2
		// |_ U6
		// |_ U9
		// Buildings
		// |_ Residential
		// |_ Non-Residential
		// Site Outlines
		// |_ Highfield Campus
		// |_ Boldrewood Campus
		// |_ Avenue Campus
		// |_ Winchester School of Art
		// |_ The University of Southampton Science Park
		// |_ National Oceanography Centre Campus
		// |_ Boat House
		// |_ Southampton General Hospital
		// |_ Royal South Hants Hospital
		// |_ Belgrave Industrial Site
		// |_ Highfield Hall
		// |_ Glen Eyre Hall
		// |_ South Hill Hall
		// |_ Chamberlain Hall
		// |_ Hartley Grove
		// |_ Bencraft Hall
		// |_ Connaught Hall
		// |_ Montefiore Hall
		// |_ Stoneham Hall
		// |_ Erasmus Park
		// Other
		// |_ Scale Bar
		// |_ Compass
		// |_ My Location

		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 = MapActivity.PREFERENCES_GROUPS[groupPosition]
					+ MapActivity.PREFERENCES_CHILDREN[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.BUS_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) {
				if (childPosition == 0) {
					cb.setChecked(activityPrefs.getBoolean(str, MapActivity.SCALE_BAR_OVERLAY_ENABLED_BY_DEFAULT));
				} else if (childPosition == 1) {
					cb.setChecked(activityPrefs.getBoolean(str, MapActivity.MY_LOCATION_OVERLAY_COMPASS_ENABLED_BY_DEFAULT));
				} else if (childPosition == 2) {
					cb.setChecked(activityPrefs.getBoolean(str, MapActivity.MY_LOCATION_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 = MapActivity.PREFERENCES_GROUPS[groupPosition]
				+ MapActivity.PREFERENCES_CHILDREN[groupPosition][childPosition];

		Log.i(TAG, "Putting " + str + " to " + (!cb.isChecked()));
		editor.putBoolean(str, !cb.isChecked());

		editor.commit();

		mAdapter.notifyDataSetInvalidated();

		if (listener != null) {
			listener.onChildClick(parent, v, groupPosition, childPosition, id);
		}

		return true;
	}

}