<commit_before>/*
 * Copyright 2013 - 2015 <NAME> (<EMAIL>)
 *
 * Licensed 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 org.traccar.client;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

public class PositionProvider implements LocationListener {

    private static final String TAG = PositionProvider.class.getSimpleName();

    public interface PositionListener {
        void onPositionUpdate(Position position);
    }

    private final PositionListener listener;

    private final Context context;
    private final SharedPreferences preferences;
    private final LocationManager locationManager;

    private String deviceId;
    private String type;
    private final long period;

    public PositionProvider(Context context, PositionListener listener) {
        this.context = context;
        this.listener = listener;

        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        deviceId = preferences.getString(MainActivity.KEY_DEVICE, null);
        type = preferences.getString(MainActivity.KEY_PROVIDER, null);
        period = Integer.parseInt(preferences.getString(MainActivity.KEY_INTERVAL, null)) * 1000;
    }

    public void startUpdates() {
        locationManager.requestLocationUpdates(type, period, 0, this);
    }

    public void stopUpdates() {
        locationManager.removeUpdates(this);
    }

    private long lastTime;

    @Override
    public void onLocationChanged(Location location) {
        if (location != null && location.getTime() != lastTime) {
            Log.i(TAG, "location new");
            lastTime = location.getTime();
            listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel()));
        } else {
            Log.i(TAG, location != null ? "location old" : "location nil");
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.i(TAG, "provider status");
    }

    @Override
    public void onProviderEnabled(String provider) {<commit_msg>Ensure that provider is correct<commit_after>/*
 * Copyright 2013 - 2015 <NAME> (<EMAIL>)
 *
 * Licensed 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 org.traccar.client;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

public class PositionProvider implements LocationListener {

    private static final String TAG = PositionProvider.class.getSimpleName();

    public interface PositionListener {
        void onPositionUpdate(Position position);
    }

    private final PositionListener listener;

    private final Context context;
    private final SharedPreferences preferences;
    private final LocationManager locationManager;

    private String deviceId;
    private String type;
    private final long period;

    public PositionProvider(Context context, PositionListener listener) {
        this.context = context;
        this.listener = listener;

        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        deviceId = preferences.getString(MainActivity.KEY_DEVICE, null);
        period = Integer.parseInt(preferences.getString(MainActivity.KEY_INTERVAL, null)) * 1000;

        type = preferences.getString(MainActivity.KEY_PROVIDER, null);
        if (!type.equals(LocationManager.NETWORK_PROVIDER)) {
            type = LocationManager.GPS_PROVIDER;
        }
    }

    public void startUpdates() {
        locationManager.requestLocationUpdates(type, period, 0, this);
    }

    public void stopUpdates() {
        locationManager.removeUpdates(this);
    }

    private long lastTime;

    @Override
    public void onLocationChanged(Location location) {
        if (location != null && location.getTime() != lastTime) {
            Log.i(TAG, "location new");
            lastTime = location.getTime();
            listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel()));
        } else {
            Log.i(TAG, location != null ? "location old" : "location nil");
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.i(TAG, "provider status");
    }

    @Override
    public void onProviderEnabled(String provider) {
{'fraction_non_alphanumeric': 0.08381689232753063, 'fraction_numerical': 0.00548033526756931, 'mean_word_length': 3.6584084084084085, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2139774', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1516, 'n_words': 482}