/* * 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; } }