导航菜单

页面标题

页面副标题

Delta Chat v1.58.3 - LocationBackgroundService.java 源代码

正在查看: Delta Chat v1.58.3 应用的 LocationBackgroundService.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package org.thoughtcrime.securesms.geolocation;

import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class LocationBackgroundService extends Service {
    private static final int INITIAL_TIMEOUT = 120000;
    private static final float LOCATION_DISTANCE = 25.0f;
    private static final int LOCATION_INTERVAL = 1000;
    private static final String TAG = "LocationBackgroundService";
    ServiceLocationListener locationListener;
    private LocationManager locationManager = null;
    private final IBinder mBinder = new LocationBackgroundServiceBinder();

    @Override
    public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
        return super.bindService(intent, serviceConnection, i);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return this.mBinder;
    }

    @Override
    public void onCreate() {
        LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService("location");
        this.locationManager = locationManager;
        if (locationManager == null) {
            Log.e(TAG, "Unable to initialize location service");
            return;
        }
        this.locationListener = new ServiceLocationListener();
        Location lastKnownLocation = this.locationManager.getLastKnownLocation("gps");
        if (lastKnownLocation != null && System.currentTimeMillis() - lastKnownLocation.getTime() <= 600000) {
            DcLocation.getInstance().updateLocation(lastKnownLocation);
        }
        requestLocationUpdate("gps");
        initialLocationUpdate();
    }

    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        super.onStartCommand(intent, i, i2);
        return 1;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LocationManager locationManager = this.locationManager;
        if (locationManager == null) {
            return;
        }
        try {
            locationManager.removeUpdates(this.locationListener);
        } catch (Exception e) {
            Log.i(TAG, "fail to remove location listeners, ignore", e);
        }
    }

    private void requestLocationUpdate(String str) {
        try {
            this.locationManager.requestLocationUpdates(str, 1000L, LOCATION_DISTANCE, this.locationListener);
        } catch (IllegalArgumentException | SecurityException e) {
            Log.e(TAG, String.format("Unable to request %s provider based location updates.", str), e);
        }
    }

    private void initialLocationUpdate() {
        try {
            Location lastKnownLocation = this.locationManager.getLastKnownLocation("gps");
            if (lastKnownLocation == null || System.currentTimeMillis() - lastKnownLocation.getTime() >= 120000) {
                return;
            }
            this.locationListener.onLocationChanged(lastKnownLocation);
        } catch (NullPointerException | SecurityException e) {
            e.printStackTrace();
        }
    }

    class LocationBackgroundServiceBinder extends Binder {
        LocationBackgroundServiceBinder getService() {
            return this;
        }

        LocationBackgroundServiceBinder() {
        }

        void stop() {
            DcLocation.getInstance().reset();
            LocationBackgroundService.this.stopSelf();
        }
    }

    private class ServiceLocationListener implements LocationListener {
        private ServiceLocationListener() {
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.d(LocationBackgroundService.TAG, "onLocationChanged: " + location);
            if (location == null) {
                return;
            }
            DcLocation.getInstance().updateLocation(location);
        }

        @Override
        public void onProviderDisabled(String str) {
            Log.e(LocationBackgroundService.TAG, "onProviderDisabled: " + str);
        }

        @Override
        public void onProviderEnabled(String str) {
            Log.e(LocationBackgroundService.TAG, "onProviderEnabled: " + str);
        }

        @Override
        public void onStatusChanged(String str, int i, Bundle bundle) {
            Log.e(LocationBackgroundService.TAG, "onStatusChanged: " + str + " status: " + i);
        }
    }
}