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 260ccf84d5f8787bba1c81c7d27bd9e3d69f6350..fdcacd9879d80413d620c58ff72cc6f30c01b1cc 100644 --- a/app/src/main/java/com/SecUpwN/AIMSICD/activities/MapViewerOsmDroid.java +++ b/app/src/main/java/com/SecUpwN/AIMSICD/activities/MapViewerOsmDroid.java @@ -180,20 +180,20 @@ public class MapViewerOsmDroid extends FragmentActivity implements OnSharedPrefe private void setupMapType(int mapType) { mMap.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE); -// switch (mapType) { -// case 0: -// mMap.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE); //setMapType(GoogleMap.MAP_TYPE_NORMAL); -// break; -// case 1: -// mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); -// break; -// case 2: -// mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); -// break; -// case 3: -// mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); -// break; -// } + switch (mapType) { + case 0: + mMap.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE); //setMapType(GoogleMap.MAP_TYPE_NORMAL); + break; + case 1: + mMap.setTileSource(TileSourceFactory.MAPNIK); //.setMapType(GoogleMap.MAP_TYPE_HYBRID); + break; + case 2: + mMap.setTileSource(TileSourceFactory.MAPQUESTAERIAL); //.setMapType(GoogleMap.MAP_TYPE_SATELLITE); + break; + case 3: + mMap.setTileSource(TileSourceFactory.CYCLEMAP); //.setMapType(GoogleMap.MAP_TYPE_TERRAIN); + break; + } } /** @@ -359,7 +359,7 @@ public class MapViewerOsmDroid extends FragmentActivity implements OnSharedPrefe CellTowerOverlayItem ovm = new CellTowerOverlayItem("CellID - " + cellID, "", - new GeoPoint(loc.getLatitude(), loc.getLongitude()), + loc, new MarkerData("" + cellID, "" + loc.getLatitude(),"" + loc.getLongitude(), "" + lac, "" + mcc, "" + mnc, "", false)); @@ -473,7 +473,7 @@ public class MapViewerOsmDroid extends FragmentActivity implements OnSharedPrefe CellTowerOverlayItem ovm = new CellTowerOverlayItem("CellID - " + cellID, "", - new GeoPoint(location.getLatitude(), location.getLongitude()), + location, new MarkerData("" + cellID, "" + location.getLatitude(),"" + location.getLongitude(), "" + lac, "", "", "", false)); diff --git a/app/src/main/java/com/SecUpwN/AIMSICD/service/AimsicdService.java b/app/src/main/java/com/SecUpwN/AIMSICD/service/AimsicdService.java index 3f0fa142033ed2770655c335d77b092bf4bc144b..916766d3e13480c96f50bf7eb981c13555938834 100644 --- a/app/src/main/java/com/SecUpwN/AIMSICD/service/AimsicdService.java +++ b/app/src/main/java/com/SecUpwN/AIMSICD/service/AimsicdService.java @@ -56,20 +56,6 @@ package com.SecUpwN.AIMSICD.service; -import com.SecUpwN.AIMSICD.AIMSICD; -import com.SecUpwN.AIMSICD.BuildConfig; -import com.SecUpwN.AIMSICD.R; -import com.SecUpwN.AIMSICD.adapters.AIMSICDDbAdapter; -import com.SecUpwN.AIMSICD.rilexecutor.DetectResult; -import com.SecUpwN.AIMSICD.rilexecutor.OemRilExecutor; -import com.SecUpwN.AIMSICD.rilexecutor.RawResult; -import com.SecUpwN.AIMSICD.rilexecutor.SamsungMulticlientRilExecutor; -import com.SecUpwN.AIMSICD.utils.Cell; -import com.SecUpwN.AIMSICD.utils.Device; -import com.SecUpwN.AIMSICD.utils.GeoLocation; -import com.SecUpwN.AIMSICD.utils.Helpers; -import com.SecUpwN.AIMSICD.utils.OemCommands; - import android.annotation.TargetApi; import android.app.AlertDialog; import android.app.Notification; @@ -84,6 +70,10 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; @@ -122,6 +112,19 @@ import android.telephony.gsm.GsmCellLocation; import android.util.Log; import android.view.WindowManager; +import com.SecUpwN.AIMSICD.AIMSICD; +import com.SecUpwN.AIMSICD.R; +import com.SecUpwN.AIMSICD.adapters.AIMSICDDbAdapter; +import com.SecUpwN.AIMSICD.rilexecutor.DetectResult; +import com.SecUpwN.AIMSICD.rilexecutor.OemRilExecutor; +import com.SecUpwN.AIMSICD.rilexecutor.RawResult; +import com.SecUpwN.AIMSICD.rilexecutor.SamsungMulticlientRilExecutor; +import com.SecUpwN.AIMSICD.utils.Cell; +import com.SecUpwN.AIMSICD.utils.Device; +import com.SecUpwN.AIMSICD.utils.GeoLocation; +import com.SecUpwN.AIMSICD.utils.Helpers; +import com.SecUpwN.AIMSICD.utils.OemCommands; + import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -154,9 +157,27 @@ public class AimsicdService extends Service implements OnSharedPreferenceChangeL private static LocationManager lm; private SharedPreferences prefs; private PhoneStateListener mPhoneStateListener; + + /** + * Location listener stuff + */ private LocationListener mLocationListener; - private static final long GPS_MIN_UPDATE_TIME = 1000; + private long lastLocationTime = 0; + private static final long GPS_MIN_UPDATE_TIME = 10000; private static final float GPS_MIN_UPDATE_DISTANCE = 10; + + /** + * Accelerometer stuff for detecting movement + */ + private float mLastX, mLastY, mLastZ; + private long lastMovementTime = 0; + private boolean mInitialized; + private SensorManager mSensorManager; + private Sensor mAccelerometer; + private SensorEventListener mSensorListener; + private final float ACCELEROMETER_NOISE = 2.0f; + private long MOVEMENT_THRESHOLD_MS = 60*1000; // time after last movement till GPS switched off + public boolean mMultiRilCompatible; public static long REFRESH_RATE; public static int LAST_DB_BACKUP_VERSION; @@ -213,6 +234,10 @@ public class AimsicdService extends Service implements OnSharedPreferenceChangeL lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); mContext = getApplicationContext(); + setupAccelerometer(); + mLocationListener = new MyLocationListener(); + lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); + PHONE_TYPE = tm.getPhoneType(); prefs = this.getSharedPreferences( @@ -260,6 +285,70 @@ public class AimsicdService extends Service implements OnSharedPreferenceChangeL Log.i(TAG, "Service launched successfully."); } + /** + * Set up the accelerometer so that when movement is detected, the GPS is enabled. + * GPS is normally disabled to save battery power. + */ + private void setupAccelerometer() { + // set up accelerometer sensor + mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); + mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); + + mSensorListener = new SensorEventListener() { + @Override + public void onSensorChanged(SensorEvent event) { + float x = event.values[0]; + float y = event.values[1]; + float z = event.values[2]; + + if (!mInitialized) { + mLastX = x; + mLastY = y; + mLastZ = z; + + mInitialized = true; + } else { + float deltaX = Math.abs(mLastX - x); + float deltaY = Math.abs(mLastY - y); + float deltaZ = Math.abs(mLastZ - z); + + if (deltaX < ACCELEROMETER_NOISE) deltaX = 0.0f; + if (deltaY < ACCELEROMETER_NOISE) deltaY = 0.0f; + if (deltaZ < ACCELEROMETER_NOISE) deltaZ = 0.0f; + + mLastX = x; + mLastY = y; + mLastZ = z; + + if (deltaX > 0 || deltaY > 0 || deltaZ > 0) { + // movement detected + Log.d("sensor", "Movement detected, enabling GPS"); + + // disable the movement sensor to save power + mSensorManager.unregisterListener(mSensorListener); + + lastMovementTime = System.currentTimeMillis(); + + // re-enable GPS, see timerRunner for when this gets switched off + lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_MIN_UPDATE_TIME, + GPS_MIN_UPDATE_DISTANCE, mLocationListener); + + // check again in a while to see if GPS should be disabled + // this runnable also re-enables this movement sensor + timerHandler.postDelayed(batterySavingRunnable, MOVEMENT_THRESHOLD_MS); + } + } + } + + @Override + public void onAccuracyChanged(Sensor sensor, int i) { + + } + }; + + mSensorManager.registerListener(mSensorListener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); + } + @Override public int onStartCommand(Intent intent, int flags, int startId) { tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); @@ -278,6 +367,8 @@ public class AimsicdService extends Service implements OnSharedPreferenceChangeL lm.removeUpdates(mLocationListener); } + mSensorManager.unregisterListener(mSensorListener); + //Samsung MultiRil Cleanup if (mRequestExecutor != null) { mRequestExecutor.stop(); @@ -876,6 +967,23 @@ public class AimsicdService extends Service implements OnSharedPreferenceChangeL setNotification(); } + // while tracking a cell, manage the power usage by switching off GPS if no movement + private final Runnable batterySavingRunnable = new Runnable() { + @Override + public void run() { + Log.d("power", "Checking to see if GPS should be disabled"); + // if no movement in a while, shut off GPS. Gets re-enabled when there is movement + if (lastLocationTime <= 0 || + System.currentTimeMillis() - lastMovementTime >= MOVEMENT_THRESHOLD_MS) { + Log.d("power", "Disabling GPS"); + lm.removeUpdates(mLocationListener); + } + + // check for movement again + mSensorManager.registerListener(mSensorListener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); + } + }; + private final Runnable timerRunnable = new Runnable() { @Override @@ -913,6 +1021,7 @@ public class AimsicdService extends Service implements OnSharedPreferenceChangeL dbHelper.close(); } } + if (REFRESH_RATE != 0) { timerHandler.postDelayed(this, REFRESH_RATE); } else { @@ -935,22 +1044,11 @@ public class AimsicdService extends Service implements OnSharedPreferenceChangeL PhoneStateListener.LISTEN_DATA_ACTIVITY | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE ); - if (lm != null) { - mLocationListener = new MyLocationListener(); - Log.i(TAG, "LocationManager already existed."); - lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_MIN_UPDATE_TIME, - GPS_MIN_UPDATE_DISTANCE, mLocationListener); - } else { - Log.i(TAG, "LocationManager did not exist."); - lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); - if (lm != null) { - if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { - Log.i(TAG, "LocationManager created."); - lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_MIN_UPDATE_TIME, - GPS_MIN_UPDATE_DISTANCE, mLocationListener); - } - } - } + + Log.i(TAG, "LocationManager already existed."); + lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_MIN_UPDATE_TIME, + GPS_MIN_UPDATE_DISTANCE, mLocationListener); + Helpers.msgShort(this, "Tracking Cell Information."); mTrackingCell = true; } else { @@ -1098,6 +1196,10 @@ public class AimsicdService extends Service implements OnSharedPreferenceChangeL @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @Override public void onLocationChanged(Location loc) { + Log.d("location", "Got location " + loc); + lastLocationTime = System.currentTimeMillis(); + timerHandler.postDelayed(batterySavingRunnable, MOVEMENT_THRESHOLD_MS); + if (Build.VERSION.SDK_INT > 16) { List<CellInfo> cellinfolist = tm.getAllCellInfo(); if (cellinfolist != null) {