package net.cbaines.suma; import java.sql.SQLException; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; public class BusRouteActivity extends OrmLiteBaseActivity implements Preferences, OnItemClickListener, OnItemLongClickListener { final static String TAG = "BusActivity"; private TextView busRouteLabel; private TextView busRouteCode; private TextView busRouteID; Toast activityToast; private ListView busRouteView; private POIArrayAdapter arrayAdapter; private Context instance; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bus_route_activity); instance = this; Log.i(TAG, "getIntent().getDataString() " + getIntent().getDataString()); String strBusRouteID; if (getIntent().getDataString().startsWith("http://data")) { String[] uriParts = getIntent().getDataString().split("/"); strBusRouteID = uriParts[uriParts.length - 1].replace(".html", ""); } else { String[] uriParts = getIntent().getDataString().split("/"); strBusRouteID = uriParts[uriParts.length - 1]; } final DatabaseHelper helper = getHelper(); BusRoute busRoute = null; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); try { busRoute = helper.getBusRouteDao().queryForId(Integer.parseInt(strBusRouteID)); busRouteLabel = (TextView) findViewById(R.id.busRouteActivityLabel); busRouteLabel.setText(busRoute.label); busRouteCode = (TextView) findViewById(R.id.busRouteActivityCode); busRouteCode.setText(busRoute.code); if (prefs.getBoolean(SHOW_IDENTIFIERS, SHOW_IDENTIFIERS_ENABLED_BY_DEFAULT)) { busRouteID = (TextView) findViewById(R.id.busRouteActivityID); busRouteID.setText(String.valueOf(busRoute.id)); busRouteID.setVisibility(View.VISIBLE); } busRouteView = (ListView) findViewById(R.id.busRouteBusStops); } catch (NumberFormatException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } arrayAdapter = new POIArrayAdapter(instance, busRoute.getRouteBusStops(instance)); busRouteView.setAdapter(arrayAdapter); busRouteView.setOnItemClickListener(this); busRouteView.setOnItemLongClickListener(this); } public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { Log.i(TAG, "OnItemClick pos " + position + " id " + id); String poiId = arrayAdapter.getItemStringId(position); Log.i(TAG, "POI " + poiId + " selected"); POI poi = null; if (poiId != null) { Log.i(TAG, "Got id " + poiId); try { poi = getHelper().getBusStopDao().queryForId(poiId); } catch (SQLException e) { e.printStackTrace(); } if (poi == null) { Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult"); } else { BusStop busStop = (BusStop) poi; Log.i(TAG, "Pressed " + busStop.id); Uri uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id); Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri); startActivity(busStopIntent); return true; } } else { Log.i(TAG, "Got null poi id"); // mapController.setZoom(15); // mapController.setCenter(new GeoPoint(50935551, -1393488)); } return false; } public void onItemClick(AdapterView parent, View view, int position, long id) { Log.i(TAG, "OnItemClick pos " + position + " id " + id); String poiId = arrayAdapter.getItemStringId(position); Log.i(TAG, "POI " + poiId + " selected"); POI poi = null; if (poiId != null) { Log.i(TAG, "Got id " + poiId); try { poi = getHelper().getBusStopDao().queryForId(poiId); } catch (SQLException e) { e.printStackTrace(); } if (poi == null) { Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult"); } else { Log.i(TAG, "Pressed " + poi.id); Uri uri = Uri.parse("geo:" + Util.E6IntToDouble(poi.point.getLatitudeE6()) + "," + Util.E6IntToDouble(poi.point.getLongitudeE6()) + "?z=18"); Log.i(TAG, "Starting a activity for " + uri); Intent mapIntent = new Intent(Intent.ACTION_VIEW, uri); startActivity(mapIntent); } } else { Log.i(TAG, "Got null poi id"); // mapController.setZoom(15); // mapController.setCenter(new GeoPoint(50935551, -1393488)); } } }