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