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

Commit ffc0b6e7 authored by Soonil Nagarkar's avatar Soonil Nagarkar
Browse files

Adds LastLocationRequest SystemApi

Creates a new LastLocationRequest object similar to LocationRequest,
specifically for useage with the getLastLocation() APIs. This allows
SystemApi clients to pass in important parameters like
locationSettingsIgnored, etc...

Bug: 173666111
Test: manual + presubmits
Change-Id: I2e0253e5275d9d17cfb5a9243de0ef12ab33348c
parent 1681b437
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -4106,6 +4106,22 @@ package android.location {
    method @Deprecated public void onStatusChanged(int);
  }
  public final class LastLocationRequest implements android.os.Parcelable {
    method public int describeContents();
    method public boolean isHiddenFromAppOps();
    method public boolean isLocationSettingsIgnored();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.location.LastLocationRequest> CREATOR;
  }
  public static final class LastLocationRequest.Builder {
    ctor public LastLocationRequest.Builder();
    ctor public LastLocationRequest.Builder(@NonNull android.location.LastLocationRequest);
    method @NonNull public android.location.LastLocationRequest build();
    method @NonNull @RequiresPermission(android.Manifest.permission.UPDATE_APP_OPS_STATS) public android.location.LastLocationRequest.Builder setHiddenFromAppOps(boolean);
    method @NonNull @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public android.location.LastLocationRequest.Builder setLocationSettingsIgnored(boolean);
  }
  public class Location implements android.os.Parcelable {
    method public boolean isComplete();
    method public void makeComplete();
@@ -4118,6 +4134,7 @@ package android.location {
    method @Deprecated @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void getCurrentLocation(@NonNull android.location.LocationRequest, @Nullable android.os.CancellationSignal, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.location.Location>);
    method @Nullable public String getExtraLocationControllerPackage();
    method @Deprecated public int getGnssBatchSize();
    method @Nullable @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public android.location.Location getLastKnownLocation(@NonNull String, @NonNull android.location.LastLocationRequest);
    method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public void injectGnssMeasurementCorrections(@NonNull android.location.GnssMeasurementCorrections);
    method public boolean isExtraLocationControllerPackageEnabled();
    method public boolean isLocationEnabledForUser(@NonNull android.os.UserHandle);
+2 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.location.IGnssStatusListener;
import android.location.IGnssNavigationMessageListener;
import android.location.ILocationCallback;
import android.location.ILocationListener;
import android.location.LastLocationRequest;
import android.location.Location;
import android.location.LocationRequest;
import android.location.LocationTime;
@@ -45,7 +46,7 @@ import com.android.internal.location.ProviderProperties;
 */
interface ILocationManager
{
    @nullable Location getLastLocation(String provider, String packageName, String attributionTag);
    @nullable Location getLastLocation(String provider, in LastLocationRequest request, String packageName, String attributionTag);
    @nullable ICancellationSignal getCurrentLocation(String provider, in LocationRequest request, in ILocationCallback callback, String packageName, String attributionTag, String listenerId);

    void registerLocationListener(String provider, in LocationRequest request, in ILocationListener listener, String packageName, String attributionTag, String listenerId);
+19 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.location;

parcelable LastLocationRequest;
+192 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.location;

import android.Manifest;
import android.annotation.NonNull;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.Objects;

/**
 * An encapsulation of various parameters for requesting last location via {@link LocationManager}.
 *
 * @hide
 */
@SystemApi
public final class LastLocationRequest implements Parcelable {

    private final boolean mHiddenFromAppOps;
    private final boolean mLocationSettingsIgnored;

    private LastLocationRequest(
            boolean hiddenFromAppOps,
            boolean locationSettingsIgnored) {
        mHiddenFromAppOps = hiddenFromAppOps;
        mLocationSettingsIgnored = locationSettingsIgnored;
    }

    /**
     * Returns true if this last location request should be ignored while updating app ops with
     * location usage. This implies that someone else (usually the creator of the last location
     * request) is responsible for updating app ops.
     *
     * @return true if this request should be ignored while updating app ops with location usage
     *
     */
    public boolean isHiddenFromAppOps() {
        return mHiddenFromAppOps;
    }

    /**
     * Returns true if location settings, throttling, background location limits, and any other
     * possible limiting factors will be ignored in order to satisfy this last location request.
     *
     * @return true if all limiting factors will be ignored to satisfy this request
     */
    public boolean isLocationSettingsIgnored() {
        return mLocationSettingsIgnored;
    }

    public static final @NonNull Parcelable.Creator<LastLocationRequest> CREATOR =
            new Parcelable.Creator<LastLocationRequest>() {
                @Override
                public LastLocationRequest createFromParcel(Parcel in) {
                    return new LastLocationRequest(
                            /* hiddenFromAppOps= */ in.readBoolean(),
                            /* locationSettingsIgnored= */ in.readBoolean());
                }
                @Override
                public LastLocationRequest[] newArray(int size) {
                    return new LastLocationRequest[size];
                }
            };
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull Parcel parcel, int flags) {
        parcel.writeBoolean(mHiddenFromAppOps);
        parcel.writeBoolean(mLocationSettingsIgnored);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        LastLocationRequest that = (LastLocationRequest) o;
        return mHiddenFromAppOps == that.mHiddenFromAppOps
                && mLocationSettingsIgnored == that.mLocationSettingsIgnored;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mHiddenFromAppOps, mLocationSettingsIgnored);
    }

    @NonNull
    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append("LastLocationRequest[");
        if (mHiddenFromAppOps) {
            s.append("hiddenFromAppOps, ");
        }
        if (mLocationSettingsIgnored) {
            s.append("locationSettingsIgnored, ");
        }
        if (s.length() > "LastLocationRequest[".length()) {
            s.setLength(s.length() - 2);
        }
        s.append(']');
        return s.toString();
    }

    /**
     * A builder class for {@link LastLocationRequest}.
     */
    public static final class Builder {

        private boolean mHiddenFromAppOps;
        private boolean mLocationSettingsIgnored;

        /**
         * Creates a new Builder.
         */
        public Builder() {
            mHiddenFromAppOps = false;
            mLocationSettingsIgnored = false;
        }

        /**
         * Creates a new Builder with all parameters copied from the given last location request.
         */
        public Builder(@NonNull LastLocationRequest lastLocationRequest) {
            mHiddenFromAppOps = lastLocationRequest.mHiddenFromAppOps;
            mLocationSettingsIgnored = lastLocationRequest.mLocationSettingsIgnored;
        }

        /**
         * If set to true, indicates that app ops should not be updated with location usage due to
         * this request. This implies that someone else (usually the creator of the last location
         * request) is responsible for updating app ops as appropriate. Defaults to false.
         *
         * <p>Permissions enforcement occurs when resulting last location request is actually used,
         * not when this method is invoked.
         */
        @RequiresPermission(Manifest.permission.UPDATE_APP_OPS_STATS)
        public @NonNull Builder setHiddenFromAppOps(boolean hiddenFromAppOps) {
            mHiddenFromAppOps = hiddenFromAppOps;
            return this;
        }

        /**
         * If set to true, indicates that location settings, throttling, background location limits,
         * and any other possible limiting factors should be ignored in order to satisfy this
         * last location request. This is only intended for use in user initiated emergency
         * situations, and should be used extremely cautiously. Defaults to false.
         *
         * <p>Permissions enforcement occurs when resulting last location request is actually used,
         * not when this method is invoked.
         */
        @RequiresPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
        public @NonNull Builder setLocationSettingsIgnored(boolean locationSettingsIgnored) {
            mLocationSettingsIgnored = locationSettingsIgnored;
            return this;
        }

        /**
         * Builds a last location request from this builder.
         *
         * @return a new last location request
         */
        public @NonNull LastLocationRequest build() {
            return new LastLocationRequest(
                    mHiddenFromAppOps,
                    mLocationSettingsIgnored);
        }
    }
}
+35 −2
Original line number Diff line number Diff line
@@ -683,6 +683,7 @@ public class LocationManager {
     * location should always be checked.
     *
     * @return the last known location, or null if not available
     *
     * @throws SecurityException if no suitable location permission is present
     *
     * @hide
@@ -706,18 +707,50 @@ public class LocationManager {
     * in the course of the attempt as compared to this method.
     *
     * @param provider a provider listed by {@link #getAllProviders()}
     *
     * @return the last known location for the given provider, or null if not available
     *
     * @throws SecurityException if no suitable permission is present
     * @throws IllegalArgumentException if provider is null or doesn't exist
     */
    @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
    @Nullable
    public Location getLastKnownLocation(@NonNull String provider) {
        return getLastKnownLocation(provider, new LastLocationRequest.Builder().build());
    }

    /**
     * Gets the last known location from the given provider, or null if there is no last known
     * location.
     *
     * <p>See {@link LastLocationRequest} documentation for an explanation of various request
     * parameters and how they can affect the returned location.
     *
     * <p>See {@link #getLastKnownLocation(String)} for more detail on how this method works.
     *
     * @param provider            a provider listed by {@link #getAllProviders()}
     * @param lastLocationRequest the last location request containing location parameters
     *
     * @return the last known location for the given provider, or null if not available
     *
     * @throws SecurityException if no suitable permission is present
     * @throws IllegalArgumentException if provider is null or doesn't exist
     * @throws IllegalArgumentException if lastLocationRequest is null
     *
     * @hide
     */
    @SystemApi
    @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
    @Nullable
    public Location getLastKnownLocation(@NonNull String provider,
            @NonNull LastLocationRequest lastLocationRequest) {
        Preconditions.checkArgument(provider != null, "invalid null provider");
        Preconditions.checkArgument(lastLocationRequest != null,
                "invalid null last location request");

        try {
            return mService.getLastLocation(provider, mContext.getPackageName(),
                    mContext.getAttributionTag());
            return mService.getLastLocation(provider, lastLocationRequest,
                    mContext.getPackageName(), mContext.getAttributionTag());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
Loading