That fuck shit the fascists are using
at master 115 lines 3.9 kB view raw
1package org.tm.archive.maps; 2 3import android.Manifest; 4import android.content.Context; 5import android.content.pm.PackageManager; 6import android.location.Location; 7import android.location.LocationListener; 8import android.location.LocationManager; 9import android.location.LocationProvider; 10import android.os.Bundle; 11 12import androidx.annotation.NonNull; 13import androidx.annotation.Nullable; 14import androidx.core.content.ContextCompat; 15import androidx.lifecycle.DefaultLifecycleObserver; 16import androidx.lifecycle.LifecycleOwner; 17 18import org.signal.core.util.logging.Log; 19import org.tm.archive.util.ServiceUtil; 20 21/** 22 * A lifecycle-safe way to retrieve a single location update. If a cached location is available, 23 * we'll use that. Otherwise we'll listen for one. 24 */ 25class LocationRetriever implements DefaultLifecycleObserver, LocationListener { 26 27 private static final String TAG = Log.tag(LocationRetriever.class); 28 29 private final Context context; 30 private final LocationManager locationManager; 31 private final SuccessListener successListener; 32 private final FailureListener failureListener; 33 34 LocationRetriever(@NonNull Context context, @NonNull LifecycleOwner lifecycleOwner, @NonNull SuccessListener successListener, @NonNull FailureListener failureListener) { 35 this.context = context; 36 this.locationManager = ServiceUtil.getLocationManager(context); 37 this.successListener = successListener; 38 this.failureListener = failureListener; 39 40 lifecycleOwner.getLifecycle().addObserver(this); 41 } 42 43 @Override 44 public void onStart(@NonNull LifecycleOwner owner) { 45 if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && 46 ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) 47 { 48 Log.w(TAG, "No location permission!"); 49 failureListener.onFailure(); 50 } 51 52 LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER); 53 54 if (provider == null) { 55 Log.w(TAG, "GPS provider is null. Trying network provider."); 56 provider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER); 57 } 58 59 if (provider == null) { 60 Log.w(TAG, "Network provider is null. Unable to retrieve location."); 61 failureListener.onFailure(); 62 return; 63 } 64 65 Location lastKnown = locationManager.getLastKnownLocation(provider.getName()); 66 67 if (lastKnown != null) { 68 Log.i(TAG, "Using last known location."); 69 successListener.onSuccess(lastKnown); 70 } else { 71 Log.i(TAG, "No last known location. Requesting a single update."); 72 locationManager.requestSingleUpdate(provider.getName(), this, null); 73 } 74 } 75 76 @Override 77 public void onStop(@NonNull LifecycleOwner owner) { 78 Log.i(TAG, "Removing any possible location listeners."); 79 locationManager.removeUpdates(this); 80 } 81 82 @Override 83 public void onLocationChanged(@Nullable Location location) { 84 if (location != null) { 85 Log.w(TAG, "[onLocationChanged] Successfully retrieved location."); 86 successListener.onSuccess(location); 87 } else { 88 Log.w(TAG, "[onLocationChanged] Null location."); 89 failureListener.onFailure(); 90 } 91 } 92 93 @Override 94 public void onStatusChanged(@NonNull String provider, int status, @Nullable Bundle extras) { 95 Log.i(TAG, "[onStatusChanged] Provider: " + provider + " Status: " + status); 96 } 97 98 @Override 99 public void onProviderEnabled(@NonNull String provider) { 100 Log.i(TAG, "[onProviderEnabled] Provider: " + provider); 101 } 102 103 @Override 104 public void onProviderDisabled(@NonNull String provider) { 105 Log.i(TAG, "[onProviderDisabled] Provider: " + provider); 106 } 107 108 interface SuccessListener { 109 void onSuccess(@NonNull Location location); 110 } 111 112 interface FailureListener { 113 void onFailure(); 114 } 115}