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.annotation.IntDef;
20 import android.location.Criteria;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import com.android.internal.util.Preconditions;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 /**
30  * A Parcelable containing (legacy) location provider properties.
31  * This object is just used inside the framework and system services.
32  *
33  * @hide
34  */
35 public final class ProviderProperties implements Parcelable {
36 
37     /** @hide */
38     @Retention(RetentionPolicy.SOURCE)
39     @IntDef({Criteria.POWER_LOW, Criteria.POWER_MEDIUM, Criteria.POWER_HIGH})
40     public @interface PowerRequirement {
41     }
42 
43     /** @hide */
44     @Retention(RetentionPolicy.SOURCE)
45     @IntDef({Criteria.ACCURACY_FINE, Criteria.ACCURACY_COARSE})
46     public @interface Accuracy {
47     }
48 
49     /**
50      * True if provider requires access to a
51      * data network (e.g., the Internet), false otherwise.
52      */
53     public final boolean mRequiresNetwork;
54 
55     /**
56      * True if the provider requires access to a
57      * satellite-based positioning system (e.g., GPS), false
58      * otherwise.
59      */
60     public final boolean mRequiresSatellite;
61 
62     /**
63      * True if the provider requires access to an appropriate
64      * cellular network (e.g., to make use of cell tower IDs), false
65      * otherwise.
66      */
67     public final boolean mRequiresCell;
68 
69     /**
70      * True if the use of this provider may result in a
71      * monetary charge to the user, false if use is free.  It is up to
72      * each provider to give accurate information. Cell (network) usage
73      * is not considered monetary cost.
74      */
75     public final boolean mHasMonetaryCost;
76 
77     /**
78      * True if the provider is able to provide altitude
79      * information, false otherwise.  A provider that reports altitude
80      * under most circumstances but may occasionally not report it
81      * should return true.
82      */
83     public final boolean mSupportsAltitude;
84 
85     /**
86      * True if the provider is able to provide speed
87      * information, false otherwise.  A provider that reports speed
88      * under most circumstances but may occasionally not report it
89      * should return true.
90      */
91     public final boolean mSupportsSpeed;
92 
93     /**
94      * True if the provider is able to provide bearing
95      * information, false otherwise.  A provider that reports bearing
96      * under most circumstances but may occasionally not report it
97      * should return true.
98      */
99     public final boolean mSupportsBearing;
100 
101     /**
102      * Power requirement for this provider.
103      */
104     @PowerRequirement
105     public final int mPowerRequirement;
106 
107     /**
108      * Constant describing the horizontal accuracy returned
109      * by this provider.
110      */
111     @Accuracy
112     public final int mAccuracy;
113 
ProviderProperties(boolean requiresNetwork, boolean requiresSatellite, boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude, boolean supportsSpeed, boolean supportsBearing, @PowerRequirement int powerRequirement, @Accuracy int accuracy)114     public ProviderProperties(boolean requiresNetwork, boolean requiresSatellite,
115             boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
116             boolean supportsSpeed, boolean supportsBearing, @PowerRequirement int powerRequirement,
117             @Accuracy int accuracy) {
118         mRequiresNetwork = requiresNetwork;
119         mRequiresSatellite = requiresSatellite;
120         mRequiresCell = requiresCell;
121         mHasMonetaryCost = hasMonetaryCost;
122         mSupportsAltitude = supportsAltitude;
123         mSupportsSpeed = supportsSpeed;
124         mSupportsBearing = supportsBearing;
125         mPowerRequirement = Preconditions.checkArgumentInRange(powerRequirement, Criteria.POWER_LOW,
126                 Criteria.POWER_HIGH, "powerRequirement");
127         mAccuracy = Preconditions.checkArgumentInRange(accuracy, Criteria.ACCURACY_FINE,
128                 Criteria.ACCURACY_COARSE, "accuracy");
129     }
130 
131     public static final Parcelable.Creator<ProviderProperties> CREATOR =
132             new Parcelable.Creator<ProviderProperties>() {
133                 @Override
134                 public ProviderProperties createFromParcel(Parcel in) {
135                     boolean requiresNetwork = in.readInt() == 1;
136                     boolean requiresSatellite = in.readInt() == 1;
137                     boolean requiresCell = in.readInt() == 1;
138                     boolean hasMonetaryCost = in.readInt() == 1;
139                     boolean supportsAltitude = in.readInt() == 1;
140                     boolean supportsSpeed = in.readInt() == 1;
141                     boolean supportsBearing = in.readInt() == 1;
142                     int powerRequirement = in.readInt();
143                     int accuracy = in.readInt();
144                     return new ProviderProperties(requiresNetwork, requiresSatellite,
145                             requiresCell, hasMonetaryCost, supportsAltitude, supportsSpeed,
146                             supportsBearing,
147                             powerRequirement, accuracy);
148                 }
149 
150                 @Override
151                 public ProviderProperties[] newArray(int size) {
152                     return new ProviderProperties[size];
153                 }
154             };
155 
156     @Override
describeContents()157     public int describeContents() {
158         return 0;
159     }
160 
161     @Override
writeToParcel(Parcel parcel, int flags)162     public void writeToParcel(Parcel parcel, int flags) {
163         parcel.writeInt(mRequiresNetwork ? 1 : 0);
164         parcel.writeInt(mRequiresSatellite ? 1 : 0);
165         parcel.writeInt(mRequiresCell ? 1 : 0);
166         parcel.writeInt(mHasMonetaryCost ? 1 : 0);
167         parcel.writeInt(mSupportsAltitude ? 1 : 0);
168         parcel.writeInt(mSupportsSpeed ? 1 : 0);
169         parcel.writeInt(mSupportsBearing ? 1 : 0);
170         parcel.writeInt(mPowerRequirement);
171         parcel.writeInt(mAccuracy);
172     }
173 
174     @Override
toString()175     public String toString() {
176         StringBuilder b = new StringBuilder("ProviderProperties[");
177         b.append("power=").append(powerToString(mPowerRequirement)).append(", ");
178         b.append("accuracy=").append(accuracyToString(mAccuracy));
179         if (mRequiresNetwork || mRequiresSatellite || mRequiresCell) {
180             b.append(", requires=");
181             if (mRequiresNetwork) {
182                 b.append("network,");
183             }
184             if (mRequiresSatellite) {
185                 b.append("satellite,");
186             }
187             if (mRequiresCell) {
188                 b.append("cell,");
189             }
190             b.setLength(b.length() - 1);
191         }
192         if (mHasMonetaryCost) {
193             b.append(", hasMonetaryCost");
194         }
195         if (mSupportsBearing || mSupportsSpeed || mSupportsAltitude) {
196             b.append(", supports=[");
197             if (mSupportsBearing) {
198                 b.append("bearing, ");
199             }
200             if (mSupportsSpeed) {
201                 b.append("speed, ");
202             }
203             if (mSupportsAltitude) {
204                 b.append("altitude, ");
205             }
206             b.setLength(b.length() - 2);
207             b.append("]");
208         }
209         b.append("]");
210         return b.toString();
211     }
212 
powerToString(@owerRequirement int power)213     private static String powerToString(@PowerRequirement int power) {
214         switch (power) {
215             case Criteria.POWER_LOW:
216                 return "Low";
217             case Criteria.POWER_MEDIUM:
218                 return "Medium";
219             case Criteria.POWER_HIGH:
220                 return "High";
221             default:
222                 return "???";
223         }
224     }
225 
accuracyToString(@ccuracy int accuracy)226     private static String accuracyToString(@Accuracy int accuracy) {
227         switch (accuracy) {
228             case Criteria.ACCURACY_COARSE:
229                 return "Coarse";
230             case Criteria.ACCURACY_FINE:
231                 return "Fine";
232             default:
233                 return "???";
234         }
235     }
236 }
237