package it.near.sdk.geopolis.geofences;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;

import it.near.sdk.logging.NearLog;


/**
 * Receiver for the BOOT_COMPLETED and PROVIDERS_CHANGED system events.
 * It sends intent to start or stop radar
 * Created by cattaneostefano on 04/10/2016.
 */

public class GeoFenceSystemEventsReceiver extends BroadcastReceiver {

    private static final String TAG = "GFSystemEventsReceiver";
    public static final String LOCATION_STATUS = "location_status";
    public static final String RESET_MONITOR_ACTION_SUFFIX = "RESET_SCAN";

    @Override
    public void onReceive(Context context, Intent intent) {
        NearLog.d(TAG, "received intent: " + intent.getAction());
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            sendResetIntent(context, true);
        }
        if (intent.getAction().equals("android.location.PROVIDERS_CHANGED")) {
            boolean anyLocationProv = false;
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

            anyLocationProv |= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            anyLocationProv |= locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            sendResetIntent(context, anyLocationProv);
        }
    }

    /**
     * Send the reset intent to start or stop the radar.
     *
     * @param context
     * @param startRadar indicates if the radar should start.
     */
    private void sendResetIntent(Context context, boolean startRadar) {
        Intent resetIntent = new Intent();
        String packageName = context.getPackageName();
        resetIntent.setAction(packageName + "." + RESET_MONITOR_ACTION_SUFFIX);
        resetIntent.putExtra(LOCATION_STATUS, startRadar);
        context.sendBroadcast(resetIntent);
    }
}

@@ -181,16 +181,67 @@
 nager;%0A%0A
+import it.near.sdk.communication.NearInstallation;%0A
 import i
@@ -269,17 +269,16 @@
 arLog;%0A%0A
-%0A
 /**%0A * R
@@ -1489,24 +1489,81 @@
 );%0A        %7D
+%0A%0A        NearInstallation.registerInstallation(context);
 %0A    %7D%0A%0A    

update installation on location settings change