| 6 | import android.content.pm.PackageManager; |
| 7 | import android.location.Location; |
| 8 | import android.os.Build; |
| 9 | import android.os.Looper; |
| 10 | import android.util.Pair; |
| 11 |
| 12 | import androidx.annotation.NonNull; |
| 13 |
| 14 | import com.google.android.gms.location.LocationCallback; |
| 15 | import com.google.android.gms.location.LocationRequest; |
| 16 | import com.google.android.gms.location.LocationResult; |
| 17 | import com.google.android.gms.location.LocationServices; |
| 18 | import com.google.android.gms.maps.LocationSource; |
| 19 |
| 20 | import org.telegram.messenger.BuildVars; |
| 21 | import org.telegram.messenger.FileLog; |
| 22 |
| 23 | import java.util.Collections; |
| 24 | import java.util.LinkedHashMap; |
| 25 | import java.util.Locale; |
| 26 | import java.util.Set; |
| 27 |
| 28 | import tw.nekomimi.nekogram.location.GeodeticTransform; |
| 29 |
| 30 | public class NekoLocationSource implements LocationSource { |
| 31 | public final static Set<Integer> recent = Collections.synchronizedSet(Collections.newSetFromMap(new Cache<>())); |
| 32 | private boolean checkPermission = true; |
| 33 | private final Context context; |
| 34 | private OnLocationChangedListener onLocationChangedListener; |
| 35 | private final LocationCallback callback = new LocationCallback() { |
| 36 | @Override |
| 37 | public void onLocationResult(@NonNull LocationResult locationResult) { |
| 38 | super.onLocationResult(locationResult); |
| 39 | if (onLocationChangedListener != null) { |
| 40 | Location location = locationResult.getLastLocation(); |
| 41 | transform(location); |
| 42 | onLocationChangedListener.onLocationChanged(location); |
| 43 | } |
| 44 | } |
| 45 | }; |
| 46 |
| 47 | public NekoLocationSource(Context context) { |
| 48 | this.context = context; |
| 49 | } |
| 50 |
| 51 | public static void transform(Location location) { |
| 52 | final double latitude = location.getLatitude(); |
| 53 | final double longitude = location.getLongitude(); |
| 54 |
| 55 | if (recent.contains(new Pair<>(latitude, longitude).hashCode())) return;// Dejavu |
| 56 |
| 57 | final Pair<Double, Double> trans = GeodeticTransform.transform(latitude, longitude); |
| 58 | location.setLatitude(trans.first); |
| 59 | location.setLongitude(trans.second); |
| 60 |
| 61 | recent.add(trans.hashCode()); |
| 62 |
| 63 | if (BuildVars.LOGS_ENABLED) { |
| 64 | FileLog.d(String.format(Locale.US, "%.4f,%.4f => %.4f,%.4f", latitude, longitude, trans.first, trans.second)); |
| 65 | } |
| 66 | } |
| 67 |
| 68 | @Override |
| 69 | public void activate(@NonNull OnLocationChangedListener onLocationChangedListener) { |
| 70 | if (checkPermission && Build.VERSION.SDK_INT >= 23) { |
| 71 | if (context instanceof Activity) { |
| 72 | checkPermission = false; |
| 73 | Activity activity = (Activity) context; |
| 74 | if (activity.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { |
| 75 | activity.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 2); |
| 76 | return; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | LocationRequest locationRequest = LocationRequest.create(); |
| 81 | locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); |
| 82 | locationRequest.setInterval(0); |
| 83 | locationRequest.setFastestInterval(0); |
| 84 | LocationServices.getFusedLocationProviderClient(context).requestLocationUpdates(locationRequest, callback, Looper.getMainLooper()); |
| 85 | this.onLocationChangedListener = onLocationChangedListener; |
| 86 | } |
| 87 |
| 88 | @Override |
| 89 | public void deactivate() { |
| 90 | LocationServices.getFusedLocationProviderClient(context).removeLocationUpdates(callback); |
| 91 | } |
| 92 |
| 93 | static class Cache<K, V> extends LinkedHashMap<K, V> { |
| 94 |
| 95 | private static final int KMaxEntries = 128; |
| 96 | private static final long serialVersionUID = 1L; |
| 97 |
| 98 | @Override |
| 99 | protected boolean removeEldestEntry(final Entry<K, V> eldest) { |
| 100 | return (size() > KMaxEntries); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 |