From 5dbed3a0fe85f0ed2a2763be0912f109bdee6c20 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Tue, 6 Mar 2012 11:42:24 +0000 Subject: New Search stuff. --- src/net/cbaines/suma/FindActivity.java | 13 ++ src/net/cbaines/suma/MapActivity.java | 169 ++++++++++++---------- src/net/cbaines/suma/MapContentProvider.java | 201 +++++++++++++++++++++++++++ 3 files changed, 313 insertions(+), 70 deletions(-) create mode 100644 src/net/cbaines/suma/MapContentProvider.java (limited to 'src') diff --git a/src/net/cbaines/suma/FindActivity.java b/src/net/cbaines/suma/FindActivity.java index 669249c..5a07807 100644 --- a/src/net/cbaines/suma/FindActivity.java +++ b/src/net/cbaines/suma/FindActivity.java @@ -27,6 +27,7 @@ import java.util.List; import org.osmdroid.util.GeoPoint; +import android.app.SearchManager; import android.content.Context; import android.content.Intent; import android.location.Location; @@ -57,6 +58,9 @@ public class FindActivity extends OrmLiteBaseActivity implements final static String TAG = "FindActivity"; + public static final String ORIGIN = "o"; + public static final int MAP_ACTIVITY = 0; + private EditText searchBar; private ListView listItems; private ProgressBar progBar; @@ -243,9 +247,18 @@ public class FindActivity extends OrmLiteBaseActivity implements super.onCreate(savedInstanceState); setContentView(R.layout.find_activity); + Log.i(TAG, "FindActivity started"); + searchBar = (EditText) findViewById(R.id.searchBar); searchBar.addTextChangedListener(this); + Intent intent = getIntent(); + if (Intent.ACTION_SEARCH.equals(intent.getAction())) { + String query = intent.getStringExtra(SearchManager.QUERY); + Log.i(TAG, "Searching for " + query); + searchBar.setText(query); + } + listItems = (ListView) findViewById(R.id.findListItems); listItems.setOnItemClickListener(this); listItems.setOnItemLongClickListener(this); diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 01e8ec6..6c4664e 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -73,8 +73,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { /** - * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets - * folder + * Enable to use the database in the assets folder, if its not enabled, the + * database is built from the csv files in the assets folder */ private boolean useBundledDatabase = true; @@ -124,10 +124,11 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private static final int NON_UNI_LINK_BUS_STOP_OVERLAY_RANK = 4; // -- Site Overlays - static final String[] SITE_NAMES = { "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", + static final String[] SITE_NAMES = { "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" }; private static final String SITE_OVERLAYS = "siteOverlays:"; @@ -165,8 +166,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants // Uni-Link routes static final String[] UNI_LINK_ROUTES = { "U1", "U1N", "U2", "U6", "U9" }; - static final String[] PREFERENCES_GROUPS = { BUS_STOP_OVERLAYS, BUS_ROUTE_OVERLAYS, BUILDING_OVERLAYS, SITE_OVERLAYS, - OTHER_OVERLAYS }; + static final String[] PREFERENCES_GROUPS = { BUS_STOP_OVERLAYS, BUS_ROUTE_OVERLAYS, BUILDING_OVERLAYS, + SITE_OVERLAYS, OTHER_OVERLAYS }; static final String[][] PREFERENCES_CHILDREN = { UNI_LINK_ROUTES, UNI_LINK_ROUTES, BUILDING_TYPES, SITE_NAMES, OTHER_OVERLAY_NAMES }; @@ -198,8 +199,10 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants pastOverlays = (HashMap) getLastNonConfigurationInstance(); /* - * SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); This code in the following - * constructor causes problems in some emulators, disable sensors to fix. + * SensorManager mSensorManager = (SensorManager) + * getSystemService(Context.SENSOR_SERVICE); This code in the following + * constructor causes problems in some emulators, disable sensors to + * fix. */ Log.i(TAG, "Starting creating myLocationOverlay"); myLocationOverlay = new MyLocationOverlay(instance, mapView); @@ -320,6 +323,14 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants return overlays; } + @Override + public boolean onSearchRequested() { + Bundle appData = new Bundle(); + appData.putInt(FindActivity.ORIGIN, FindActivity.MAP_ACTIVITY); + startSearch(null, false, appData, false); + return true; + } + public void run() { Log.i(TAG, "Begining loading database " + (System.currentTimeMillis() - startTime)); @@ -513,7 +524,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants if (scaleBarOverlay != null) { Log.v(TAG, "ScaleBarOverlay is already created"); } else { - if (pastOverlays != null && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get(SCALE_BAR_OVERLAY)) != null) { + if (pastOverlays != null + && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get(SCALE_BAR_OVERLAY)) != null) { Log.i(TAG, "Finished restoring utility overlays " + (System.currentTimeMillis() - startTime)); } else { scaleBarOverlay = new ScaleBarOverlay(instance); @@ -542,7 +554,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private void showRouteOverlay(final BusRoute route) { new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing route " + route.code + " overlay at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing route " + route.code + " overlay at " + + (System.currentTimeMillis() - startTime)); final SharedPreferences activityPrefs = getPreferences(0); final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); @@ -567,8 +580,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants // TODO Is this a route like U1N or, something else, // this hack works somewhat for now? - PathOverlay routeOverlayU1E = DataManager.getRoutePath(getResources().openRawResource(R.raw.u1e), - colour, mResourceProxy); + PathOverlay routeOverlayU1E = DataManager.getRoutePath( + getResources().openRawResource(R.raw.u1e), colour, mResourceProxy); routeOverlayU1E.getPaint().setAntiAlias(true); routeOverlayU1E.getPaint().setAlpha(145); routeOverlayU1E.getPaint().setStrokeWidth(12); @@ -623,7 +636,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants mapView.postInvalidate(); - Log.i(TAG, "Finished showing route " + route.code + " overlay at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing route " + route.code + " overlay at " + + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -632,7 +646,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing site " + site.name + " overlay at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing site " + site.name + " overlay at " + + (System.currentTimeMillis() - startTime)); final SharedPreferences activityPrefs = getPreferences(0); final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); @@ -641,7 +656,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants if ((siteOverlay = siteOverlays.get(site)) != null) { } else { - if (pastOverlays != null && (siteOverlay = (PathOverlay) pastOverlays.get(SITE_OVERLAYS + site.name)) != null) { + if (pastOverlays != null + && (siteOverlay = (PathOverlay) pastOverlays.get(SITE_OVERLAYS + site.name)) != null) { Log.i(TAG, "Restored " + site.name + " site overlay"); } else { @@ -667,11 +683,13 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - siteOverlay.setEnabled(activityPrefs.getBoolean(SITE_OVERLAYS + site.name, SITE_OVERLAYS_ENABLED_BY_DEFAULT)); + siteOverlay.setEnabled(activityPrefs.getBoolean(SITE_OVERLAYS + site.name, + SITE_OVERLAYS_ENABLED_BY_DEFAULT)); mapView.postInvalidate(); - Log.i(TAG, "Finished showing site " + site.name + " overlay at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing site " + site.name + " overlay at " + + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -688,8 +706,10 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } else { if (pastOverlays != null - && (residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get(RESIDENTIAL_BUILDING_OVERLAY)) != null) { - nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get(NON_RESIDENTIAL_BUILDING_OVERLAY); + && (residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays + .get(RESIDENTIAL_BUILDING_OVERLAY)) != null) { + nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays + .get(NON_RESIDENTIAL_BUILDING_OVERLAY); Log.i(TAG, "Restored building overlays"); } else { @@ -704,7 +724,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants buildingDao = getHelper().getBuildingDao(); - final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, MODE_PRIVATE); + final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, + MODE_PRIVATE); for (Building building : buildingDao) { if (building.residential == true) { @@ -742,8 +763,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + RESIDENTIAL_BUILDING_OVERLAY, - RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); + residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + + RESIDENTIAL_BUILDING_OVERLAY, RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); nonResidentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + NON_RESIDENTIAL_BUILDING_OVERLAY, NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); @@ -763,11 +784,16 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); if (uniLinkBusStopOverlay != null) { - if (!activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + if (!activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { Log.i(TAG, "Uni-Link bus stop overlay not needed"); overlays.remove(UNI_LINK_BUS_STOP_OVERLAY); @@ -778,25 +804,31 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants uniLinkBusStopOverlay = null; } else { - uniLinkBusStopOverlay.setRoutes(0, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(1, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(2, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(3, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(4, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); } } else { - if (activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + if (activityPrefs + .getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { if (pastOverlays != null - && (uniLinkBusStopOverlay = (BusStopOverlay) pastOverlays.get(UNI_LINK_BUS_STOP_OVERLAY)) != null) { + && (uniLinkBusStopOverlay = (BusStopOverlay) pastOverlays + .get(UNI_LINK_BUS_STOP_OVERLAY)) != null) { Log.i(TAG, "Restored Uni-Link bus stop overlay"); } else { @@ -814,16 +846,16 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - uniLinkBusStopOverlay.setRoutes(0, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(1, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(2, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(3, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(4, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); overlays.put(UNI_LINK_BUS_STOP_OVERLAY, uniLinkBusStopOverlay); @@ -846,7 +878,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private void showNonUniLinkBusStopOverlay() { new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing non uni link bus stop overlays at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing non uni link bus stop overlays at " + + (System.currentTimeMillis() - startTime)); // final SharedPreferences activityPrefs = getPreferences(0); final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); @@ -863,12 +896,14 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } else if (sharedPrefs.getBoolean(NON_UNI_LINK_BUS_STOPS_OVERLAY, NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { if (pastOverlays != null - && (nonUniLinkBusStopOverlay = (BusStopOverlay) pastOverlays.get(NON_UNI_LINK_BUS_STOPS_OVERLAY)) != null) { + && (nonUniLinkBusStopOverlay = (BusStopOverlay) pastOverlays + .get(NON_UNI_LINK_BUS_STOPS_OVERLAY)) != null) { Log.i(TAG, "Restored non Uni-Link bus stop overlays"); } else { try { List busStops; - Log.v(TAG, "Begin fetching non Uni-Link BusStops at " + (System.currentTimeMillis() - startTime)); + Log.v(TAG, "Begin fetching non Uni-Link BusStops at " + + (System.currentTimeMillis() - startTime)); busStops = getHelper().getBusStopDao().queryForEq(BusStop.UNI_LINK_FIELD_NAME, false); @@ -898,7 +933,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants mapView.postInvalidate(); - Log.i(TAG, "Finished showing non Uni-Link bus stop overlays at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing non Uni-Link bus stop overlays at " + + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -915,8 +951,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants // Handle item selection switch (item.getItemId()) { case R.id.menu_find: - Intent i = new Intent(MapActivity.this, FindActivity.class); - startActivityForResult(i, 0); + onSearchRequested(); return true; case R.id.menu_preferences: Intent settingsActivity = new Intent(getBaseContext(), PreferencesActivity.class); @@ -950,8 +985,9 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants }; AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage("GPS is not enabled, do you wish to enable it?").setPositiveButton("Yes", dialogClickListener) - .setNegativeButton("No", dialogClickListener).show(); + builder.setMessage("GPS is not enabled, do you wish to enable it?") + .setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener) + .show(); } return true; @@ -1015,13 +1051,6 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - @Override - public boolean onSearchRequested() { - Intent i = new Intent(MapActivity.this, FindActivity.class); - startActivityForResult(i, 0); - return false; - } - protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.i(TAG, "Got activity result"); if (resultCode == RESULT_OK) { diff --git a/src/net/cbaines/suma/MapContentProvider.java b/src/net/cbaines/suma/MapContentProvider.java new file mode 100644 index 0000000..46986ab --- /dev/null +++ b/src/net/cbaines/suma/MapContentProvider.java @@ -0,0 +1,201 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.cbaines.suma; + +import com.j256.ormlite.android.apptools.OpenHelperManager; + +import android.app.SearchManager; +import android.content.ContentProvider; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.UriMatcher; +import android.database.Cursor; +import android.net.Uri; +import android.provider.BaseColumns; + +/** + * Provides access to the dictionary database. + */ +public class MapContentProvider extends ContentProvider { + String TAG = "MapContentProvider"; + + public static String AUTHORITY = "net.cbaines.suma.provider"; + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/building"); + + // MIME types used for searching words or looking up a single definition + public static final String WORDS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + + "/vnd.example.android.searchabledict"; + public static final String DEFINITION_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + + "/vnd.example.android.searchabledict"; + + private DatabaseHelper helper; + + // UriMatcher stuff + private static final int SEARCH_WORDS = 0; + private static final int GET_WORD = 1; + private static final int SEARCH_SUGGEST = 2; + private static final int REFRESH_SHORTCUT = 3; + private static final UriMatcher sURIMatcher = buildUriMatcher(); + + /** + * Builds up a UriMatcher for search suggestion and shortcut refresh + * queries. + */ + private static UriMatcher buildUriMatcher() { + UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); + // to get definitions... + matcher.addURI(AUTHORITY, "dictionary", SEARCH_WORDS); + matcher.addURI(AUTHORITY, "dictionary/#", GET_WORD); + // to get suggestions... + matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST); + matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST); + + /* + * The following are unused in this implementation, but if we include + * {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column in our + * suggestions table, we could expect to receive refresh queries when a + * shortcutted suggestion is displayed in Quick Search Box, in which + * case, the following Uris would be provided and we would return a + * cursor with a single item representing the refreshed suggestion data. + */ + matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT, REFRESH_SHORTCUT); + matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", REFRESH_SHORTCUT); + return matcher; + } + + @Override + public boolean onCreate() { + helper = OpenHelperManager.getHelper(this.getContext(), DatabaseHelper.class); + return true; + } + + /** + * Handles all the dictionary searches and suggestion queries from the + * Search Manager. When requesting a specific word, the uri alone is + * required. When searching all of the dictionary for matches, the + * selectionArgs argument must carry the search query as the first element. + * All other arguments are ignored. + */ + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { + + // Use the UriMatcher to see what kind of query we have and format the + // db query accordingly + switch (sURIMatcher.match(uri)) { + case SEARCH_SUGGEST: + if (selectionArgs == null) { + throw new IllegalArgumentException("selectionArgs must be provided for the Uri: " + uri); + } + return getSuggestions(selectionArgs[0]); + case SEARCH_WORDS: + if (selectionArgs == null) { + throw new IllegalArgumentException("selectionArgs must be provided for the Uri: " + uri); + } + return search(selectionArgs[0]); + case GET_WORD: + return getWord(uri); + case REFRESH_SHORTCUT: + return refreshShortcut(uri); + default: + throw new IllegalArgumentException("Unknown Uri: " + uri); + } + } + + private Cursor getSuggestions(String query) { + query = query.toLowerCase(); + String[] columns = new String[] { BaseColumns._ID, DictionaryDatabase.KEY_WORD, + DictionaryDatabase.KEY_DEFINITION, + /* + * SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, (only if you want + * to refresh shortcuts) + */ + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID }; + + return mDictionary.getWordMatches(query, columns); + } + + private Cursor search(String query) { + query = query.toLowerCase(); + String[] columns = new String[] { BaseColumns._ID, DictionaryDatabase.KEY_WORD, + DictionaryDatabase.KEY_DEFINITION }; + + return mDictionary.getWordMatches(query, columns); + } + + private Cursor getWord(Uri uri) { + String rowId = uri.getLastPathSegment(); + String[] columns = new String[] { DictionaryDatabase.KEY_WORD, DictionaryDatabase.KEY_DEFINITION }; + + return mDictionary.getWord(rowId, columns); + } + + private Cursor refreshShortcut(Uri uri) { + /* + * This won't be called with the current implementation, but if we + * include {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column + * in our suggestions table, we could expect to receive refresh queries + * when a shortcutted suggestion is displayed in Quick Search Box. In + * which case, this method will query the table for the specific word, + * using the given item Uri and provide all the columns originally + * provided with the suggestion query. + */ + String rowId = uri.getLastPathSegment(); + String[] columns = new String[] { BaseColumns._ID, DictionaryDatabase.KEY_WORD, + DictionaryDatabase.KEY_DEFINITION, SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID }; + + return mDictionary.getWord(rowId, columns); + } + + /** + * This method is required in order to query the supported types. It's also + * useful in our own query() method to determine the type of Uri received. + */ + @Override + public String getType(Uri uri) { + switch (sURIMatcher.match(uri)) { + case SEARCH_WORDS: + return WORDS_MIME_TYPE; + case GET_WORD: + return DEFINITION_MIME_TYPE; + case SEARCH_SUGGEST: + return SearchManager.SUGGEST_MIME_TYPE; + case REFRESH_SHORTCUT: + return SearchManager.SHORTCUT_MIME_TYPE; + default: + throw new IllegalArgumentException("Unknown URL " + uri); + } + } + + // Other required implementations... + + @Override + public Uri insert(Uri uri, ContentValues values) { + throw new UnsupportedOperationException(); + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + throw new UnsupportedOperationException(); + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + throw new UnsupportedOperationException(); + } + +} -- cgit v1.2.3 From a722eac92e7154563bd144ad91b092d23dae8f2a Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Tue, 6 Mar 2012 14:46:01 +0000 Subject: Basic searchable stuff. --- src/net/cbaines/suma/Bus.java | 10 +- src/net/cbaines/suma/BusStopSpecificStopView.java | 3 +- src/net/cbaines/suma/DataManager.java | 95 +++++++---- src/net/cbaines/suma/MapContentProvider.java | 194 ++++++++++++++++------ src/net/cbaines/suma/POI.java | 83 ++++----- 5 files changed, 254 insertions(+), 131 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/Bus.java b/src/net/cbaines/suma/Bus.java index 865384f..34e20a6 100644 --- a/src/net/cbaines/suma/Bus.java +++ b/src/net/cbaines/suma/Bus.java @@ -35,9 +35,6 @@ public class Bus { final static String ROUTE_FIELD_NAME = "route"; final static String DIRECTION_FIELD_NAME = "direction"; - @DatabaseField(generatedId = true) - int gid; - /** * The identification number of the bus. */ @@ -115,7 +112,7 @@ public class Bus { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + gid; + result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @@ -128,7 +125,10 @@ public class Bus { if (getClass() != obj.getClass()) return false; Bus other = (Bus) obj; - if (id != other.id) + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) return false; return true; } diff --git a/src/net/cbaines/suma/BusStopSpecificStopView.java b/src/net/cbaines/suma/BusStopSpecificStopView.java index b7a16cb..eade3fe 100644 --- a/src/net/cbaines/suma/BusStopSpecificStopView.java +++ b/src/net/cbaines/suma/BusStopSpecificStopView.java @@ -149,7 +149,8 @@ public class BusStopSpecificStopView extends LinearLayout implements OnClickList busDao.refresh(stop.bus); busRouteDao.refresh(stop.bus.route); - Log.i("StopView", "Bus route " + stop.bus.route + " Uni-Link " + stop.bus.route.uniLink + " Bus ID " + stop.bus.id); + Log.i("StopView", "Bus route " + stop.bus.route + " Uni-Link " + stop.bus.route.uniLink + " Bus ID " + + stop.bus.id); if (stop.bus.id != null && stop.bus.route.uniLink) { Uri uri = Uri.parse("http://id.southampton.ac.uk/bus/" + stop.bus.id); diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index ab1f3a7..fd47360 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -137,9 +137,13 @@ public class DataManager { /* * Polygon poly = buildingPolys.get(dataBits[1]); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // + * Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } */ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name @@ -163,9 +167,13 @@ public class DataManager { /* * Polygon poly = buildingPolys.get(dataBits[1]); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // + * Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } */ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name @@ -184,13 +192,17 @@ public class DataManager { } /* - * for (Iterator iter = buildingPoints.keySet().iterator(); iter.hasNext();) { String key = iter.next(); + * for (Iterator iter = buildingPoints.keySet().iterator(); + * iter.hasNext();) { String key = iter.next(); * - * Building bdg = new Building(key, buildingPoints.get(key), false); Polygon poly = buildingPolys.get(key); + * Building bdg = new Building(key, buildingPoints.get(key), false); + * Polygon poly = buildingPolys.get(key); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " - * + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } * * buildingDao.create(bdg); } */ @@ -227,18 +239,18 @@ public class DataManager { // Log.i(TAG, "Whole " + dataBits[3] + " First bit " + // quBitsLat[0] + " last bit " + quBitsLat[1]); - double lat = Double.valueOf(quBitsLat[0]) + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) - / 60d; + double lat = Double.valueOf(quBitsLat[0]) + + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d; // Log.i(TAG, "Whole " + dataBits[4] + " First bit " + // quBitsLng[0] + " last bit " + quBitsLng[1]); - double lng = Double.valueOf(quBitsLng[0]) + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) - / 60d; + double lng = Double.valueOf(quBitsLng[0]) + + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d; GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * -1e6)); // Log.i(TAG, "Lat " + point.getLatitudeE6() + " lng " + // point.getLongitudeE6()); - busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2].replace( - "\"", ""), point)); + busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2] + .replace("\"", ""), point)); } @@ -359,26 +371,36 @@ public class DataManager { } /* - * for (Iterator busStopIter = busStopDao.iterator(); busStopIter.hasNext();) { BusStop stop = - * busStopIter.next(); // Log.i(TAG, "Looking at stop " + stop.id); + * for (Iterator busStopIter = busStopDao.iterator(); + * busStopIter.hasNext();) { BusStop stop = busStopIter.next(); // + * Log.i(TAG, "Looking at stop " + stop.id); * * - * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); + * QueryBuilder routeStopsQueryBuilder = + * routeStopsDao.queryBuilder(); * routeStopsQueryBuilder.where().eq(columnName, value) * - * DeleteBuilder deleteBuilder = busStopDao.deleteBuilder(); // only delete the rows where password is - * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) accountDao.delete(deleteBuilder.prepare()); + * DeleteBuilder deleteBuilder = + * busStopDao.deleteBuilder(); // only delete the rows where password is + * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) + * accountDao.delete(deleteBuilder.prepare()); * * - * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); - * routeStopsQueryBuilder.setCountOf(true); routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, stop); + * QueryBuilder routeStopsQueryBuilder = + * routeStopsDao.queryBuilder(); + * routeStopsQueryBuilder.setCountOf(true); + * routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, + * stop); * - * PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); List routeStops = - * routeStopsDao.query(routeStopsPreparedQuery); // long num = routeStopsDao.query(routeStopsPreparedQuery).size(); // - * Log.i(TAG, "Number is " + num); + * PreparedQuery routeStopsPreparedQuery = + * routeStopsQueryBuilder.prepare(); List routeStops = + * routeStopsDao.query(routeStopsPreparedQuery); // long num = + * routeStopsDao.query(routeStopsPreparedQuery).size(); // Log.i(TAG, + * "Number is " + num); * - * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if (routeStop.busRoute.uniLink) { stop.uniLink = true; - * } } busStopDao.update(stop); } + * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if + * (routeStop.busRoute.uniLink) { stop.uniLink = true; } } + * busStopDao.update(stop); } */ Log.i(TAG, "Finished loading bus data"); @@ -438,8 +460,6 @@ public class DataManager { Dao routeStopsDao = null; if (routeStopsDao == null) routeStopsDao = helper.getRouteStopsDao(); - if (busDao == null) - busDao = helper.getBusDao(); if (busStopDao == null) busStopDao = helper.getBusStopDao(); @@ -554,9 +574,11 @@ public class DataManager { List routeStops = routeStopsDao.query(routeStopsPreparedQuery); if (routeStops.size() > 0) { - Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + route.code); + Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + + route.code); } else { - Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + route.code); + Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + + route.code); } } @@ -642,7 +664,8 @@ public class DataManager { if (route != null) { busRoutes.add(route); } else { - throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " + key); + throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " + + key); } } @@ -750,8 +773,8 @@ public class DataManager { continue; } - Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " - + stop.arivalTime); + Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + + " at " + stop.arivalTime); timetable.add(stop); diff --git a/src/net/cbaines/suma/MapContentProvider.java b/src/net/cbaines/suma/MapContentProvider.java index 46986ab..a2b42c3 100644 --- a/src/net/cbaines/suma/MapContentProvider.java +++ b/src/net/cbaines/suma/MapContentProvider.java @@ -16,7 +16,8 @@ package net.cbaines.suma; -import com.j256.ormlite.android.apptools.OpenHelperManager; +import java.sql.SQLException; +import java.util.List; import android.app.SearchManager; import android.content.ContentProvider; @@ -24,8 +25,17 @@ import android.content.ContentResolver; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; +import android.database.MatrixCursor; import android.net.Uri; import android.provider.BaseColumns; +import android.util.Log; + +import com.j256.ormlite.android.AndroidCompiledStatement; +import com.j256.ormlite.android.apptools.OpenHelperManager; +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.stmt.PreparedQuery; +import com.j256.ormlite.stmt.QueryBuilder; +import com.j256.ormlite.stmt.StatementBuilder.StatementType; /** * Provides access to the dictionary database. @@ -34,21 +44,40 @@ public class MapContentProvider extends ContentProvider { String TAG = "MapContentProvider"; public static String AUTHORITY = "net.cbaines.suma.provider"; - public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/building"); + // public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + // + "/building"); // MIME types used for searching words or looking up a single definition - public static final String WORDS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE - + "/vnd.example.android.searchabledict"; - public static final String DEFINITION_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE - + "/vnd.example.android.searchabledict"; + public static final String BUILDINGS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + + "/vnd.net.cbaines.suma.provider.building"; + public static final String BUILDING_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + + "/vnd.net.cbaines.suma.provider.building"; + public static final String BUS_STOPS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + + "/vnd.net.cbaines.suma.provider.bus-stop"; + public static final String BUS_STOP_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + + "/vnd.net.cbaines.suma.provider.bus-stop"; + public static final String SITES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + + "/vnd.net.cbaines.suma.provider.site"; + public static final String SITE_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + + "/vnd.net.cbaines.suma.provider.site"; + public static final String BUSES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + + "/vnd.net.cbaines.suma.provider.bus"; + public static final String BUS_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + + "/vnd.net.cbaines.suma.provider.bus"; private DatabaseHelper helper; // UriMatcher stuff - private static final int SEARCH_WORDS = 0; - private static final int GET_WORD = 1; - private static final int SEARCH_SUGGEST = 2; - private static final int REFRESH_SHORTCUT = 3; + private static final int SEARCH_BUILDINGS = 0; + private static final int GET_BUILDING = 1; + private static final int SEARCH_BUS_STOPS = 2; + private static final int GET_BUS_STOP = 3; + private static final int SEARCH_SITES = 4; + private static final int GET_SITE = 5; + private static final int SEARCH_BUSES = 6; + private static final int GET_BUS = 7; + private static final int SEARCH_SUGGEST = 8; + private static final int REFRESH_SHORTCUT = 9; private static final UriMatcher sURIMatcher = buildUriMatcher(); /** @@ -58,8 +87,15 @@ public class MapContentProvider extends ContentProvider { private static UriMatcher buildUriMatcher() { UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); // to get definitions... - matcher.addURI(AUTHORITY, "dictionary", SEARCH_WORDS); - matcher.addURI(AUTHORITY, "dictionary/#", GET_WORD); + matcher.addURI(AUTHORITY, "building", SEARCH_BUILDINGS); + matcher.addURI(AUTHORITY, "building/*", GET_BUILDING); + matcher.addURI(AUTHORITY, "bus-stop", SEARCH_BUS_STOPS); + matcher.addURI(AUTHORITY, "bus-stop/*", GET_BUS_STOP); + matcher.addURI(AUTHORITY, "site", SEARCH_SITES); + matcher.addURI(AUTHORITY, "site/*", GET_SITE); + matcher.addURI(AUTHORITY, "bus", SEARCH_BUSES); + matcher.addURI(AUTHORITY, "bus/*", GET_BUS); + // to get suggestions... matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST); matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST); @@ -100,50 +136,94 @@ public class MapContentProvider extends ContentProvider { if (selectionArgs == null) { throw new IllegalArgumentException("selectionArgs must be provided for the Uri: " + uri); } - return getSuggestions(selectionArgs[0]); - case SEARCH_WORDS: + try { + return getSuggestions(selectionArgs[0]); + } catch (SQLException e1) { + e1.printStackTrace(); + } + case SEARCH_BUILDINGS: if (selectionArgs == null) { throw new IllegalArgumentException("selectionArgs must be provided for the Uri: " + uri); } - return search(selectionArgs[0]); - case GET_WORD: - return getWord(uri); + try { + return searchBuildings(selectionArgs[0]); + } catch (SQLException e) { + e.printStackTrace(); + } + case GET_BUILDING: + try { + return getBuilding(uri); + } catch (SQLException e) { + e.printStackTrace(); + } case REFRESH_SHORTCUT: - return refreshShortcut(uri); + try { + return refreshShortcut(uri); + } catch (SQLException e) { + e.printStackTrace(); + } default: throw new IllegalArgumentException("Unknown Uri: " + uri); } } - private Cursor getSuggestions(String query) { - query = query.toLowerCase(); - String[] columns = new String[] { BaseColumns._ID, DictionaryDatabase.KEY_WORD, - DictionaryDatabase.KEY_DEFINITION, - /* - * SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, (only if you want - * to refresh shortcuts) - */ - SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID }; - - return mDictionary.getWordMatches(query, columns); + private Cursor getSuggestions(String query) throws SQLException { + Log.v(TAG, "Got query for " + query); + + Dao buildingDao = helper.getBuildingDao(); + + QueryBuilder qb = buildingDao.queryBuilder(); + qb.where().like(Building.ID_FIELD_NAME, "%" + query + "%").or() + .like(Building.NAME_FIELD_NAME, "%" + query + "%"); + PreparedQuery preparedQuery = qb.prepare(); + + List buildings = buildingDao.query(preparedQuery); + Log.v(TAG, "Returning " + buildings.size() + " buildings"); + + String[] columnNames = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1 }; + + MatrixCursor cursor = new MatrixCursor(columnNames, buildings.size()); + + int id = 0; + for (Building building : buildings) { + Log.v(TAG, "Building " + id + ", " + building.name); + Object[] values = { id++, building.name }; + cursor.addRow(values); + } + + return cursor; } - private Cursor search(String query) { - query = query.toLowerCase(); - String[] columns = new String[] { BaseColumns._ID, DictionaryDatabase.KEY_WORD, - DictionaryDatabase.KEY_DEFINITION }; + private Cursor searchBuildings(String query) throws SQLException { + Dao buildingDao = helper.getBuildingDao(); + + QueryBuilder qb = buildingDao.queryBuilder(); + qb.where().eq(Building.ID_FIELD_NAME, "%" + query + "%").or().eq(Building.NAME_FIELD_NAME, "%" + query + "%"); + PreparedQuery preparedQuery = qb.prepare(); - return mDictionary.getWordMatches(query, columns); + AndroidCompiledStatement compiledStatement = (AndroidCompiledStatement) preparedQuery.compile(helper + .getConnectionSource().getReadOnlyConnection(), StatementType.SELECT); + Cursor cursor = compiledStatement.getCursor(); + + return cursor; } - private Cursor getWord(Uri uri) { - String rowId = uri.getLastPathSegment(); - String[] columns = new String[] { DictionaryDatabase.KEY_WORD, DictionaryDatabase.KEY_DEFINITION }; + private Cursor getBuilding(Uri uri) throws SQLException { + String buildingID = uri.getLastPathSegment(); + Dao buildingDao = helper.getBuildingDao(); + + QueryBuilder qb = buildingDao.queryBuilder(); + qb.where().eq(Building.ID_FIELD_NAME, buildingID); + PreparedQuery preparedQuery = qb.prepare(); + + AndroidCompiledStatement compiledStatement = (AndroidCompiledStatement) preparedQuery.compile(helper + .getConnectionSource().getReadOnlyConnection(), StatementType.SELECT); + Cursor cursor = compiledStatement.getCursor(); - return mDictionary.getWord(rowId, columns); + return cursor; } - private Cursor refreshShortcut(Uri uri) { + private Cursor refreshShortcut(Uri uri) throws SQLException { /* * This won't be called with the current implementation, but if we * include {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column @@ -153,12 +233,18 @@ public class MapContentProvider extends ContentProvider { * using the given item Uri and provide all the columns originally * provided with the suggestion query. */ - String rowId = uri.getLastPathSegment(); - String[] columns = new String[] { BaseColumns._ID, DictionaryDatabase.KEY_WORD, - DictionaryDatabase.KEY_DEFINITION, SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, - SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID }; + String buildingID = uri.getLastPathSegment(); + Dao buildingDao = helper.getBuildingDao(); + + QueryBuilder qb = buildingDao.queryBuilder(); + qb.where().eq(Building.ID_FIELD_NAME, buildingID); + PreparedQuery preparedQuery = qb.prepare(); + + AndroidCompiledStatement compiledStatement = (AndroidCompiledStatement) preparedQuery.compile(helper + .getConnectionSource().getReadOnlyConnection(), StatementType.SELECT); + Cursor cursor = compiledStatement.getCursor(); - return mDictionary.getWord(rowId, columns); + return cursor; } /** @@ -168,10 +254,22 @@ public class MapContentProvider extends ContentProvider { @Override public String getType(Uri uri) { switch (sURIMatcher.match(uri)) { - case SEARCH_WORDS: - return WORDS_MIME_TYPE; - case GET_WORD: - return DEFINITION_MIME_TYPE; + case SEARCH_BUILDINGS: + return BUILDINGS_MIME_TYPE; + case GET_BUILDING: + return BUILDING_MIME_TYPE; + case SEARCH_BUS_STOPS: + return BUS_STOPS_MIME_TYPE; + case GET_BUS_STOP: + return BUS_STOP_MIME_TYPE; + case SEARCH_SITES: + return SITES_MIME_TYPE; + case GET_SITE: + return SITE_MIME_TYPE; + case SEARCH_BUSES: + return BUSES_MIME_TYPE; + case GET_BUS: + return BUS_MIME_TYPE; case SEARCH_SUGGEST: return SearchManager.SUGGEST_MIME_TYPE; case REFRESH_SHORTCUT: diff --git a/src/net/cbaines/suma/POI.java b/src/net/cbaines/suma/POI.java index 485845c..f8c1f89 100644 --- a/src/net/cbaines/suma/POI.java +++ b/src/net/cbaines/suma/POI.java @@ -25,54 +25,55 @@ import com.j256.ormlite.field.DataType; import com.j256.ormlite.field.DatabaseField; public abstract class POI { - public static final String BUS_STOP = "busstop"; - public static final String BUILDING = "building"; - public static final String WAYPOINT = "waypoint"; - public static final String SITE = "site"; + public static final String BUS_STOP = "busstop"; + public static final String BUILDING = "building"; + public static final String WAYPOINT = "waypoint"; + public static final String SITE = "site"; - public static final String ID_FIELD_NAME = "id"; - public static final String POINT_FIELD_NAME = "point"; + public static final String ID_FIELD_NAME = "id"; + public static final String POINT_FIELD_NAME = "point"; - POI() { - } + POI() { + } - public POI(String id, GeoPoint point) { - this.id = id; - this.point = point; - } + public POI(String id, GeoPoint point) { + this.id = id; + this.point = point; + } - @DatabaseField(dataType = DataType.SERIALIZABLE, canBeNull = false) - public GeoPoint point; + @DatabaseField(dataType = DataType.SERIALIZABLE, canBeNull = false) + public GeoPoint point; - @DatabaseField(id = true) - public String id; + @DatabaseField(id = true) + public String id; - public int distTo = -1; // Used by the comparator to store distances, then later by the gui to display them. + public int distTo = -1; // Used by the comparator to store distances, then + // later by the gui to display them. - public String type; + public String type; - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((id == null) ? 0 : id.hashCode()); - return result; - } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - POI other = (POI) obj; - if (id == null) { - if (other.id != null) - return false; - } else if (!id.equals(other.id)) - return false; - return true; - } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + POI other = (POI) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } } -- cgit v1.2.3 From 4a45362d8823a84cf9c3c805c006331303a0da33 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Tue, 6 Mar 2012 15:11:31 +0000 Subject: Bumped android:minSdkVersion to 8, as implementing search stuff. --- src/net/cbaines/suma/Bus.java | 3 + src/net/cbaines/suma/FindActivity.java | 380 ------------------------------- src/net/cbaines/suma/MapActivity.java | 2 +- src/net/cbaines/suma/SearchActivity.java | 380 +++++++++++++++++++++++++++++++ 4 files changed, 384 insertions(+), 381 deletions(-) delete mode 100644 src/net/cbaines/suma/FindActivity.java create mode 100644 src/net/cbaines/suma/SearchActivity.java (limited to 'src') diff --git a/src/net/cbaines/suma/Bus.java b/src/net/cbaines/suma/Bus.java index 34e20a6..9894e6b 100644 --- a/src/net/cbaines/suma/Bus.java +++ b/src/net/cbaines/suma/Bus.java @@ -35,6 +35,9 @@ public class Bus { final static String ROUTE_FIELD_NAME = "route"; final static String DIRECTION_FIELD_NAME = "direction"; + @DatabaseField(generatedId = true) + int gid; + /** * The identification number of the bus. */ diff --git a/src/net/cbaines/suma/FindActivity.java b/src/net/cbaines/suma/FindActivity.java deleted file mode 100644 index 5a07807..0000000 --- a/src/net/cbaines/suma/FindActivity.java +++ /dev/null @@ -1,380 +0,0 @@ -/* - * 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 java.util.Collections; -import java.util.Iterator; -import java.util.List; - -import org.osmdroid.util.GeoPoint; - -import android.app.SearchManager; -import android.content.Context; -import android.content.Intent; -import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.net.Uri; -import android.os.Bundle; -import android.text.Editable; -import android.text.TextWatcher; -import android.util.Log; -import android.view.Gravity; -import android.view.View; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.AdapterView.OnItemLongClickListener; -import android.widget.EditText; -import android.widget.LinearLayout; -import android.widget.ListView; -import android.widget.ProgressBar; - -import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; -import com.j256.ormlite.dao.Dao; -import com.j256.ormlite.stmt.PreparedQuery; -import com.j256.ormlite.stmt.QueryBuilder; - -public class FindActivity extends OrmLiteBaseActivity implements Runnable, TextWatcher, - OnItemClickListener, LocationListener, OnItemLongClickListener { - - final static String TAG = "FindActivity"; - - public static final String ORIGIN = "o"; - public static final int MAP_ACTIVITY = 0; - - private EditText searchBar; - private ListView listItems; - private ProgressBar progBar; - private LinearLayout findContentLayout; - - private String searchTerm = ""; - - private Dao buildingDao; - private Dao busStopDao; - private Dao siteDao; - - private POIArrayAdapter adapter; - - private GeoPoint userLocation; - - private Thread searchThread; - - ArrayList getNearestPOIs(int distance) { - Log.i(TAG, "Getting nearest POI's"); - ArrayList nearestPOIs = new ArrayList(); - for (Iterator buildingDaoIter = buildingDao.iterator(); buildingDaoIter.hasNext();) { - POI poi = buildingDaoIter.next(); - int dist = poi.point.distanceTo(userLocation); - if (dist < distance) { - poi.distTo = dist; - nearestPOIs.add(poi); - } - } - - for (Iterator busStopDaoIter = busStopDao.iterator(); busStopDaoIter.hasNext();) { - POI poi = busStopDaoIter.next(); - int dist = poi.point.distanceTo(userLocation); - if (dist < distance) { - poi.distTo = dist; - nearestPOIs.add(poi); - } - } - - Collections.sort(nearestPOIs, new POIDistanceComparator(userLocation, true)); - - Log.i(TAG, "Got " + nearestPOIs.size() + " nearest POI's"); - return nearestPOIs; - } - - // Search thread - public void run() { - POIArrayAdapter tempAdaptor; - GeoPoint thisUserLocation = userLocation; - - Log.i(TAG, "Search thread started"); - String thisSearchTerm = searchTerm; - - ArrayList foundPOIsArray = null; - - Log.i(TAG, "Search term length " + thisSearchTerm.length() + " userLocation == null " - + (thisUserLocation == null)); - if (thisSearchTerm.length() == 0 && thisUserLocation != null) { - foundPOIsArray = getNearestPOIs(200); - - } - - if (foundPOIsArray != null && foundPOIsArray.size() != 0) { - - tempAdaptor = new POIArrayAdapter(this, foundPOIsArray); - } else { - - try { - - foundPOIsArray = new ArrayList(); - - if (thisSearchTerm.length() == 0) { - for (Building building : buildingDao) { - foundPOIsArray.add(building); - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - for (BusStop busStop : busStopDao) { - foundPOIsArray.add(busStop); - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - for (Site site : siteDao) { - foundPOIsArray.add(site); - } - - } else { - - QueryBuilder buildingQueryBuilder = buildingDao.queryBuilder(); - buildingQueryBuilder.where().like(Building.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() - .like(Building.NAME_FIELD_NAME, "%" + thisSearchTerm + "%"); - PreparedQuery buildingPreparedQuery = buildingQueryBuilder.prepare(); - List buildings = buildingDao.query(buildingPreparedQuery); - for (Building building : buildings) { - foundPOIsArray.add(building); - } - buildings = null; - - if (!thisSearchTerm.equals(searchTerm)) - return; - - if (thisSearchTerm.contains("site")) { - for (Site site : siteDao) { - foundPOIsArray.add(site); - } - } else { - QueryBuilder siteQueryBuilder = siteDao.queryBuilder(); - siteQueryBuilder.where().like(Site.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() - .like(Site.NAME_FIELD_NAME, "%" + thisSearchTerm + "%"); - PreparedQuery sitePreparedQuery = siteQueryBuilder.prepare(); - List sites = siteDao.query(sitePreparedQuery); - for (Site site : sites) { - foundPOIsArray.add(site); - } - sites = null; - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - // if (thisSearchTerm.contains("bus")) { - // for (BusStop busStop : busStopDao) { - // foundPOIsArray.add(busStop); - // } - // } else { - QueryBuilder busStopQueryBuilder = busStopDao.queryBuilder(); - busStopQueryBuilder.where().like(BusStop.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() - .like(BusStop.DESCRIPTION_FIELD_NAME, "%" + thisSearchTerm + "%"); - PreparedQuery busStopPreparedQuery = busStopQueryBuilder.prepare(); - List busStops = busStopDao.query(busStopPreparedQuery); - for (BusStop busStop : busStops) { - foundPOIsArray.add(busStop); - } - busStops = null; - // } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - Log.i(TAG, "Found " + foundPOIsArray.size() + " pois"); - - if (thisUserLocation != null) { - Collections.sort(foundPOIsArray, new POIDistanceComparator(userLocation)); - } else { - Collections.sort(foundPOIsArray, new StringDistanceComparator(thisSearchTerm)); - } - - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - tempAdaptor = new POIArrayAdapter(this, foundPOIsArray); - - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return; - } - } - - if (thisSearchTerm.equals(searchTerm)) { - Log.i(TAG, "Search terms still equal, starting post"); - adapter = tempAdaptor; - listItems.post(new Runnable() { - public void run() { - listItems.setAdapter(adapter); - if (progBar.getVisibility() != View.GONE) { - progBar.setVisibility(View.GONE); - findContentLayout.setGravity(Gravity.TOP); - } - } - }); - } else { - Log.i(TAG, "Search terms no longer equal, exiting"); - } - } - - /** Called when the activity is first created. */ - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.find_activity); - - Log.i(TAG, "FindActivity started"); - - searchBar = (EditText) findViewById(R.id.searchBar); - searchBar.addTextChangedListener(this); - - Intent intent = getIntent(); - if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - String query = intent.getStringExtra(SearchManager.QUERY); - Log.i(TAG, "Searching for " + query); - searchBar.setText(query); - } - - listItems = (ListView) findViewById(R.id.findListItems); - listItems.setOnItemClickListener(this); - listItems.setOnItemLongClickListener(this); - - progBar = (ProgressBar) findViewById(R.id.findLoadBar); - findContentLayout = (LinearLayout) findViewById(R.id.findContentLayout); - - try { - buildingDao = getHelper().getBuildingDao(); - busStopDao = getHelper().getBusStopDao(); - siteDao = getHelper().getSiteDao(); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - // Acquire a reference to the system Location Manager - LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); - Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); - if (lastKnownLocation != null) { - userLocation = Util.locationToGeoPoint(lastKnownLocation); - } else { - lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); - if (lastKnownLocation != null) { - userLocation = Util.locationToGeoPoint(lastKnownLocation); - - } - } - // Register the listener with the Location Manager to receive location - // updates - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, this); - try { - locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, this); - } catch (Exception e) { - // Ignore anything that goes wrong here... - } - - searchThread = new Thread(this); - searchThread.start(); - - } - - public void afterTextChanged(Editable s) { - searchTerm = s.toString(); - Log.i(TAG, "Text changed " + searchTerm + " starting search thread"); - new Thread(this).start(); - } - - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - public void onItemClick(AdapterView parent, View view, int position, long id) { - Log.i(TAG, "OnItemClick pos " + position + " id " + id); - - POI poi = adapter.getPOIItem(position); - - 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); - } - - public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { - - POI poi = adapter.getPOIItem(position); - - Log.i(TAG, "Long Click Event ID: " + poi.id); - Uri uri = null; - - if (poi.type.equals(POI.BUS_STOP)) { - Log.i(TAG, "Its a bus stop"); - - BusStop busStop = (BusStop) poi; - - uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id); - - } else if (poi.type.equals(POI.BUILDING)) { - Log.i(TAG, "Its a buildings"); - - Building building = (Building) poi; - - uri = Uri.parse("http://id.southampton.ac.uk/building/" + building.id); - } else { - Log.e(TAG, "Error in onItemLongClick"); - return false; - } - - Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); - - Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri); - startActivity(busStopIntent); - - return true; - } - - public void onLocationChanged(Location location) { - Log.i(TAG, "Got location update for FindActivity"); - userLocation = Util.locationToGeoPoint(location); - if (!searchThread.isAlive()) { - searchThread = new Thread(this); - searchThread.start(); - } - } - - public void onProviderDisabled(String arg0) { - } - - public void onProviderEnabled(String provider) { - } - - public void onStatusChanged(String provider, int status, Bundle extras) { - } -} \ No newline at end of file diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 6c4664e..e966b57 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -326,7 +326,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants @Override public boolean onSearchRequested() { Bundle appData = new Bundle(); - appData.putInt(FindActivity.ORIGIN, FindActivity.MAP_ACTIVITY); + appData.putInt(SearchActivity.ORIGIN, SearchActivity.MAP_ACTIVITY); startSearch(null, false, appData, false); return true; } diff --git a/src/net/cbaines/suma/SearchActivity.java b/src/net/cbaines/suma/SearchActivity.java new file mode 100644 index 0000000..f7f5e55 --- /dev/null +++ b/src/net/cbaines/suma/SearchActivity.java @@ -0,0 +1,380 @@ +/* + * 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 java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.osmdroid.util.GeoPoint; + +import android.app.SearchManager; +import android.content.Context; +import android.content.Intent; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.net.Uri; +import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Log; +import android.view.Gravity; +import android.view.View; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.AdapterView.OnItemLongClickListener; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.ListView; +import android.widget.ProgressBar; + +import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.stmt.PreparedQuery; +import com.j256.ormlite.stmt.QueryBuilder; + +public class SearchActivity extends OrmLiteBaseActivity implements Runnable, TextWatcher, + OnItemClickListener, LocationListener, OnItemLongClickListener { + + final static String TAG = "FindActivity"; + + public static final String ORIGIN = "o"; + public static final int MAP_ACTIVITY = 0; + + private EditText searchBar; + private ListView listItems; + private ProgressBar progBar; + private LinearLayout findContentLayout; + + private String searchTerm = ""; + + private Dao buildingDao; + private Dao busStopDao; + private Dao siteDao; + + private POIArrayAdapter adapter; + + private GeoPoint userLocation; + + private Thread searchThread; + + ArrayList getNearestPOIs(int distance) { + Log.i(TAG, "Getting nearest POI's"); + ArrayList nearestPOIs = new ArrayList(); + for (Iterator buildingDaoIter = buildingDao.iterator(); buildingDaoIter.hasNext();) { + POI poi = buildingDaoIter.next(); + int dist = poi.point.distanceTo(userLocation); + if (dist < distance) { + poi.distTo = dist; + nearestPOIs.add(poi); + } + } + + for (Iterator busStopDaoIter = busStopDao.iterator(); busStopDaoIter.hasNext();) { + POI poi = busStopDaoIter.next(); + int dist = poi.point.distanceTo(userLocation); + if (dist < distance) { + poi.distTo = dist; + nearestPOIs.add(poi); + } + } + + Collections.sort(nearestPOIs, new POIDistanceComparator(userLocation, true)); + + Log.i(TAG, "Got " + nearestPOIs.size() + " nearest POI's"); + return nearestPOIs; + } + + // Search thread + public void run() { + POIArrayAdapter tempAdaptor; + GeoPoint thisUserLocation = userLocation; + + Log.i(TAG, "Search thread started"); + String thisSearchTerm = searchTerm; + + ArrayList foundPOIsArray = null; + + Log.i(TAG, "Search term length " + thisSearchTerm.length() + " userLocation == null " + + (thisUserLocation == null)); + if (thisSearchTerm.length() == 0 && thisUserLocation != null) { + foundPOIsArray = getNearestPOIs(200); + + } + + if (foundPOIsArray != null && foundPOIsArray.size() != 0) { + + tempAdaptor = new POIArrayAdapter(this, foundPOIsArray); + } else { + + try { + + foundPOIsArray = new ArrayList(); + + if (thisSearchTerm.length() == 0) { + for (Building building : buildingDao) { + foundPOIsArray.add(building); + } + + if (!thisSearchTerm.equals(searchTerm)) + return; + + for (BusStop busStop : busStopDao) { + foundPOIsArray.add(busStop); + } + + if (!thisSearchTerm.equals(searchTerm)) + return; + + for (Site site : siteDao) { + foundPOIsArray.add(site); + } + + } else { + + QueryBuilder buildingQueryBuilder = buildingDao.queryBuilder(); + buildingQueryBuilder.where().like(Building.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() + .like(Building.NAME_FIELD_NAME, "%" + thisSearchTerm + "%"); + PreparedQuery buildingPreparedQuery = buildingQueryBuilder.prepare(); + List buildings = buildingDao.query(buildingPreparedQuery); + for (Building building : buildings) { + foundPOIsArray.add(building); + } + buildings = null; + + if (!thisSearchTerm.equals(searchTerm)) + return; + + if (thisSearchTerm.contains("site")) { + for (Site site : siteDao) { + foundPOIsArray.add(site); + } + } else { + QueryBuilder siteQueryBuilder = siteDao.queryBuilder(); + siteQueryBuilder.where().like(Site.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() + .like(Site.NAME_FIELD_NAME, "%" + thisSearchTerm + "%"); + PreparedQuery sitePreparedQuery = siteQueryBuilder.prepare(); + List sites = siteDao.query(sitePreparedQuery); + for (Site site : sites) { + foundPOIsArray.add(site); + } + sites = null; + } + + if (!thisSearchTerm.equals(searchTerm)) + return; + + // if (thisSearchTerm.contains("bus")) { + // for (BusStop busStop : busStopDao) { + // foundPOIsArray.add(busStop); + // } + // } else { + QueryBuilder busStopQueryBuilder = busStopDao.queryBuilder(); + busStopQueryBuilder.where().like(BusStop.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() + .like(BusStop.DESCRIPTION_FIELD_NAME, "%" + thisSearchTerm + "%"); + PreparedQuery busStopPreparedQuery = busStopQueryBuilder.prepare(); + List busStops = busStopDao.query(busStopPreparedQuery); + for (BusStop busStop : busStops) { + foundPOIsArray.add(busStop); + } + busStops = null; + // } + + if (!thisSearchTerm.equals(searchTerm)) + return; + + Log.i(TAG, "Found " + foundPOIsArray.size() + " pois"); + + if (thisUserLocation != null) { + Collections.sort(foundPOIsArray, new POIDistanceComparator(userLocation)); + } else { + Collections.sort(foundPOIsArray, new StringDistanceComparator(thisSearchTerm)); + } + + } + + if (!thisSearchTerm.equals(searchTerm)) + return; + + tempAdaptor = new POIArrayAdapter(this, foundPOIsArray); + + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return; + } + } + + if (thisSearchTerm.equals(searchTerm)) { + Log.i(TAG, "Search terms still equal, starting post"); + adapter = tempAdaptor; + listItems.post(new Runnable() { + public void run() { + listItems.setAdapter(adapter); + if (progBar.getVisibility() != View.GONE) { + progBar.setVisibility(View.GONE); + findContentLayout.setGravity(Gravity.TOP); + } + } + }); + } else { + Log.i(TAG, "Search terms no longer equal, exiting"); + } + } + + /** Called when the activity is first created. */ + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.find_activity); + + Log.i(TAG, "FindActivity started"); + + searchBar = (EditText) findViewById(R.id.searchBar); + searchBar.addTextChangedListener(this); + + Intent intent = getIntent(); + if (Intent.ACTION_SEARCH.equals(intent.getAction())) { + String query = intent.getStringExtra(SearchManager.QUERY); + Log.i(TAG, "Searching for " + query); + searchBar.setText(query); + } + + listItems = (ListView) findViewById(R.id.findListItems); + listItems.setOnItemClickListener(this); + listItems.setOnItemLongClickListener(this); + + progBar = (ProgressBar) findViewById(R.id.findLoadBar); + findContentLayout = (LinearLayout) findViewById(R.id.findContentLayout); + + try { + buildingDao = getHelper().getBuildingDao(); + busStopDao = getHelper().getBusStopDao(); + siteDao = getHelper().getSiteDao(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // Acquire a reference to the system Location Manager + LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); + Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); + if (lastKnownLocation != null) { + userLocation = Util.locationToGeoPoint(lastKnownLocation); + } else { + lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + if (lastKnownLocation != null) { + userLocation = Util.locationToGeoPoint(lastKnownLocation); + + } + } + // Register the listener with the Location Manager to receive location + // updates + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, this); + try { + locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, this); + } catch (Exception e) { + // Ignore anything that goes wrong here... + } + + searchThread = new Thread(this); + searchThread.start(); + + } + + public void afterTextChanged(Editable s) { + searchTerm = s.toString(); + Log.i(TAG, "Text changed " + searchTerm + " starting search thread"); + new Thread(this).start(); + } + + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + public void onItemClick(AdapterView parent, View view, int position, long id) { + Log.i(TAG, "OnItemClick pos " + position + " id " + id); + + POI poi = adapter.getPOIItem(position); + + 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); + } + + public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { + + POI poi = adapter.getPOIItem(position); + + Log.i(TAG, "Long Click Event ID: " + poi.id); + Uri uri = null; + + if (poi.type.equals(POI.BUS_STOP)) { + Log.i(TAG, "Its a bus stop"); + + BusStop busStop = (BusStop) poi; + + uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id); + + } else if (poi.type.equals(POI.BUILDING)) { + Log.i(TAG, "Its a buildings"); + + Building building = (Building) poi; + + uri = Uri.parse("http://id.southampton.ac.uk/building/" + building.id); + } else { + Log.e(TAG, "Error in onItemLongClick"); + return false; + } + + Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); + + Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri); + startActivity(busStopIntent); + + return true; + } + + public void onLocationChanged(Location location) { + Log.i(TAG, "Got location update for FindActivity"); + userLocation = Util.locationToGeoPoint(location); + if (!searchThread.isAlive()) { + searchThread = new Thread(this); + searchThread.start(); + } + } + + public void onProviderDisabled(String arg0) { + } + + public void onProviderEnabled(String provider) { + } + + public void onStatusChanged(String provider, int status, Bundle extras) { + } +} \ No newline at end of file -- cgit v1.2.3 From ac9a84e8bb5dc43a5d72840eaa325c5e99ac610a Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 8 Mar 2012 22:03:48 +0000 Subject: Beginings of a welcome dialog, and overlay improvements. --- src/net/cbaines/suma/BuildingNumOverlay.java | 58 +++++++++++++--------------- src/net/cbaines/suma/BusStopOverlay.java | 33 +++++++++------- src/net/cbaines/suma/MapActivity.java | 7 ++++ src/net/cbaines/suma/WelcomeDialog.java | 48 +++++++++++++++++++++++ 4 files changed, 101 insertions(+), 45 deletions(-) create mode 100644 src/net/cbaines/suma/WelcomeDialog.java (limited to 'src') diff --git a/src/net/cbaines/suma/BuildingNumOverlay.java b/src/net/cbaines/suma/BuildingNumOverlay.java index f11a166..b541aa1 100644 --- a/src/net/cbaines/suma/BuildingNumOverlay.java +++ b/src/net/cbaines/suma/BuildingNumOverlay.java @@ -94,33 +94,24 @@ public class BuildingNumOverlay extends Overlay implements Preferences, OnShared } /** - * Draw a marker on each of our items. populate() must have been called - * first.
+ * Draw a marker on each of our items. populate() must have been called first.
*
- * The marker will be drawn twice for each Item in the Overlay--once in the - * shadow phase, skewed and darkened, then again in the non-shadow phase. - * The bottom-center of the marker will be aligned with the geographical - * coordinates of the Item.
+ * The marker will be drawn twice for each Item in the Overlay--once in the shadow phase, skewed and darkened, then again in + * the non-shadow phase. The bottom-center of the marker will be aligned with the geographical coordinates of the Item.
*
- * The order of drawing may be changed by overriding the getIndexToDraw(int) - * method. An item may provide an alternate marker via its - * OverlayItem.getMarker(int) method. If that method returns null, the - * default marker is used.
+ * The order of drawing may be changed by overriding the getIndexToDraw(int) method. An item may provide an alternate marker + * via its OverlayItem.getMarker(int) method. If that method returns null, the default marker is used.
*
- * The focused item is always drawn last, which puts it visually on top of - * the other items.
+ * The focused item is always drawn last, which puts it visually on top of the other items.
* * @param canvas - * the Canvas upon which to draw. Note that this may already have - * a transformation applied, so be sure to leave it the way you - * found it + * the Canvas upon which to draw. Note that this may already have a transformation applied, so be sure to leave it + * the way you found it * @param mapView - * the MapView that requested the draw. Use - * MapView.getProjection() to convert between on-screen pixels - * and latitude/longitude pairs + * the MapView that requested the draw. Use MapView.getProjection() to convert between on-screen pixels and + * latitude/longitude pairs * @param shadow - * if true, draw the shadow layer. If false, draw the overlay - * contents. + * if true, draw the shadow layer. If false, draw the overlay contents. */ @Override public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) { @@ -144,8 +135,7 @@ public class BuildingNumOverlay extends Overlay implements Preferences, OnShared final SharedPreferences favouritesPrefs = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0); /* - * Draw in backward cycle, so the items with the least index are on the - * front. + * Draw in backward cycle, so the items with the least index are on the front. */ for (Iterator buildingIter = buildings.iterator(); buildingIter.hasNext();) { final Building building = buildingIter.next(); @@ -219,8 +209,7 @@ public class BuildingNumOverlay extends Overlay implements Preferences, OnShared if (showIdentifiers) { str += " (" + building.id + ")"; } - context.makeToast(str, context.getResources().getString(R.string.map_activity_toast_help_message), - Toast.LENGTH_SHORT); + context.makeToast(str, context.getResources().getString(R.string.map_activity_toast_help_message), Toast.LENGTH_SHORT); return true; } @@ -230,7 +219,7 @@ public class BuildingNumOverlay extends Overlay implements Preferences, OnShared public boolean onDoubleTap(final MotionEvent event, final MapView mapView) { if (!this.isEnabled()) return false; - + Building building = getSelectedItem(event, mapView); if (building == null) { @@ -265,19 +254,27 @@ public class BuildingNumOverlay extends Overlay implements Preferences, OnShared Log.v(TAG, "building Pressed " + building.id); final SharedPreferences favouritesPrefs = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0); + final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); if (favouritesPrefs.getBoolean(building.id, false)) { favouritesPrefs.edit().remove(building.id).commit(); - context.makeToast(building.id + " removed from favourites", Toast.LENGTH_SHORT); + // if (sharedPrefs.getBoolean(SHOW_IDENTIFIERS, SHOW_IDENTIFIERS_ENABLED_BY_DEFAULT)) { + context.makeToast(building.name + " removed from favourites", "(" + building.id + ")", Toast.LENGTH_SHORT); + // } else { + // context.makeToast(building.name + " removed from favourites", Toast.LENGTH_SHORT); + // } } else { - context.makeToast(building.id + " made a favourite", Toast.LENGTH_SHORT); + // if (sharedPrefs.getBoolean(SHOW_IDENTIFIERS, SHOW_IDENTIFIERS_ENABLED_BY_DEFAULT)) { + context.makeToast(building.name + " added to favourites", "(" + building.id + ")", Toast.LENGTH_SHORT); + // } else { + // context.makeToast(building.name + " added to favourites", Toast.LENGTH_SHORT); + // } favouritesPrefs.edit().putBoolean(building.id, true).commit(); } - Collections.sort(buildings, - new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0))); + Collections.sort(buildings, new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0))); mapView.invalidate(); @@ -310,8 +307,7 @@ public class BuildingNumOverlay extends Overlay implements Preferences, OnShared public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Log.i(TAG, "Got a favourites change in the BuildingNumOverlay for key " + key); - Collections - .sort(buildings, new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0))); + Collections.sort(buildings, new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0))); } } diff --git a/src/net/cbaines/suma/BusStopOverlay.java b/src/net/cbaines/suma/BusStopOverlay.java index 778b306..b8377cf 100644 --- a/src/net/cbaines/suma/BusStopOverlay.java +++ b/src/net/cbaines/suma/BusStopOverlay.java @@ -110,8 +110,7 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants, Pref final SharedPreferences favouritesPreferences = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0); /* - * Draw in backward cycle, so the items with the least index are on the - * front. + * Draw in backward cycle, so the items with the least index are on the front. */ for (int stopNum = 0; stopNum < busStops.size(); stopNum++) { @@ -184,9 +183,9 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants, Pref Log.e(TAG, "Unknown route code"); } - canvas.drawRect(rectLeft, mCurScreenCoords.y - + ((yOfsetPerMarker * makersPlaced) - (45 * scale)), rectRight, mCurScreenCoords.y - + (yOfsetPerMarker * makersPlaced) - ((45 * scale) - markerYSize), paint); + canvas.drawRect(rectLeft, mCurScreenCoords.y + ((yOfsetPerMarker * makersPlaced) - (45 * scale)), + rectRight, mCurScreenCoords.y + (yOfsetPerMarker * makersPlaced) - ((45 * scale) - markerYSize), + paint); makersPlaced++; } @@ -210,8 +209,7 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants, Pref if (showIdentifiers) { str += " (" + busStop.id + ")"; } - context.makeToast(str, context.getResources().getString(R.string.map_activity_toast_help_message), - Toast.LENGTH_SHORT); + context.makeToast(str, context.getResources().getString(R.string.map_activity_toast_help_message), Toast.LENGTH_SHORT); return true; } @@ -250,21 +248,28 @@ public class BusStopOverlay extends Overlay implements RouteColorConstants, Pref } else { Log.i(TAG, "Pressed " + busStop.id); - SharedPreferences favouritesPreferences = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0); + final SharedPreferences favouritesPreferences = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0); + final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); if (favouritesPreferences.getBoolean(busStop.id, false)) { favouritesPreferences.edit().remove(busStop.id).commit(); - context.makeToast(busStop.id + " removed from favourites", Toast.LENGTH_SHORT); - + if (sharedPrefs.getBoolean(SHOW_IDENTIFIERS, SHOW_IDENTIFIERS_ENABLED_BY_DEFAULT)) { + context.makeToast(busStop.description + " removed from favourites", "(" + busStop.id + ")", + Toast.LENGTH_SHORT); + } else { + context.makeToast(busStop.description + " removed from favourites", Toast.LENGTH_SHORT); + } } else { - context.makeToast(busStop.id + " made a favourite", Toast.LENGTH_SHORT); - + if (sharedPrefs.getBoolean(SHOW_IDENTIFIERS, SHOW_IDENTIFIERS_ENABLED_BY_DEFAULT)) { + context.makeToast(busStop.description + " added to favourites", "(" + busStop.id + ")", Toast.LENGTH_SHORT); + } else { + context.makeToast(busStop.description + " added to favourites", Toast.LENGTH_SHORT); + } favouritesPreferences.edit().putBoolean(busStop.id, true).commit(); } - Collections.sort(busStops, - new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0))); + Collections.sort(busStops, new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0))); mapView.invalidate(); diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 6eab356..413ec48 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -86,6 +86,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants static final int VIEW_DIALOG_ID = 0; static final int FAVOURITE_DIALOG_ID = 1; + static final int WELCOME_DIALOG_ID = 2; + private POIDialog favDialog; private HashMap overlays = new HashMap(); @@ -271,6 +273,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants mapController.setCenter(userLocation); + showDialog(WELCOME_DIALOG_ID); + Log.i(TAG, "Finished onCreate " + (System.currentTimeMillis() - startTime)); } @@ -1079,6 +1083,9 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants favDialog.setOnItemLongClickListener(this); favDialog.setTitle(R.string.favourites_dialog_title); return favDialog; + case WELCOME_DIALOG_ID: + WelcomeDialog welcomeDialog = new WelcomeDialog(instance); + return welcomeDialog; } return null; } diff --git a/src/net/cbaines/suma/WelcomeDialog.java b/src/net/cbaines/suma/WelcomeDialog.java new file mode 100644 index 0000000..d0d6fd0 --- /dev/null +++ b/src/net/cbaines/suma/WelcomeDialog.java @@ -0,0 +1,48 @@ +/* + * 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.view.View; +import android.widget.Button; + +public class WelcomeDialog extends Dialog implements android.view.View.OnClickListener { + + // private static final String TAG = "WelcomeDialog"; + + private Button continueButton; + + public WelcomeDialog(Context context) { + super(context); + + this.setTitle(R.string.welcome_dialog_title); + + setContentView(R.layout.welcome_dialog); + + continueButton = (Button) findViewById(R.id.welcomeDialogButton); + continueButton.setOnClickListener(this); + } + + public void onClick(View arg0) { + this.dismiss(); + } + +} -- cgit v1.2.3 From 86d0e42fa3300f5c765982a9e75ee37b42cb1edf Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 8 Mar 2012 23:41:16 +0000 Subject: More improvements. --- src/net/cbaines/suma/DataManager.java | 47 ++++++++ src/net/cbaines/suma/MapActivity.java | 202 +++++++++----------------------- src/net/cbaines/suma/Preferences.java | 4 + src/net/cbaines/suma/WelcomeDialog.java | 15 ++- 4 files changed, 118 insertions(+), 150 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index 08425a7..2dcba40 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -75,6 +75,53 @@ public class DataManager { private static Dao busDao; private static Dao busStopDao; + static void createDatabase(final Context context) throws SQLException, IOException { + Log.i(TAG, "Begining loading databases"); + Dao buildingDao; + + buildingDao = helper.getBuildingDao(); + + long buildingCount = buildingDao.countOf(); + Log.i(TAG, "Building count " + buildingCount); + if (buildingCount < 260) { + + loadBuildings(context); + Log.i(TAG, "Loaded building database"); + + } + + Dao busStopDao = helper.getBusStopDao(); + Dao busRouteDao = helper.getBusRouteDao(); + Dao routeStopsDao = helper.getRouteStopsDao(); + + long busStopCount = busStopDao.countOf(); + long busRouteCount = busRouteDao.countOf(); + long routeStopsCount = routeStopsDao.countOf(); + + Log.i(TAG, "BusStop count " + busStopCount); + Log.i(TAG, "BusRoute count " + busRouteCount); + Log.i(TAG, "RouteStops count " + routeStopsCount); + if (busStopCount < 217 || busRouteCount < 5 || routeStopsCount < 327) { + + loadBusData(context, true); + Log.i(TAG, "Loaded bus stop database"); + + } + + Dao siteDao = helper.getSiteDao(); + + long siteCount = siteDao.countOf(); + Log.i(TAG, "Sites count " + siteCount); + if (siteCount < 21) { + + loadSiteData(context); + + } + + Log.i(TAG, "Finished loading databases"); + + } + public static void loadBuildings(Context context) throws SQLException, IOException { DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); Dao buildingDao = helper.getBuildingDao(); diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 413ec48..9e6ccf1 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -69,8 +69,8 @@ import com.j256.ormlite.dao.Dao; * @author Christopher Baines * */ -public class MapActivity extends ToastHelperActivity implements MapViewConstants, Runnable, RouteColorConstants, - OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { +public class MapActivity extends ToastHelperActivity implements MapViewConstants, RouteColorConstants, OnItemClickListener, + OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { /** * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets @@ -185,8 +185,45 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants instance = this; - Thread databaseThread = new Thread(this); // Start the database thread - databaseThread.start(); + Log.i(TAG, "Begining loading database " + (System.currentTimeMillis() - startTime)); + + DatabaseHelper helper = getHelper(); + Log.i(TAG, "Got the helper at " + (System.currentTimeMillis() - startTime)); + + boolean dbExist = helper.checkDataBase(); + Log.i(TAG, "Finished checking the database at " + (System.currentTimeMillis() - startTime)); + + if (dbExist) { + // do nothing - database already exist + } else { + + if (useBundledDatabase) { + try { + // By calling this method and empty database will be created + // into the default system path + // of your application so we are gonna be able to overwrite + // that database with our database. + Log.i(TAG, "GetReadableDatabase"); + helper.getWritableDatabase().close(); + + helper.copyDataBase(); + Log.i(TAG, "Out of copy database"); + } catch (IOException ioe) { + throw new Error("Unable to create database"); + } + } else { + try { + DataManager.createDatabase(instance); + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + Log.i(TAG, "Finished the database " + (System.currentTimeMillis() - startTime)); setContentView(R.layout.map_activity); @@ -208,13 +245,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants myLocationOverlay.setEnabled(true); Log.i(TAG, "Finished creating myLocationOverlay"); - while (databaseThread.isAlive()) { - Thread.yield(); - } - new Thread(new Runnable() { public void run() { - Thread.currentThread().setPriority(Thread.MAX_PRIORITY); try { showOverlays(); } catch (SQLException e) { @@ -272,15 +304,20 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } mapController.setCenter(userLocation); - - showDialog(WELCOME_DIALOG_ID); + + final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + String appVersion = sharedPrefs.getString(APP_VERSION, APP_NOT_INSTALLED); + + if (appVersion.equals(APP_NOT_INSTALLED)) { + showDialog(WELCOME_DIALOG_ID); + } Log.i(TAG, "Finished onCreate " + (System.currentTimeMillis() - startTime)); } public void onResume() { super.onResume(); - Log.i(TAG, "OnResume"); + Log.i(TAG, "OnResume " + (System.currentTimeMillis() - startTime)); if (myLocationOverlay != null) { final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences activityPrefs = getPreferences(0); @@ -303,7 +340,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants sharedPrefs.registerOnSharedPreferenceChangeListener(this); activityPrefs.registerOnSharedPreferenceChangeListener(this); } - Log.i(TAG, "Finished OnResume"); + Log.i(TAG, "Finished OnResume " + (System.currentTimeMillis() - startTime)); } public void onPause() { @@ -324,135 +361,6 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants return overlays; } - public void run() { - Log.i(TAG, "Begining loading database " + (System.currentTimeMillis() - startTime)); - - DatabaseHelper helper = getHelper(); - Log.i(TAG, "Got the helper at " + (System.currentTimeMillis() - startTime)); - - boolean dbExist = helper.checkDataBase(); - Log.i(TAG, "Finished checking the database at " + (System.currentTimeMillis() - startTime)); - - if (dbExist) { - // do nothing - database already exist - } else { - - if (useBundledDatabase) { - try { - // By calling this method and empty database will be created - // into the default system path - // of your application so we are gonna be able to overwrite - // that database with our database. - Log.i(TAG, "GetReadableDatabase"); - helper.getWritableDatabase().close(); - - helper.copyDataBase(); - Log.i(TAG, "Out of copy database"); - } catch (IOException ioe) { - throw new Error("Unable to create database"); - } - } else { - Thread buildingThread = null; - Thread busStopThread = null; - Thread siteThread = null; - - Log.i(TAG, "Begining loading databases " + (System.currentTimeMillis() - startTime)); - try { - Dao buildingDao; - - buildingDao = helper.getBuildingDao(); - - long buildingCount = buildingDao.countOf(); - Log.i(TAG, "Building count " + buildingCount); - if (buildingCount < 260) { - buildingThread = new Thread(new Runnable() { - public void run() { - try { - DataManager.loadBuildings(instance); - Log.i(TAG, "Loaded building database " + (System.currentTimeMillis() - startTime)); - } catch (SQLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - }); - - buildingThread.start(); - } - - Dao busStopDao = helper.getBusStopDao(); - Dao busRouteDao = helper.getBusRouteDao(); - Dao routeStopsDao = helper.getRouteStopsDao(); - - long busStopCount = busStopDao.countOf(); - long busRouteCount = busRouteDao.countOf(); - long routeStopsCount = routeStopsDao.countOf(); - - Log.i(TAG, "BusStop count " + busStopCount); - Log.i(TAG, "BusRoute count " + busRouteCount); - Log.i(TAG, "RouteStops count " + routeStopsCount); - if (busStopCount < 217 || busRouteCount < 5 || routeStopsCount < 327) { - busStopThread = new Thread(new Runnable() { - public void run() { - try { - DataManager.loadBusData(instance, true); - Log.i(TAG, "Loaded bus stop database " + (System.currentTimeMillis() - startTime)); - } catch (SQLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - }); - - busStopThread.start(); - } - - Dao siteDao = helper.getSiteDao(); - - long siteCount = siteDao.countOf(); - Log.i(TAG, "Sites count " + siteCount); - if (siteCount < 21) { - siteThread = new Thread(new Runnable() { - public void run() { - try { - DataManager.loadSiteData(instance); - Log.i(TAG, "Loaded site database " + (System.currentTimeMillis() - startTime)); - } catch (SQLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - }); - - siteThread.start(); - } - - while (true) { - if ((buildingThread == null || !buildingThread.isAlive()) - && (busStopThread == null || !busStopThread.isAlive()) - && (siteThread == null || !siteThread.isAlive())) - break; - - Thread.yield(); - } - - Log.i(TAG, "Finished loading databases " + (System.currentTimeMillis() - startTime)); - - } catch (SQLException e1) { - e1.printStackTrace(); - } - } - - } - - Log.i(TAG, "Begining setting up the static values " + (System.currentTimeMillis() - startTime)); - - Log.i(TAG, "Finished the database thread " + (System.currentTimeMillis() - startTime)); - } - private void showOverlays() throws SQLException { Log.i(TAG, "Began showing overlays at " + (System.currentTimeMillis() - startTime)); @@ -477,7 +385,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants showBuildingOverlays(); } - Log.i(TAG, "Begining to show the route overlays at " + (System.currentTimeMillis() - startTime)); + Log.v(TAG, "Begining to show the route overlays at " + (System.currentTimeMillis() - startTime)); for (BusRoute busRoute : getHelper().getBusRouteDao()) { if (!busRoute.uniLink) { // Log.v(TAG, "Bus route " + busRoute.code + "(" + busRoute.id + ") is not unilink"); @@ -488,9 +396,9 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants showRouteOverlay(busRoute); } } - Log.i(TAG, "Finished loading routes " + (System.currentTimeMillis() - startTime)); + Log.v(TAG, "Finished loading routes " + (System.currentTimeMillis() - startTime)); - Log.i(TAG, "Begining to show the site overlays at " + (System.currentTimeMillis() - startTime)); + Log.v(TAG, "Begining to show the site overlays at " + (System.currentTimeMillis() - startTime)); try { for (Site site : getHelper().getSiteDao()) { // Log.v(TAG, "Looking at showing " + site.name + " site overlay"); @@ -501,9 +409,9 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } catch (SQLException e) { e.printStackTrace(); } - Log.i(TAG, "Finished showing the site overlays " + (System.currentTimeMillis() - startTime)); + Log.v(TAG, "Finished showing the site overlays " + (System.currentTimeMillis() - startTime)); - Log.i(TAG, "Finished showing all the overlays " + (System.currentTimeMillis() - startTime)); + Log.v(TAG, "Finished showing all the overlays " + (System.currentTimeMillis() - startTime)); } private void showUtilityOverlays() { diff --git a/src/net/cbaines/suma/Preferences.java b/src/net/cbaines/suma/Preferences.java index 0508251..fde8b70 100644 --- a/src/net/cbaines/suma/Preferences.java +++ b/src/net/cbaines/suma/Preferences.java @@ -14,5 +14,9 @@ public interface Preferences { static final String SHOW_IDENTIFIERS = "showIdentifiers"; static final boolean SHOW_IDENTIFIERS_ENABLED_BY_DEFAULT = false; + static final String APP_VERSION = "appVersion"; + static final String APP_NOT_INSTALLED = ""; + static final String CURRENT_APP_VERSION = "0.8"; + static final String FAVOURITES_PREFERENCES = "favourites"; } diff --git a/src/net/cbaines/suma/WelcomeDialog.java b/src/net/cbaines/suma/WelcomeDialog.java index d0d6fd0..d89abf0 100644 --- a/src/net/cbaines/suma/WelcomeDialog.java +++ b/src/net/cbaines/suma/WelcomeDialog.java @@ -21,13 +21,18 @@ package net.cbaines.suma; import android.app.Dialog; import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.view.View; +import android.view.View.OnClickListener; import android.widget.Button; -public class WelcomeDialog extends Dialog implements android.view.View.OnClickListener { +public class WelcomeDialog extends Dialog implements OnClickListener, Preferences { // private static final String TAG = "WelcomeDialog"; + final SharedPreferences sharedPrefs; + private Button continueButton; public WelcomeDialog(Context context) { @@ -36,13 +41,17 @@ public class WelcomeDialog extends Dialog implements android.view.View.OnClickLi this.setTitle(R.string.welcome_dialog_title); setContentView(R.layout.welcome_dialog); - + + sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + continueButton = (Button) findViewById(R.id.welcomeDialogButton); continueButton.setOnClickListener(this); } public void onClick(View arg0) { + + sharedPrefs.edit().putString(APP_VERSION, CURRENT_APP_VERSION).commit(); + this.dismiss(); } - } -- cgit v1.2.3 From a32b31c29d45b68918ec2a28fff7d87e675c6768 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 9 Mar 2012 10:43:53 +0000 Subject: More changes for welcome dialog. --- src/net/cbaines/suma/DataManager.java | 101 ++++++++++++------- src/net/cbaines/suma/MapActivity.java | 169 +++++++++++++++++++------------- src/net/cbaines/suma/WelcomeDialog.java | 7 ++ 3 files changed, 173 insertions(+), 104 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index 2dcba40..7f94662 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -77,6 +77,9 @@ public class DataManager { static void createDatabase(final Context context) throws SQLException, IOException { Log.i(TAG, "Begining loading databases"); + + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + Dao buildingDao; buildingDao = helper.getBuildingDao(); @@ -184,9 +187,13 @@ public class DataManager { /* * Polygon poly = buildingPolys.get(dataBits[1]); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // + * Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } */ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name @@ -210,9 +217,13 @@ public class DataManager { /* * Polygon poly = buildingPolys.get(dataBits[1]); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // + * Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } */ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name @@ -231,13 +242,17 @@ public class DataManager { } /* - * for (Iterator iter = buildingPoints.keySet().iterator(); iter.hasNext();) { String key = iter.next(); + * for (Iterator iter = buildingPoints.keySet().iterator(); + * iter.hasNext();) { String key = iter.next(); * - * Building bdg = new Building(key, buildingPoints.get(key), false); Polygon poly = buildingPolys.get(key); + * Building bdg = new Building(key, buildingPoints.get(key), false); + * Polygon poly = buildingPolys.get(key); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " - * + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } * * buildingDao.create(bdg); } */ @@ -274,18 +289,18 @@ public class DataManager { // Log.i(TAG, "Whole " + dataBits[3] + " First bit " + // quBitsLat[0] + " last bit " + quBitsLat[1]); - double lat = Double.valueOf(quBitsLat[0]) + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) - / 60d; + double lat = Double.valueOf(quBitsLat[0]) + + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d; // Log.i(TAG, "Whole " + dataBits[4] + " First bit " + // quBitsLng[0] + " last bit " + quBitsLng[1]); - double lng = Double.valueOf(quBitsLng[0]) + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) - / 60d; + double lng = Double.valueOf(quBitsLng[0]) + + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d; GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * -1e6)); // Log.i(TAG, "Lat " + point.getLatitudeE6() + " lng " + // point.getLongitudeE6()); - busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2].replace( - "\"", ""), point)); + busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2] + .replace("\"", ""), point)); } @@ -406,26 +421,36 @@ public class DataManager { } /* - * for (Iterator busStopIter = busStopDao.iterator(); busStopIter.hasNext();) { BusStop stop = - * busStopIter.next(); // Log.i(TAG, "Looking at stop " + stop.id); + * for (Iterator busStopIter = busStopDao.iterator(); + * busStopIter.hasNext();) { BusStop stop = busStopIter.next(); // + * Log.i(TAG, "Looking at stop " + stop.id); * * - * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); + * QueryBuilder routeStopsQueryBuilder = + * routeStopsDao.queryBuilder(); * routeStopsQueryBuilder.where().eq(columnName, value) * - * DeleteBuilder deleteBuilder = busStopDao.deleteBuilder(); // only delete the rows where password is - * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) accountDao.delete(deleteBuilder.prepare()); + * DeleteBuilder deleteBuilder = + * busStopDao.deleteBuilder(); // only delete the rows where password is + * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) + * accountDao.delete(deleteBuilder.prepare()); * * - * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); - * routeStopsQueryBuilder.setCountOf(true); routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, stop); + * QueryBuilder routeStopsQueryBuilder = + * routeStopsDao.queryBuilder(); + * routeStopsQueryBuilder.setCountOf(true); + * routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, + * stop); * - * PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); List routeStops = - * routeStopsDao.query(routeStopsPreparedQuery); // long num = routeStopsDao.query(routeStopsPreparedQuery).size(); // - * Log.i(TAG, "Number is " + num); + * PreparedQuery routeStopsPreparedQuery = + * routeStopsQueryBuilder.prepare(); List routeStops = + * routeStopsDao.query(routeStopsPreparedQuery); // long num = + * routeStopsDao.query(routeStopsPreparedQuery).size(); // Log.i(TAG, + * "Number is " + num); * - * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if (routeStop.busRoute.uniLink) { stop.uniLink = true; - * } } busStopDao.update(stop); } + * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if + * (routeStop.busRoute.uniLink) { stop.uniLink = true; } } + * busStopDao.update(stop); } */ Log.i(TAG, "Finished loading bus data"); @@ -601,10 +626,12 @@ public class DataManager { List routeStops = routeStopsDao.query(routeStopsPreparedQuery); if (routeStops.size() > 0) { - // Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + + // Log.i(TAG, "Found " + routeStops.size() + + // " stops matching the destStop " + destStop + " on route " + // route.code); } else { - Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + route.code); + Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + + route.code); } } @@ -690,7 +717,8 @@ public class DataManager { if (route != null) { busRoutes.add(route); } else { - throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " + key); + throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " + + key); } } @@ -702,12 +730,14 @@ public class DataManager { JSONObject stopObj = stopsArray.getJSONObject(stopNum); if (!keepNonUniLink && !stopObj.getString("name").startsWith("U")) { - // Log.v(TAG, "Skipping non uni-link stop " + stopObj.getString("name")); + // Log.v(TAG, "Skipping non uni-link stop " + + // stopObj.getString("name")); continue; } if (!keepUniLink && stopObj.getString("name").startsWith("U")) { - // Log.v(TAG, "Skipping uni-link stop " + stopObj.getString("name")); + // Log.v(TAG, "Skipping uni-link stop " + + // stopObj.getString("name")); continue; } @@ -798,7 +828,8 @@ public class DataManager { continue; } - // Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " + // Log.v(TAG, "Found stop for a unidentified " + + // stop.bus.toString() + " at " + stop.busStop.id + " at " // + stop.arivalTime); timetable.add(stop); diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 9e6ccf1..374f568 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -69,14 +69,14 @@ import com.j256.ormlite.dao.Dao; * @author Christopher Baines * */ -public class MapActivity extends ToastHelperActivity implements MapViewConstants, RouteColorConstants, OnItemClickListener, - OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { +public class MapActivity extends ToastHelperActivity implements MapViewConstants, RouteColorConstants, + OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { /** - * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets - * folder + * Enable to use the database in the assets folder, if its not enabled, the + * database is built from the csv files in the assets folder */ - private boolean useBundledDatabase = false; + private boolean useBundledDatabase = true; private MapView mapView; private MapController mapController; @@ -126,10 +126,11 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private static final int NON_UNI_LINK_BUS_STOP_OVERLAY_RANK = 4; // -- Site Overlays - static final String[] SITE_NAMES = { "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", + static final String[] SITE_NAMES = { "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" }; private static final String SITE_OVERLAYS = "siteOverlays:"; @@ -167,8 +168,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants // Uni-Link routes static final String[] UNI_LINK_ROUTES = { "U1", "U1N", "U2", "U6", "U9" }; - static final String[] PREFERENCES_GROUPS = { BUS_STOP_OVERLAYS, BUS_ROUTE_OVERLAYS, BUILDING_OVERLAYS, SITE_OVERLAYS, - OTHER_OVERLAYS }; + static final String[] PREFERENCES_GROUPS = { BUS_STOP_OVERLAYS, BUS_ROUTE_OVERLAYS, BUILDING_OVERLAYS, + SITE_OVERLAYS, OTHER_OVERLAYS }; static final String[][] PREFERENCES_CHILDREN = { UNI_LINK_ROUTES, UNI_LINK_ROUTES, BUILDING_TYPES, SITE_NAMES, OTHER_OVERLAY_NAMES }; @@ -237,8 +238,10 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants pastOverlays = (HashMap) getLastNonConfigurationInstance(); /* - * SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); This code in the following - * constructor causes problems in some emulators, disable sensors to fix. + * SensorManager mSensorManager = (SensorManager) + * getSystemService(Context.SENSOR_SERVICE); This code in the following + * constructor causes problems in some emulators, disable sensors to + * fix. */ Log.i(TAG, "Starting creating myLocationOverlay"); myLocationOverlay = new MyLocationOverlay(instance, mapView); @@ -304,11 +307,11 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } mapController.setCenter(userLocation); - + final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); String appVersion = sharedPrefs.getString(APP_VERSION, APP_NOT_INSTALLED); - - if (appVersion.equals(APP_NOT_INSTALLED)) { + + if (appVersion.equals(APP_NOT_INSTALLED) || !appVersion.equals(CURRENT_APP_VERSION)) { showDialog(WELCOME_DIALOG_ID); } @@ -388,7 +391,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants Log.v(TAG, "Begining to show the route overlays at " + (System.currentTimeMillis() - startTime)); for (BusRoute busRoute : getHelper().getBusRouteDao()) { if (!busRoute.uniLink) { - // Log.v(TAG, "Bus route " + busRoute.code + "(" + busRoute.id + ") is not unilink"); + // Log.v(TAG, "Bus route " + busRoute.code + "(" + busRoute.id + + // ") is not unilink"); continue; } Log.v(TAG, "Looking at showing " + busRoute.code + " route overlay"); @@ -401,7 +405,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants Log.v(TAG, "Begining to show the site overlays at " + (System.currentTimeMillis() - startTime)); try { for (Site site : getHelper().getSiteDao()) { - // Log.v(TAG, "Looking at showing " + site.name + " site overlay"); + // Log.v(TAG, "Looking at showing " + site.name + + // " site overlay"); if (activityPrefs.getBoolean(SITE_OVERLAYS + site.name, SITE_OVERLAYS_ENABLED_BY_DEFAULT)) { showSiteOverlay(site); } @@ -425,7 +430,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants if (scaleBarOverlay != null) { Log.v(TAG, "ScaleBarOverlay is already created"); } else { - if (pastOverlays != null && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get(SCALE_BAR_OVERLAY)) != null) { + if (pastOverlays != null + && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get(SCALE_BAR_OVERLAY)) != null) { Log.i(TAG, "Finished restoring utility overlays " + (System.currentTimeMillis() - startTime)); } else { scaleBarOverlay = new ScaleBarOverlay(instance); @@ -454,7 +460,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private void showRouteOverlay(final BusRoute route) { new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing route " + route.code + " overlay at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing route " + route.code + " overlay at " + + (System.currentTimeMillis() - startTime)); final SharedPreferences activityPrefs = getPreferences(0); final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); @@ -479,8 +486,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants // TODO Is this a route like U1N or, something else, // this hack works somewhat for now? - PathOverlay routeOverlayU1E = DataManager.getRoutePath(getResources().openRawResource(R.raw.u1e), - colour, mResourceProxy); + PathOverlay routeOverlayU1E = DataManager.getRoutePath( + getResources().openRawResource(R.raw.u1e), colour, mResourceProxy); routeOverlayU1E.getPaint().setAntiAlias(true); routeOverlayU1E.getPaint().setAlpha(145); routeOverlayU1E.getPaint().setStrokeWidth(12); @@ -535,7 +542,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants mapView.postInvalidate(); - Log.i(TAG, "Finished showing route " + route.code + " overlay at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing route " + route.code + " overlay at " + + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -544,7 +552,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing site " + site.name + " overlay at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing site " + site.name + " overlay at " + + (System.currentTimeMillis() - startTime)); final SharedPreferences activityPrefs = getPreferences(0); final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); @@ -553,7 +562,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants if ((siteOverlay = siteOverlays.get(site)) != null) { } else { - if (pastOverlays != null && (siteOverlay = (PathOverlay) pastOverlays.get(SITE_OVERLAYS + site.name)) != null) { + if (pastOverlays != null + && (siteOverlay = (PathOverlay) pastOverlays.get(SITE_OVERLAYS + site.name)) != null) { Log.i(TAG, "Restored " + site.name + " site overlay"); } else { @@ -579,11 +589,13 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - siteOverlay.setEnabled(activityPrefs.getBoolean(SITE_OVERLAYS + site.name, SITE_OVERLAYS_ENABLED_BY_DEFAULT)); + siteOverlay.setEnabled(activityPrefs.getBoolean(SITE_OVERLAYS + site.name, + SITE_OVERLAYS_ENABLED_BY_DEFAULT)); mapView.postInvalidate(); - Log.i(TAG, "Finished showing site " + site.name + " overlay at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing site " + site.name + " overlay at " + + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -600,8 +612,10 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } else { if (pastOverlays != null - && (residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get(RESIDENTIAL_BUILDING_OVERLAY)) != null) { - nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get(NON_RESIDENTIAL_BUILDING_OVERLAY); + && (residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays + .get(RESIDENTIAL_BUILDING_OVERLAY)) != null) { + nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays + .get(NON_RESIDENTIAL_BUILDING_OVERLAY); Log.i(TAG, "Restored building overlays"); } else { @@ -616,7 +630,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants buildingDao = getHelper().getBuildingDao(); - final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, MODE_PRIVATE); + final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, + MODE_PRIVATE); for (Building building : buildingDao) { if (building.residential == true) { @@ -654,8 +669,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + RESIDENTIAL_BUILDING_OVERLAY, - RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); + residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + + RESIDENTIAL_BUILDING_OVERLAY, RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); nonResidentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + NON_RESIDENTIAL_BUILDING_OVERLAY, NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); @@ -675,11 +690,16 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); if (uniLinkBusStopOverlay != null) { - if (!activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + if (!activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { Log.i(TAG, "Uni-Link bus stop overlay not needed"); overlays.remove(UNI_LINK_BUS_STOP_OVERLAY); @@ -690,25 +710,31 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants uniLinkBusStopOverlay = null; } else { - uniLinkBusStopOverlay.setRoutes(0, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(1, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(2, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(3, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(4, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); } } else { - if (activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + if (activityPrefs + .getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { if (pastOverlays != null - && (uniLinkBusStopOverlay = (BusStopOverlay) pastOverlays.get(UNI_LINK_BUS_STOP_OVERLAY)) != null) { + && (uniLinkBusStopOverlay = (BusStopOverlay) pastOverlays + .get(UNI_LINK_BUS_STOP_OVERLAY)) != null) { Log.i(TAG, "Restored Uni-Link bus stop overlay"); } else { @@ -726,16 +752,16 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - uniLinkBusStopOverlay.setRoutes(0, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(1, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(2, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(3, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(4, - activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", + UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); overlays.put(UNI_LINK_BUS_STOP_OVERLAY, uniLinkBusStopOverlay); @@ -758,7 +784,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private void showNonUniLinkBusStopOverlay() { new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing non uni link bus stop overlays at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing non uni link bus stop overlays at " + + (System.currentTimeMillis() - startTime)); // final SharedPreferences activityPrefs = getPreferences(0); final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); @@ -775,12 +802,14 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } else if (sharedPrefs.getBoolean(NON_UNI_LINK_BUS_STOPS_OVERLAY, NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { if (pastOverlays != null - && (nonUniLinkBusStopOverlay = (BusStopOverlay) pastOverlays.get(NON_UNI_LINK_BUS_STOPS_OVERLAY)) != null) { + && (nonUniLinkBusStopOverlay = (BusStopOverlay) pastOverlays + .get(NON_UNI_LINK_BUS_STOPS_OVERLAY)) != null) { Log.i(TAG, "Restored non Uni-Link bus stop overlays"); } else { try { List busStops; - Log.v(TAG, "Begin fetching non Uni-Link BusStops at " + (System.currentTimeMillis() - startTime)); + Log.v(TAG, "Begin fetching non Uni-Link BusStops at " + + (System.currentTimeMillis() - startTime)); busStops = getHelper().getBusStopDao().queryForEq(BusStop.UNI_LINK_FIELD_NAME, false); @@ -810,7 +839,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants mapView.postInvalidate(); - Log.i(TAG, "Finished showing non Uni-Link bus stop overlays at " + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing non Uni-Link bus stop overlays at " + + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -862,8 +892,9 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants }; AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage("GPS is not enabled, do you wish to enable it?").setPositiveButton("Yes", dialogClickListener) - .setNegativeButton("No", dialogClickListener).show(); + builder.setMessage("GPS is not enabled, do you wish to enable it?") + .setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener) + .show(); } return true; diff --git a/src/net/cbaines/suma/WelcomeDialog.java b/src/net/cbaines/suma/WelcomeDialog.java index d89abf0..7262f48 100644 --- a/src/net/cbaines/suma/WelcomeDialog.java +++ b/src/net/cbaines/suma/WelcomeDialog.java @@ -26,6 +26,7 @@ import android.preference.PreferenceManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; +import android.widget.TextView; public class WelcomeDialog extends Dialog implements OnClickListener, Preferences { @@ -43,6 +44,12 @@ public class WelcomeDialog extends Dialog implements OnClickListener, Preference setContentView(R.layout.welcome_dialog); sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + String appVersion = sharedPrefs.getString(APP_VERSION, APP_NOT_INSTALLED); + + if (!appVersion.equals(CURRENT_APP_VERSION)) { + TextView welcomeDialogMessage = (TextView) findViewById(R.id.welcomeDialogMessage); + welcomeDialogMessage.setText(R.string.welcome_dialog_upgrade_message); + } continueButton = (Button) findViewById(R.id.welcomeDialogButton); continueButton.setOnClickListener(this); -- cgit v1.2.3 From 606d952ef33ee74dbb303a8870451252d7785f32 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 9 Mar 2012 12:35:17 +0000 Subject: More search improvements. --- src/net/cbaines/suma/MapActivity.java | 67 ++++++++++----- src/net/cbaines/suma/MapContentProvider.java | 120 +++++++++++++++++++++++---- src/net/cbaines/suma/SearchActivity.java | 12 +-- 3 files changed, 151 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 8ed8e9e..3b8efe5 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -42,6 +42,7 @@ import org.osmdroid.views.util.constants.MapViewConstants; import android.app.AlertDialog; import android.app.Dialog; +import android.app.SearchManager; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -186,6 +187,19 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants instance = this; + Intent intent = getIntent(); + Bundle extras = intent.getExtras(); + + if (Intent.ACTION_SEARCH.equals(intent.getAction())) { + String query = intent.getStringExtra(SearchManager.QUERY); + + Intent searchIntent = new Intent(instance, SearchActivity.class); + searchIntent.setAction(Intent.ACTION_SEARCH); + searchIntent.putExtra(SearchManager.QUERY, query); + startActivity(searchIntent); + + } + Log.i(TAG, "Begining loading database " + (System.currentTimeMillis() - startTime)); DatabaseHelper helper = getHelper(); @@ -263,50 +277,59 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants mapController = mapView.getController(); mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext()); - GeoPoint userLocation = null; + GeoPoint startLocation = null; - Bundle extras = getIntent().getExtras(); if (getIntent().getDataString() != null) { + String dataString = getIntent().getDataString(); - Log.i(TAG, "getIntent().getDataString() " + getIntent().getDataString()); + Log.i(TAG, "getIntent().getDataString() " + dataString); - String str = getIntent().getDataString().substring(4, getIntent().getDataString().length()); - String[] strParts = str.split(","); + if (dataString.startsWith("content")) { - int lat = Util.doubleToIntE6(Double.valueOf(strParts[0])); - int lng; + Uri data = intent.getData(); - if (strParts[1].contains("?")) { - String zoom = strParts[1].substring(strParts[1].indexOf("?") + 3, strParts[1].length()); - String strLng = strParts[1].substring(0, strParts[1].indexOf("?") - 1); - lng = Util.doubleToIntE6(Double.valueOf(strLng)); + Log.i("Data: ", data.toString()); - mapController.setZoom(Integer.valueOf(zoom)); + startLocation = new GeoPoint(50935551, -1393488); } else { - lng = Util.doubleToIntE6(Double.valueOf(strParts[1])); - mapController.setZoom(15); - } - userLocation = new GeoPoint(lat, lng); + String str = getIntent().getDataString().substring(4, getIntent().getDataString().length()); + String[] strParts = str.split(","); + int lat = Util.doubleToIntE6(Double.valueOf(strParts[0])); + int lng; + + if (strParts[1].contains("?")) { + String zoom = strParts[1].substring(strParts[1].indexOf("?") + 3, strParts[1].length()); + String strLng = strParts[1].substring(0, strParts[1].indexOf("?") - 1); + lng = Util.doubleToIntE6(Double.valueOf(strLng)); + + mapController.setZoom(Integer.valueOf(zoom)); + } else { + lng = Util.doubleToIntE6(Double.valueOf(strParts[1])); + mapController.setZoom(15); + } + + startLocation = new GeoPoint(lat, lng); + } } else if (extras != null && extras.containsKey("poiPoint")) { String poiPoint = getIntent().getExtras().getString("poiPoint"); Log.i(TAG, "poiPoint " + poiPoint); String[] bits = poiPoint.split(","); - userLocation = new GeoPoint(Double.valueOf(bits[0]), Double.valueOf(bits[1])); + startLocation = new GeoPoint(Double.valueOf(bits[0]), Double.valueOf(bits[1])); mapController.setZoom(20); } else { - userLocation = myLocationOverlay.getMyLocation(); - if (userLocation == null) { - userLocation = new GeoPoint(50935551, -1393488); // ECS + startLocation = myLocationOverlay.getMyLocation(); + if (startLocation == null) { + startLocation = new GeoPoint(50935551, -1393488); // ECS } mapController.setZoom(15); } - mapController.setCenter(userLocation); + mapController.setCenter(startLocation); final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); String appVersion = sharedPrefs.getString(APP_VERSION, APP_NOT_INSTALLED); @@ -857,7 +880,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants // Handle item selection switch (item.getItemId()) { case R.id.menu_find: - onSearchRequested(); + onSearchRequested(); return true; case R.id.menu_preferences: Intent settingsActivity = new Intent(getBaseContext(), PreferencesActivity.class); diff --git a/src/net/cbaines/suma/MapContentProvider.java b/src/net/cbaines/suma/MapContentProvider.java index a2b42c3..d920b21 100644 --- a/src/net/cbaines/suma/MapContentProvider.java +++ b/src/net/cbaines/suma/MapContentProvider.java @@ -48,6 +48,10 @@ public class MapContentProvider extends ContentProvider { // + "/building"); // MIME types used for searching words or looking up a single definition + public static final String ALLS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + + "/vnd.net.cbaines.suma.provider.all"; + public static final String ALL_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + + "/vnd.net.cbaines.suma.provider.all"; public static final String BUILDINGS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.net.cbaines.suma.provider.building"; public static final String BUILDING_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE @@ -68,16 +72,18 @@ public class MapContentProvider extends ContentProvider { private DatabaseHelper helper; // UriMatcher stuff - private static final int SEARCH_BUILDINGS = 0; - private static final int GET_BUILDING = 1; - private static final int SEARCH_BUS_STOPS = 2; - private static final int GET_BUS_STOP = 3; - private static final int SEARCH_SITES = 4; - private static final int GET_SITE = 5; - private static final int SEARCH_BUSES = 6; - private static final int GET_BUS = 7; - private static final int SEARCH_SUGGEST = 8; - private static final int REFRESH_SHORTCUT = 9; + private static final int SEARCH_ALL = 0; + private static final int GET_ALL = 1; + private static final int SEARCH_BUILDINGS = 2; + private static final int GET_BUILDING = 3; + private static final int SEARCH_BUS_STOPS = 4; + private static final int GET_BUS_STOP = 5; + private static final int SEARCH_SITES = 6; + private static final int GET_SITE = 7; + private static final int SEARCH_BUSES = 8; + private static final int GET_BUS = 9; + private static final int SEARCH_SUGGEST = 10; + private static final int REFRESH_SHORTCUT = 11; private static final UriMatcher sURIMatcher = buildUriMatcher(); /** @@ -87,6 +93,8 @@ public class MapContentProvider extends ContentProvider { private static UriMatcher buildUriMatcher() { UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); // to get definitions... + matcher.addURI(AUTHORITY, "all", SEARCH_BUILDINGS); + matcher.addURI(AUTHORITY, "all/*", GET_BUILDING); matcher.addURI(AUTHORITY, "building", SEARCH_BUILDINGS); matcher.addURI(AUTHORITY, "building/*", GET_BUILDING); matcher.addURI(AUTHORITY, "bus-stop", SEARCH_BUS_STOPS); @@ -141,6 +149,21 @@ public class MapContentProvider extends ContentProvider { } catch (SQLException e1) { e1.printStackTrace(); } + case SEARCH_ALL: + if (selectionArgs == null) { + throw new IllegalArgumentException("selectionArgs must be provided for the Uri: " + uri); + } + try { + return searchAll(selectionArgs[0]); + } catch (SQLException e) { + e.printStackTrace(); + } + case GET_ALL: + try { + return getAll(uri); + } catch (SQLException e) { + e.printStackTrace(); + } case SEARCH_BUILDINGS: if (selectionArgs == null) { throw new IllegalArgumentException("selectionArgs must be provided for the Uri: " + uri); @@ -170,6 +193,13 @@ public class MapContentProvider extends ContentProvider { private Cursor getSuggestions(String query) throws SQLException { Log.v(TAG, "Got query for " + query); + String[] columnNames = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_ICON_1, + SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_TEXT_2, + SearchManager.SUGGEST_COLUMN_INTENT_DATA }; + + MatrixCursor cursor = new MatrixCursor(columnNames); + int id = 0; + Dao buildingDao = helper.getBuildingDao(); QueryBuilder qb = buildingDao.queryBuilder(); @@ -180,14 +210,37 @@ public class MapContentProvider extends ContentProvider { List buildings = buildingDao.query(preparedQuery); Log.v(TAG, "Returning " + buildings.size() + " buildings"); - String[] columnNames = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1 }; - - MatrixCursor cursor = new MatrixCursor(columnNames, buildings.size()); - - int id = 0; for (Building building : buildings) { - Log.v(TAG, "Building " + id + ", " + building.name); - Object[] values = { id++, building.name }; + // Log.v(TAG, "Building " + id + ", " + building.name); + Object[] values = { + id++, + R.drawable.building, + building.name, + building.id, + "geo:" + Util.E6IntToDouble(building.point.getLatitudeE6()) + "," + + Util.E6IntToDouble(building.point.getLongitudeE6()) + "?z=18" }; + cursor.addRow(values); + } + + Dao busStopDao = helper.getBusStopDao(); + + QueryBuilder busStopQB = busStopDao.queryBuilder(); + busStopQB.where().like(BusStop.ID_FIELD_NAME, "%" + query + "%").or() + .like(BusStop.DESCRIPTION_FIELD_NAME, "%" + query + "%"); + PreparedQuery busStopPreparedQuery = busStopQB.prepare(); + + List busStops = busStopDao.query(busStopPreparedQuery); + Log.v(TAG, "Returning " + busStops.size() + " busStops"); + + for (BusStop busStop : busStops) { + // Log.v(TAG, "Building " + id + ", " + building.name); + Object[] values = { + id++, + R.drawable.busstop, + busStop.description, + busStop.id, + "geo:" + Util.E6IntToDouble(busStop.point.getLatitudeE6()) + "," + + Util.E6IntToDouble(busStop.point.getLongitudeE6()) + "?z=18" }; cursor.addRow(values); } @@ -223,6 +276,35 @@ public class MapContentProvider extends ContentProvider { return cursor; } + private Cursor searchAll(String query) throws SQLException { + Dao buildingDao = helper.getBuildingDao(); + + QueryBuilder qb = buildingDao.queryBuilder(); + qb.where().eq(Building.ID_FIELD_NAME, "%" + query + "%").or().eq(Building.NAME_FIELD_NAME, "%" + query + "%"); + PreparedQuery preparedQuery = qb.prepare(); + + AndroidCompiledStatement compiledStatement = (AndroidCompiledStatement) preparedQuery.compile(helper + .getConnectionSource().getReadOnlyConnection(), StatementType.SELECT); + Cursor cursor = compiledStatement.getCursor(); + + return cursor; + } + + private Cursor getAll(Uri uri) throws SQLException { + String buildingID = uri.getLastPathSegment(); + Dao buildingDao = helper.getBuildingDao(); + + QueryBuilder qb = buildingDao.queryBuilder(); + qb.where().eq(Building.ID_FIELD_NAME, buildingID); + PreparedQuery preparedQuery = qb.prepare(); + + AndroidCompiledStatement compiledStatement = (AndroidCompiledStatement) preparedQuery.compile(helper + .getConnectionSource().getReadOnlyConnection(), StatementType.SELECT); + Cursor cursor = compiledStatement.getCursor(); + + return cursor; + } + private Cursor refreshShortcut(Uri uri) throws SQLException { /* * This won't be called with the current implementation, but if we @@ -254,6 +336,10 @@ public class MapContentProvider extends ContentProvider { @Override public String getType(Uri uri) { switch (sURIMatcher.match(uri)) { + case SEARCH_ALL: + return ALLS_MIME_TYPE; + case GET_ALL: + return ALL_MIME_TYPE; case SEARCH_BUILDINGS: return BUILDINGS_MIME_TYPE; case GET_BUILDING: diff --git a/src/net/cbaines/suma/SearchActivity.java b/src/net/cbaines/suma/SearchActivity.java index f7f5e55..850996a 100644 --- a/src/net/cbaines/suma/SearchActivity.java +++ b/src/net/cbaines/suma/SearchActivity.java @@ -43,7 +43,6 @@ import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; -import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; @@ -61,7 +60,6 @@ public class SearchActivity extends OrmLiteBaseActivity implemen public static final String ORIGIN = "o"; public static final int MAP_ACTIVITY = 0; - private EditText searchBar; private ListView listItems; private ProgressBar progBar; private LinearLayout findContentLayout; @@ -245,18 +243,14 @@ public class SearchActivity extends OrmLiteBaseActivity implemen /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.find_activity); + setContentView(R.layout.search_activity); Log.i(TAG, "FindActivity started"); - searchBar = (EditText) findViewById(R.id.searchBar); - searchBar.addTextChangedListener(this); - Intent intent = getIntent(); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - String query = intent.getStringExtra(SearchManager.QUERY); - Log.i(TAG, "Searching for " + query); - searchBar.setText(query); + searchTerm = intent.getStringExtra(SearchManager.QUERY); + Log.i(TAG, "Searching for " + searchTerm); } listItems = (ListView) findViewById(R.id.findListItems); -- cgit v1.2.3 From 264b93bd42753d66dbcaea74ed2024ecb757b5f7 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 9 Mar 2012 17:59:14 +0000 Subject: Some changes. --- src/net/cbaines/suma/BusStopActivity.java | 5 +- src/net/cbaines/suma/DataManager.java | 89 +++--- src/net/cbaines/suma/MapActivity.java | 167 +++++------ src/net/cbaines/suma/SearchActivity.java | 380 ------------------------ src/net/cbaines/suma/SearchResultsActivity.java | 380 ++++++++++++++++++++++++ 5 files changed, 489 insertions(+), 532 deletions(-) delete mode 100644 src/net/cbaines/suma/SearchActivity.java create mode 100644 src/net/cbaines/suma/SearchResultsActivity.java (limited to 'src') diff --git a/src/net/cbaines/suma/BusStopActivity.java b/src/net/cbaines/suma/BusStopActivity.java index b80d5b4..e28086a 100644 --- a/src/net/cbaines/suma/BusStopActivity.java +++ b/src/net/cbaines/suma/BusStopActivity.java @@ -308,7 +308,7 @@ public class BusStopActivity extends ToastHelperActivity implements OnCheckedCha return timetable; } - private class GetTimetableTask extends AsyncTask { + private class GetTimetableTask extends AsyncTask { String errorMessage; protected void onPreExecute() { @@ -337,7 +337,8 @@ public class BusStopActivity extends ToastHelperActivity implements OnCheckedCha errorMessage = "Error parsing bus times"; e.printStackTrace(); } catch (Exception e) { - Log.e(TAG, e.getMessage(), e.getCause()); + Log.e(TAG, "Exception in new timetable " + e.getMessage(), e.getCause()); + e.printStackTrace(); } return newTimetable; diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index 082631c..ca27672 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -187,13 +187,9 @@ public class DataManager { /* * Polygon poly = buildingPolys.get(dataBits[1]); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, - * "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6() + " " + poly); } else { // - * Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } */ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name @@ -217,13 +213,9 @@ public class DataManager { /* * Polygon poly = buildingPolys.get(dataBits[1]); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, - * "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6() + " " + poly); } else { // - * Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } */ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name @@ -242,17 +234,13 @@ public class DataManager { } /* - * for (Iterator iter = buildingPoints.keySet().iterator(); - * iter.hasNext();) { String key = iter.next(); + * for (Iterator iter = buildingPoints.keySet().iterator(); iter.hasNext();) { String key = iter.next(); * - * Building bdg = new Building(key, buildingPoints.get(key), false); - * Polygon poly = buildingPolys.get(key); + * Building bdg = new Building(key, buildingPoints.get(key), false); Polygon poly = buildingPolys.get(key); * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + * + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } * * buildingDao.create(bdg); } */ @@ -289,18 +277,18 @@ public class DataManager { // Log.i(TAG, "Whole " + dataBits[3] + " First bit " + // quBitsLat[0] + " last bit " + quBitsLat[1]); - double lat = Double.valueOf(quBitsLat[0]) - + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d; + double lat = Double.valueOf(quBitsLat[0]) + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) + / 60d; // Log.i(TAG, "Whole " + dataBits[4] + " First bit " + // quBitsLng[0] + " last bit " + quBitsLng[1]); - double lng = Double.valueOf(quBitsLng[0]) - + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d; + double lng = Double.valueOf(quBitsLng[0]) + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) + / 60d; GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * -1e6)); // Log.i(TAG, "Lat " + point.getLatitudeE6() + " lng " + // point.getLongitudeE6()); - busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2] - .replace("\"", ""), point)); + busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2].replace( + "\"", ""), point)); } @@ -421,36 +409,26 @@ public class DataManager { } /* - * for (Iterator busStopIter = busStopDao.iterator(); - * busStopIter.hasNext();) { BusStop stop = busStopIter.next(); // - * Log.i(TAG, "Looking at stop " + stop.id); + * for (Iterator busStopIter = busStopDao.iterator(); busStopIter.hasNext();) { BusStop stop = + * busStopIter.next(); // Log.i(TAG, "Looking at stop " + stop.id); * * - * QueryBuilder routeStopsQueryBuilder = - * routeStopsDao.queryBuilder(); + * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); * routeStopsQueryBuilder.where().eq(columnName, value) * - * DeleteBuilder deleteBuilder = - * busStopDao.deleteBuilder(); // only delete the rows where password is - * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) - * accountDao.delete(deleteBuilder.prepare()); + * DeleteBuilder deleteBuilder = busStopDao.deleteBuilder(); // only delete the rows where password is + * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) accountDao.delete(deleteBuilder.prepare()); * * - * QueryBuilder routeStopsQueryBuilder = - * routeStopsDao.queryBuilder(); - * routeStopsQueryBuilder.setCountOf(true); - * routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, - * stop); + * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); + * routeStopsQueryBuilder.setCountOf(true); routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, stop); * - * PreparedQuery routeStopsPreparedQuery = - * routeStopsQueryBuilder.prepare(); List routeStops = - * routeStopsDao.query(routeStopsPreparedQuery); // long num = - * routeStopsDao.query(routeStopsPreparedQuery).size(); // Log.i(TAG, - * "Number is " + num); + * PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); List routeStops = + * routeStopsDao.query(routeStopsPreparedQuery); // long num = routeStopsDao.query(routeStopsPreparedQuery).size(); // + * Log.i(TAG, "Number is " + num); * - * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if - * (routeStop.busRoute.uniLink) { stop.uniLink = true; } } - * busStopDao.update(stop); } + * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if (routeStop.busRoute.uniLink) { stop.uniLink = true; + * } } busStopDao.update(stop); } */ Log.i(TAG, "Finished loading bus data"); @@ -512,6 +490,8 @@ public class DataManager { routeStopsDao = helper.getRouteStopsDao(); if (busStopDao == null) busStopDao = helper.getBusStopDao(); + if (busDao == null) + busDao = helper.getBusDao(); String time = stopObj.getString("time"); @@ -628,8 +608,7 @@ public class DataManager { // " stops matching the destStop " + destStop + " on route " + // route.code); } else { - Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " - + route.code); + Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + route.code); } } @@ -715,8 +694,8 @@ public class DataManager { if (route != null) { busRoutes.add(route); } else { - throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " - + key); + Log.e(TAG, "Route not found " + key.substring(key.length() - 3, key.length()) + " " + key); + throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " + key); } } diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 8ed8e9e..12fdeeb 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -42,6 +42,7 @@ import org.osmdroid.views.util.constants.MapViewConstants; import android.app.AlertDialog; import android.app.Dialog; +import android.app.SearchManager; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -69,12 +70,12 @@ import com.j256.ormlite.dao.Dao; * @author Christopher Baines * */ -public class MapActivity extends ToastHelperActivity implements MapViewConstants, RouteColorConstants, - OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { +public class MapActivity extends ToastHelperActivity implements MapViewConstants, RouteColorConstants, OnItemClickListener, + OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { /** - * Enable to use the database in the assets folder, if its not enabled, the - * database is built from the csv files in the assets folder + * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets + * folder */ private boolean useBundledDatabase = true; @@ -126,11 +127,10 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private static final int NON_UNI_LINK_BUS_STOP_OVERLAY_RANK = 4; // -- Site Overlays - static final String[] SITE_NAMES = { "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", + static final String[] SITE_NAMES = { "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" }; private static final String SITE_OVERLAYS = "siteOverlays:"; @@ -168,8 +168,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants // Uni-Link routes static final String[] UNI_LINK_ROUTES = { "U1", "U1N", "U2", "U6", "U9" }; - static final String[] PREFERENCES_GROUPS = { BUS_STOP_OVERLAYS, BUS_ROUTE_OVERLAYS, BUILDING_OVERLAYS, - SITE_OVERLAYS, OTHER_OVERLAYS }; + static final String[] PREFERENCES_GROUPS = { BUS_STOP_OVERLAYS, BUS_ROUTE_OVERLAYS, BUILDING_OVERLAYS, SITE_OVERLAYS, + OTHER_OVERLAYS }; static final String[][] PREFERENCES_CHILDREN = { UNI_LINK_ROUTES, UNI_LINK_ROUTES, BUILDING_TYPES, SITE_NAMES, OTHER_OVERLAY_NAMES }; @@ -238,10 +238,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants pastOverlays = (HashMap) getLastNonConfigurationInstance(); /* - * SensorManager mSensorManager = (SensorManager) - * getSystemService(Context.SENSOR_SERVICE); This code in the following - * constructor causes problems in some emulators, disable sensors to - * fix. + * SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); This code in the following + * constructor causes problems in some emulators, disable sensors to fix. */ Log.i(TAG, "Starting creating myLocationOverlay"); myLocationOverlay = new MyLocationOverlay(instance, mapView); @@ -265,8 +263,13 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants GeoPoint userLocation = null; - Bundle extras = getIntent().getExtras(); - if (getIntent().getDataString() != null) { + Intent intent = getIntent(); + Bundle extras = intent.getExtras(); + if (Intent.ACTION_SEARCH.equals(intent.getAction())) { + String query = intent.getStringExtra(SearchManager.QUERY); + Log.i(TAG, "Searching for " + query); + + } else if (getIntent().getDataString() != null) { Log.i(TAG, "getIntent().getDataString() " + getIntent().getDataString()); @@ -430,8 +433,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants if (scaleBarOverlay != null) { Log.v(TAG, "ScaleBarOverlay is already created"); } else { - if (pastOverlays != null - && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get(SCALE_BAR_OVERLAY)) != null) { + if (pastOverlays != null && (scaleBarOverlay = (ScaleBarOverlay) pastOverlays.get(SCALE_BAR_OVERLAY)) != null) { Log.i(TAG, "Finished restoring utility overlays " + (System.currentTimeMillis() - startTime)); } else { scaleBarOverlay = new ScaleBarOverlay(instance); @@ -460,8 +462,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private void showRouteOverlay(final BusRoute route) { new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing route " + route.code + " overlay at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing route " + route.code + " overlay at " + (System.currentTimeMillis() - startTime)); final SharedPreferences activityPrefs = getPreferences(0); final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); @@ -486,8 +487,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants // TODO Is this a route like U1N or, something else, // this hack works somewhat for now? - PathOverlay routeOverlayU1E = DataManager.getRoutePath( - getResources().openRawResource(R.raw.u1e), colour, mResourceProxy); + PathOverlay routeOverlayU1E = DataManager.getRoutePath(getResources().openRawResource(R.raw.u1e), + colour, mResourceProxy); routeOverlayU1E.getPaint().setAntiAlias(true); routeOverlayU1E.getPaint().setAlpha(145); routeOverlayU1E.getPaint().setStrokeWidth(12); @@ -542,8 +543,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants mapView.postInvalidate(); - Log.i(TAG, "Finished showing route " + route.code + " overlay at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing route " + route.code + " overlay at " + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -552,8 +552,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing site " + site.name + " overlay at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing site " + site.name + " overlay at " + (System.currentTimeMillis() - startTime)); final SharedPreferences activityPrefs = getPreferences(0); final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); @@ -562,8 +561,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants if ((siteOverlay = siteOverlays.get(site)) != null) { } else { - if (pastOverlays != null - && (siteOverlay = (PathOverlay) pastOverlays.get(SITE_OVERLAYS + site.name)) != null) { + if (pastOverlays != null && (siteOverlay = (PathOverlay) pastOverlays.get(SITE_OVERLAYS + site.name)) != null) { Log.i(TAG, "Restored " + site.name + " site overlay"); } else { @@ -589,13 +587,11 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - siteOverlay.setEnabled(activityPrefs.getBoolean(SITE_OVERLAYS + site.name, - SITE_OVERLAYS_ENABLED_BY_DEFAULT)); + siteOverlay.setEnabled(activityPrefs.getBoolean(SITE_OVERLAYS + site.name, SITE_OVERLAYS_ENABLED_BY_DEFAULT)); mapView.postInvalidate(); - Log.i(TAG, "Finished showing site " + site.name + " overlay at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing site " + site.name + " overlay at " + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -612,10 +608,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } else { if (pastOverlays != null - && (residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays - .get(RESIDENTIAL_BUILDING_OVERLAY)) != null) { - nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays - .get(NON_RESIDENTIAL_BUILDING_OVERLAY); + && (residentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get(RESIDENTIAL_BUILDING_OVERLAY)) != null) { + nonResidentialBuildingOverlay = (BuildingNumOverlay) pastOverlays.get(NON_RESIDENTIAL_BUILDING_OVERLAY); Log.i(TAG, "Restored building overlays"); } else { @@ -630,8 +624,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants buildingDao = getHelper().getBuildingDao(); - final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, - MODE_PRIVATE); + final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, MODE_PRIVATE); for (Building building : buildingDao) { if (building.residential == true) { @@ -669,8 +662,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS - + RESIDENTIAL_BUILDING_OVERLAY, RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); + residentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + RESIDENTIAL_BUILDING_OVERLAY, + RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); nonResidentialBuildingOverlay.setEnabled(activityPrefs.getBoolean(BUILDING_OVERLAYS + NON_RESIDENTIAL_BUILDING_OVERLAY, NON_RESIDENTIAL_BUILDING_OVERLAY_ENABLED_BY_DEFAULT)); @@ -690,16 +683,11 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants final OverlayRankComparator comparator = new OverlayRankComparator(getPreferences(0)); if (uniLinkBusStopOverlay != null) { - if (!activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + if (!activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + && !activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { Log.i(TAG, "Uni-Link bus stop overlay not needed"); overlays.remove(UNI_LINK_BUS_STOP_OVERLAY); @@ -710,31 +698,25 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants uniLinkBusStopOverlay = null; } else { - uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(0, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(1, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(2, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(3, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(4, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); } } else { - if (activityPrefs - .getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) - || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { + if (activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT) + || activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { if (pastOverlays != null - && (uniLinkBusStopOverlay = (BusStopOverlay) pastOverlays - .get(UNI_LINK_BUS_STOP_OVERLAY)) != null) { + && (uniLinkBusStopOverlay = (BusStopOverlay) pastOverlays.get(UNI_LINK_BUS_STOP_OVERLAY)) != null) { Log.i(TAG, "Restored Uni-Link bus stop overlay"); } else { @@ -752,16 +734,16 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - uniLinkBusStopOverlay.setRoutes(0, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(1, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(2, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(3, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); - uniLinkBusStopOverlay.setRoutes(4, activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", - UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(0, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(1, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U1N", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(2, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U2", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(3, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U6", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); + uniLinkBusStopOverlay.setRoutes(4, + activityPrefs.getBoolean(BUS_STOP_OVERLAYS + "U9", UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)); overlays.put(UNI_LINK_BUS_STOP_OVERLAY, uniLinkBusStopOverlay); @@ -784,8 +766,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private void showNonUniLinkBusStopOverlay() { new Thread(new Runnable() { public void run() { - Log.i(TAG, "Begining showing non uni link bus stop overlays at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Begining showing non uni link bus stop overlays at " + (System.currentTimeMillis() - startTime)); // final SharedPreferences activityPrefs = getPreferences(0); final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(instance); @@ -802,14 +783,12 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } else if (sharedPrefs.getBoolean(NON_UNI_LINK_BUS_STOPS_OVERLAY, NON_UNI_LINK_BUS_STOP_OVERLAY_ENABLED_BY_DEFAULT)) { if (pastOverlays != null - && (nonUniLinkBusStopOverlay = (BusStopOverlay) pastOverlays - .get(NON_UNI_LINK_BUS_STOPS_OVERLAY)) != null) { + && (nonUniLinkBusStopOverlay = (BusStopOverlay) pastOverlays.get(NON_UNI_LINK_BUS_STOPS_OVERLAY)) != null) { Log.i(TAG, "Restored non Uni-Link bus stop overlays"); } else { try { List busStops; - Log.v(TAG, "Begin fetching non Uni-Link BusStops at " - + (System.currentTimeMillis() - startTime)); + Log.v(TAG, "Begin fetching non Uni-Link BusStops at " + (System.currentTimeMillis() - startTime)); busStops = getHelper().getBusStopDao().queryForEq(BusStop.UNI_LINK_FIELD_NAME, false); @@ -839,8 +818,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants mapView.postInvalidate(); - Log.i(TAG, "Finished showing non Uni-Link bus stop overlays at " - + (System.currentTimeMillis() - startTime)); + Log.i(TAG, "Finished showing non Uni-Link bus stop overlays at " + (System.currentTimeMillis() - startTime)); } }).start(); } @@ -857,7 +835,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants // Handle item selection switch (item.getItemId()) { case R.id.menu_find: - onSearchRequested(); + onSearchRequested(); return true; case R.id.menu_preferences: Intent settingsActivity = new Intent(getBaseContext(), PreferencesActivity.class); @@ -891,9 +869,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants }; AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage("GPS is not enabled, do you wish to enable it?") - .setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener) - .show(); + builder.setMessage("GPS is not enabled, do you wish to enable it?").setPositiveButton("Yes", dialogClickListener) + .setNegativeButton("No", dialogClickListener).show(); } return true; diff --git a/src/net/cbaines/suma/SearchActivity.java b/src/net/cbaines/suma/SearchActivity.java deleted file mode 100644 index f7f5e55..0000000 --- a/src/net/cbaines/suma/SearchActivity.java +++ /dev/null @@ -1,380 +0,0 @@ -/* - * 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 java.util.Collections; -import java.util.Iterator; -import java.util.List; - -import org.osmdroid.util.GeoPoint; - -import android.app.SearchManager; -import android.content.Context; -import android.content.Intent; -import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.net.Uri; -import android.os.Bundle; -import android.text.Editable; -import android.text.TextWatcher; -import android.util.Log; -import android.view.Gravity; -import android.view.View; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.AdapterView.OnItemLongClickListener; -import android.widget.EditText; -import android.widget.LinearLayout; -import android.widget.ListView; -import android.widget.ProgressBar; - -import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; -import com.j256.ormlite.dao.Dao; -import com.j256.ormlite.stmt.PreparedQuery; -import com.j256.ormlite.stmt.QueryBuilder; - -public class SearchActivity extends OrmLiteBaseActivity implements Runnable, TextWatcher, - OnItemClickListener, LocationListener, OnItemLongClickListener { - - final static String TAG = "FindActivity"; - - public static final String ORIGIN = "o"; - public static final int MAP_ACTIVITY = 0; - - private EditText searchBar; - private ListView listItems; - private ProgressBar progBar; - private LinearLayout findContentLayout; - - private String searchTerm = ""; - - private Dao buildingDao; - private Dao busStopDao; - private Dao siteDao; - - private POIArrayAdapter adapter; - - private GeoPoint userLocation; - - private Thread searchThread; - - ArrayList getNearestPOIs(int distance) { - Log.i(TAG, "Getting nearest POI's"); - ArrayList nearestPOIs = new ArrayList(); - for (Iterator buildingDaoIter = buildingDao.iterator(); buildingDaoIter.hasNext();) { - POI poi = buildingDaoIter.next(); - int dist = poi.point.distanceTo(userLocation); - if (dist < distance) { - poi.distTo = dist; - nearestPOIs.add(poi); - } - } - - for (Iterator busStopDaoIter = busStopDao.iterator(); busStopDaoIter.hasNext();) { - POI poi = busStopDaoIter.next(); - int dist = poi.point.distanceTo(userLocation); - if (dist < distance) { - poi.distTo = dist; - nearestPOIs.add(poi); - } - } - - Collections.sort(nearestPOIs, new POIDistanceComparator(userLocation, true)); - - Log.i(TAG, "Got " + nearestPOIs.size() + " nearest POI's"); - return nearestPOIs; - } - - // Search thread - public void run() { - POIArrayAdapter tempAdaptor; - GeoPoint thisUserLocation = userLocation; - - Log.i(TAG, "Search thread started"); - String thisSearchTerm = searchTerm; - - ArrayList foundPOIsArray = null; - - Log.i(TAG, "Search term length " + thisSearchTerm.length() + " userLocation == null " - + (thisUserLocation == null)); - if (thisSearchTerm.length() == 0 && thisUserLocation != null) { - foundPOIsArray = getNearestPOIs(200); - - } - - if (foundPOIsArray != null && foundPOIsArray.size() != 0) { - - tempAdaptor = new POIArrayAdapter(this, foundPOIsArray); - } else { - - try { - - foundPOIsArray = new ArrayList(); - - if (thisSearchTerm.length() == 0) { - for (Building building : buildingDao) { - foundPOIsArray.add(building); - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - for (BusStop busStop : busStopDao) { - foundPOIsArray.add(busStop); - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - for (Site site : siteDao) { - foundPOIsArray.add(site); - } - - } else { - - QueryBuilder buildingQueryBuilder = buildingDao.queryBuilder(); - buildingQueryBuilder.where().like(Building.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() - .like(Building.NAME_FIELD_NAME, "%" + thisSearchTerm + "%"); - PreparedQuery buildingPreparedQuery = buildingQueryBuilder.prepare(); - List buildings = buildingDao.query(buildingPreparedQuery); - for (Building building : buildings) { - foundPOIsArray.add(building); - } - buildings = null; - - if (!thisSearchTerm.equals(searchTerm)) - return; - - if (thisSearchTerm.contains("site")) { - for (Site site : siteDao) { - foundPOIsArray.add(site); - } - } else { - QueryBuilder siteQueryBuilder = siteDao.queryBuilder(); - siteQueryBuilder.where().like(Site.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() - .like(Site.NAME_FIELD_NAME, "%" + thisSearchTerm + "%"); - PreparedQuery sitePreparedQuery = siteQueryBuilder.prepare(); - List sites = siteDao.query(sitePreparedQuery); - for (Site site : sites) { - foundPOIsArray.add(site); - } - sites = null; - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - // if (thisSearchTerm.contains("bus")) { - // for (BusStop busStop : busStopDao) { - // foundPOIsArray.add(busStop); - // } - // } else { - QueryBuilder busStopQueryBuilder = busStopDao.queryBuilder(); - busStopQueryBuilder.where().like(BusStop.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() - .like(BusStop.DESCRIPTION_FIELD_NAME, "%" + thisSearchTerm + "%"); - PreparedQuery busStopPreparedQuery = busStopQueryBuilder.prepare(); - List busStops = busStopDao.query(busStopPreparedQuery); - for (BusStop busStop : busStops) { - foundPOIsArray.add(busStop); - } - busStops = null; - // } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - Log.i(TAG, "Found " + foundPOIsArray.size() + " pois"); - - if (thisUserLocation != null) { - Collections.sort(foundPOIsArray, new POIDistanceComparator(userLocation)); - } else { - Collections.sort(foundPOIsArray, new StringDistanceComparator(thisSearchTerm)); - } - - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - tempAdaptor = new POIArrayAdapter(this, foundPOIsArray); - - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return; - } - } - - if (thisSearchTerm.equals(searchTerm)) { - Log.i(TAG, "Search terms still equal, starting post"); - adapter = tempAdaptor; - listItems.post(new Runnable() { - public void run() { - listItems.setAdapter(adapter); - if (progBar.getVisibility() != View.GONE) { - progBar.setVisibility(View.GONE); - findContentLayout.setGravity(Gravity.TOP); - } - } - }); - } else { - Log.i(TAG, "Search terms no longer equal, exiting"); - } - } - - /** Called when the activity is first created. */ - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.find_activity); - - Log.i(TAG, "FindActivity started"); - - searchBar = (EditText) findViewById(R.id.searchBar); - searchBar.addTextChangedListener(this); - - Intent intent = getIntent(); - if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - String query = intent.getStringExtra(SearchManager.QUERY); - Log.i(TAG, "Searching for " + query); - searchBar.setText(query); - } - - listItems = (ListView) findViewById(R.id.findListItems); - listItems.setOnItemClickListener(this); - listItems.setOnItemLongClickListener(this); - - progBar = (ProgressBar) findViewById(R.id.findLoadBar); - findContentLayout = (LinearLayout) findViewById(R.id.findContentLayout); - - try { - buildingDao = getHelper().getBuildingDao(); - busStopDao = getHelper().getBusStopDao(); - siteDao = getHelper().getSiteDao(); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - // Acquire a reference to the system Location Manager - LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); - Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); - if (lastKnownLocation != null) { - userLocation = Util.locationToGeoPoint(lastKnownLocation); - } else { - lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); - if (lastKnownLocation != null) { - userLocation = Util.locationToGeoPoint(lastKnownLocation); - - } - } - // Register the listener with the Location Manager to receive location - // updates - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, this); - try { - locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, this); - } catch (Exception e) { - // Ignore anything that goes wrong here... - } - - searchThread = new Thread(this); - searchThread.start(); - - } - - public void afterTextChanged(Editable s) { - searchTerm = s.toString(); - Log.i(TAG, "Text changed " + searchTerm + " starting search thread"); - new Thread(this).start(); - } - - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - public void onItemClick(AdapterView parent, View view, int position, long id) { - Log.i(TAG, "OnItemClick pos " + position + " id " + id); - - POI poi = adapter.getPOIItem(position); - - 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); - } - - public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { - - POI poi = adapter.getPOIItem(position); - - Log.i(TAG, "Long Click Event ID: " + poi.id); - Uri uri = null; - - if (poi.type.equals(POI.BUS_STOP)) { - Log.i(TAG, "Its a bus stop"); - - BusStop busStop = (BusStop) poi; - - uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id); - - } else if (poi.type.equals(POI.BUILDING)) { - Log.i(TAG, "Its a buildings"); - - Building building = (Building) poi; - - uri = Uri.parse("http://id.southampton.ac.uk/building/" + building.id); - } else { - Log.e(TAG, "Error in onItemLongClick"); - return false; - } - - Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); - - Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri); - startActivity(busStopIntent); - - return true; - } - - public void onLocationChanged(Location location) { - Log.i(TAG, "Got location update for FindActivity"); - userLocation = Util.locationToGeoPoint(location); - if (!searchThread.isAlive()) { - searchThread = new Thread(this); - searchThread.start(); - } - } - - public void onProviderDisabled(String arg0) { - } - - public void onProviderEnabled(String provider) { - } - - public void onStatusChanged(String provider, int status, Bundle extras) { - } -} \ No newline at end of file diff --git a/src/net/cbaines/suma/SearchResultsActivity.java b/src/net/cbaines/suma/SearchResultsActivity.java new file mode 100644 index 0000000..a7b5a55 --- /dev/null +++ b/src/net/cbaines/suma/SearchResultsActivity.java @@ -0,0 +1,380 @@ +/* + * 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 java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.osmdroid.util.GeoPoint; + +import android.app.SearchManager; +import android.content.Context; +import android.content.Intent; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.net.Uri; +import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Log; +import android.view.Gravity; +import android.view.View; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.AdapterView.OnItemLongClickListener; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.ListView; +import android.widget.ProgressBar; + +import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.stmt.PreparedQuery; +import com.j256.ormlite.stmt.QueryBuilder; + +public class SearchResultsActivity extends OrmLiteBaseActivity implements Runnable, TextWatcher, + OnItemClickListener, LocationListener, OnItemLongClickListener { + + final static String TAG = "FindActivity"; + + public static final String ORIGIN = "o"; + public static final int MAP_ACTIVITY = 0; + + private EditText searchBar; + private ListView listItems; + private ProgressBar progBar; + private LinearLayout findContentLayout; + + private String searchTerm = ""; + + private Dao buildingDao; + private Dao busStopDao; + private Dao siteDao; + + private POIArrayAdapter adapter; + + private GeoPoint userLocation; + + private Thread searchThread; + + ArrayList getNearestPOIs(int distance) { + Log.i(TAG, "Getting nearest POI's"); + ArrayList nearestPOIs = new ArrayList(); + for (Iterator buildingDaoIter = buildingDao.iterator(); buildingDaoIter.hasNext();) { + POI poi = buildingDaoIter.next(); + int dist = poi.point.distanceTo(userLocation); + if (dist < distance) { + poi.distTo = dist; + nearestPOIs.add(poi); + } + } + + for (Iterator busStopDaoIter = busStopDao.iterator(); busStopDaoIter.hasNext();) { + POI poi = busStopDaoIter.next(); + int dist = poi.point.distanceTo(userLocation); + if (dist < distance) { + poi.distTo = dist; + nearestPOIs.add(poi); + } + } + + Collections.sort(nearestPOIs, new POIDistanceComparator(userLocation, true)); + + Log.i(TAG, "Got " + nearestPOIs.size() + " nearest POI's"); + return nearestPOIs; + } + + // Search thread + public void run() { + POIArrayAdapter tempAdaptor; + GeoPoint thisUserLocation = userLocation; + + Log.i(TAG, "Search thread started"); + String thisSearchTerm = searchTerm; + + ArrayList foundPOIsArray = null; + + Log.i(TAG, "Search term length " + thisSearchTerm.length() + " userLocation == null " + + (thisUserLocation == null)); + if (thisSearchTerm.length() == 0 && thisUserLocation != null) { + foundPOIsArray = getNearestPOIs(200); + + } + + if (foundPOIsArray != null && foundPOIsArray.size() != 0) { + + tempAdaptor = new POIArrayAdapter(this, foundPOIsArray); + } else { + + try { + + foundPOIsArray = new ArrayList(); + + if (thisSearchTerm.length() == 0) { + for (Building building : buildingDao) { + foundPOIsArray.add(building); + } + + if (!thisSearchTerm.equals(searchTerm)) + return; + + for (BusStop busStop : busStopDao) { + foundPOIsArray.add(busStop); + } + + if (!thisSearchTerm.equals(searchTerm)) + return; + + for (Site site : siteDao) { + foundPOIsArray.add(site); + } + + } else { + + QueryBuilder buildingQueryBuilder = buildingDao.queryBuilder(); + buildingQueryBuilder.where().like(Building.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() + .like(Building.NAME_FIELD_NAME, "%" + thisSearchTerm + "%"); + PreparedQuery buildingPreparedQuery = buildingQueryBuilder.prepare(); + List buildings = buildingDao.query(buildingPreparedQuery); + for (Building building : buildings) { + foundPOIsArray.add(building); + } + buildings = null; + + if (!thisSearchTerm.equals(searchTerm)) + return; + + if (thisSearchTerm.contains("site")) { + for (Site site : siteDao) { + foundPOIsArray.add(site); + } + } else { + QueryBuilder siteQueryBuilder = siteDao.queryBuilder(); + siteQueryBuilder.where().like(Site.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() + .like(Site.NAME_FIELD_NAME, "%" + thisSearchTerm + "%"); + PreparedQuery sitePreparedQuery = siteQueryBuilder.prepare(); + List sites = siteDao.query(sitePreparedQuery); + for (Site site : sites) { + foundPOIsArray.add(site); + } + sites = null; + } + + if (!thisSearchTerm.equals(searchTerm)) + return; + + // if (thisSearchTerm.contains("bus")) { + // for (BusStop busStop : busStopDao) { + // foundPOIsArray.add(busStop); + // } + // } else { + QueryBuilder busStopQueryBuilder = busStopDao.queryBuilder(); + busStopQueryBuilder.where().like(BusStop.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() + .like(BusStop.DESCRIPTION_FIELD_NAME, "%" + thisSearchTerm + "%"); + PreparedQuery busStopPreparedQuery = busStopQueryBuilder.prepare(); + List busStops = busStopDao.query(busStopPreparedQuery); + for (BusStop busStop : busStops) { + foundPOIsArray.add(busStop); + } + busStops = null; + // } + + if (!thisSearchTerm.equals(searchTerm)) + return; + + Log.i(TAG, "Found " + foundPOIsArray.size() + " pois"); + + if (thisUserLocation != null) { + Collections.sort(foundPOIsArray, new POIDistanceComparator(userLocation)); + } else { + Collections.sort(foundPOIsArray, new StringDistanceComparator(thisSearchTerm)); + } + + } + + if (!thisSearchTerm.equals(searchTerm)) + return; + + tempAdaptor = new POIArrayAdapter(this, foundPOIsArray); + + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return; + } + } + + if (thisSearchTerm.equals(searchTerm)) { + Log.i(TAG, "Search terms still equal, starting post"); + adapter = tempAdaptor; + listItems.post(new Runnable() { + public void run() { + listItems.setAdapter(adapter); + if (progBar.getVisibility() != View.GONE) { + progBar.setVisibility(View.GONE); + findContentLayout.setGravity(Gravity.TOP); + } + } + }); + } else { + Log.i(TAG, "Search terms no longer equal, exiting"); + } + } + + /** Called when the activity is first created. */ + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.find_activity); + + Log.i(TAG, "FindActivity started"); + + searchBar = (EditText) findViewById(R.id.searchBar); + searchBar.addTextChangedListener(this); + + Intent intent = getIntent(); + if (Intent.ACTION_SEARCH.equals(intent.getAction())) { + String query = intent.getStringExtra(SearchManager.QUERY); + Log.i(TAG, "Searching for " + query); + searchBar.setText(query); + } + + listItems = (ListView) findViewById(R.id.findListItems); + listItems.setOnItemClickListener(this); + listItems.setOnItemLongClickListener(this); + + progBar = (ProgressBar) findViewById(R.id.findLoadBar); + findContentLayout = (LinearLayout) findViewById(R.id.findContentLayout); + + try { + buildingDao = getHelper().getBuildingDao(); + busStopDao = getHelper().getBusStopDao(); + siteDao = getHelper().getSiteDao(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // Acquire a reference to the system Location Manager + LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); + Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); + if (lastKnownLocation != null) { + userLocation = Util.locationToGeoPoint(lastKnownLocation); + } else { + lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + if (lastKnownLocation != null) { + userLocation = Util.locationToGeoPoint(lastKnownLocation); + + } + } + // Register the listener with the Location Manager to receive location + // updates + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, this); + try { + locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, this); + } catch (Exception e) { + // Ignore anything that goes wrong here... + } + + searchThread = new Thread(this); + searchThread.start(); + + } + + public void afterTextChanged(Editable s) { + searchTerm = s.toString(); + Log.i(TAG, "Text changed " + searchTerm + " starting search thread"); + new Thread(this).start(); + } + + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + public void onItemClick(AdapterView parent, View view, int position, long id) { + Log.i(TAG, "OnItemClick pos " + position + " id " + id); + + POI poi = adapter.getPOIItem(position); + + 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); + } + + public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { + + POI poi = adapter.getPOIItem(position); + + Log.i(TAG, "Long Click Event ID: " + poi.id); + Uri uri = null; + + if (poi.type.equals(POI.BUS_STOP)) { + Log.i(TAG, "Its a bus stop"); + + BusStop busStop = (BusStop) poi; + + uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id); + + } else if (poi.type.equals(POI.BUILDING)) { + Log.i(TAG, "Its a buildings"); + + Building building = (Building) poi; + + uri = Uri.parse("http://id.southampton.ac.uk/building/" + building.id); + } else { + Log.e(TAG, "Error in onItemLongClick"); + return false; + } + + Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); + + Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri); + startActivity(busStopIntent); + + return true; + } + + public void onLocationChanged(Location location) { + Log.i(TAG, "Got location update for FindActivity"); + userLocation = Util.locationToGeoPoint(location); + if (!searchThread.isAlive()) { + searchThread = new Thread(this); + searchThread.start(); + } + } + + public void onProviderDisabled(String arg0) { + } + + public void onProviderEnabled(String provider) { + } + + public void onStatusChanged(String provider, int status, Bundle extras) { + } +} \ No newline at end of file -- cgit v1.2.3 From a45c1858d22622ef80253743c15c5a28760dbbae Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sun, 11 Mar 2012 17:19:24 +0000 Subject: Major progress on implementing the new search stuff. --- src/net/cbaines/suma/BuildingNumOverlay.java | 24 +- src/net/cbaines/suma/DatabaseHelper.java | 4 +- src/net/cbaines/suma/MapActivity.java | 2 +- src/net/cbaines/suma/SearchResultsActivity.java | 374 ------------------------ 4 files changed, 26 insertions(+), 378 deletions(-) delete mode 100644 src/net/cbaines/suma/SearchResultsActivity.java (limited to 'src') diff --git a/src/net/cbaines/suma/BuildingNumOverlay.java b/src/net/cbaines/suma/BuildingNumOverlay.java index b541aa1..6acffa1 100644 --- a/src/net/cbaines/suma/BuildingNumOverlay.java +++ b/src/net/cbaines/suma/BuildingNumOverlay.java @@ -239,12 +239,34 @@ public class BuildingNumOverlay extends Overlay implements Preferences, OnShared } } + /** + * You can prevent all(!) other Touch-related events from happening!
+ * By default does nothing (return false). If you handled the Event, return true, otherwise return + * false. If you returned true none of the following Overlays or the underlying {@link MapView} has + * the chance to handle this event. + */ + public boolean onTouchEvent(final MotionEvent event, final MapView mapView) { + Log.i(TAG, "getAction " + event.getAction()); + Log.i(TAG, "getDownTime " + event.getDownTime()); + Log.i(TAG, "getPointerCount " + event.getPointerCount()); + + return true; + } + @Override public boolean onLongPress(final MotionEvent event, final MapView mapView) { if (!this.isEnabled()) return false; + int pointerCount = event.getPointerCount(); + if (pointerCount > 1) { + Log.v(TAG, "Detected a zoom " + pointerCount); + return false; + } else { + Log.v(TAG, "Zoom not detected " + pointerCount); + } + final Building building = getSelectedItem(event, mapView); if (building == null) { @@ -254,7 +276,7 @@ public class BuildingNumOverlay extends Overlay implements Preferences, OnShared Log.v(TAG, "building Pressed " + building.id); final SharedPreferences favouritesPrefs = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0); - final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + // final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); if (favouritesPrefs.getBoolean(building.id, false)) { favouritesPrefs.edit().remove(building.id).commit(); diff --git a/src/net/cbaines/suma/DatabaseHelper.java b/src/net/cbaines/suma/DatabaseHelper.java index 17ef720..21ee99b 100644 --- a/src/net/cbaines/suma/DatabaseHelper.java +++ b/src/net/cbaines/suma/DatabaseHelper.java @@ -210,9 +210,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { myOutput.flush(); myOutput.close(); myInput.close(); - + // getWritableDatabase().close(); - + Log.i(TAG, "Finished copying db"); } diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index 2bdc3d7..f462060 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -195,7 +195,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants Log.i(TAG, "Begining loading database " + (System.currentTimeMillis() - startTime)); - DatabaseHelper helper = getHelper(); + DatabaseHelper helper = getHelperInternal(this); Log.i(TAG, "Got the helper at " + (System.currentTimeMillis() - startTime)); boolean dbExist = helper.checkDataBase(); diff --git a/src/net/cbaines/suma/SearchResultsActivity.java b/src/net/cbaines/suma/SearchResultsActivity.java deleted file mode 100644 index 503c876..0000000 --- a/src/net/cbaines/suma/SearchResultsActivity.java +++ /dev/null @@ -1,374 +0,0 @@ -/* - * 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 java.util.Collections; -import java.util.Iterator; -import java.util.List; - -import org.osmdroid.util.GeoPoint; - -import android.app.SearchManager; -import android.content.Context; -import android.content.Intent; -import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.net.Uri; -import android.os.Bundle; -import android.text.Editable; -import android.text.TextWatcher; -import android.util.Log; -import android.view.Gravity; -import android.view.View; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.AdapterView.OnItemLongClickListener; -import android.widget.LinearLayout; -import android.widget.ListView; -import android.widget.ProgressBar; - -import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; -import com.j256.ormlite.dao.Dao; -import com.j256.ormlite.stmt.PreparedQuery; -import com.j256.ormlite.stmt.QueryBuilder; - -public class SearchResultsActivity extends OrmLiteBaseActivity implements Runnable, TextWatcher, - OnItemClickListener, LocationListener, OnItemLongClickListener { - - final static String TAG = "FindActivity"; - - public static final String ORIGIN = "o"; - public static final int MAP_ACTIVITY = 0; - - private ListView listItems; - private ProgressBar progBar; - private LinearLayout findContentLayout; - - private String searchTerm = ""; - - private Dao buildingDao; - private Dao busStopDao; - private Dao siteDao; - - private POIArrayAdapter adapter; - - private GeoPoint userLocation; - - private Thread searchThread; - - ArrayList getNearestPOIs(int distance) { - Log.i(TAG, "Getting nearest POI's"); - ArrayList nearestPOIs = new ArrayList(); - for (Iterator buildingDaoIter = buildingDao.iterator(); buildingDaoIter.hasNext();) { - POI poi = buildingDaoIter.next(); - int dist = poi.point.distanceTo(userLocation); - if (dist < distance) { - poi.distTo = dist; - nearestPOIs.add(poi); - } - } - - for (Iterator busStopDaoIter = busStopDao.iterator(); busStopDaoIter.hasNext();) { - POI poi = busStopDaoIter.next(); - int dist = poi.point.distanceTo(userLocation); - if (dist < distance) { - poi.distTo = dist; - nearestPOIs.add(poi); - } - } - - Collections.sort(nearestPOIs, new POIDistanceComparator(userLocation, true)); - - Log.i(TAG, "Got " + nearestPOIs.size() + " nearest POI's"); - return nearestPOIs; - } - - // Search thread - public void run() { - POIArrayAdapter tempAdaptor; - GeoPoint thisUserLocation = userLocation; - - Log.i(TAG, "Search thread started"); - String thisSearchTerm = searchTerm; - - ArrayList foundPOIsArray = null; - - Log.i(TAG, "Search term length " + thisSearchTerm.length() + " userLocation == null " - + (thisUserLocation == null)); - if (thisSearchTerm.length() == 0 && thisUserLocation != null) { - foundPOIsArray = getNearestPOIs(200); - - } - - if (foundPOIsArray != null && foundPOIsArray.size() != 0) { - - tempAdaptor = new POIArrayAdapter(this, foundPOIsArray); - } else { - - try { - - foundPOIsArray = new ArrayList(); - - if (thisSearchTerm.length() == 0) { - for (Building building : buildingDao) { - foundPOIsArray.add(building); - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - for (BusStop busStop : busStopDao) { - foundPOIsArray.add(busStop); - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - for (Site site : siteDao) { - foundPOIsArray.add(site); - } - - } else { - - QueryBuilder buildingQueryBuilder = buildingDao.queryBuilder(); - buildingQueryBuilder.where().like(Building.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() - .like(Building.NAME_FIELD_NAME, "%" + thisSearchTerm + "%"); - PreparedQuery buildingPreparedQuery = buildingQueryBuilder.prepare(); - List buildings = buildingDao.query(buildingPreparedQuery); - for (Building building : buildings) { - foundPOIsArray.add(building); - } - buildings = null; - - if (!thisSearchTerm.equals(searchTerm)) - return; - - if (thisSearchTerm.contains("site")) { - for (Site site : siteDao) { - foundPOIsArray.add(site); - } - } else { - QueryBuilder siteQueryBuilder = siteDao.queryBuilder(); - siteQueryBuilder.where().like(Site.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() - .like(Site.NAME_FIELD_NAME, "%" + thisSearchTerm + "%"); - PreparedQuery sitePreparedQuery = siteQueryBuilder.prepare(); - List sites = siteDao.query(sitePreparedQuery); - for (Site site : sites) { - foundPOIsArray.add(site); - } - sites = null; - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - // if (thisSearchTerm.contains("bus")) { - // for (BusStop busStop : busStopDao) { - // foundPOIsArray.add(busStop); - // } - // } else { - QueryBuilder busStopQueryBuilder = busStopDao.queryBuilder(); - busStopQueryBuilder.where().like(BusStop.ID_FIELD_NAME, "%" + thisSearchTerm + "%").or() - .like(BusStop.DESCRIPTION_FIELD_NAME, "%" + thisSearchTerm + "%"); - PreparedQuery busStopPreparedQuery = busStopQueryBuilder.prepare(); - List busStops = busStopDao.query(busStopPreparedQuery); - for (BusStop busStop : busStops) { - foundPOIsArray.add(busStop); - } - busStops = null; - // } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - Log.i(TAG, "Found " + foundPOIsArray.size() + " pois"); - - if (thisUserLocation != null) { - Collections.sort(foundPOIsArray, new POIDistanceComparator(userLocation)); - } else { - Collections.sort(foundPOIsArray, new StringDistanceComparator(thisSearchTerm)); - } - - } - - if (!thisSearchTerm.equals(searchTerm)) - return; - - tempAdaptor = new POIArrayAdapter(this, foundPOIsArray); - - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return; - } - } - - if (thisSearchTerm.equals(searchTerm)) { - Log.i(TAG, "Search terms still equal, starting post"); - adapter = tempAdaptor; - listItems.post(new Runnable() { - public void run() { - listItems.setAdapter(adapter); - if (progBar.getVisibility() != View.GONE) { - progBar.setVisibility(View.GONE); - findContentLayout.setGravity(Gravity.TOP); - } - } - }); - } else { - Log.i(TAG, "Search terms no longer equal, exiting"); - } - } - - /** Called when the activity is first created. */ - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.search_activity); - - Log.i(TAG, "FindActivity started"); - - Intent intent = getIntent(); - if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - searchTerm = intent.getStringExtra(SearchManager.QUERY); - Log.i(TAG, "Searching for " + searchTerm); - } - - listItems = (ListView) findViewById(R.id.findListItems); - listItems.setOnItemClickListener(this); - listItems.setOnItemLongClickListener(this); - - progBar = (ProgressBar) findViewById(R.id.findLoadBar); - findContentLayout = (LinearLayout) findViewById(R.id.findContentLayout); - - try { - buildingDao = getHelper().getBuildingDao(); - busStopDao = getHelper().getBusStopDao(); - siteDao = getHelper().getSiteDao(); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - // Acquire a reference to the system Location Manager - LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); - Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); - if (lastKnownLocation != null) { - userLocation = Util.locationToGeoPoint(lastKnownLocation); - } else { - lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); - if (lastKnownLocation != null) { - userLocation = Util.locationToGeoPoint(lastKnownLocation); - - } - } - // Register the listener with the Location Manager to receive location - // updates - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, this); - try { - locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, this); - } catch (Exception e) { - // Ignore anything that goes wrong here... - } - - searchThread = new Thread(this); - searchThread.start(); - - } - - public void afterTextChanged(Editable s) { - searchTerm = s.toString(); - Log.i(TAG, "Text changed " + searchTerm + " starting search thread"); - new Thread(this).start(); - } - - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - public void onItemClick(AdapterView parent, View view, int position, long id) { - Log.i(TAG, "OnItemClick pos " + position + " id " + id); - - POI poi = adapter.getPOIItem(position); - - 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); - } - - public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { - - POI poi = adapter.getPOIItem(position); - - Log.i(TAG, "Long Click Event ID: " + poi.id); - Uri uri = null; - - if (poi.type.equals(POI.BUS_STOP)) { - Log.i(TAG, "Its a bus stop"); - - BusStop busStop = (BusStop) poi; - - uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id); - - } else if (poi.type.equals(POI.BUILDING)) { - Log.i(TAG, "Its a buildings"); - - Building building = (Building) poi; - - uri = Uri.parse("http://id.southampton.ac.uk/building/" + building.id); - } else { - Log.e(TAG, "Error in onItemLongClick"); - return false; - } - - Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); - - Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri); - startActivity(busStopIntent); - - return true; - } - - public void onLocationChanged(Location location) { - Log.i(TAG, "Got location update for FindActivity"); - userLocation = Util.locationToGeoPoint(location); - if (!searchThread.isAlive()) { - searchThread = new Thread(this); - searchThread.start(); - } - } - - public void onProviderDisabled(String arg0) { - } - - public void onProviderEnabled(String provider) { - } - - public void onStatusChanged(String provider, int status, Bundle extras) { - } -} \ No newline at end of file -- cgit v1.2.3 From 9f1db756efbe2fdbf540b17e2eb93d7bc8a6ae37 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 12 Mar 2012 10:31:33 +0000 Subject: Removed overlay test code. --- src/net/cbaines/suma/BuildingNumOverlay.java | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/BuildingNumOverlay.java b/src/net/cbaines/suma/BuildingNumOverlay.java index 6acffa1..8ec5ec4 100644 --- a/src/net/cbaines/suma/BuildingNumOverlay.java +++ b/src/net/cbaines/suma/BuildingNumOverlay.java @@ -239,20 +239,6 @@ public class BuildingNumOverlay extends Overlay implements Preferences, OnShared } } - /** - * You can prevent all(!) other Touch-related events from happening!
- * By default does nothing (return false). If you handled the Event, return true, otherwise return - * false. If you returned true none of the following Overlays or the underlying {@link MapView} has - * the chance to handle this event. - */ - public boolean onTouchEvent(final MotionEvent event, final MapView mapView) { - Log.i(TAG, "getAction " + event.getAction()); - Log.i(TAG, "getDownTime " + event.getDownTime()); - Log.i(TAG, "getPointerCount " + event.getPointerCount()); - - return true; - } - @Override public boolean onLongPress(final MotionEvent event, final MapView mapView) { -- cgit v1.2.3 From 16721bd07d3dd892631d82024a259997cd152ba4 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Tue, 13 Mar 2012 19:20:50 +0000 Subject: Fixed database upgrade bug, and re-added data files (removed incorrectly). --- src/net/cbaines/suma/DatabaseHelper.java | 110 ++----------------------------- src/net/cbaines/suma/MapActivity.java | 105 ++++++++++++++++++----------- src/net/cbaines/suma/Preferences.java | 11 ++++ 3 files changed, 85 insertions(+), 141 deletions(-) (limited to 'src') diff --git a/src/net/cbaines/suma/DatabaseHelper.java b/src/net/cbaines/suma/DatabaseHelper.java index 21ee99b..d039518 100644 --- a/src/net/cbaines/suma/DatabaseHelper.java +++ b/src/net/cbaines/suma/DatabaseHelper.java @@ -19,11 +19,6 @@ package net.cbaines.suma; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.sql.SQLException; import android.content.Context; @@ -35,12 +30,7 @@ import com.j256.ormlite.dao.Dao; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; -public class DatabaseHelper extends OrmLiteSqliteOpenHelper { - - private static final String DATABASE_PATH = "/data/data/net.cbaines.suma/databases/"; - private static final String DATABASE_NAME = "data.db"; - - private static final int DATABASE_VERSION = 41; +public class DatabaseHelper extends OrmLiteSqliteOpenHelper implements Preferences { private static final String TAG = "DatabaseHelper"; @@ -52,24 +42,25 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private Dao siteDao = null; private Dao busDao = null; - private Context context; + volatile boolean doneUpgrade = false; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config); Log.i(TAG, "Database Helper created"); - this.context = context; } @Override public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) { try { - Log.i(DatabaseHelper.class.getName(), "onCreate"); + Log.i(TAG, "onCreate"); + TableUtils.createTable(connectionSource, Building.class); TableUtils.createTable(connectionSource, BusStop.class); TableUtils.createTable(connectionSource, BusRoute.class); TableUtils.createTable(connectionSource, RouteStop.class); TableUtils.createTable(connectionSource, Site.class); TableUtils.createTable(connectionSource, Bus.class); + } catch (SQLException e) { Log.e(DatabaseHelper.class.getName(), "Can't create database", e); throw new RuntimeException(e); @@ -78,13 +69,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { @Override public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) { - try { - Log.i(DatabaseHelper.class.getName(), "onUpgrade"); - copyDataBase(); - } catch (IOException e) { - e.printStackTrace(); - } + Log.i(TAG, "onUpgrade"); } @@ -153,90 +139,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { * * @return true if it exists, false if it doesn't */ - public boolean checkDataBase() { - Log.i(TAG, "Check database"); - - /* - * SQLiteDatabase checkDB = null; - * - * try { String myPath = DATABASE_PATH + DATABASE_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, - * SQLiteDatabase.OPEN_READONLY); } catch (SQLiteException e) { - * - * // database does't exist yet. - * - * } - * - * if (checkDB != null) { - * - * checkDB.close(); - * - * } - * - * Log.i(TAG, "Finished checking database"); return checkDB != null ? true : false; - */ - - File dbFile = new File(DATABASE_PATH + DATABASE_NAME); - return dbFile.exists(); - } - - /** - * Copies your database from your local assets-folder to the just created empty database in the system folder, from where it - * can be accessed and handled. This is done by transfering bytestream. - * */ - public void copyDataBase() throws IOException { - Log.i(TAG, "Begining copy database"); - - InputStream myInput = context.getAssets().open(DATABASE_NAME); - - // Path to the just created empty db - String outFileName = DATABASE_PATH + DATABASE_NAME; - - File database = new File(outFileName); - if (database.exists()) { - database.delete(); - } - - // Open the empty db as the output stream - OutputStream myOutput = new FileOutputStream(outFileName); - - // transfer bytes from the inputfile to the outputfile - byte[] buffer = new byte[1024]; - int length; - while ((length = myInput.read(buffer)) > 0) { - myOutput.write(buffer, 0, length); - } - - // Close the streams - myOutput.flush(); - myOutput.close(); - myInput.close(); - - // getWritableDatabase().close(); - - Log.i(TAG, "Finished copying db"); - - } - - /** - * Creates a empty database on the system and rewrites it with your own database. - * */ - public void createDataBase() throws IOException { - - boolean dbExist = checkDataBase(); - - if (dbExist) { - // do nothing - database already exist - } else { - - try { - Log.i(TAG, "Copy database"); - copyDataBase(); - } catch (IOException e) { - throw new Error("Error copying database"); - } - } - - } /** * Close the database connections and clear any cached DAOs. diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index f462060..effd9b4 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -19,8 +19,11 @@ package net.cbaines.suma; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; @@ -48,6 +51,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.database.sqlite.SQLiteDatabase; import android.graphics.Color; import android.graphics.DashPathEffect; import android.graphics.Paint; @@ -75,12 +79,6 @@ import com.j256.ormlite.stmt.QueryBuilder; public class MapActivity extends ToastHelperActivity implements MapViewConstants, RouteColorConstants, OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences { - /** - * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets - * folder - */ - private boolean useBundledDatabase = true; - private MapView mapView; private MapController mapController; private ResourceProxy mResourceProxy; @@ -193,46 +191,46 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants instance = this; - Log.i(TAG, "Begining loading database " + (System.currentTimeMillis() - startTime)); - - DatabaseHelper helper = getHelperInternal(this); - Log.i(TAG, "Got the helper at " + (System.currentTimeMillis() - startTime)); + try { + File dbFile = new File(DATABASE_PATH + DATABASE_NAME); + if (dbFile.exists()) { + Log.i(TAG, "Database exists"); - boolean dbExist = helper.checkDataBase(); - Log.i(TAG, "Finished checking the database at " + (System.currentTimeMillis() - startTime)); + SQLiteDatabase checkDB = SQLiteDatabase.openDatabase(DATABASE_PATH + DATABASE_NAME, null, + SQLiteDatabase.OPEN_READONLY); + int version = checkDB.getVersion(); + checkDB.close(); - if (dbExist) { - // do nothing - database already exist - } else { + if (version != DATABASE_VERSION) { + Log.i(TAG, "Not the right version"); - if (useBundledDatabase) { - try { - // By calling this method and empty database will be created - // into the default system path - // of your application so we are gonna be able to overwrite - // that database with our database. - Log.i(TAG, "GetReadableDatabase"); - helper.getWritableDatabase().close(); - - helper.copyDataBase(); - Log.i(TAG, "Out of copy database"); - } catch (IOException ioe) { - throw new Error("Unable to create database"); + copyDatabase(); } } else { - try { - DataManager.createDatabase(instance); - } catch (SQLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + Log.i(TAG, "Database does not exist"); + + SQLiteDatabase db = getHelper().getWritableDatabase(); + + if (USE_BUNDLED_DATABASE) { + db.close(); + + copyDatabase(); + } else { + Log.i(TAG, "Creating database"); + + try { + DataManager.createDatabase(instance); + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } } } - + } catch (IOException e) { + e.printStackTrace(); } - Log.i(TAG, "Finished the database " + (System.currentTimeMillis() - startTime)); - setContentView(R.layout.map_activity); Log.i(TAG, "Finished setting content view " + (System.currentTimeMillis() - startTime)); @@ -370,6 +368,39 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants Log.i(TAG, "Finished onCreate " + (System.currentTimeMillis() - startTime)); } + private void copyDatabase() throws IOException { + Log.i(TAG, "Begining copy database"); + + InputStream myInput = getAssets().open(DATABASE_NAME); + + // Path to the just created empty db + String outFileName = DATABASE_PATH + DATABASE_NAME; + + File database = new File(outFileName); + if (database.exists()) { + database.delete(); + } + + // Open the empty db as the output stream + OutputStream myOutput = new FileOutputStream(outFileName); + + // transfer bytes from the inputfile to the outputfile + byte[] buffer = new byte[1024]; + int length; + while ((length = myInput.read(buffer)) > 0) { + myOutput.write(buffer, 0, length); + } + + // Close the streams + + myOutput.flush(); + + myOutput.close(); + myInput.close(); + + Log.i(TAG, "Finished copying db"); + } + public void onResume() { super.onResume(); Log.i(TAG, "OnResume " + (System.currentTimeMillis() - startTime)); diff --git a/src/net/cbaines/suma/Preferences.java b/src/net/cbaines/suma/Preferences.java index fde8b70..9f2eaca 100644 --- a/src/net/cbaines/suma/Preferences.java +++ b/src/net/cbaines/suma/Preferences.java @@ -19,4 +19,15 @@ public interface Preferences { static final String CURRENT_APP_VERSION = "0.8"; static final String FAVOURITES_PREFERENCES = "favourites"; + + static final String DATABASE_PATH = "/data/data/net.cbaines.suma/databases/"; + static final String DATABASE_NAME = "data.db"; + + static final int DATABASE_VERSION = 41; + + /** + * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets + * folder + */ + static final boolean USE_BUNDLED_DATABASE = false; } -- cgit v1.2.3 From 19e189ef9ed67f382fea00e48f997ce8e979f030 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Wed, 14 Mar 2012 01:00:56 +0000 Subject: Update for beta release. --- src/net/cbaines/suma/Building.java | 2 +- src/net/cbaines/suma/BusStop.java | 2 +- src/net/cbaines/suma/DataManager.java | 5 +- src/net/cbaines/suma/FavouritesDialog.java | 11 + src/net/cbaines/suma/MapActivity.java | 249 ++++++++++----------- src/net/cbaines/suma/MapContentProvider.java | 141 ++++++------ src/net/cbaines/suma/Preferences.java | 6 +- src/net/cbaines/suma/SearchResultsDialog.java | 10 + src/net/cbaines/suma/StringDistanceComparator.java | 134 ----------- .../cbaines/suma/StringPOIDistanceComparator.java | 134 +++++++++++ src/net/cbaines/suma/WelcomeDialog.java | 2 +- 11 files changed, 362 insertions(+), 334 deletions(-) create mode 100644 src/net/cbaines/suma/FavouritesDialog.java create mode 100644 src/net/cbaines/suma/SearchResultsDialog.java delete mode 100644 src/net/cbaines/suma/StringDistanceComparator.java create mode 100644 src/net/cbaines/suma/StringPOIDistanceComparator.java (limited to 'src') diff --git a/src/net/cbaines/suma/Building.java b/src/net/cbaines/suma/Building.java index 84c9b6c..38c8265 100644 --- a/src/net/cbaines/suma/Building.java +++ b/src/net/cbaines/suma/Building.java @@ -59,6 +59,6 @@ public class Building extends POI { } public String toString() { - return name + " (" + id + ")"; + return name; } } diff --git a/src/net/cbaines/suma/BusStop.java b/src/net/cbaines/suma/BusStop.java index 85f6d01..3bfd9a4 100644 --- a/src/net/cbaines/suma/BusStop.java +++ b/src/net/cbaines/suma/BusStop.java @@ -71,6 +71,6 @@ public class BusStop extends POI { } public String toString() { - return description + " (" + id + ")"; + return description; } } diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index ca27672..8b04928 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -457,9 +457,10 @@ public class DataManager { GeoPoint point = null; if (dataBits[2].length() > 1 && dataBits[3].length() > 1) { - point = Util.csLatLongToGeoPoint(dataBits[2], dataBits[3]); + point = Util.csLatLongToGeoPoint(dataBits[3], dataBits[2]); } else { - point = new GeoPoint(0, 0); + Log.e(TAG, "Missing point for site " + dataBits[1]); + throw new RuntimeException(); } Polygon poly = Util.csPolygonToPolygon(strLine.split("\"")[1]); diff --git a/src/net/cbaines/suma/FavouritesDialog.java b/src/net/cbaines/suma/FavouritesDialog.java new file mode 100644 index 0000000..0884b63 --- /dev/null +++ b/src/net/cbaines/suma/FavouritesDialog.java @@ -0,0 +1,11 @@ +package net.cbaines.suma; + +import android.content.Context; + +public class FavouritesDialog extends POIDialog implements Preferences { + + public FavouritesDialog(Context context) { + super(context); + } + +} diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java index effd9b4..58c7918 100644 --- a/src/net/cbaines/suma/MapActivity.java +++ b/src/net/cbaines/suma/MapActivity.java @@ -92,7 +92,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private ArrayList searchResults = null; - private POIDialog favDialog; + private FavouritesDialog favDialog; private HashMap overlays = new HashMap(); private HashMap pastOverlays; @@ -179,7 +179,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants private MapActivity instance; - private POIDialog searchResultsDialog; + private SearchResultsDialog searchResultsDialog; private static final String TAG = "MapActivity"; @@ -297,11 +297,30 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants searchResults.addAll(busStops); busStops = null; + Dao siteDao = getHelper().getSiteDao(); + + QueryBuilder siteQueryBuilder = siteDao.queryBuilder(); + siteQueryBuilder.where().like(Site.NAME_FIELD_NAME, "%" + query + "%").or() + .like(Site.ID_FIELD_NAME, "%" + query + "%"); + PreparedQuery sitePreparedQuery = siteQueryBuilder.prepare(); + List sites = siteDao.query(sitePreparedQuery); + searchResults.addAll(sites); + sites = null; + + Collections.sort(searchResults, new StringPOIDistanceComparator(query)); + startLocation = new GeoPoint(50935551, -1393488); mapController.setZoom(15); showDialog(SEARCH_RESULTS_DIALOG_ID); + searchResultsDialog.setTitle(getResources().getString(R.string.search_results_dialog_title) + query); + if (searchResults.size() == 0) { + searchResultsDialog.setMessage("No results found"); + } else { + searchResultsDialog.setItems(searchResults); + } + } catch (SQLException e) { e.printStackTrace(); } @@ -967,8 +986,6 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants return false; } - refreshFavouriteDialog(); - return false; case R.id.menu_about: Intent aboutIntent = new Intent(MapActivity.this, AboutActivity.class); @@ -980,40 +997,6 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants } } - private void refreshFavouriteDialog() { - ArrayList newFavouriteItems = new ArrayList(); - - try { - Dao buildingDao = getHelper().getBuildingDao(); - Dao busStopDao = getHelper().getBusStopDao(); - - final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, MODE_PRIVATE); - for (String id : favouritesPrefs.getAll().keySet()) { - Building building; - BusStop busStop; - if ((building = buildingDao.queryForId(id)) != null) { - newFavouriteItems.add(building); - } else if ((busStop = busStopDao.queryForId(id)) != null) { - newFavouriteItems.add(busStop); - } else { - Log.e(TAG, "Item in favourites " + id + " cannot be found"); - } - } - } catch (SQLException e) { - e.printStackTrace(); - } - - Log.i(TAG, "There are " + newFavouriteItems.size() + " favourites"); - if (newFavouriteItems.size() == 0) { - Log.i(TAG, "Favourite dialog has no favourites, displaying message"); - favDialog.setMessage(getResources().getString(R.string.favourites_dialog_message)); - favDialog.setItems(null); - } else { - favDialog.setMessage(""); - favDialog.setItems(newFavouriteItems); - } - } - protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.i(TAG, "Got activity result"); if (resultCode == RESULT_OK) { @@ -1050,10 +1033,6 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants Log.i(TAG, "Got null poi id"); } - if (favDialog != null) { - refreshFavouriteDialog(); - } - mapView.invalidate(); } } @@ -1066,17 +1045,15 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants ViewDialog viewDialog = new ViewDialog(instance); return viewDialog; case FAVOURITE_DIALOG_ID: - favDialog = new POIDialog(instance); + favDialog = new FavouritesDialog(instance); favDialog.setOnItemClickListener(this); favDialog.setOnItemLongClickListener(this); favDialog.setTitle(R.string.favourites_dialog_title); return favDialog; case SEARCH_RESULTS_DIALOG_ID: - searchResultsDialog = new POIDialog(instance); + searchResultsDialog = new SearchResultsDialog(instance); searchResultsDialog.setOnItemClickListener(this); searchResultsDialog.setOnItemLongClickListener(this); - searchResultsDialog.setTitle(R.string.search_results_dialog_title); - searchResultsDialog.setItems(searchResults); return searchResultsDialog; case WELCOME_DIALOG_ID: WelcomeDialog welcomeDialog = new WelcomeDialog(instance); @@ -1085,54 +1062,75 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants return null; } - public void onItemClick(AdapterView parent, View view, int position, long id) { - Log.i(TAG, "OnItemClick pos " + position + " id " + id); + protected void onPrepareDialog(int id, Dialog dialog) { + if (dialog instanceof FavouritesDialog) { + favDialog = (FavouritesDialog) dialog; - String poiId = null; - if (favDialog != null) { - poiId = favDialog.adapter.getItemStringId(position); - } else if (searchResultsDialog != null) { - poiId = searchResultsDialog.adapter.getItemStringId(position); - } + ArrayList newFavouriteItems = new ArrayList(); - Log.i(TAG, "POI " + poiId + " selected"); - - POI poi = null; - - if (poiId != null) { - Log.i(TAG, "Got id " + poiId); try { - poi = getHelper().getBuildingDao().queryForId(poiId); - if (poi == null) { - poi = getHelper().getBusStopDao().queryForId(poiId); + Dao buildingDao = getHelper().getBuildingDao(); + Dao busStopDao = getHelper().getBusStopDao(); + + final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, 0); + for (String idStr : favouritesPrefs.getAll().keySet()) { + Building building; + BusStop busStop; + if ((building = buildingDao.queryForId(idStr)) != null) { + newFavouriteItems.add(building); + } else if ((busStop = busStopDao.queryForId(idStr)) != null) { + newFavouriteItems.add(busStop); + } else { + Log.e(TAG, "Item in favourites " + idStr + " cannot be found"); + } } } catch (SQLException e) { e.printStackTrace(); } - if (poi == null) { - Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult"); + Log.i(TAG, "There are " + newFavouriteItems.size() + " favourites"); + if (newFavouriteItems.size() == 0) { + Log.i(TAG, "Favourite dialog has no favourites, displaying message"); + favDialog.setMessage(getResources().getString(R.string.favourites_dialog_message)); + favDialog.setItems(null); } else { - if (myLocationOverlay != null) { - myLocationOverlay.disableFollowLocation(); - } - mapController.setZoom(20); - mapController.setCenter(poi.point); + favDialog.setMessage(""); + favDialog.setItems(newFavouriteItems); + } + } else if (dialog instanceof SearchResultsDialog) { + searchResultsDialog = (SearchResultsDialog) dialog; + } + } - if (favDialog != null) { - favDialog.dismiss(); - favDialog = null; - } else if (searchResultsDialog != null) { - searchResultsDialog.dismiss(); - searchResultsDialog = null; - } + public void onItemClick(AdapterView parent, View view, int position, long id) { + Log.i(TAG, "OnItemClick pos " + position + " id " + id); + + POI poi = null; + if (favDialog != null) { + poi = favDialog.adapter.getPOIItem(position); + } else if (searchResultsDialog != null) { + poi = searchResultsDialog.adapter.getPOIItem(position); + } + + if (poi != null) { + Log.i(TAG, "Got id " + poi.id); + if (myLocationOverlay != null) { + myLocationOverlay.disableFollowLocation(); + } + mapController.setZoom(20); + mapController.setCenter(poi.point); + + if (favDialog != null) { + favDialog.dismiss(); + favDialog = null; + } else if (searchResultsDialog != null) { + searchResultsDialog.dismiss(); + searchResultsDialog = null; } + } else { Log.i(TAG, "Got null poi id"); - - // mapController.setZoom(15); - // mapController.setCenter(new GeoPoint(50935551, -1393488)); } } @@ -1144,77 +1142,76 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants Log.i(TAG, "OnItemClick pos " + position + " id " + id); - String poiId = null; + POI poi = null; if (favDialog != null) { - poiId = favDialog.adapter.getItemStringId(position); + poi = favDialog.adapter.getPOIItem(position); } else if (searchResultsDialog != null) { - poiId = searchResultsDialog.adapter.getItemStringId(position); + poi = searchResultsDialog.adapter.getPOIItem(position); } - Log.i(TAG, "POI " + poiId + " selected"); + if (poi != null) { + Log.i(TAG, "Got id " + poi.id); - POI poi = null; + if (poi.type == POI.BUS_STOP) { + BusStop busStop = (BusStop) poi; - if (poiId != null) { - Log.i(TAG, "Got id " + poiId); - try { - poi = getHelper().getBuildingDao().queryForId(poiId); - if (poi == null) { - poi = getHelper().getBusStopDao().queryForId(poiId); - } - } catch (SQLException e) { - e.printStackTrace(); - } + Log.i(TAG, "Pressed " + busStop.id); - if (poi == null) { - Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult"); - } else { - if (poi.type == POI.BUS_STOP) { - BusStop busStop = (BusStop) poi; + Uri uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id); - Log.i(TAG, "Pressed " + busStop.id); + Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); - Uri uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id); + Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri); + startActivity(busStopIntent); - Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); + return true; - Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri); - startActivity(busStopIntent); + } else if (poi.type == POI.BUILDING) { + Building building = (Building) poi; - return true; + Log.i(TAG, "Pressed " + building.id); - } else if (poi.type == POI.BUILDING) { - Building building = (Building) poi; + Uri uri = Uri.parse("http://id.southampton.ac.uk/building/" + building.id); - Log.i(TAG, "Pressed " + building.id); + Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); - Uri uri = Uri.parse("http://id.southampton.ac.uk/building/" + building.id); + Intent buildingIntent = new Intent(Intent.ACTION_VIEW, uri); + startActivity(buildingIntent); - Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); + return true; - Intent buildingIntent = new Intent(Intent.ACTION_VIEW, uri); - startActivity(buildingIntent); + } else if (poi.type == POI.SITE) { + Site site = (Site) poi; - return true; + Log.i(TAG, "Pressed " + site.id); - } else { + Uri uri = Uri.parse("http://id.southampton.ac.uk/site/" + site.id); - myLocationOverlay.disableFollowLocation(); - mapController.setZoom(20); - mapController.setCenter(poi.point); + Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath()); - if (favDialog != null) { - favDialog.dismiss(); - favDialog = null; - } else if (searchResultsDialog != null) { - searchResultsDialog.dismiss(); - searchResultsDialog = null; - } + Intent siteIntent = new Intent(Intent.ACTION_VIEW, uri); + startActivity(siteIntent); + + return true; + + } else { + + myLocationOverlay.disableFollowLocation(); + mapController.setZoom(20); + mapController.setCenter(poi.point); + if (favDialog != null) { + favDialog.dismiss(); + favDialog = null; + } else if (searchResultsDialog != null) { + searchResultsDialog.dismiss(); + searchResultsDialog = null; } + } + } else { - Log.i(TAG, "Got null poi id"); + Log.i(TAG, "Got null poi"); // mapController.setZoom(15); // mapController.setCenter(new GeoPoint(50935551, -1393488)); diff --git a/src/net/cbaines/suma/MapContentProvider.java b/src/net/cbaines/suma/MapContentProvider.java index d920b21..efbdf2f 100644 --- a/src/net/cbaines/suma/MapContentProvider.java +++ b/src/net/cbaines/suma/MapContentProvider.java @@ -17,6 +17,8 @@ package net.cbaines.suma; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import android.app.SearchManager; @@ -48,10 +50,8 @@ public class MapContentProvider extends ContentProvider { // + "/building"); // MIME types used for searching words or looking up a single definition - public static final String ALLS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.all"; - public static final String ALL_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.all"; + public static final String ALLS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.net.cbaines.suma.provider.all"; + public static final String ALL_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.net.cbaines.suma.provider.all"; public static final String BUILDINGS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.net.cbaines.suma.provider.building"; public static final String BUILDING_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE @@ -60,14 +60,10 @@ public class MapContentProvider extends ContentProvider { + "/vnd.net.cbaines.suma.provider.bus-stop"; public static final String BUS_STOP_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.net.cbaines.suma.provider.bus-stop"; - public static final String SITES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.site"; - public static final String SITE_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.site"; - public static final String BUSES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.bus"; - public static final String BUS_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.bus"; + public static final String SITES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.net.cbaines.suma.provider.site"; + public static final String SITE_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.net.cbaines.suma.provider.site"; + public static final String BUSES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.net.cbaines.suma.provider.bus"; + public static final String BUS_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.net.cbaines.suma.provider.bus"; private DatabaseHelper helper; @@ -87,8 +83,7 @@ public class MapContentProvider extends ContentProvider { private static final UriMatcher sURIMatcher = buildUriMatcher(); /** - * Builds up a UriMatcher for search suggestion and shortcut refresh - * queries. + * Builds up a UriMatcher for search suggestion and shortcut refresh queries. */ private static UriMatcher buildUriMatcher() { UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); @@ -109,12 +104,10 @@ public class MapContentProvider extends ContentProvider { matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST); /* - * The following are unused in this implementation, but if we include - * {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column in our - * suggestions table, we could expect to receive refresh queries when a - * shortcutted suggestion is displayed in Quick Search Box, in which - * case, the following Uris would be provided and we would return a - * cursor with a single item representing the refreshed suggestion data. + * The following are unused in this implementation, but if we include {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as + * a column in our suggestions table, we could expect to receive refresh queries when a shortcutted suggestion is + * displayed in Quick Search Box, in which case, the following Uris would be provided and we would return a cursor with a + * single item representing the refreshed suggestion data. */ matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT, REFRESH_SHORTCUT); matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", REFRESH_SHORTCUT); @@ -128,11 +121,9 @@ public class MapContentProvider extends ContentProvider { } /** - * Handles all the dictionary searches and suggestion queries from the - * Search Manager. When requesting a specific word, the uri alone is - * required. When searching all of the dictionary for matches, the - * selectionArgs argument must carry the search query as the first element. - * All other arguments are ignored. + * Handles all the dictionary searches and suggestion queries from the Search Manager. When requesting a specific word, the + * uri alone is required. When searching all of the dictionary for matches, the selectionArgs argument must carry the search + * query as the first element. All other arguments are ignored. */ @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { @@ -193,9 +184,10 @@ public class MapContentProvider extends ContentProvider { private Cursor getSuggestions(String query) throws SQLException { Log.v(TAG, "Got query for " + query); - String[] columnNames = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_ICON_1, - SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_TEXT_2, - SearchManager.SUGGEST_COLUMN_INTENT_DATA }; + String[] columnNames = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_ICON_1, SearchManager.SUGGEST_COLUMN_TEXT_1, + SearchManager.SUGGEST_COLUMN_TEXT_2, SearchManager.SUGGEST_COLUMN_INTENT_DATA }; + + List results = new ArrayList(); MatrixCursor cursor = new MatrixCursor(columnNames); int id = 0; @@ -203,24 +195,10 @@ public class MapContentProvider extends ContentProvider { Dao buildingDao = helper.getBuildingDao(); QueryBuilder qb = buildingDao.queryBuilder(); - qb.where().like(Building.ID_FIELD_NAME, "%" + query + "%").or() - .like(Building.NAME_FIELD_NAME, "%" + query + "%"); + qb.where().like(Building.ID_FIELD_NAME, "%" + query + "%").or().like(Building.NAME_FIELD_NAME, "%" + query + "%"); PreparedQuery preparedQuery = qb.prepare(); - List buildings = buildingDao.query(preparedQuery); - Log.v(TAG, "Returning " + buildings.size() + " buildings"); - - for (Building building : buildings) { - // Log.v(TAG, "Building " + id + ", " + building.name); - Object[] values = { - id++, - R.drawable.building, - building.name, - building.id, - "geo:" + Util.E6IntToDouble(building.point.getLatitudeE6()) + "," - + Util.E6IntToDouble(building.point.getLongitudeE6()) + "?z=18" }; - cursor.addRow(values); - } + results.addAll(buildingDao.query(preparedQuery)); Dao busStopDao = helper.getBusStopDao(); @@ -229,19 +207,52 @@ public class MapContentProvider extends ContentProvider { .like(BusStop.DESCRIPTION_FIELD_NAME, "%" + query + "%"); PreparedQuery busStopPreparedQuery = busStopQB.prepare(); - List busStops = busStopDao.query(busStopPreparedQuery); - Log.v(TAG, "Returning " + busStops.size() + " busStops"); - - for (BusStop busStop : busStops) { - // Log.v(TAG, "Building " + id + ", " + building.name); - Object[] values = { - id++, - R.drawable.busstop, - busStop.description, - busStop.id, - "geo:" + Util.E6IntToDouble(busStop.point.getLatitudeE6()) + "," - + Util.E6IntToDouble(busStop.point.getLongitudeE6()) + "?z=18" }; - cursor.addRow(values); + results.addAll(busStopDao.query(busStopPreparedQuery)); + + Dao siteDao = helper.getSiteDao(); + + QueryBuilder siteQB = siteDao.queryBuilder(); + siteQB.where().like(Site.NAME_FIELD_NAME, "%" + query + "%").or().like(Site.ID_FIELD_NAME, "%" + query + "%"); + PreparedQuery sitePreparedQuery = siteQB.prepare(); + + results.addAll(siteDao.query(sitePreparedQuery)); + + Collections.sort(results, new StringPOIDistanceComparator(query)); + + for (POI poi : results) { + if (poi instanceof Site) { + Site site = (Site) poi; + Object[] values = { + id++, + R.drawable.empty, + site.name, + site.id, + "geo:" + Util.E6IntToDouble(site.point.getLatitudeE6()) + "," + + Util.E6IntToDouble(site.point.getLongitudeE6()) + "?z=18" }; + cursor.addRow(values); + } else if (poi instanceof Building) { + Building building = (Building) poi; + Object[] values = { + id++, + R.drawable.building, + building.name, + building.id, + "geo:" + Util.E6IntToDouble(building.point.getLatitudeE6()) + "," + + Util.E6IntToDouble(building.point.getLongitudeE6()) + "?z=18" }; + cursor.addRow(values); + } else if (poi instanceof BusStop) { + BusStop busStop = (BusStop) poi; + Object[] values = { + id++, + R.drawable.busstop, + busStop.description, + busStop.id, + "geo:" + Util.E6IntToDouble(busStop.point.getLatitudeE6()) + "," + + Util.E6IntToDouble(busStop.point.getLongitudeE6()) + "?z=18" }; + cursor.addRow(values); + } else { + Log.e(TAG, "Error, unexpected class"); + } } return cursor; @@ -307,13 +318,11 @@ public class MapContentProvider extends ContentProvider { private Cursor refreshShortcut(Uri uri) throws SQLException { /* - * This won't be called with the current implementation, but if we - * include {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column - * in our suggestions table, we could expect to receive refresh queries - * when a shortcutted suggestion is displayed in Quick Search Box. In - * which case, this method will query the table for the specific word, - * using the given item Uri and provide all the columns originally - * provided with the suggestion query. + * This won't be called with the current implementation, but if we include {@link + * SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column in our suggestions table, we could expect to receive refresh + * queries when a shortcutted suggestion is displayed in Quick Search Box. In which case, this method will query the table + * for the specific word, using the given item Uri and provide all the columns originally provided with the suggestion + * query. */ String buildingID = uri.getLastPathSegment(); Dao buildingDao = helper.getBuildingDao(); @@ -330,8 +339,8 @@ public class MapContentProvider extends ContentProvider { } /** - * This method is required in order to query the supported types. It's also - * useful in our own query() method to determine the type of Uri received. + * This method is required in order to query the supported types. It's also useful in our own query() method to determine the + * type of Uri received. */ @Override public String getType(Uri uri) { diff --git a/src/net/cbaines/suma/Preferences.java b/src/net/cbaines/suma/Preferences.java index 9f2eaca..791aad8 100644 --- a/src/net/cbaines/suma/Preferences.java +++ b/src/net/cbaines/suma/Preferences.java @@ -16,18 +16,18 @@ public interface Preferences { static final String APP_VERSION = "appVersion"; static final String APP_NOT_INSTALLED = ""; - static final String CURRENT_APP_VERSION = "0.8"; + static final String CURRENT_APP_VERSION = "0.9"; static final String FAVOURITES_PREFERENCES = "favourites"; static final String DATABASE_PATH = "/data/data/net.cbaines.suma/databases/"; static final String DATABASE_NAME = "data.db"; - static final int DATABASE_VERSION = 41; + static final int DATABASE_VERSION = 42; /** * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets * folder */ - static final boolean USE_BUNDLED_DATABASE = false; + static final boolean USE_BUNDLED_DATABASE = true; } diff --git a/src/net/cbaines/suma/SearchResultsDialog.java b/src/net/cbaines/suma/SearchResultsDialog.java new file mode 100644 index 0000000..43fa206 --- /dev/null +++ b/src/net/cbaines/suma/SearchResultsDialog.java @@ -0,0 +1,10 @@ +package net.cbaines.suma; + +import android.content.Context; + +public class SearchResultsDialog extends POIDialog implements Preferences { + + public SearchResultsDialog(Context context) { + super(context); + } +} diff --git a/src/net/cbaines/suma/StringDistanceComparator.java b/src/net/cbaines/suma/StringDistanceComparator.java deleted file mode 100644 index d42451f..0000000 --- a/src/net/cbaines/suma/StringDistanceComparator.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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.util.Comparator; - -public class StringDistanceComparator implements Comparator { - private String userString; - - // private static final String TAG = "StringDistanceComparator"; - - public StringDistanceComparator(String userString) { - super(); - this.userString = userString; - } - - public int compare(POI poi1, POI poi2) { - int distTo1 = LD(userString, poi1.toString()); - // Log.i(TAG, "Comparing " + userString + " and " + poi1.toString() + - // " got dist " + distTo1); - int distTo2 = LD(userString, poi2.toString()); - // Log.i(TAG, "Comparing " + userString + " and " + poi2.toString() + - // " got dist " + distTo2); - return distTo1 - distTo2; - } - - // Below is public domain code from http://www.merriampark.com/ld.htm - - // **************************** - // Get minimum of three values - // **************************** - - private int Minimum(int a, int b, int c) { - int mi; - - mi = a; - if (b < mi) { - mi = b; - } - if (c < mi) { - mi = c; - } - return mi; - - } - - // ***************************** - // Compute Levenshtein distance - // ***************************** - - public int LD(String s, String t) { - int d[][]; // matrix - int n; // length of s - int m; // length of t - int i; // iterates through s - int j; // iterates through t - char s_i; // ith character of s - char t_j; // jth character of t - int cost; // cost - - // Step 1 - - n = s.length(); - m = t.length(); - if (n == 0) { - return m; - } - if (m == 0) { - return n; - } - d = new int[n + 1][m + 1]; - - // Step 2 - - for (i = 0; i <= n; i++) { - d[i][0] = i; - } - - for (j = 0; j <= m; j++) { - d[0][j] = j; - } - - // Step 3 - - for (i = 1; i <= n; i++) { - - s_i = s.charAt(i - 1); - - // Step 4 - - for (j = 1; j <= m; j++) { - - t_j = t.charAt(j - 1); - - // Step 5 - - if (s_i == t_j) { - cost = 0; - } else { - cost = 1; - } - - // Step 6 - - d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost); - - } - - } - - // Step 7 - - return d[n][m]; - - } - -} diff --git a/src/net/cbaines/suma/StringPOIDistanceComparator.java b/src/net/cbaines/suma/StringPOIDistanceComparator.java new file mode 100644 index 0000000..e8a7539 --- /dev/null +++ b/src/net/cbaines/suma/StringPOIDistanceComparator.java @@ -0,0 +1,134 @@ +/* + * 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.util.Comparator; + +public class StringPOIDistanceComparator implements Comparator { + private String userString; + + // private static final String TAG = "StringDistanceComparator"; + + public StringPOIDistanceComparator(String userString) { + super(); + this.userString = userString; + } + + public int compare(POI poi1, POI poi2) { + int distTo1 = Math.min(LD(userString, poi1.toString()), LD(userString, poi1.id)); + // Log.i(TAG, "Comparing " + userString + " and " + poi1.toString() + + // " got dist " + distTo1); + int distTo2 = Math.min(LD(userString, poi2.toString()), LD(userString, poi2.id)); + // Log.i(TAG, "Comparing " + userString + " and " + poi2.toString() + + // " got dist " + distTo2); + return distTo1 - distTo2; + } + + // Below is public domain code from http://www.merriampark.com/ld.htm + + // **************************** + // Get minimum of three values + // **************************** + + private int Minimum(int a, int b, int c) { + int mi; + + mi = a; + if (b < mi) { + mi = b; + } + if (c < mi) { + mi = c; + } + return mi; + + } + + // ***************************** + // Compute Levenshtein distance + // ***************************** + + public int LD(String s, String t) { + int d[][]; // matrix + int n; // length of s + int m; // length of t + int i; // iterates through s + int j; // iterates through t + char s_i; // ith character of s + char t_j; // jth character of t + int cost; // cost + + // Step 1 + + n = s.length(); + m = t.length(); + if (n == 0) { + return m; + } + if (m == 0) { + return n; + } + d = new int[n + 1][m + 1]; + + // Step 2 + + for (i = 0; i <= n; i++) { + d[i][0] = i; + } + + for (j = 0; j <= m; j++) { + d[0][j] = j; + } + + // Step 3 + + for (i = 1; i <= n; i++) { + + s_i = s.charAt(i - 1); + + // Step 4 + + for (j = 1; j <= m; j++) { + + t_j = t.charAt(j - 1); + + // Step 5 + + if (s_i == t_j) { + cost = 0; + } else { + cost = 1; + } + + // Step 6 + + d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost); + + } + + } + + // Step 7 + + return d[n][m]; + + } + +} diff --git a/src/net/cbaines/suma/WelcomeDialog.java b/src/net/cbaines/suma/WelcomeDialog.java index 7262f48..7afec03 100644 --- a/src/net/cbaines/suma/WelcomeDialog.java +++ b/src/net/cbaines/suma/WelcomeDialog.java @@ -46,7 +46,7 @@ public class WelcomeDialog extends Dialog implements OnClickListener, Preference sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); String appVersion = sharedPrefs.getString(APP_VERSION, APP_NOT_INSTALLED); - if (!appVersion.equals(CURRENT_APP_VERSION)) { + if (!appVersion.equals(APP_NOT_INSTALLED)) { TextView welcomeDialogMessage = (TextView) findViewById(R.id.welcomeDialogMessage); welcomeDialogMessage.setText(R.string.welcome_dialog_upgrade_message); } -- cgit v1.2.3