FileDocCategorySizeDatePackage
TrackerService.javaAPI DocAndroid 5.1 API16340Thu Mar 12 22:22:44 GMT 2015com.android.locationtracker

TrackerService

public class TrackerService extends android.app.Service
Location Tracking service Records location updates for all registered location providers, and cell location updates

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
Constructors Summary
public TrackerService()


      
    
Methods Summary
private booleandoDebugLogging()

        return getPreferences().getBoolean(DEBUG_PREF, false);
    
private synchronized floatgetDistanceFromNetwork(android.location.Location location)
Determine the current distance from given location to the last approximated network location

param
location - new location
return
float distance in meters

        float value = 0;
        if (mNetworkLocation != null) {
            value = location.distanceTo(mNetworkLocation);
        }
        if (LocationManager.NETWORK_PROVIDER.equals(location.getProvider())) {
            mNetworkLocation = location;
        }
        return value;
    
private android.location.LocationManagergetLocationManager()

        return (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    
private floatgetLocationMinDistance()

        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 longgetLocationUpdateTime()

        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.SharedPreferencesgetPreferences()

        return PreferenceManager.getDefaultSharedPreferences(this);
    
private java.util.SetgetTrackedProviders()

        Set<String> providerSet = new HashSet<String>();

        if (trackGPS()) {
            providerSet.add(LocationManager.GPS_PROVIDER);
        }
        if (trackNetwork()) {
            providerSet.add(LocationManager.NETWORK_PROVIDER);
        }
        return providerSet;
    
private synchronized voidinitLocationListeners()

        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.IBinderonBind(android.content.Intent intent)

        // ignore - nothing to do
        return null;
    
public voidonDestroy()
Shuts down this service

        super.onDestroy();
        Log.d(LOG_TAG, "Removing location listeners");
        stopListeners();
        Toast.makeText(this, "Tracking service stopped", Toast.LENGTH_SHORT);
    
public voidonStart(android.content.Intent intent, int startId)
registers location listeners

param
intent
param
startId

        super.onStart(intent, startId);
        mNetworkLocation = null;

        initLocationListeners();
        Toast.makeText(this, "Tracking service started", Toast.LENGTH_SHORT);
    
protected synchronized voidstopListeners()
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 booleantrackGPS()

        return getPreferences().getBoolean(GPS_PREF, true);
    
private booleantrackNetwork()

        return getPreferences().getBoolean(NETWORK_PREF, true);
    
private booleantrackSignalStrength()

        return getPreferences().getBoolean(SIGNAL_PREF, false);