Home | History | Annotate | Download | only in location
      1 /*
      2  * Copyright (C) 2009 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.server.location;
     18 
     19 import android.location.Criteria;
     20 import android.location.ILocationManager;
     21 import android.location.Location;
     22 import android.location.LocationProvider;
     23 import android.net.NetworkInfo;
     24 import android.os.Bundle;
     25 import android.os.RemoteException;
     26 import android.os.WorkSource;
     27 import android.util.Log;
     28 import android.util.PrintWriterPrinter;
     29 
     30 import java.io.PrintWriter;
     31 
     32 /**
     33  * A mock location provider used by LocationManagerService to implement test providers.
     34  *
     35  * {@hide}
     36  */
     37 public class MockProvider implements LocationProviderInterface {
     38     private final String mName;
     39     private final ILocationManager mLocationManager;
     40     private final boolean mRequiresNetwork;
     41     private final boolean mRequiresSatellite;
     42     private final boolean mRequiresCell;
     43     private final boolean mHasMonetaryCost;
     44     private final boolean mSupportsAltitude;
     45     private final boolean mSupportsSpeed;
     46     private final boolean mSupportsBearing;
     47     private final int mPowerRequirement;
     48     private final int mAccuracy;
     49     private final Location mLocation;
     50     private int mStatus;
     51     private long mStatusUpdateTime;
     52     private final Bundle mExtras = new Bundle();
     53     private boolean mHasLocation;
     54     private boolean mHasStatus;
     55     private boolean mEnabled;
     56 
     57     private static final String TAG = "MockProvider";
     58 
     59     public MockProvider(String name,  ILocationManager locationManager,
     60         boolean requiresNetwork, boolean requiresSatellite,
     61         boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
     62         boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
     63         mName = name;
     64         mLocationManager = locationManager;
     65         mRequiresNetwork = requiresNetwork;
     66         mRequiresSatellite = requiresSatellite;
     67         mRequiresCell = requiresCell;
     68         mHasMonetaryCost = hasMonetaryCost;
     69         mSupportsAltitude = supportsAltitude;
     70         mSupportsBearing = supportsBearing;
     71         mSupportsSpeed = supportsSpeed;
     72         mPowerRequirement = powerRequirement;
     73         mAccuracy = accuracy;
     74         mLocation = new Location(name);
     75     }
     76 
     77     public String getName() {
     78         return mName;
     79     }
     80 
     81     public void disable() {
     82         mEnabled = false;
     83     }
     84 
     85     public void enable() {
     86         mEnabled = true;
     87     }
     88 
     89     public boolean isEnabled() {
     90         return mEnabled;
     91     }
     92 
     93     public int getStatus(Bundle extras) {
     94         if (mHasStatus) {
     95             extras.clear();
     96             extras.putAll(mExtras);
     97             return mStatus;
     98         } else {
     99             return LocationProvider.AVAILABLE;
    100         }
    101     }
    102 
    103     public long getStatusUpdateTime() {
    104         return mStatusUpdateTime;
    105     }
    106 
    107     public int getAccuracy() {
    108         return mAccuracy;
    109     }
    110 
    111     public int getPowerRequirement() {
    112         return mPowerRequirement;
    113     }
    114 
    115     public boolean hasMonetaryCost() {
    116         return mHasMonetaryCost;
    117     }
    118 
    119     public boolean requiresCell() {
    120         return mRequiresCell;
    121     }
    122 
    123     public boolean requiresNetwork() {
    124         return mRequiresNetwork;
    125     }
    126 
    127     public boolean requiresSatellite() {
    128         return mRequiresSatellite;
    129     }
    130 
    131     public boolean supportsAltitude() {
    132         return mSupportsAltitude;
    133     }
    134 
    135     public boolean supportsBearing() {
    136         return mSupportsBearing;
    137     }
    138 
    139     public boolean supportsSpeed() {
    140         return mSupportsSpeed;
    141     }
    142 
    143     public boolean meetsCriteria(Criteria criteria) {
    144         if ((criteria.getAccuracy() != Criteria.NO_REQUIREMENT) &&
    145             (criteria.getAccuracy() < mAccuracy)) {
    146             return false;
    147         }
    148         int criteriaPower = criteria.getPowerRequirement();
    149         if ((criteriaPower != Criteria.NO_REQUIREMENT) &&
    150             (criteriaPower < mPowerRequirement)) {
    151             return false;
    152         }
    153         if (criteria.isAltitudeRequired() && !mSupportsAltitude) {
    154             return false;
    155         }
    156         if (criteria.isSpeedRequired() && !mSupportsSpeed) {
    157             return false;
    158         }
    159         if (criteria.isBearingRequired() && !mSupportsBearing) {
    160             return false;
    161         }
    162         return true;
    163     }
    164 
    165     public void setLocation(Location l) {
    166         mLocation.set(l);
    167         mHasLocation = true;
    168         try {
    169             mLocationManager.reportLocation(mLocation, false);
    170         } catch (RemoteException e) {
    171             Log.e(TAG, "RemoteException calling reportLocation");
    172         }
    173     }
    174 
    175     public void clearLocation() {
    176         mHasLocation = false;
    177     }
    178 
    179     public void setStatus(int status, Bundle extras, long updateTime) {
    180         mStatus = status;
    181         mStatusUpdateTime = updateTime;
    182         mExtras.clear();
    183         if (extras != null) {
    184             mExtras.putAll(extras);
    185         }
    186         mHasStatus = true;
    187     }
    188 
    189     public void clearStatus() {
    190         mHasStatus = false;
    191         mStatusUpdateTime = 0;
    192     }
    193 
    194     public String getInternalState() {
    195         return null;
    196     }
    197 
    198     public void enableLocationTracking(boolean enable) {
    199     }
    200 
    201     public boolean requestSingleShotFix() {
    202         return false;
    203     }
    204 
    205     public void setMinTime(long minTime, WorkSource ws) {
    206     }
    207 
    208     public void updateNetworkState(int state, NetworkInfo info) {
    209     }
    210 
    211     public void updateLocation(Location location) {
    212     }
    213 
    214     public boolean sendExtraCommand(String command, Bundle extras) {
    215         return false;
    216     }
    217 
    218     public void addListener(int uid) {
    219     }
    220 
    221     public void removeListener(int uid) {
    222     }
    223 
    224     public void dump(PrintWriter pw, String prefix) {
    225         pw.println(prefix + mName);
    226         pw.println(prefix + "mHasLocation=" + mHasLocation);
    227         pw.println(prefix + "mLocation:");
    228         mLocation.dump(new PrintWriterPrinter(pw), prefix + "  ");
    229         pw.println(prefix + "mHasStatus=" + mHasStatus);
    230         pw.println(prefix + "mStatus=" + mStatus);
    231         pw.println(prefix + "mStatusUpdateTime=" + mStatusUpdateTime);
    232         pw.println(prefix + "mExtras=" + mExtras);
    233     }
    234 }
    235