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. --- AndroidManifest.xml | 14 ++ gen/net/cbaines/suma/R.java | Bin 14562 -> 14686 bytes res/values/strings.xml | 1 + res/xml/searchable.xml | 6 + src/net/cbaines/suma/FindActivity.java | 13 ++ src/net/cbaines/suma/MapActivity.java | 169 ++++++++++++---------- src/net/cbaines/suma/MapContentProvider.java | 201 +++++++++++++++++++++++++++ 7 files changed, 334 insertions(+), 70 deletions(-) create mode 100644 res/xml/searchable.xml create mode 100644 src/net/cbaines/suma/MapContentProvider.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index c9efb4a..a9bccd3 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -50,8 +50,22 @@ + + + + + + + + + + diff --git a/gen/net/cbaines/suma/R.java b/gen/net/cbaines/suma/R.java index 49cba3e..e1704a7 100644 Binary files a/gen/net/cbaines/suma/R.java and b/gen/net/cbaines/suma/R.java differ diff --git a/res/values/strings.xml b/res/values/strings.xml index e17ab11..6126937 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1,6 +1,7 @@ + Search Find Preferences View diff --git a/res/xml/searchable.xml b/res/xml/searchable.xml new file mode 100644 index 0000000..e4602a5 --- /dev/null +++ b/res/xml/searchable.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file 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. --- AndroidManifest.xml | 5 + res/xml/searchable.xml | 9 +- 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 ++++----- 7 files changed, 267 insertions(+), 132 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index a9bccd3..4dfff99 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -169,6 +169,11 @@ android:name=".PreferencesActivity" android:label="@string/preferences" > + + + \ No newline at end of file diff --git a/res/xml/searchable.xml b/res/xml/searchable.xml index e4602a5..7262b3d 100644 --- a/res/xml/searchable.xml +++ b/res/xml/searchable.xml @@ -1,6 +1,13 @@ + android:includeInGlobalSearch="true" + android:label="@string/app_name" + android:searchSettingsDescription="@string/map_activity_search_hint" + android:searchSuggestAuthority="net.cbaines.suma.provider" + android:searchSuggestIntentAction="android.intent.action.VIEW" + android:searchSuggestIntentData="content://net.cbaines.suma.provider/building" + android:searchSuggestSelection=" ?" + android:searchSuggestThreshold="1" > \ No newline at end of file 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. --- AndroidManifest.xml | 8 +- 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 +++++++++++++++++++++++++++++++ 5 files changed, 386 insertions(+), 387 deletions(-) delete mode 100644 src/net/cbaines/suma/FindActivity.java create mode 100644 src/net/cbaines/suma/SearchActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 4dfff99..423c58b 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -5,7 +5,7 @@ android:versionName="0.6.5 (alpha)" > @@ -53,12 +53,8 @@ - - - + 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. --- gen/net/cbaines/suma/R.java | Bin 15051 -> 15604 bytes res/layout/welcome_dialog.xml | 52 +++++++++++++++++++ res/values/strings.xml | 72 +++++++++++++++++---------- 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 ++++++++++++++++++ 7 files changed, 200 insertions(+), 70 deletions(-) create mode 100644 res/layout/welcome_dialog.xml create mode 100644 src/net/cbaines/suma/WelcomeDialog.java diff --git a/gen/net/cbaines/suma/R.java b/gen/net/cbaines/suma/R.java index 7ff7855..67c7f33 100644 Binary files a/gen/net/cbaines/suma/R.java and b/gen/net/cbaines/suma/R.java differ diff --git a/res/layout/welcome_dialog.xml b/res/layout/welcome_dialog.xml new file mode 100644 index 0000000..49b7c27 --- /dev/null +++ b/res/layout/welcome_dialog.xml @@ -0,0 +1,52 @@ + + + + + +