From 75b6e40b45f3748e057989d96c15144eb906dfab Mon Sep 17 00:00:00 2001 From: Marek Sebera Date: Tue, 17 Nov 2015 19:01:03 +0100 Subject: [PATCH] Fixed StringIndexOutOfBoundsException, Closes #632 --- .../AIMSICD/activities/MapViewerOsmDroid.java | 196 +++++++++--------- 1 file changed, 95 insertions(+), 101 deletions(-) diff --git a/app/src/main/java/com/SecUpwN/AIMSICD/activities/MapViewerOsmDroid.java b/app/src/main/java/com/SecUpwN/AIMSICD/activities/MapViewerOsmDroid.java index 16b787a49..9447634e3 100644 --- a/app/src/main/java/com/SecUpwN/AIMSICD/activities/MapViewerOsmDroid.java +++ b/app/src/main/java/com/SecUpwN/AIMSICD/activities/MapViewerOsmDroid.java @@ -45,7 +45,6 @@ import com.SecUpwN.AIMSICD.utils.RequestTask; import com.SecUpwN.AIMSICD.utils.TinyDB; -import org.osmdroid.api.IProjection; import org.osmdroid.tileprovider.tilesource.TileSourceFactory; import org.osmdroid.util.GeoPoint; import org.osmdroid.views.MapView; @@ -59,39 +58,38 @@ import java.util.List; /** - * Description: TODO: add details - * - * Variables: TODO: add a list of variables that can be tuned (Max/MinZoom factors etc) - * - * Current Issues: - * - * [x] Map is not immediately updated with the BTS info. It take a "long" time ( >10 seconds) - * before map is updated. Any way to shorten this? - * [ ] See: #272 #250 #228 - * [ ] Some pins remain clustered even on the greatest zoom, this is probably - * due to over sized icons, or too low zoom level. - * [x] pin icons are too big. We need to reduce pin dot diameter by ~50% - * [ ] Need a manual way to add GPS coordinates of current location (see code comments below) - * [ ] - * - * Notes: - * a) Latest OSM version can use MaxZoomLevel of 21, please see: - * https://github.com/osmdroid/osmdroid/issues/49 - * https://github.com/osmdroid/osmdroid/issues/81 - * https://code.google.com/p/osmbonuspack/issues/detail?id=102 - * - * ChangeLog: - * - * 2015-01-22 E:V:A Changed: setLocationUpdateMinTime: 60000 to 10000 ms - * setLocationUpdateMinDistance: 1000 to 100 meters - * 2015-02-12 E:V:A Added: mMap.setMaxZoomLevel(19); - * + * Description: TODO: add details + *

+ * Variables: TODO: add a list of variables that can be tuned (Max/MinZoom factors etc) + *

+ * Current Issues: + *

+ * [x] Map is not immediately updated with the BTS info. It take a "long" time ( >10 seconds) + * before map is updated. Any way to shorten this? + * [ ] See: #272 #250 #228 + * [ ] Some pins remain clustered even on the greatest zoom, this is probably + * due to over sized icons, or too low zoom level. + * [x] pin icons are too big. We need to reduce pin dot diameter by ~50% + * [ ] Need a manual way to add GPS coordinates of current location (see code comments below) + * [ ] + *

+ * Notes: + * a) Latest OSM version can use MaxZoomLevel of 21, please see: + * https://github.com/osmdroid/osmdroid/issues/49 + * https://github.com/osmdroid/osmdroid/issues/81 + * https://code.google.com/p/osmbonuspack/issues/detail?id=102 + *

+ * ChangeLog: + *

