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

Commit 0d1890b0 authored by Soonil Nagarkar's avatar Soonil Nagarkar
Browse files

Make GnssStatus and GpsStatus testable

Allow clients to create GnssStatus and GpsStatus objects for test.

Bug: 144027538
Test: na
Change-Id: Ice3eb966a88e6de5bc8834b60b54bcac31bf31db
parent 2dbc22cb
Loading
Loading
Loading
Loading
+20 −12
Original line number Diff line number Diff line
@@ -23038,17 +23038,17 @@ package android.location {
  }
  public final class GnssStatus {
    method public float getAzimuthDegrees(int);
    method public float getCarrierFrequencyHz(int);
    method public float getCn0DbHz(int);
    method public int getConstellationType(int);
    method public float getElevationDegrees(int);
    method public int getSatelliteCount();
    method public int getSvid(int);
    method public boolean hasAlmanacData(int);
    method public boolean hasCarrierFrequencyHz(int);
    method public boolean hasEphemerisData(int);
    method public boolean usedInFix(int);
    method @FloatRange(from=0, to=360) public float getAzimuthDegrees(@IntRange(from=0) int);
    method @FloatRange(from=0) public float getCarrierFrequencyHz(@IntRange(from=0) int);
    method @FloatRange(from=0, to=63) public float getCn0DbHz(@IntRange(from=0) int);
    method public int getConstellationType(@IntRange(from=0) int);
    method @FloatRange(from=0xffffffa6, to=90) public float getElevationDegrees(@IntRange(from=0) int);
    method @IntRange(from=0) public int getSatelliteCount();
    method @IntRange(from=1, to=200) public int getSvid(@IntRange(from=0) int);
    method public boolean hasAlmanacData(@IntRange(from=0) int);
    method public boolean hasCarrierFrequencyHz(@IntRange(from=0) int);
    method public boolean hasEphemerisData(@IntRange(from=0) int);
    method public boolean usedInFix(@IntRange(from=0) int);
    field public static final int CONSTELLATION_BEIDOU = 5; // 0x5
    field public static final int CONSTELLATION_GALILEO = 6; // 0x6
    field public static final int CONSTELLATION_GLONASS = 3; // 0x3
@@ -23059,10 +23059,17 @@ package android.location {
    field public static final int CONSTELLATION_UNKNOWN = 0; // 0x0
  }
  public static final class GnssStatus.Builder {
    ctor public GnssStatus.Builder();
    method @NonNull public android.location.GnssStatus.Builder addSatellite(int, @IntRange(from=1, to=200) int, @FloatRange(from=0, to=63) float, @FloatRange(from=0xffffffa6, to=90) float, @FloatRange(from=0, to=360) float, boolean, boolean, boolean, boolean, @FloatRange(from=0) float);
    method @NonNull public android.location.GnssStatus build();
    method @NonNull public android.location.GnssStatus.Builder clearSatellites();
  }
  public abstract static class GnssStatus.Callback {
    ctor public GnssStatus.Callback();
    method public void onFirstFix(int);
    method public void onSatelliteStatusChanged(android.location.GnssStatus);
    method public void onSatelliteStatusChanged(@NonNull android.location.GnssStatus);
    method public void onStarted();
    method public void onStopped();
  }
@@ -23078,6 +23085,7 @@ package android.location {
  }
  @Deprecated public final class GpsStatus {
    method @Deprecated @NonNull public static android.location.GpsStatus create(@NonNull android.location.GnssStatus, int);
    method @Deprecated public int getMaxSatellites();
    method @Deprecated public Iterable<android.location.GpsSatellite> getSatellites();
    method @Deprecated public int getTimeToFirstFix();
+202 −75
Original line number Diff line number Diff line
@@ -16,15 +16,21 @@

package android.location;

import android.annotation.FloatRange;
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;

/**
 * This class represents the current state of the GNSS engine.
 * This class is used in conjunction with the {@link GnssStatus.Callback}.
 * This class represents the current state of the GNSS engine and is used in conjunction with
 * {@link GnssStatus.Callback}.
 *
 * @see LocationManager#registerGnssStatusCallback
 * @see GnssStatus.Callback
 */
public final class GnssStatus {

@@ -52,75 +58,88 @@ public final class GnssStatus {
    /** @hide */
    public static final int CONSTELLATION_COUNT = 8;

    /** @hide */
    public static final int GNSS_SV_FLAGS_NONE = 0;
    /** @hide */
    public static final int GNSS_SV_FLAGS_HAS_EPHEMERIS_DATA = (1 << 0);
    /** @hide */
    public static final int GNSS_SV_FLAGS_HAS_ALMANAC_DATA = (1 << 1);
    /** @hide */
    public static final int GNSS_SV_FLAGS_USED_IN_FIX = (1 << 2);
    /** @hide */
    public static final int GNSS_SV_FLAGS_HAS_CARRIER_FREQUENCY = (1 << 3);
    private static final int SVID_FLAGS_NONE = 0;
    private static final int SVID_FLAGS_HAS_EPHEMERIS_DATA = (1 << 0);
    private static final int SVID_FLAGS_HAS_ALMANAC_DATA = (1 << 1);
    private static final int SVID_FLAGS_USED_IN_FIX = (1 << 2);
    private static final int SVID_FLAGS_HAS_CARRIER_FREQUENCY = (1 << 3);

    /** @hide */
    public static final int SVID_SHIFT_WIDTH = 8;
    /** @hide */
    public static final int CONSTELLATION_TYPE_SHIFT_WIDTH = 4;
    /** @hide */
    public static final int CONSTELLATION_TYPE_MASK = 0xf;
    private static final int SVID_SHIFT_WIDTH = 8;
    private static final int CONSTELLATION_TYPE_SHIFT_WIDTH = 4;
    private static final int CONSTELLATION_TYPE_MASK = 0xf;

    /**
     * Used for receiving notifications when GNSS events happen.
     *
     * @see LocationManager#registerGnssStatusCallback
     */
    public static abstract class Callback {
        /**
         * Called when GNSS system has started.
         */
        public void onStarted() {}
        public void onStarted() {
        }

        /**
         * Called when GNSS system has stopped.
         */
        public void onStopped() {}
        public void onStopped() {
        }

        /**
         * Called when the GNSS system has received its first fix since starting.
         *
         * @param ttffMillis the time from start to first fix in milliseconds.
         */
        public void onFirstFix(int ttffMillis) {}
        public void onFirstFix(int ttffMillis) {
        }

        /**
         * Called periodically to report GNSS satellite status.
         *
         * @param status the current status of all satellites.
         */
        public void onSatelliteStatusChanged(GnssStatus status) {}
        public void onSatelliteStatusChanged(@NonNull GnssStatus status) {
        }
    }

    /**
     * Constellation type.
     *
     * @hide
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({CONSTELLATION_UNKNOWN, CONSTELLATION_GPS, CONSTELLATION_SBAS, CONSTELLATION_GLONASS,
            CONSTELLATION_QZSS, CONSTELLATION_BEIDOU, CONSTELLATION_GALILEO, CONSTELLATION_IRNSS})
    public @interface ConstellationType {}

    final int[] mSvidWithFlags;
    final float[] mCn0DbHz;
    final float[] mElevations;
    final float[] mAzimuths;
    final int mSvCount;
    final float[] mCarrierFrequencies;
    public @interface ConstellationType {
    }

    /**
     * Create a GnssStatus that wraps the given arguments without any additional overhead. Callers
     * are responsible for guaranteeing that the arguments are never modified after calling this
     * method.
     *
     * @hide
     */
    public GnssStatus(int svCount, int[] svidWithFlags, float[] cn0s, float[] elevations,
    @NonNull
    public static GnssStatus wrap(int svCount, int[] svidWithFlags, float[] cn0DbHzs,
            float[] elevations, float[] azimuths, float[] carrierFrequencies) {
        return new GnssStatus(svCount, svidWithFlags, cn0DbHzs, elevations, azimuths,
                carrierFrequencies);
    }

    private final int mSvCount;
    private final int[] mSvidWithFlags;
    private final float[] mCn0DbHzs;
    private final float[] mElevations;
    private final float[] mAzimuths;
    private final float[] mCarrierFrequencies;

    private GnssStatus(int svCount, int[] svidWithFlags, float[] cn0DbHzs, float[] elevations,
            float[] azimuths, float[] carrierFrequencies) {
        mSvCount = svCount;
        mSvidWithFlags = svidWithFlags;
        mCn0DbHz = cn0s;
        mCn0DbHzs = cn0DbHzs;
        mElevations = elevations;
        mAzimuths = azimuths;
        mCarrierFrequencies = carrierFrequencies;
@@ -129,6 +148,7 @@ public final class GnssStatus {
    /**
     * Gets the total number of satellites in satellite list.
     */
    @IntRange(from = 0)
    public int getSatelliteCount() {
        return mSvCount;
    }
@@ -136,11 +156,11 @@ public final class GnssStatus {
    /**
     * Retrieves the constellation type of the satellite at the specified index.
     *
     * @param satIndex the index of the satellite in the list.
     * @param satelliteIndex An index from zero to {@link #getSatelliteCount()} - 1
     */
    @ConstellationType
    public int getConstellationType(int satIndex) {
        return ((mSvidWithFlags[satIndex] >> CONSTELLATION_TYPE_SHIFT_WIDTH)
    public int getConstellationType(@IntRange(from = 0) int satelliteIndex) {
        return ((mSvidWithFlags[satelliteIndex] >> CONSTELLATION_TYPE_SHIFT_WIDTH)
                & CONSTELLATION_TYPE_MASK);
    }

@@ -167,101 +187,104 @@ public final class GnssStatus {
     * <li>Beidou: 1-37</li>
     * </ul>
     *
     * @param satIndex the index of the satellite in the list.
     * @param satelliteIndex An index from zero to {@link #getSatelliteCount()} - 1
     */
    public int getSvid(int satIndex) {
        return mSvidWithFlags[satIndex] >> SVID_SHIFT_WIDTH;
    @IntRange(from = 1, to = 200)
    public int getSvid(@IntRange(from = 0) int satelliteIndex) {
        return mSvidWithFlags[satelliteIndex] >> SVID_SHIFT_WIDTH;
    }

    /**
     * Retrieves the carrier-to-noise density at the antenna of the satellite at the specified index
     * in dB-Hz.
     *
     * @param satIndex the index of the satellite in the list.
     * @param satelliteIndex An index from zero to {@link #getSatelliteCount()} - 1
     */
    public float getCn0DbHz(int satIndex) {
        return mCn0DbHz[satIndex];
    @FloatRange(from = 0, to = 63)
    public float getCn0DbHz(@IntRange(from = 0) int satelliteIndex) {
        return mCn0DbHzs[satelliteIndex];
    }

    /**
     * Retrieves the elevation of the satellite at the specified index.
     *
     * @param satIndex the index of the satellite in the list.
     * @param satelliteIndex An index from zero to {@link #getSatelliteCount()} - 1
     */
    public float getElevationDegrees(int satIndex) {
        return mElevations[satIndex];
    @FloatRange(from = -90, to = 90)
    public float getElevationDegrees(@IntRange(from = 0) int satelliteIndex) {
        return mElevations[satelliteIndex];
    }

    /**
     * Retrieves the azimuth the satellite at the specified index.
     *
     * @param satIndex the index of the satellite in the list.
     * @param satelliteIndex An index from zero to {@link #getSatelliteCount()} - 1
     */
    public float getAzimuthDegrees(int satIndex) {
        return mAzimuths[satIndex];
    @FloatRange(from = 0, to = 360)
    public float getAzimuthDegrees(@IntRange(from = 0) int satelliteIndex) {
        return mAzimuths[satelliteIndex];
    }

    /**
     * Reports whether the satellite at the specified index has ephemeris data.
     *
     * @param satIndex the index of the satellite in the list.
     * @param satelliteIndex An index from zero to {@link #getSatelliteCount()} - 1
     */
    public boolean hasEphemerisData(int satIndex) {
        return (mSvidWithFlags[satIndex] & GNSS_SV_FLAGS_HAS_EPHEMERIS_DATA) != 0;
    public boolean hasEphemerisData(@IntRange(from = 0) int satelliteIndex) {
        return (mSvidWithFlags[satelliteIndex] & SVID_FLAGS_HAS_EPHEMERIS_DATA) != 0;
    }

    /**
     * Reports whether the satellite at the specified index has almanac data.
     *
     * @param satIndex the index of the satellite in the list.
     * @param satelliteIndex An index from zero to {@link #getSatelliteCount()} - 1
     */
    public boolean hasAlmanacData(int satIndex) {
        return (mSvidWithFlags[satIndex] & GNSS_SV_FLAGS_HAS_ALMANAC_DATA) != 0;
    public boolean hasAlmanacData(@IntRange(from = 0) int satelliteIndex) {
        return (mSvidWithFlags[satelliteIndex] & SVID_FLAGS_HAS_ALMANAC_DATA) != 0;
    }

    /**
     * Reports whether the satellite at the specified index was used in the calculation of the most
     * recent position fix.
     *
     * @param satIndex the index of the satellite in the list.
     * @param satelliteIndex An index from zero to {@link #getSatelliteCount()} - 1
     */
    public boolean usedInFix(int satIndex) {
        return (mSvidWithFlags[satIndex] & GNSS_SV_FLAGS_USED_IN_FIX) != 0;
    public boolean usedInFix(@IntRange(from = 0) int satelliteIndex) {
        return (mSvidWithFlags[satelliteIndex] & SVID_FLAGS_USED_IN_FIX) != 0;
    }

    /**
     * Reports whether a valid {@link #getCarrierFrequencyHz(int satIndex)} is available.
     * Reports whether a valid {@link #getCarrierFrequencyHz(int satelliteIndex)} is available.
     *
     * @param satIndex the index of the satellite in the list.
     * @param satelliteIndex An index from zero to {@link #getSatelliteCount()} - 1
     */
    public boolean hasCarrierFrequencyHz(int satIndex) {
        return (mSvidWithFlags[satIndex] & GNSS_SV_FLAGS_HAS_CARRIER_FREQUENCY) != 0;
    public boolean hasCarrierFrequencyHz(@IntRange(from = 0) int satelliteIndex) {
        return (mSvidWithFlags[satelliteIndex] & SVID_FLAGS_HAS_CARRIER_FREQUENCY) != 0;
    }

    /**
     * Gets the carrier frequency of the signal tracked.
     *
     * <p>For example it can be the GPS central frequency for L1 = 1575.45 MHz, or L2 = 1227.60 MHz,
     * L5 = 1176.45 MHz, varying GLO channels, etc. If the field is not set, it is the primary
     * <p>For example it can be the GPS central frequency for L1 = 1575.45 MHz, or L2 = 1227.60
     * MHz, L5 = 1176.45 MHz, varying GLO channels, etc. If the field is not set, it is the primary
     * common use central frequency, e.g. L1 = 1575.45 MHz for GPS.
     *
     * For an L1, L5 receiver tracking a satellite on L1 and L5 at the same time, two measurements
     * will be reported for this same satellite, in one all the values related to L1 will be filled,
     * and in the other all of the values related to L5 will be filled.
     * will be reported for this same satellite, in one all the values related to L1 will be
     * filled, and in the other all of the values related to L5 will be filled.
     *
     * <p>The value is only available if {@link #hasCarrierFrequencyHz(int satIndex)} is {@code true}.
     * <p>The value is only available if {@link #hasCarrierFrequencyHz(int satelliteIndex)} is
     * {@code true}.
     *
     * @param satIndex the index of the satellite in the list.
     *
     * @return the carrier frequency of the signal tracked in Hz.
     * @param satelliteIndex An index from zero to {@link #getSatelliteCount()} - 1
     */
    public float getCarrierFrequencyHz(int satIndex) {
        return mCarrierFrequencies[satIndex];
    @FloatRange(from = 0)
    public float getCarrierFrequencyHz(@IntRange(from = 0) int satelliteIndex) {
        return mCarrierFrequencies[satelliteIndex];
    }

    /**
     * Returns the string representation of a constellation type. For example,
     * {@link #CONSTELLATION_GPS} is represented by the string GPS.
     * Returns the string representation of a constellation type.
     *
     * @param constellationType the constellation type.
     * @return the string representation.
@@ -290,4 +313,108 @@ public final class GnssStatus {
                return Integer.toString(constellationType);
        }
    }

    /**
     * Builder class to help create new GnssStatus instances.
     */
    public static final class Builder {

        private final ArrayList<GnssSvInfo> mSatellites = new ArrayList<>();

        /**
         * Adds a new satellite to the Builder.
         *
         * @param constellationType one of the CONSTELLATION_* constants
         * @param svid the space vehicle identifier
         * @param cn0DbHz carrier-to-noise density at the antenna in dB-Hz
         * @param elevation satellite elevation in degrees
         * @param azimuth satellite azimuth in degrees
         * @param hasEphemeris whether the satellite has ephemeris data
         * @param hasAlmanac whether the satellite has almanac data
         * @param usedInFix whether the satellite was used in the most recent location fix
         * @param hasCarrierFrequency whether carrier frequency data is available
         * @param carrierFrequency satellite carrier frequency in Hz
         */
        @NonNull
        public Builder addSatellite(@ConstellationType int constellationType,
                @IntRange(from = 1, to = 200) int svid,
                @FloatRange(from = 0, to = 63) float cn0DbHz,
                @FloatRange(from = -90, to = 90) float elevation,
                @FloatRange(from = 0, to = 360) float azimuth,
                boolean hasEphemeris,
                boolean hasAlmanac,
                boolean usedInFix,
                boolean hasCarrierFrequency,
                @FloatRange(from = 0) float carrierFrequency) {
            mSatellites.add(new GnssSvInfo(constellationType, svid, cn0DbHz, elevation, azimuth,
                    hasEphemeris, hasAlmanac, usedInFix, hasCarrierFrequency, carrierFrequency));
            return this;
        }

        /**
         * Clears all satellites in the Builder.
         */
        @NonNull
        public Builder clearSatellites() {
            mSatellites.clear();
            return this;
        }

        /**
         * Builds a new GnssStatus based on the satellite information in the Builder.
         */
        @NonNull
        public GnssStatus build() {
            int svCount = mSatellites.size();
            int[] svidWithFlags = new int[svCount];
            float[] cn0DbHzs = new float[svCount];
            float[] elevations = new float[svCount];
            float[] azimuths = new float[svCount];
            float[] carrierFrequencies = new float[svCount];

            for (int i = 0; i < svidWithFlags.length; i++) {
                svidWithFlags[i] = mSatellites.get(i).mSvidWithFlags;
            }
            for (int i = 0; i < cn0DbHzs.length; i++) {
                cn0DbHzs[i] = mSatellites.get(i).mCn0DbHz;
            }
            for (int i = 0; i < elevations.length; i++) {
                elevations[i] = mSatellites.get(i).mElevation;
            }
            for (int i = 0; i < azimuths.length; i++) {
                azimuths[i] = mSatellites.get(i).mAzimuth;
            }
            for (int i = 0; i < carrierFrequencies.length; i++) {
                carrierFrequencies[i] = mSatellites.get(i).mCarrierFrequency;
            }

            return wrap(svCount, svidWithFlags, cn0DbHzs, elevations, azimuths,
                    carrierFrequencies);
        }
    }

    private static class GnssSvInfo {

        private final int mSvidWithFlags;
        private final float mCn0DbHz;
        private final float mElevation;
        private final float mAzimuth;
        private final float mCarrierFrequency;

        private GnssSvInfo(int constellationType, int svid, float cn0DbHz,
                float elevation, float azimuth, boolean hasEphemeris, boolean hasAlmanac,
                boolean usedInFix, boolean hasCarrierFrequency, float carrierFrequency) {
            mSvidWithFlags = (svid << SVID_SHIFT_WIDTH)
                    | ((constellationType & CONSTELLATION_TYPE_MASK)
                    << CONSTELLATION_TYPE_SHIFT_WIDTH)
                    | (hasEphemeris ? SVID_FLAGS_HAS_EPHEMERIS_DATA : SVID_FLAGS_NONE)
                    | (hasAlmanac ? SVID_FLAGS_HAS_ALMANAC_DATA : SVID_FLAGS_NONE)
                    | (usedInFix ? SVID_FLAGS_USED_IN_FIX : SVID_FLAGS_NONE)
                    | (hasCarrierFrequency ? SVID_FLAGS_HAS_CARRIER_FREQUENCY : SVID_FLAGS_NONE);
            mCn0DbHz = cn0DbHz;
            mElevation = elevation;
            mAzimuth = azimuth;
            mCarrierFrequency = carrierFrequency;
        }
    }
}
+49 −67

File changed.

Preview size limit exceeded, changes collapsed.

+7 −8
Original line number Diff line number Diff line
@@ -1818,16 +1818,15 @@ public class LocationManager {
            Log.w(TAG, ex);
        }

        if (status == null) {
            status = new GpsStatus();
        }
        // When mGnssStatus is null, that means that this method is called outside
        // onGpsStatusChanged().  Return an empty status to maintain backwards compatibility.
        GnssStatus gnssStatus = mGnssStatusListenerManager.getGnssStatus();
        int ttff = mGnssStatusListenerManager.getTtff();
        if (gnssStatus != null) {
            if (status == null) {
                status = GpsStatus.create(gnssStatus, ttff);
            } else {
                status.setStatus(gnssStatus, ttff);
            }
        }
        return status;
    }

@@ -2783,8 +2782,8 @@ public class LocationManager {
            @Override
            public void onSvStatusChanged(int svCount, int[] svidWithFlags, float[] cn0s,
                    float[] elevations, float[] azimuths, float[] carrierFreqs) {
                GnssStatus localStatus = new GnssStatus(svCount, svidWithFlags, cn0s, elevations,
                        azimuths, carrierFreqs);
                GnssStatus localStatus = GnssStatus.wrap(svCount, svidWithFlags, cn0s,
                        elevations, azimuths, carrierFreqs);
                mGnssStatus = localStatus;
                execute((callback) -> callback.onSatelliteStatusChanged(localStatus));
            }
+5 −14
Original line number Diff line number Diff line
@@ -188,22 +188,18 @@ public class GnssMetrics {

    /**
    * Logs sv status data
    *
    * @param svCount
    * @param svidWithFlags
    * @param svCarrierFreqs
    */
    public void logSvStatus(int svCount, int[] svidWithFlags, float[] svCarrierFreqs) {
    public void logSvStatus(GnssStatus status) {
        boolean isL5 = false;
        // Calculate SvStatus Information
        for (int i = 0; i < svCount; i++) {
            if ((svidWithFlags[i] & GnssStatus.GNSS_SV_FLAGS_HAS_CARRIER_FREQUENCY) != 0) {
        for (int i = 0; i < status.getSatelliteCount(); i++) {
            if (status.hasCarrierFrequencyHz(i)) {
                mNumSvStatus++;
                isL5 = isL5Sv(svCarrierFreqs[i]);
                isL5 = isL5Sv(status.getCarrierFrequencyHz(i));
                if (isL5) {
                    mNumL5SvStatus++;
                }
                if ((svidWithFlags[i] & GnssStatus.GNSS_SV_FLAGS_USED_IN_FIX) != 0) {
                if (status.usedInFix(i)) {
                    mNumSvStatusUsedInFix++;
                    if (isL5) {
                        mNumL5SvStatusUsedInFix++;
@@ -211,15 +207,10 @@ public class GnssMetrics {
                }
            }
        }
        return;
    }

    /**
    * Logs CN0 when at least 4 SVs are available L5 Only
    *
    * @param svCount
    * @param cn0s
    * @param svCarrierFreqs
    */
    private void logCn0L5(int svCount, float[] cn0s, float[] svCarrierFreqs) {
        if (svCount == 0 || cn0s == null || cn0s.length == 0 || cn0s.length < svCount
Loading