<commit_before>package com.mapswithme.maps.location;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.hardware.GeomagneticField;
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;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

import com.mapswithme.maps.MWMApplication;

public class LocationService implements LocationListener, SensorEventListener, WifiLocation.Listener
{
  private static final String TAG = "LocationService";

  /// These constants should correspond to values defined in platform/location.hpp
  public static final int STOPPED = 0;
  public static final int STARTED = 1;
  public static final int FIRST_EVENT = 2;
  public static final int NOT_SUPPORTED = 3;
  public static final int DISABLED_BY_USER = 4;

  public interface Listener
  {
    public void onLocationUpdated(long time, double lat, double lon, float accuracy);
    public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy);
    public void onLocationStatusChanged(int status);
  };

  private HashSet<Listener> m_observers = new HashSet<Listener>(10);

  // Used to filter locations from different providers
  private Location m_lastLocation = null;

  private WifiLocation m_wifiScanner = null;

  private LocationManager m_locationManager;

  private SensorManager m_sensorManager;
  private Sensor m_accelerometer = null;
  private Sensor m_magnetometer = null;
  /// To calculate true north for compass
  private GeomagneticField m_magneticField = null;

  /// true when GPS is on
  private boolean m_isActive = false;

  private WakeLock m_wakeLock = null;
  private MWMApplication mApplication = null;

  public LocationService(MWMApplication application)
  {
    mApplication = application;

    m_locationManager = (LocationManager) mApplication.getSystemService(Context.LOCATION_SERVICE);
    m_sensorManager = (SensorManager) mApplication.getSystemService(Context.SENSOR_SERVICE);

    if (m_sensorManager != null)
    {
      m_accelerometer = m_sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      m_magnetometer = m_sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }
  }

  private void notifyStatusChanged(int newStatus)
  {
    Iterator<Listener> it = m_observers.iterator();
    while (it.hasNext())
      it.next().onLocationStatusChanged(newStatus);
  }

  private void notifyLocationUpdated(long time, double lat, double lon, float accuracy)
  {
    Iterator<Listener> it = m_observers.iterator();
    while (it.hasNext())
      it.next().onLocationUpdated(time, lat, lon, accuracy);
  }

  private void notifyCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy)
  {
    Iterator<Listener> it = m_observers.iterator();
    while (it.hasNext())
      it.next().onCompassUpdated(time, magneticNorth, trueNorth, accuracy);
  }


  private void disableAutomaticStandby()
  {
    if (m_wakeLock == null)
    {
      PowerManager pm = (PowerManager) mApplication.getSystemService(Context.POWER_SERVICE);
      m_wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TAG);
      m_wakeLock.acquire();
    }
  }

  private void enableAutomaticStandby()
  {
    if (m_wakeLock != null)
    {
      m_wakeLock.release();
      m_wakeLock = null;
    }
  }

  public void startUpdate(Listener observer)
  {
    m_observers.add(observer);

    if (!m_isActive)
    {
      List<String> enabledProviders = m_locationManager.getProviders(true);

      // Remove passive provider, we don't use it in the current implementation
      for (int i = 0; i < enabledProviders.size(); ++i)
        if (enabledProviders.get(i).equals("passive"))
        {
          enabledProviders.remove(i);
          break;
        }

      if (enabledProviders.size() == 0)
      {
        // Use WiFi BSSIDS and Google Internet location service if no other options are available
        // But only if connection is available
        if (com.mapswithme.util.ConnectionState.isConnected(mApplication))
        {
          observer.onLocationStatusChanged(STARTED);

          if (m_wifiScanner == null)
            m_wifiScanner = new WifiLocation();
          m_wifiScanner.StartScan(mApplication, this);

          disableAutomaticStandby();
        }
        else
          observer.onLocationStatusChanged(DISABLED_BY_USER);
      }
      else
      {
        m_isActive = true;

        observer.onLocationStatusChanged(STARTED);
        disableAutomaticStandby();

        Location lastKnown = null;

        for (String provider : enabledProviders)
        {
          // @TODO change frequency and accuracy to save battery
          if (m_locationManager.isProviderEnabled(provider))
          {
            m_locationManager.requestLocationUpdates(provider, 0, 0, this);

            // Remember last known location
            final Location l = m_locationManager.getLastKnownLocation(provider);
            if (l != null)
            {
              if (lastKnown == null || isBetterLocation(l, lastKnown))
                lastKnown = l;
            }
          }
        }

        if (m_sensorManager != null)
        {
          // How often compass is updated (may be SensorManager.SENSOR_DELAY_UI)
          final int COMPASS_REFRESH_MKS = SensorManager.SENSOR_DELAY_NORMAL;

          if (m_accelerometer != null)
            m_sensorManager.registerListener(this, m_accelerometer, COMPASS_REFRESH_MKS);
          if (m_magnetometer != null)
            m_sensorManager.registerListener(this, m_magnetometer, COMPASS_REFRESH_MKS);
        }

        // Pass last known location only in the end of all registerListener
        // in case, when we want to disconnect in listener.
        onLocationChanged(lastKnown);
      }
    }<commit_msg>[android] Fix dummy bug in location.<commit_after>package com.mapswithme.maps.location;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.hardware.GeomagneticField;
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;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

import com.mapswithme.maps.MWMApplication;

public class LocationService implements LocationListener, SensorEventListener, WifiLocation.Listener
{
  private static final String TAG = "LocationService";

  /// These constants should correspond to values defined in platform/location.hpp
  public static final int STOPPED = 0;
  public static final int STARTED = 1;
  public static final int FIRST_EVENT = 2;
  public static final int NOT_SUPPORTED = 3;
  public static final int DISABLED_BY_USER = 4;

  public interface Listener
  {
    public void onLocationUpdated(long time, double lat, double lon, float accuracy);
    public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy);
    public void onLocationStatusChanged(int status);
  };

  private HashSet<Listener> m_observers = new HashSet<Listener>(10);

  // Used to filter locations from different providers
  private Location m_lastLocation = null;

  private WifiLocation m_wifiScanner = null;

  private LocationManager m_locationManager;

  private SensorManager m_sensorManager;
  private Sensor m_accelerometer = null;
  private Sensor m_magnetometer = null;
  /// To calculate true north for compass
  private GeomagneticField m_magneticField = null;

  /// true when GPS is on
  private boolean m_isActive = false;

  private WakeLock m_wakeLock = null;
  private MWMApplication mApplication = null;

  public LocationService(MWMApplication application)
  {
    mApplication = application;

    m_locationManager = (LocationManager) mApplication.getSystemService(Context.LOCATION_SERVICE);
    m_sensorManager = (SensorManager) mApplication.getSystemService(Context.SENSOR_SERVICE);

    if (m_sensorManager != null)
    {
      m_accelerometer = m_sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      m_magnetometer = m_sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }
  }

  private void notifyStatusChanged(int newStatus)
  {
    Iterator<Listener> it = m_observers.iterator();
    while (it.hasNext())
      it.next().onLocationStatusChanged(newStatus);
  }

  private void notifyLocationUpdated(long time, double lat, double lon, float accuracy)
  {
    Iterator<Listener> it = m_observers.iterator();
    while (it.hasNext())
      it.next().onLocationUpdated(time, lat, lon, accuracy);
  }

  private void notifyCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy)
  {
    Iterator<Listener> it = m_observers.iterator();
    while (it.hasNext())
      it.next().onCompassUpdated(time, magneticNorth, trueNorth, accuracy);
  }


  private void disableAutomaticStandby()
  {
    if (m_wakeLock == null)
    {
      PowerManager pm = (PowerManager) mApplication.getSystemService(Context.POWER_SERVICE);
      m_wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TAG);
      m_wakeLock.acquire();
    }
  }

  private void enableAutomaticStandby()
  {
    if (m_wakeLock != null)
    {
      m_wakeLock.release();
      m_wakeLock = null;
    }
  }

  public void startUpdate(Listener observer)
  {
    m_observers.add(observer);

    if (!m_isActive)
    {
      List<String> enabledProviders = m_locationManager.getProviders(true);

      // Remove passive provider, we don't use it in the current implementation
      for (int i = 0; i < enabledProviders.size(); ++i)
        if (enabledProviders.get(i).equals("passive"))
        {
          enabledProviders.remove(i);
          break;
        }

      if (enabledProviders.size() == 0)
      {
        // Use WiFi BSSIDS and Google Internet location service if no other options are available
        // But only if connection is available
        if (com.mapswithme.util.ConnectionState.isConnected(mApplication))
        {
          observer.onLocationStatusChanged(STARTED);

          if (m_wifiScanner == null)
            m_wifiScanner = new WifiLocation();
          m_wifiScanner.StartScan(mApplication, this);

          disableAutomaticStandby();
        }
        else
          observer.onLocationStatusChanged(DISABLED_BY_USER);
      }
      else
      {
        m_isActive = true;

        observer.onLocationStatusChanged(STARTED);
        disableAutomaticStandby();

        Location lastKnown = null;

        for (String provider : enabledProviders)
        {
          // @TODO change frequency and accuracy to save battery
          if (m_locationManager.isProviderEnabled(provider))
          {
            m_locationManager.requestLocationUpdates(provider, 0, 0, this);

            // Remember last known location
            final Location l = m_locationManager.getLastKnownLocation(provider);
            if (l != null)
            {
              if (lastKnown == null || isBetterLocation(l, lastKnown))
                lastKnown = l;
            }
          }
        }

        if (m_sensorManager != null)
        {
          // How often compass is updated (may be SensorManager.SENSOR_DELAY_UI)
          final int COMPASS_REFRESH_MKS = SensorManager.SENSOR_DELAY_NORMAL;

          if (m_accelerometer != null)
            m_sensorManager.registerListener(this, m_accelerometer, COMPASS_REFRESH_MKS);
          if (m_magnetometer != null)
            m_sensorManager.registerListener(this, m_magnetometer, COMPASS_REFRESH_MKS);
        }

        // Pass last known location only in the end of all registerListener
        // in case, when we want to disconnect in listener.
        if (lastKnown != null)
          onLocationChanged(lastKnown);
      }
    }
{'fraction_non_alphanumeric': 0.0822464949462015, 'fraction_numerical': 0.0017932833387675252, 'mean_word_length': 3.2205022359821123, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1888346', 'n_tokens_mistral': 3550, 'n_tokens_neox': 3436, 'n_words': 860}