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