package com.futurice.hereandnow.services;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.futurice.hereandnow.Constants;

public class WifiBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "NetworkInfo";

    @Override
    public void onReceive(Context context, Intent intent) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);

        if (networkInfo != null) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {

                if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
                    WifiInfo info = wifiManager.getConnectionInfo();
                    if (info.getSSID().equals(Constants.NETWORK_SSID)) {
                        Log.d(TAG, "Connected to correct Wifi.");

                        // Start the location service.
                        context.startService(new Intent(context, LocationService.class));
                    }
                }

                if (networkInfo.getState() == NetworkInfo.State.DISCONNECTED) {
                    Log.d(TAG, "Disconnected from the Wifi.");

                    // Stop the location service if it's running.
                    context.stopService(new Intent(context, LocationService.class));
                }
            }
        }
    }
}

@@ -141,16 +141,58 @@
 Intent;%0A
+import android.content.SharedPreferences;%0A
 import a
@@ -318,32 +318,77 @@
 fi.WifiManager;%0A
+import android.preference.PreferenceManager;%0A
 import android.u
@@ -438,16 +438,74 @@
 nstants;
+%0Aimport com.futurice.hereandnow.activity.SettingsActivity;
 %0A%0Apublic
@@ -1267,32 +1267,265 @@
 rrect Wifi.%22);%0A%0A
+                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);%0A                        Boolean serviceEnabled = preferences.getBoolean(SettingsActivity.BACKGROUND_SERVICE_KEY, true);%0A%0A
                 
@@ -1563,16 +1563,66 @@
 ervice.%0A
+                        if (serviceEnabled) %7B%0A    
         
@@ -1695,32 +1695,58 @@
 ervice.class));%0A
+                        %7D%0A
                 

Check that the background service is enabled also in the wifi broadcast receiver