+ * 2015-01-22 E:V:A Changed: setLocationUpdateMinTime: 60000 to 10000 ms + * setLocationUpdateMinDistance: 1000 to 100 meters + * 2015-02-12 E:V:A Added: mMap.setMaxZoomLevel(19); */ -public class MapViewerOsmDroid extends BaseActivity implements OnSharedPreferenceChangeListener { +public final class MapViewerOsmDroid extends BaseActivity implements OnSharedPreferenceChangeListener { private final String TAG = "AIMSICD_MapViewer"; - public static final String updateOpenCellIDMarkers = "update_opencell_markers"; + public static final String updateOpenCellIDMarkers = "update_open_cell_markers"; private MapView mMap; private AIMSICDDbAdapter mDbHelper; @@ -104,7 +102,6 @@ public class MapViewerOsmDroid extends BaseActivity implements OnSharedPreferenc private MyLocationNewOverlay mMyLocationOverlay; private CompassOverlay mCompassOverlay; - private ScaleBarOverlay mScaleBarOverlay; private CellTowerGridMarkerClusterer mCellTowerGridMarkerClusterer; private Menu mOptionsMenu; TelephonyManager tm; @@ -134,7 +131,7 @@ public void onCreate(Bundle savedInstanceState) { setUpMapIfNeeded(); mDbHelper = new AIMSICDDbAdapter(mContext); - tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); + tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // Bind to LocalService Intent intent = new Intent(mContext, AimsicdService.class); @@ -209,7 +206,7 @@ protected void onPause() { @Override public void onReceive(Context context, Intent intent) { loadEntries(); - if(BuildConfig.DEBUG && mCellTowerGridMarkerClusterer != null && mCellTowerGridMarkerClusterer.getItems() != null) { + if (BuildConfig.DEBUG && mCellTowerGridMarkerClusterer != null && mCellTowerGridMarkerClusterer.getItems() != null) { Log.v(TAG, "mMessageReceiver CellTowerMarkers.invalidate() markers.size():" + mCellTowerGridMarkerClusterer.getItems().size()); } @@ -218,7 +215,7 @@ public void onReceive(Context context, Intent intent) { /** * Service Connection to bind the activity to the service - * + *

* This seem to setup the connection and animates the map window movement to the * last known location. */ @@ -276,12 +273,11 @@ private void setupMapType(int mapType) { /** * Description: Initialises the Map and sets initial options such as: - * Zoom levels and controls - * Compass - * ScaleBar - * Cluster Pin colors - * Location update settings - * + * Zoom levels and controls + * Compass + * ScaleBar + * Cluster Pin colors + * Location update settings */ private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. @@ -297,19 +293,19 @@ private void setUpMapIfNeeded() { mMap.getTileProvider().createTileCache(); mCompassOverlay = new CompassOverlay(this, new InternalCompassOrientationProvider(this), mMap); - mScaleBarOverlay = new ScaleBarOverlay(this); + ScaleBarOverlay mScaleBarOverlay = new ScaleBarOverlay(this); mScaleBarOverlay.setScaleBarOffset(getResources().getDisplayMetrics().widthPixels / 2, 10); mScaleBarOverlay.setCentred(true); // Sets cluster pin color mCellTowerGridMarkerClusterer = new CellTowerGridMarkerClusterer(MapViewerOsmDroid.this); - mCellTowerGridMarkerClusterer.setIcon(((BitmapDrawable)mContext.getResources(). - getDrawable(R.drawable.ic_map_pin_orange)).getBitmap()); + BitmapDrawable mapPinDrawable = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ic_map_pin_orange); + mCellTowerGridMarkerClusterer.setIcon(mapPinDrawable == null ? null : mapPinDrawable.getBitmap()); - GpsMyLocationProvider imlp = new GpsMyLocationProvider(MapViewerOsmDroid.this.getBaseContext()); - imlp.setLocationUpdateMinDistance(100); // [m] // Set the minimum distance for location updates - imlp.setLocationUpdateMinTime(10000); // [ms] // Set the minimum time interval for location updates - mMyLocationOverlay = new MyLocationNewOverlay(MapViewerOsmDroid.this.getBaseContext(), imlp, mMap); + GpsMyLocationProvider gpsMyLocationProvider = new GpsMyLocationProvider(MapViewerOsmDroid.this.getBaseContext()); + gpsMyLocationProvider.setLocationUpdateMinDistance(100); // [m] // Set the minimum distance for location updates + gpsMyLocationProvider.setLocationUpdateMinTime(10000); // [ms] // Set the minimum time interval for location updates + mMyLocationOverlay = new MyLocationNewOverlay(MapViewerOsmDroid.this.getBaseContext(), gpsMyLocationProvider, mMap); mMyLocationOverlay.setDrawAccuracyEnabled(true); mMap.getOverlays().add(mCellTowerGridMarkerClusterer); @@ -349,7 +345,7 @@ public boolean onOptionsItemSelected(MenuItem item) { GeoLocation lastKnown = mAimsicdService.lastKnownLocation(); if (lastKnown != null) { Helpers.msgLong(mContext, - getString(R.string.contacting_opencellid_for_data)); + getString(R.string.contacting_opencellid_for_data)); Cell cell; cell = mAimsicdService.getCell(); cell.setLon(lastKnown.getLongitudeInDegrees()); @@ -372,7 +368,7 @@ public boolean onOptionsItemSelected(MenuItem item) { Helpers.getOpenCellData(mContext, cell, RequestTask.DBE_DOWNLOAD_REQUEST_FROM_MAP); } else { Helpers.msgLong(mContext, - getString(R.string.unable_to_determine_last_location)); + getString(R.string.unable_to_determine_last_location)); } return true; } @@ -382,17 +378,15 @@ public boolean onOptionsItemSelected(MenuItem item) { } /** - * Description: Loads Signal Strength Database details to plot on the map, - * only entries which have a location (lon, lat) are used. - * - * + * Description: Loads Signal Strength Database details to plot on the map, + * only entries which have a location (lon, lat) are used. */ private void loadEntries() { - new AsyncTask() { + new AsyncTask() { @Override protected GeoPoint doInBackground(Void... voids) { - int signal; + //int signal; mCellTowerGridMarkerClusterer.getItems().clear(); @@ -405,7 +399,7 @@ protected GeoPoint doInBackground(Void... voids) { try { // Grab cell data from CELL_TABLE (cellinfo) --> DBi_bts c = mDbHelper.getCellData(); - } catch(IllegalStateException ix) { + } catch (IllegalStateException ix) { Log.e(TAG, "Problem getting data from CELL_TABLE", ix); } @@ -420,15 +414,15 @@ protected GeoPoint doInBackground(Void... voids) { final int lac = c.getInt(c.getColumnIndex(DBTableColumnIds.DBI_BTS_LAC)); // LAC final int mcc = c.getInt(c.getColumnIndex(DBTableColumnIds.DBI_BTS_MCC)); // MCC final int mnc = c.getInt(c.getColumnIndex(DBTableColumnIds.DBI_BTS_MNC)); // MNC - final double dlat = c.getDouble(c.getColumnIndex(DBTableColumnIds.DBI_BTS_LAT)); // Lat - final double dlng = c.getDouble(c.getColumnIndex(DBTableColumnIds.DBI_BTS_LON)); // Lon + final double dLat = c.getDouble(c.getColumnIndex(DBTableColumnIds.DBI_BTS_LAT)); // Lat + final double dLng = c.getDouble(c.getColumnIndex(DBTableColumnIds.DBI_BTS_LON)); // Lon - if (Double.doubleToRawLongBits(dlat) == 0 - && Double.doubleToRawLongBits(dlng) == 0) { + if (Double.doubleToRawLongBits(dLat) == 0 + && Double.doubleToRawLongBits(dLng) == 0) { continue; } // TODO this (signal) is not in DBi_bts - signal = 1; + // signal = 1; //c.getInt(c.getColumnIndex(DBTableColumnIds.DBE_IMPORT_AVG_SIGNAL)); // signal // In case of missing or negative signal, set a default fake signal, // so that we can still draw signal circles. ? @@ -436,21 +430,21 @@ protected GeoPoint doInBackground(Void... voids) { // signal = 20; //} - if (Double.doubleToRawLongBits(dlat) != 0 - || Double.doubleToRawLongBits(dlng) != 0) { - loc = new GeoPoint(dlat, dlng); + if (Double.doubleToRawLongBits(dLat) != 0 + || Double.doubleToRawLongBits(dLng) != 0) { + loc = new GeoPoint(dLat, dLng); CellTowerMarker ovm = new CellTowerMarker(mContext, mMap, - "Cell ID: " + cellID, - "", loc, - new MarkerData( - String.valueOf(cellID), - String.valueOf(loc.getLatitude()), - String.valueOf(loc.getLongitude()), - String.valueOf(lac), - String.valueOf(mcc), - String.valueOf(mnc), - "", false) + "Cell ID: " + cellID, + "", loc, + new MarkerData( + String.valueOf(cellID), + String.valueOf(loc.getLatitude()), + String.valueOf(loc.getLongitude()), + String.valueOf(lac), + String.valueOf(mcc), + String.valueOf(mnc), + "", false) ); // The pin of our current position ovm.setIcon(getResources().getDrawable(R.drawable.ic_map_pin_blue)); @@ -468,7 +462,7 @@ public void run() { }); } - GeoPoint ret = new GeoPoint(0,0); + GeoPoint ret = new GeoPoint(0, 0); if (mBound) { try { int mcc = mAimsicdService.getCell().getMCC(); @@ -478,7 +472,7 @@ public void run() { Log.e("map", "Error getting default location!", e); } } - if(c != null) { + if (c != null) { c.close(); } // plot neighbouring cells @@ -495,7 +489,7 @@ public void run() { if (isCancelled()) return null; try { loc = new GeoPoint(cell.getLat(), cell.getLon()); - CellTowerMarker ovm = new CellTowerMarker(mContext,mMap, + CellTowerMarker ovm = new CellTowerMarker(mContext, mMap, getString(R.string.cell_id_label) + cell.getCID(), "", loc, new MarkerData( @@ -505,7 +499,7 @@ public void run() { String.valueOf(cell.getLAC()), String.valueOf(cell.getMCC()), String.valueOf(cell.getMNC()), - "", false)); + "", false)); // The pin of other BTS ovm.setIcon(getResources().getDrawable(R.drawable.ic_map_pin_orange)); @@ -526,12 +520,12 @@ public void run() { * b) WiFi location is not used * c) Default MCC is too far off * - * @param defaultLoc + * @param defaultLoc Default location to open map on */ @Override protected void onPostExecute(GeoPoint defaultLoc) { if (loc != null && (Double.doubleToRawLongBits(loc.getLatitude()) != 0 - && Double.doubleToRawLongBits(loc.getLongitude()) != 0)) { + && Double.doubleToRawLongBits(loc.getLongitude()) != 0)) { mMap.getController().setZoom(16); mMap.getController().animateTo(new GeoPoint(loc.getLatitude(), loc.getLongitude())); } else { @@ -553,8 +547,8 @@ protected void onPostExecute(GeoPoint defaultLoc) { } } } - if(mCellTowerGridMarkerClusterer != null) { - if(BuildConfig.DEBUG && mCellTowerGridMarkerClusterer.getItems() != null) { + if (mCellTowerGridMarkerClusterer != null) { + if (BuildConfig.DEBUG && mCellTowerGridMarkerClusterer.getItems() != null) { Log.v(TAG, "CellTowerMarkers.invalidate() markers.size():" + mCellTowerGridMarkerClusterer.getItems().size()); } //Drawing markers of cell tower immediately as possible @@ -568,17 +562,16 @@ private void loadOcidMarkersByNetwork() { // Check if OpenCellID data exists and if so load this now LinkedList items = new LinkedList<>(); String networkOperator = tm.getNetworkOperator(); - int imcc =0; - int imnc =0; - if (networkOperator != null) { - imcc = Integer.parseInt(networkOperator.substring(0, 3)); - imnc = Integer.parseInt(networkOperator.substring(3)); + int currentMmc = 0; + int currentMnc = 0; + if (networkOperator != null && networkOperator.length() > 3) { + currentMmc = Integer.parseInt(networkOperator.substring(0, 3)); + currentMnc = Integer.parseInt(networkOperator.substring(3)); } // DBe_import tower pins. Drawable cellTowerMarkerIcon = getResources().getDrawable(R.drawable.ic_map_pin_green); - IProjection p = mMap.getProjection(); - Cursor c = mDbHelper.returnOcidBtsByNetwork(imcc,imnc); + Cursor c = mDbHelper.returnOcidBtsByNetwork(currentMmc, currentMnc); if (c.moveToFirst()) { do { // CellID,Lac,Mcc,Mnc,Lat,Lng,AvgSigStr,Samples @@ -586,9 +579,9 @@ private void loadOcidMarkersByNetwork() { final int lac = c.getInt(c.getColumnIndex(DBTableColumnIds.DBE_IMPORT_LAC)); final int mcc = c.getInt(c.getColumnIndex(DBTableColumnIds.DBE_IMPORT_MCC)); final int mnc = c.getInt(c.getColumnIndex(DBTableColumnIds.DBE_IMPORT_MNC)); - final double dlat = Double.parseDouble(c.getString(c.getColumnIndex(DBTableColumnIds.DBE_IMPORT_GPS_LAT))); - final double dlng = Double.parseDouble(c.getString(c.getColumnIndex(DBTableColumnIds.DBE_IMPORT_GPS_LON))); - final GeoPoint location = new GeoPoint(dlat, dlng); + final double dLat = Double.parseDouble(c.getString(c.getColumnIndex(DBTableColumnIds.DBE_IMPORT_GPS_LAT))); + final double dLng = Double.parseDouble(c.getString(c.getColumnIndex(DBTableColumnIds.DBE_IMPORT_GPS_LON))); + final GeoPoint location = new GeoPoint(dLat, dLng); //where is c.getString(6)AvgSigStr final int samples = c.getInt(c.getColumnIndex(DBTableColumnIds.DBE_IMPORT_SAMPLES)); // Add map marker for CellID @@ -596,14 +589,14 @@ private void loadOcidMarkersByNetwork() { "Cell ID: " + cellID, "", location, new MarkerData( - String.valueOf(cellID), - String.valueOf(location.getLatitude()), - String.valueOf(location.getLongitude()), - String.valueOf(lac), - String.valueOf(mcc), - String.valueOf(mnc), - String.valueOf(samples), - false)); + String.valueOf(cellID), + String.valueOf(location.getLatitude()), + String.valueOf(location.getLongitude()), + String.valueOf(lac), + String.valueOf(mcc), + String.valueOf(mnc), + String.valueOf(samples), + false)); ovm.setIcon(cellTowerMarkerIcon); items.add(ovm); @@ -613,6 +606,7 @@ private void loadOcidMarkersByNetwork() { mCellTowerGridMarkerClusterer.addAll(items); } + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { final String KEY_MAP_TYPE = getBaseContext().getString(R.string.pref_map_type_key); if (key.equals(KEY_MAP_TYPE)) { @@ -644,7 +638,7 @@ public void onStop() { public void onStart() { super.onStart(); ((AppAIMSICD) getApplication()).attach(this); - if(TinyDB.getInstance().getBoolean(TinyDbKeys.FINISHED_LOAD_IN_MAP)) { + if (TinyDB.getInstance().getBoolean(TinyDbKeys.FINISHED_LOAD_IN_MAP)) { setRefreshActionButtonState(false); } }