Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9da3cf12 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Use fused provider for Android >= 12

parent 33884b8a
Loading
Loading
Loading
Loading
+11 −37
Original line number Diff line number Diff line
@@ -6,13 +6,13 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.SystemClock;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.location.LocationManagerCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.google.gson.JsonArray;
@@ -24,10 +24,12 @@ import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Locale;
import java.util.concurrent.Executors;

import foundation.e.blisslauncher.R;
import foundation.e.blisslauncher.core.Preferences;
import foundation.e.blisslauncher.features.weather.location.FusedLocationFetcher;
import foundation.e.blisslauncher.features.weather.location.LocationFetcher;
import foundation.e.blisslauncher.features.weather.location.NetworkGpsLocationFetcher;
import lineageos.weather.LineageWeatherManager;
import lineageos.weather.WeatherInfo;
import lineageos.weather.WeatherLocation;
@@ -44,10 +46,7 @@ public class WeatherUpdater {
    private static final String TAG = "WeatherUpdater";
    private static final long DEFAULT_FORCE_REQUEST_PERIOD_IN_MS = 10L * 1000L;

    private final LocationManager mLocationManager;
    private final WeakReference<Context> mWeakContext;
    private Location mGpsLocation;
    private Location mNetworkLocation;
    private long mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS;

    private static WeatherUpdater mInstance = null;
@@ -62,7 +61,6 @@ public class WeatherUpdater {

    private WeatherUpdater(@NonNull Context context) {
        mWeakContext = new WeakReference<>(context);
        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    public void checkWeatherRequest() {
@@ -123,11 +121,11 @@ public class WeatherUpdater {
            return;
        }

        LocationManagerCompat.getCurrentLocation(mLocationManager, LocationManager.GPS_PROVIDER, null,
                Executors.newFixedThreadPool(1), this::onNewLocationFetched);
        LocationFetcher locationFetcher = VERSION.SDK_INT >= VERSION_CODES.S
                ? new FusedLocationFetcher(mWeakContext.get(), this::onNewLocationFetched)
                : new NetworkGpsLocationFetcher(mWeakContext.get(), this::onNewLocationFetched);

        LocationManagerCompat.getCurrentLocation(mLocationManager, LocationManager.NETWORK_PROVIDER, null,
                Executors.newFixedThreadPool(1), this::onNewLocationFetched);
        locationFetcher.fetchLocation();
    }

    protected boolean hasMissingPermissions() {
@@ -166,16 +164,10 @@ public class WeatherUpdater {

        Timber.tag(TAG).i("New location fetched:%s", location);

        if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
            mGpsLocation = location;
        } else if (location.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
            mNetworkLocation = location;
        }

        requestWeatherUpdate(getMostRecentLocation());
        requestWeatherUpdate(location);

        if (!Preferences.useCustomWeatherLocation(mWeakContext.get())) {
            reverseGeocodeLocation(getMostRecentLocation());
            reverseGeocodeLocation(location);
        } else {
            Timber.tag(TAG).w("Do not reverse geocode location. User is using a custom location.");
        }
@@ -198,24 +190,6 @@ public class WeatherUpdater {
        mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS;
    }

    private Location getMostRecentLocation() {
        if (mNetworkLocation == null && mGpsLocation == null) {
            throw new IllegalStateException();
        }

        if (mGpsLocation == null) {
            return mNetworkLocation;
        }

        if (mNetworkLocation == null) {
            return mGpsLocation;
        }

        long gpsTime = mGpsLocation.getTime();
        long networkTime = mNetworkLocation.getTime();
        return gpsTime >= networkTime ? mGpsLocation : mNetworkLocation;
    }

    private void reverseGeocodeLocation(@NonNull Location location) {
        Timber.tag(TAG).i("Reverse geocoding location %s", location);

+35 −0
Original line number Diff line number Diff line
package foundation.e.blisslauncher.features.weather.location;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.location.LocationManagerCompat;

import java.util.concurrent.Executors;

public class FusedLocationFetcher extends LocationFetcher {

    public FusedLocationFetcher(@NonNull Context context, @NonNull Callback callback) {
        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        mCallback = callback;
    }

    @Override
    @SuppressLint("MissingPermission")
    public void fetchLocation() {
        if (VERSION.SDK_INT >= VERSION_CODES.S) {
            LocationManagerCompat.getCurrentLocation(mLocationManager, LocationManager.FUSED_PROVIDER, null,
                    Executors.newFixedThreadPool(1), this::onLocationFetched);
        }
    }

    private void onLocationFetched(@Nullable Location location) {
        mCallback.onNewLocation(location);
    }
}
+18 −0
Original line number Diff line number Diff line
package foundation.e.blisslauncher.features.weather.location;

import android.location.Location;
import android.location.LocationManager;

import androidx.annotation.Nullable;

public abstract class LocationFetcher {

    protected LocationManager mLocationManager;
    protected Callback mCallback;

    public interface Callback {
        void onNewLocation(@Nullable Location location);
    }

    public abstract void fetchLocation();
}
+67 −0
Original line number Diff line number Diff line
package foundation.e.blisslauncher.features.weather.location;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.location.LocationManagerCompat;

import java.util.concurrent.Executors;

public class NetworkGpsLocationFetcher extends LocationFetcher {

    private Location gpsLocation;
    private Location networkLocation;

    public NetworkGpsLocationFetcher(@NonNull Context context, @NonNull Callback callback) {
        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        mCallback = callback;
    }

    @Override
    @SuppressLint("MissingPermission")
    public void fetchLocation() {
        LocationManagerCompat.getCurrentLocation(mLocationManager, LocationManager.GPS_PROVIDER, null,
                Executors.newFixedThreadPool(1), this::onLocationFetched);

        LocationManagerCompat.getCurrentLocation(mLocationManager, LocationManager.NETWORK_PROVIDER, null,
                Executors.newFixedThreadPool(1), this::onLocationFetched);
    }

    private void onLocationFetched(@Nullable Location location) {

        if (location == null) {
            return;
        }

        if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
            gpsLocation = location;
        } else if (location.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
            networkLocation = location;
        }

        mCallback.onNewLocation(getMostRecentLocation());
    }

    private Location getMostRecentLocation() {
        if (networkLocation == null && gpsLocation == null) {
            throw new IllegalStateException();
        }

        if (gpsLocation == null) {
            return networkLocation;
        }

        if (networkLocation == null) {
            return gpsLocation;
        }

        long gpsTime = gpsLocation.getTime();
        long networkTime = networkLocation.getTime();
        return gpsTime >= networkTime ? gpsLocation : networkLocation;
    }

}