• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.location;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.location.LocationRequest;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.os.WorkSource;
24 import android.util.TimeUtils;
25 
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Objects;
29 
30 /** @hide */
31 public final class ProviderRequest implements Parcelable {
32 
33     public static final ProviderRequest EMPTY_REQUEST = new ProviderRequest(false, Long.MAX_VALUE,
34             false, false,
35             Collections.emptyList(), new WorkSource());
36 
37     /** Location reporting is requested (true) */
38     @UnsupportedAppUsage
39     public final boolean reportLocation;
40 
41     /** The smallest requested interval */
42     @UnsupportedAppUsage
43     public final long interval;
44 
45     /**
46      * Whether provider shall make stronger than normal tradeoffs to substantially restrict power
47      * use.
48      */
49     public final boolean lowPowerMode;
50 
51     /**
52      * When this flag is true, providers should ignore all location settings, user consents, power
53      * restrictions or any other restricting factors and always satisfy this request to the best of
54      * their ability. This flag should only be used in event of an emergency.
55      */
56     public final boolean locationSettingsIgnored;
57 
58     /**
59      * A more detailed set of requests.
60      * <p>Location Providers can optionally use this to
61      * fine tune location updates, for example when there
62      * is a high power slow interval request and a
63      * low power fast interval request.
64      */
65     @UnsupportedAppUsage
66     public final List<LocationRequest> locationRequests;
67 
68     public final WorkSource workSource;
69 
ProviderRequest(boolean reportLocation, long interval, boolean lowPowerMode, boolean locationSettingsIgnored, List<LocationRequest> locationRequests, WorkSource workSource)70     private ProviderRequest(boolean reportLocation, long interval, boolean lowPowerMode,
71             boolean locationSettingsIgnored, List<LocationRequest> locationRequests,
72             WorkSource workSource) {
73         this.reportLocation = reportLocation;
74         this.interval = interval;
75         this.lowPowerMode = lowPowerMode;
76         this.locationSettingsIgnored = locationSettingsIgnored;
77         this.locationRequests = Objects.requireNonNull(locationRequests);
78         this.workSource = Objects.requireNonNull(workSource);
79     }
80 
81     public static final Parcelable.Creator<ProviderRequest> CREATOR =
82             new Parcelable.Creator<ProviderRequest>() {
83                 @Override
84                 public ProviderRequest createFromParcel(Parcel in) {
85                     return new ProviderRequest(
86                             /* reportLocation= */ in.readBoolean(),
87                             /* interval= */ in.readLong(),
88                             /* lowPowerMode= */ in.readBoolean(),
89                             /* locationSettingsIgnored= */ in.readBoolean(),
90                             /* locationRequests= */
91                             in.createTypedArrayList(LocationRequest.CREATOR),
92                             /* workSource= */ in.readTypedObject(WorkSource.CREATOR));
93                 }
94 
95                 @Override
96                 public ProviderRequest[] newArray(int size) {
97                     return new ProviderRequest[size];
98                 }
99             };
100 
101     @Override
describeContents()102     public int describeContents() {
103         return 0;
104     }
105 
106     @Override
writeToParcel(Parcel parcel, int flags)107     public void writeToParcel(Parcel parcel, int flags) {
108         parcel.writeBoolean(reportLocation);
109         parcel.writeLong(interval);
110         parcel.writeBoolean(lowPowerMode);
111         parcel.writeBoolean(locationSettingsIgnored);
112         parcel.writeTypedList(locationRequests);
113         parcel.writeTypedObject(workSource, flags);
114     }
115 
116     @Override
toString()117     public String toString() {
118         StringBuilder s = new StringBuilder();
119         s.append("ProviderRequest[");
120         if (reportLocation) {
121             s.append("interval=");
122             TimeUtils.formatDuration(interval, s);
123             if (lowPowerMode) {
124                 s.append(", lowPowerMode");
125             }
126             if (locationSettingsIgnored) {
127                 s.append(", locationSettingsIgnored");
128             }
129         } else {
130             s.append("OFF");
131         }
132         s.append(']');
133         return s.toString();
134     }
135 
136     /**
137      * A Builder for {@link ProviderRequest}s.
138      */
139     public static class Builder {
140         private long mInterval = Long.MAX_VALUE;
141         private boolean mLowPowerMode;
142         private boolean mLocationSettingsIgnored;
143         private List<LocationRequest> mLocationRequests = Collections.emptyList();
144         private WorkSource mWorkSource = new WorkSource();
145 
getInterval()146         public long getInterval() {
147             return mInterval;
148         }
149 
150         /** Sets the request interval. */
setInterval(long interval)151         public Builder setInterval(long interval) {
152             this.mInterval = interval;
153             return this;
154         }
155 
isLowPowerMode()156         public boolean isLowPowerMode() {
157             return mLowPowerMode;
158         }
159 
160         /** Sets whether low power mode is enabled. */
setLowPowerMode(boolean lowPowerMode)161         public Builder setLowPowerMode(boolean lowPowerMode) {
162             this.mLowPowerMode = lowPowerMode;
163             return this;
164         }
165 
isLocationSettingsIgnored()166         public boolean isLocationSettingsIgnored() {
167             return mLocationSettingsIgnored;
168         }
169 
170         /** Sets whether location settings should be ignored. */
setLocationSettingsIgnored(boolean locationSettingsIgnored)171         public Builder setLocationSettingsIgnored(boolean locationSettingsIgnored) {
172             this.mLocationSettingsIgnored = locationSettingsIgnored;
173             return this;
174         }
175 
getLocationRequests()176         public List<LocationRequest> getLocationRequests() {
177             return mLocationRequests;
178         }
179 
180         /** Sets the {@link LocationRequest}s associated with this request. */
setLocationRequests(List<LocationRequest> locationRequests)181         public Builder setLocationRequests(List<LocationRequest> locationRequests) {
182             this.mLocationRequests = Objects.requireNonNull(locationRequests);
183             return this;
184         }
185 
getWorkSource()186         public WorkSource getWorkSource() {
187             return mWorkSource;
188         }
189 
190         /** Sets the work source. */
setWorkSource(WorkSource workSource)191         public Builder setWorkSource(WorkSource workSource) {
192             mWorkSource = Objects.requireNonNull(workSource);
193             return this;
194         }
195 
196         /**
197          * Builds a ProviderRequest object with the set information.
198          */
build()199         public ProviderRequest build() {
200             if (mInterval == Long.MAX_VALUE) {
201                 return EMPTY_REQUEST;
202             } else {
203                 return new ProviderRequest(true, mInterval, mLowPowerMode,
204                         mLocationSettingsIgnored, mLocationRequests, mWorkSource);
205             }
206         }
207     }
208 }
209