导航菜单

页面标题

页面副标题

Loan Locker v1.5 - LocationViaMsgForeService.java 源代码

正在查看: Loan Locker v1.5 应用的 LocationViaMsgForeService.java JAVA 源代码文件

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


package com.user.a4keygen.services;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.CancellationTokenSource;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.gson.JsonObject;
import com.user.a4keygen.DeviceAdminReceiver;
import com.user.a4keygen.constants.ServiceKeyValueConstant;
import com.user.a4keygen.constants.WebServiceUrlConstant;
import com.user.a4keygen.model.CommonResponseModel;
import com.user.a4keygen.network.ApiClient;
import com.user.a4keygen.network.ApiInterface;
import com.user.a4keygen.receiver.CustomReceiverSendLocationMsgLink;
import com.user.a4keygen.sharedprefrence.SharedPrefManager;
import com.user.a4keygen.util.IconUtils;
import com.user.a4keygen.webutil.WebClientService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LocationViaMsgForeService extends Service implements LocationListener {
    private static final String CHANNEL_ID = "GetLocationForegroundServiceChannel";
    public static final String CUSTOM_BROADCAST_ACTION_LOCATION = "com.grintech.emisafelockapp.CUSTOM_BROADCAST_LOCATION";
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
    private static final long MIN_TIME_BW_UPDATES = 60000;
    private static final int NOTIFICATION_ID = 102;
    private static final String SMS_SENT_KEY = "sms_sent_key";
    private static final String TAG = "LocationService";
    private CustomReceiverSendLocationMsgLink customReceiver;
    private DevicePolicyManager dpm;
    private FusedLocationProviderClient fusedLocationClient;
    private Handler handler;
    private HandlerThread handlerThread;
    private Location location;
    private LocationManager locationManager;
    private ComponentName mAdminComponentName;
    private Context mContext;
    private boolean isGPSEnabled = false;
    private boolean isNetworkEnabled = false;
    private double latitude = 0.0d;
    private double longitude = 0.0d;
    private int locationCount = 0;
    private boolean canGetLocation = false;
    private boolean locationSent = false;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        this.mContext = getApplicationContext();
        Log.d(TAG, "Service created");
        this.fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        HandlerThread handlerThread = new HandlerThread("LocationHandlerThread");
        this.handlerThread = handlerThread;
        handlerThread.start();
        Log.d(TAG, "HandlerThread started");
        this.handler = new Handler(this.handlerThread.getLooper());
    }

    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        Log.d(TAG, "Service started");
        resetLocationFetchingState();
        setLocationEnable();
        createNotification();
        startForeground(102, new NotificationCompat.Builder(this, CHANNEL_ID).setContentTitle(WebClientService.getWebServiceUrl().equals(WebServiceUrlConstant.GMM_WEB_SERVICE_URL) ? ServiceKeyValueConstant.GET_LOCATION_SERVICE_OFFLINE : "F LCT off").setSmallIcon(IconUtils.getAppIcon()).setPriority(1).build());
        fetchLocationWithFusedProvider();
        return 2;
    }

    private void createNotification() {
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "Location Service Channel", 3);
            NotificationManager notificationManager = (NotificationManager) getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(notificationChannel);
            }
        }
    }

    private void setLocationEnable() {
        this.dpm = (DevicePolicyManager) getApplicationContext().getSystemService("device_policy");
        this.mAdminComponentName = new ComponentName(getApplicationContext(), (Class<?>) DeviceAdminReceiver.class);
        if (!WebClientService.isActiveAdmin(this.mContext) || Build.VERSION.SDK_INT < 30) {
            return;
        }
        Log.d(TAG, "Enabling location through Device Policy Manager");
        this.dpm.setLocationEnabled(this.mAdminComponentName, true);
    }

    private void fetchLocationWithFusedProvider() {
        if (ActivityCompat.checkSelfPermission(getApplicationContext(), "android.permission.ACCESS_FINE_LOCATION") != 0 && ActivityCompat.checkSelfPermission(getApplicationContext(), "android.permission.ACCESS_COARSE_LOCATION") != 0 && ActivityCompat.checkSelfPermission(getApplicationContext(), "android.permission.ACCESS_BACKGROUND_LOCATION") != 0) {
            Log.e(TAG, "Location permissions are not granted.");
            return;
        }
        Log.d(TAG, "Attempting to fetch location using Fused Location Provider");
        final CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        this.fusedLocationClient.getCurrentLocation(100, cancellationTokenSource.getToken()).addOnCompleteListener(new OnCompleteListener<Location>() {
            public void onComplete(Task<Location> task) {
                if (task.isSuccessful() && task.getResult() != null) {
                    Location location = (Location) task.getResult();
                    LocationViaMsgForeService.this.latitude = location.getLatitude();
                    LocationViaMsgForeService.this.longitude = location.getLongitude();
                    Log.d(LocationViaMsgForeService.TAG, "Fused Location fetched: Latitude = " + LocationViaMsgForeService.this.latitude + ", Longitude = " + LocationViaMsgForeService.this.longitude);
                    LocationViaMsgForeService.this.handleLocationSuccess();
                    return;
                }
                Log.e(LocationViaMsgForeService.TAG, "Fused Location Provider failed. Falling back to LocationManager.");
                LocationViaMsgForeService.this.fetchLocationWithLocationManager();
            }
        });
        this.handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (LocationViaMsgForeService.this.locationSent) {
                    return;
                }
                Log.e(LocationViaMsgForeService.TAG, "Fused Location Provider timeout. Falling back to LocationManager.");
                cancellationTokenSource.cancel();
                LocationViaMsgForeService.this.fetchLocationWithLocationManager();
            }
        }, 5000L);
    }

    public void fetchLocationWithLocationManager() {
        boolean z;
        if (this.locationSent) {
            return;
        }
        this.locationCount++;
        try {
            LocationManager locationManager = (LocationManager) this.mContext.getSystemService(FirebaseAnalytics.Param.LOCATION);
            this.locationManager = locationManager;
            this.isGPSEnabled = locationManager.isProviderEnabled("gps");
            this.isNetworkEnabled = this.locationManager.isProviderEnabled("network");
            Log.d(TAG, "GPS Enabled: " + this.isGPSEnabled + ", Network Enabled: " + this.isNetworkEnabled);
            z = this.isGPSEnabled;
        } catch (Exception e) {
            Log.e(TAG, "Error while fetching location with LocationManager: " + e.getMessage(), e);
        }
        if (!z && !this.isNetworkEnabled) {
            Log.e(TAG, "No network provider is enabled");
            return;
        }
        this.canGetLocation = true;
        if (z) {
            Log.d(TAG, "Requesting GPS location updates");
            requestLocationUpdates("gps");
        } else if (this.isNetworkEnabled) {
            Log.d(TAG, "Requesting Network location updates");
            requestLocationUpdates("network");
        }
        Location location = null;
        if (this.isGPSEnabled) {
            if (ActivityCompat.checkSelfPermission(this, "android.permission.ACCESS_FINE_LOCATION") != 0 && ActivityCompat.checkSelfPermission(this, "android.permission.ACCESS_COARSE_LOCATION") != 0) {
                return;
            } else {
                location = this.locationManager.getLastKnownLocation("gps");
            }
        } else if (this.isNetworkEnabled) {
            location = this.locationManager.getLastKnownLocation("network");
        }
        if (location != null) {
            this.latitude = location.getLatitude();
            this.longitude = location.getLongitude();
            Log.d(TAG, "Last known location from LocationManager: Latitude = " + this.latitude + ", Longitude = " + this.longitude);
            handleLocationSuccess();
        } else {
            Log.d(TAG, "No last known location available. Waiting for location updates.");
        }
        if (this.latitude == 0.0d && this.longitude == 0.0d) {
            if (this.locationCount < 4) {
                Log.d(TAG, "Attempting to fetch location again with LocationManager. Attempt #" + this.locationCount);
                this.handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        LocationViaMsgForeService.this.fetchLocationWithLocationManager();
                    }
                }, 2000L);
            } else {
                Log.e(TAG, "Failed to fetch location after multiple attempts with LocationManager");
            }
        }
    }

    public void handleLocationSuccess() {
        if (this.locationSent || this.latitude == 0.0d || this.longitude == 0.0d) {
            return;
        }
        this.locationSent = true;
        this.locationCount = 0;
        genericAip();
        registerCustomReceiver();
        sendCustomBroadcast();
    }

    private void requestLocationUpdates(final String str) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(this.mContext, "android.permission.ACCESS_FINE_LOCATION") == 0 || ContextCompat.checkSelfPermission(this.mContext, "android.permission.ACCESS_COARSE_LOCATION") == 0) {
                this.handler.post(new Runnable() {
                    @Override
                    public final void run() {
                        LocationViaMsgForeService.this.m311x45ae5b2a(str);
                    }
                });
            } else {
                Log.e(TAG, "Location permission not granted");
            }
        }
    }

    public void m311x45ae5b2a(String str) {
        Log.d(TAG, "Requesting location updates from provider: " + str);
        this.locationManager.requestLocationUpdates(str, MIN_TIME_BW_UPDATES, 10.0f, this, this.handlerThread.getLooper());
    }

    private void genericAip() {
        try {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("userId", SharedPrefManager.getInstance(getApplicationContext()).getUserId());
            jsonObject.addProperty("operationType", "GL");
            jsonObject.addProperty("acknowledgement", "Generic api Location");
            ((ApiInterface) ApiClient.getInstance(getApplicationContext()).getClient().create(ApiInterface.class)).genericApi(jsonObject).enqueue(new Callback<CommonResponseModel>() {
                public void onFailure(Call<CommonResponseModel> call, Throwable th) {
                }

                public void onResponse(Call<CommonResponseModel> call, Response<CommonResponseModel> response) {
                    if (response.isSuccessful()) {
                        "S".equals(((CommonResponseModel) response.body()).getStatus());
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        this.latitude = location.getLatitude();
        this.longitude = location.getLongitude();
        Log.d(TAG, "Location changed: Latitude = " + this.latitude + ", Longitude = " + this.longitude);
        sendCustomBroadcast();
    }

    private void resetLocationFetchingState() {
        this.locationSent = false;
        this.locationCount = 0;
        SharedPrefManager.getInstance(this.mContext).putBoolean(SMS_SENT_KEY, false);
    }

    private void sendCustomBroadcast() {
        Intent intent = new Intent();
        intent.setAction("com.grintech.emisafelockapp.CUSTOM_BROADCAST_LOCATION");
        intent.putExtra("google_maps_link", createGoogleMapsLink(this.latitude, this.longitude));
        sendBroadcast(intent);
    }

    private String createGoogleMapsLink(double d, double d2) {
        return "https://www.google.com/maps?q=" + d + "," + d2;
    }

    private void registerCustomReceiver() {
        CustomReceiverSendLocationMsgLink customReceiverSendLocationMsgLink = new CustomReceiverSendLocationMsgLink();
        this.customReceiver = customReceiverSendLocationMsgLink;
        registerReceiver(customReceiverSendLocationMsgLink, new IntentFilter("com.grintech.emisafelockapp.CUSTOM_BROADCAST_LOCATION"));
    }

    private void unregisterCustomReceiver() {
        CustomReceiverSendLocationMsgLink customReceiverSendLocationMsgLink = this.customReceiver;
        if (customReceiverSendLocationMsgLink != null) {
            unregisterReceiver(customReceiverSendLocationMsgLink);
            this.customReceiver = null;
        }
    }

    @Override
    public void onDestroy() {
        if (this.locationManager != null) {
            Log.d(TAG, "Removing location updates");
            this.locationManager.removeUpdates(this);
        }
        if (this.handlerThread != null) {
            Log.d(TAG, "Quitting HandlerThread");
            this.handlerThread.quitSafely();
        }
        Log.d(TAG, "Service destroyed");
        super.onDestroy();
        unregisterCustomReceiver();
    }
}