<commit_before>/*
 * Copyright 2014, Leanplum, Inc. All rights reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.leanplum;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.location.Location;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.Geofence.Builder;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.leanplum.internal.Constants;
import com.leanplum.internal.JsonConverter;
import com.leanplum.internal.LeanplumInternal;
import com.leanplum.internal.LeanplumMessageMatchFilter;
import com.leanplum.internal.Log;
import com.leanplum.internal.Util;
import com.leanplum.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

class GeofenceStatus {
  static final int UNKNOWN = 1;
  static final int INSIDE = 2;
  static final int OUTSIDE = 4;

  static boolean shouldTriggerEnteredGeofence(Number currentStatus, Number newStatus) {
    return ((currentStatus.intValue() == OUTSIDE || currentStatus.intValue() == UNKNOWN) &&
        newStatus.intValue() == INSIDE);
  }

  static boolean shouldTriggerExitedGeofence(Number currentStatus, Number newStatus) {
    return (currentStatus.intValue() == INSIDE &&
        newStatus.intValue() == OUTSIDE);
  }
}

/**
 * Handles geo-fencing and sending user location.
 *
 * @author <NAME>
 */
class LocationManagerImplementation implements
    GoogleApiClient.ConnectionCallbacks, OnConnectionFailedListener, LocationManager,
    LocationListener {
  private static final long LOCATION_UPDATE_INTERVAL = 7200000; // 2 hours in milliseconds.
  private static final long LOCATION_REQUEST_INTERVAL = 60000; // a minute in milliseconds.
  private static final double ACCURACY_THRESHOLD_GPS = 100; // 100m

  private static final String PERMISSION = "android.permission.ACCESS_FINE_LOCATION";
  private static final String METADATA = "com.google.android.gms.version";

  private Map<String, Object> lastKnownState;
  private Map<String, Object> stateBeforeBackground;
  private List<Geofence> allGeofences;
  private List<Geofence> backgroundGeofences;
  private List<String> trackedGeofenceIds;
  private boolean isInBackground;
  private boolean isSendingLocation;
  private Date lastLocationSentDate;
  private LeanplumLocationAccuracyType lastLocationSentAccuracyType;

  private GoogleApiClient googleApiClient;

  private static LocationManagerImplementation instance;

  public static synchronized LocationManager instance() {
    if (instance == null) {
      instance = new LocationManagerImplementation();
    }
    return instance;
  }

  private LocationManagerImplementation() {
    trackedGeofenceIds = new ArrayList<>();
    loadLastKnownRegionState();
    isInBackground = Util.isInBackground();
    isSendingLocation = false;
    lastLocationSentAccuracyType = LeanplumLocationAccuracyType.IP;
    // When an app starts LocationManager will be initialized. Update user location.
    updateUserLocation();
  }

  @SuppressWarnings("unchecked")
  public void setRegionsData(Map<String, Object> regionData,
      Set<String> foregroundRegionNames, Set<String> backgroundRegionNames) {
    if (!Util.hasPlayServices()) {
      return;
    }

    allGeofences = new ArrayList<>();
    backgroundGeofences = new ArrayList<>();
    for (Map.Entry<String, Object> entry : regionData.entrySet()) {
      String regionName = entry.getKey();
      boolean isForeground = foregroundRegionNames.contains(regionName);
      boolean isBackground = backgroundRegionNames.contains(regionName);
      if (isForeground || isBackground) {
        Geofence geofence = geofenceFromMap((Map<String, Object>) entry.getValue(),
            regionName);
        if (geofence != null) {
          if (isBackground) {
            backgroundGeofences.add(geofence);
          }
          allGeofences.add(geofence);
          if (lastKnownState.get(geofence.getRequestId()) == null) {
            lastKnownState.put(geofence.getRequestId(), GeofenceStatus.UNKNOWN);
          }<commit_msg>fix(NPE): Adds null checker at LocationManagerImplementation.setRegionsData.<commit_after>/*
 * Copyright 2014, Leanplum, Inc. All rights reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.leanplum;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.location.Location;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.Geofence.Builder;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.leanplum.internal.Constants;
import com.leanplum.internal.JsonConverter;
import com.leanplum.internal.LeanplumInternal;
import com.leanplum.internal.LeanplumMessageMatchFilter;
import com.leanplum.internal.Log;
import com.leanplum.internal.Util;
import com.leanplum.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

class GeofenceStatus {
  static final int UNKNOWN = 1;
  static final int INSIDE = 2;
  static final int OUTSIDE = 4;

  static boolean shouldTriggerEnteredGeofence(Number currentStatus, Number newStatus) {
    return ((currentStatus.intValue() == OUTSIDE || currentStatus.intValue() == UNKNOWN) &&
        newStatus.intValue() == INSIDE);
  }

  static boolean shouldTriggerExitedGeofence(Number currentStatus, Number newStatus) {
    return (currentStatus.intValue() == INSIDE &&
        newStatus.intValue() == OUTSIDE);
  }
}

/**
 * Handles geo-fencing and sending user location.
 *
 * @author <NAME>
 */
class LocationManagerImplementation implements
    GoogleApiClient.ConnectionCallbacks, OnConnectionFailedListener, LocationManager,
    LocationListener {
  private static final long LOCATION_UPDATE_INTERVAL = 7200000; // 2 hours in milliseconds.
  private static final long LOCATION_REQUEST_INTERVAL = 60000; // a minute in milliseconds.
  private static final double ACCURACY_THRESHOLD_GPS = 100; // 100m

  private static final String PERMISSION = "android.permission.ACCESS_FINE_LOCATION";
  private static final String METADATA = "com.google.android.gms.version";

  private Map<String, Object> lastKnownState;
  private Map<String, Object> stateBeforeBackground;
  private List<Geofence> allGeofences;
  private List<Geofence> backgroundGeofences;
  private List<String> trackedGeofenceIds;
  private boolean isInBackground;
  private boolean isSendingLocation;
  private Date lastLocationSentDate;
  private LeanplumLocationAccuracyType lastLocationSentAccuracyType;

  private GoogleApiClient googleApiClient;

  private static LocationManagerImplementation instance;

  public static synchronized LocationManager instance() {
    if (instance == null) {
      instance = new LocationManagerImplementation();
    }
    return instance;
  }

  private LocationManagerImplementation() {
    trackedGeofenceIds = new ArrayList<>();
    loadLastKnownRegionState();
    isInBackground = Util.isInBackground();
    isSendingLocation = false;
    lastLocationSentAccuracyType = LeanplumLocationAccuracyType.IP;
    // When an app starts LocationManager will be initialized. Update user location.
    updateUserLocation();
  }

  @SuppressWarnings("unchecked")
  public void setRegionsData(Map<String, Object> regionData,
      Set<String> foregroundRegionNames, Set<String> backgroundRegionNames) {
    if (!Util.hasPlayServices()) {
      return;
    }

    allGeofences = new ArrayList<>();
    backgroundGeofences = new ArrayList<>();
    for (Map.Entry<String, Object> entry : regionData.entrySet()) {
      String regionName = entry.getKey();
      boolean isForeground = foregroundRegionNames.contains(regionName);
      boolean isBackground = backgroundRegionNames.contains(regionName);
      if (isForeground || isBackground) {
        Geofence geofence = geofenceFromMap((Map<String, Object>) entry.getValue(),
            regionName);
        if (geofence != null) {
          if (isBackground) {
            backgroundGeofences.add(geofence);
          }
          allGeofences.add(geofence);
          if (lastKnownState != null && geofence.getRequestId() != null
              && lastKnownState.get(geofence.getRequestId()) == null) {
            lastKnownState.put(geofence.getRequestId(), GeofenceStatus.UNKNOWN);
          }
{'fraction_non_alphanumeric': 0.08801999642920907, 'fraction_numerical': 0.005356186395286556, 'mean_word_length': 4.521439132577624, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6443237', 'n_tokens_mistral': 3140, 'n_tokens_neox': 3051, 'n_words': 821}