Fields Summary |
---|
private List | mListeners |
private static final String | LOG_TAG |
private Set | mTrackedProviders |
private com.android.locationtracker.data.TrackerDataHelper | mTrackerData |
private android.telephony.TelephonyManager | mTelephonyManager |
private android.location.Location | mNetworkLocation |
private NetworkStateBroadcastReceiver | mNetwork |
private static final String | CELL_PROVIDER_TAG |
private static final String | SIGNAL_PROVIDER_TAG |
private static final String | WIFI_PROVIDER_TAG |
private static final String | DATA_CONN_PROVIDER_TAG |
private static final String | MIN_TIME_PREF |
private static final String | MIN_DIS_PREF |
private static final String | GPS_PREF |
private static final String | NETWORK_PREF |
private static final String | SIGNAL_PREF |
private static final String | DEBUG_PREF |
private PreferenceListener | mPrefListener |
android.telephony.PhoneStateListener | mPhoneStateListener |
Methods Summary |
---|
private boolean | doDebugLogging()
return getPreferences().getBoolean(DEBUG_PREF, false);
|
private synchronized float | getDistanceFromNetwork(android.location.Location location)Determine the current distance from given location to the last
approximated network location
float value = 0;
if (mNetworkLocation != null) {
value = location.distanceTo(mNetworkLocation);
}
if (LocationManager.NETWORK_PROVIDER.equals(location.getProvider())) {
mNetworkLocation = location;
}
return value;
|
private android.location.LocationManager | getLocationManager()
return (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
private float | getLocationMinDistance()
try {
String disString = getPreferences().getString(MIN_DIS_PREF, "0");
return Float.parseFloat(disString);
}
catch (NumberFormatException e) {
Log.e(LOG_TAG, "Invalid preference for location min distance", e);
}
return 0;
|
private long | getLocationUpdateTime()
try {
String timeString = getPreferences().getString(MIN_TIME_PREF, "0");
long secondsTime = Long.valueOf(timeString);
return secondsTime * 1000;
}
catch (NumberFormatException e) {
Log.e(LOG_TAG, "Invalid preference for location min time", e);
}
return 0;
|
private android.content.SharedPreferences | getPreferences()
return PreferenceManager.getDefaultSharedPreferences(this);
|
private java.util.Set | getTrackedProviders()
Set<String> providerSet = new HashSet<String>();
if (trackGPS()) {
providerSet.add(LocationManager.GPS_PROVIDER);
}
if (trackNetwork()) {
providerSet.add(LocationManager.NETWORK_PROVIDER);
}
return providerSet;
|
private synchronized void | initLocationListeners()
mTrackerData = new TrackerDataHelper(this);
LocationManager lm = getLocationManager();
mTrackedProviders = getTrackedProviders();
List<String> locationProviders = lm.getAllProviders();
mListeners = new ArrayList<LocationTrackingListener>(
locationProviders.size());
long minUpdateTime = getLocationUpdateTime();
float minDistance = getLocationMinDistance();
for (String providerName : locationProviders) {
if (mTrackedProviders.contains(providerName)) {
Log.d(LOG_TAG, "Adding location listener for provider " +
providerName);
if (doDebugLogging()) {
mTrackerData.writeEntry("init", String.format(
"start listening to %s : %d ms; %f meters",
providerName, minUpdateTime, minDistance));
}
LocationTrackingListener listener =
new LocationTrackingListener();
lm.requestLocationUpdates(providerName, minUpdateTime,
minDistance, listener);
mListeners.add(listener);
}
}
mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
if (doDebugLogging()) {
// register for cell location updates
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
// Register for Network (Wifi or Mobile) updates
mNetwork = new NetworkStateBroadcastReceiver();
IntentFilter mIntentFilter;
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
mIntentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
Log.d(LOG_TAG, "registering receiver");
registerReceiver(mNetwork, mIntentFilter);
}
if (trackSignalStrength()) {
mTelephonyManager.listen(mPhoneStateListener,
PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
// register for preference changes, so we can restart listeners on
// pref changes
mPrefListener = new PreferenceListener();
getPreferences().registerOnSharedPreferenceChangeListener(mPrefListener);
|
public android.os.IBinder | onBind(android.content.Intent intent)
// ignore - nothing to do
return null;
|
public void | onDestroy()Shuts down this service
super.onDestroy();
Log.d(LOG_TAG, "Removing location listeners");
stopListeners();
Toast.makeText(this, "Tracking service stopped", Toast.LENGTH_SHORT);
|
public void | onStart(android.content.Intent intent, int startId)registers location listeners
super.onStart(intent, startId);
mNetworkLocation = null;
initLocationListeners();
Toast.makeText(this, "Tracking service started", Toast.LENGTH_SHORT);
|
protected synchronized void | stopListeners()De-registers all location listeners, closes persistent storage
LocationManager lm = getLocationManager();
if (mListeners != null) {
for (LocationTrackingListener listener : mListeners) {
lm.removeUpdates(listener);
}
mListeners.clear();
}
mListeners = null;
// stop cell state listener
if (mTelephonyManager != null) {
mTelephonyManager.listen(mPhoneStateListener, 0);
}
// stop network/wifi listener
if (mNetwork != null) {
unregisterReceiver(mNetwork);
}
mNetwork = null;
mTrackerData = null;
if (mPrefListener != null) {
getPreferences().unregisterOnSharedPreferenceChangeListener(mPrefListener);
mPrefListener = null;
}
|
private boolean | trackGPS()
return getPreferences().getBoolean(GPS_PREF, true);
|
private boolean | trackNetwork()
return getPreferences().getBoolean(NETWORK_PREF, true);
|
private boolean | trackSignalStrength()
return getPreferences().getBoolean(SIGNAL_PREF, false);
|