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.Address; 20import android.location.ILocationProvider; 21import android.location.Location; 22import android.location.LocationManager; 23import android.net.NetworkInfo; 24import android.os.Bundle; 25import android.os.IBinder; 26import android.os.RemoteException; 27import android.util.Log; 28 29import java.util.List; 30 31/** 32 * A class for proxying remote ILocationProvider implementations. 33 * 34 * {@hide} 35 */ 36public class LocationProviderProxy implements IBinder.DeathRecipient { 37 38 private static final String TAG = "LocationProviderProxy"; 39 40 private final String mName; 41 private final ILocationProvider mProvider; 42 private boolean mLocationTracking = false; 43 private long mMinTime = 0; 44 private boolean mDead; 45 46 public LocationProviderProxy(String name, ILocationProvider provider) { 47 mName = name; 48 mProvider = provider; 49 try { 50 provider.asBinder().linkToDeath(this, 0); 51 } catch (RemoteException e) { 52 Log.e(TAG, "linkToDeath failed", e); 53 mDead = true; 54 } 55 } 56 57 public void unlinkProvider() { 58 if (mProvider != null) { 59 mProvider.asBinder().unlinkToDeath(this, 0); 60 } 61 } 62 63 public String getName() { 64 return mName; 65 } 66 67 public boolean isDead() { 68 return mDead; 69 } 70 71 public boolean requiresNetwork() { 72 try { 73 return mProvider.requiresNetwork(); 74 } catch (RemoteException e) { 75 Log.e(TAG, "requiresNetwork failed", e); 76 return false; 77 } 78 } 79 80 public boolean requiresSatellite() { 81 try { 82 return mProvider.requiresSatellite(); 83 } catch (RemoteException e) { 84 Log.e(TAG, "requiresSatellite failed", e); 85 return false; 86 } 87 } 88 89 public boolean requiresCell() { 90 try { 91 return mProvider.requiresCell(); 92 } catch (RemoteException e) { 93 Log.e(TAG, "requiresCell failed", e); 94 return false; 95 } 96 } 97 98 public boolean hasMonetaryCost() { 99 try { 100 return mProvider.hasMonetaryCost(); 101 } catch (RemoteException e) { 102 Log.e(TAG, "hasMonetaryCost failed", e); 103 return false; 104 } 105 } 106 107 public boolean supportsAltitude() { 108 try { 109 return mProvider.supportsAltitude(); 110 } catch (RemoteException e) { 111 Log.e(TAG, "supportsAltitude failed", e); 112 return false; 113 } 114 } 115 116 public boolean supportsSpeed() { 117 try { 118 return mProvider.supportsSpeed(); 119 } catch (RemoteException e) { 120 Log.e(TAG, "supportsSpeed failed", e); 121 return false; 122 } 123 } 124 125 public boolean supportsBearing() { 126 try { 127 return mProvider.supportsBearing(); 128 } catch (RemoteException e) { 129 Log.e(TAG, "supportsBearing failed", e); 130 return false; 131 } 132 } 133 134 public int getPowerRequirement() { 135 try { 136 return mProvider.getPowerRequirement(); 137 } catch (RemoteException e) { 138 Log.e(TAG, "getPowerRequirement failed", e); 139 return 0; 140 } 141 } 142 143 public int getAccuracy() { 144 try { 145 return mProvider.getAccuracy(); 146 } catch (RemoteException e) { 147 Log.e(TAG, "getAccuracy failed", e); 148 return 0; 149 } 150 } 151 152 public void enable() { 153 try { 154 mProvider.enable(); 155 } catch (RemoteException e) { 156 Log.e(TAG, "enable failed", e); 157 } 158 } 159 160 public void disable() { 161 try { 162 mProvider.disable(); 163 } catch (RemoteException e) { 164 Log.e(TAG, "disable failed", e); 165 } 166 } 167 168 public boolean isEnabled() { 169 try { 170 return mProvider.isEnabled(); 171 } catch (RemoteException e) { 172 Log.e(TAG, "isEnabled failed", e); 173 return false; 174 } 175 } 176 177 public int getStatus(Bundle extras) { 178 try { 179 return mProvider.getStatus(extras); 180 } catch (RemoteException e) { 181 Log.e(TAG, "getStatus failed", e); 182 return 0; 183 } 184 } 185 186 public long getStatusUpdateTime() { 187 try { 188 return mProvider.getStatusUpdateTime(); 189 } catch (RemoteException e) { 190 Log.e(TAG, "getStatusUpdateTime failed", e); 191 return 0; 192 } 193 } 194 195 public boolean isLocationTracking() { 196 return mLocationTracking; 197 } 198 199 public void enableLocationTracking(boolean enable) { 200 mLocationTracking = enable; 201 try { 202 mProvider.enableLocationTracking(enable); 203 } catch (RemoteException e) { 204 Log.e(TAG, "enableLocationTracking failed", e); 205 } 206 } 207 208 public long getMinTime() { 209 return mMinTime; 210 } 211 212 public void setMinTime(long minTime) { 213 mMinTime = minTime; 214 try { 215 mProvider.setMinTime(minTime); 216 } catch (RemoteException e) { 217 Log.e(TAG, "setMinTime failed", e); 218 } 219 } 220 221 public void updateNetworkState(int state, NetworkInfo info) { 222 try { 223 mProvider.updateNetworkState(state, info); 224 } catch (RemoteException e) { 225 Log.e(TAG, "updateNetworkState failed", e); 226 } 227 } 228 229 public void updateLocation(Location location) { 230 try { 231 mProvider.updateLocation(location); 232 } catch (RemoteException e) { 233 Log.e(TAG, "updateLocation failed", e); 234 } 235 } 236 237 public boolean sendExtraCommand(String command, Bundle extras) { 238 try { 239 return mProvider.sendExtraCommand(command, extras); 240 } catch (RemoteException e) { 241 Log.e(TAG, "sendExtraCommand failed", e); 242 return false; 243 } 244 } 245 246 public void addListener(int uid) { 247 try { 248 mProvider.addListener(uid); 249 } catch (RemoteException e) { 250 Log.e(TAG, "addListener failed", e); 251 } 252 } 253 254 public void removeListener(int uid) { 255 try { 256 mProvider.removeListener(uid); 257 } catch (RemoteException e) { 258 Log.e(TAG, "removeListener failed", e); 259 } 260 } 261 262 public void binderDied() { 263 Log.w(TAG, "Location Provider " + mName + " died"); 264 mDead = true; 265 mProvider.asBinder().unlinkToDeath(this, 0); 266 } 267} 268