LocationProviderProxy.java revision 6fa9ad4afcd762aea519ff61811386c23d18ddb2
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.server.location; 18 19import java.io.FileDescriptor; 20import java.io.PrintWriter; 21import java.util.List; 22 23import android.content.Context; 24import android.location.LocationProvider; 25import android.os.Bundle; 26import android.os.Handler; 27import android.os.RemoteException; 28import android.os.WorkSource; 29import android.util.Log; 30 31import com.android.internal.location.ProviderProperties; 32import com.android.internal.location.ILocationProvider; 33import com.android.internal.location.ProviderRequest; 34import com.android.server.LocationManagerService; 35import com.android.server.ServiceWatcher; 36 37/** 38 * Proxy for ILocationProvider implementations. 39 */ 40public class LocationProviderProxy implements LocationProviderInterface { 41 private static final String TAG = "LocationProviderProxy"; 42 private static final boolean D = LocationManagerService.D; 43 44 private final Context mContext; 45 private final String mName; 46 private final ServiceWatcher mServiceWatcher; 47 48 private Object mLock = new Object(); 49 50 // cached values set by the location manager, synchronized on mLock 51 private ProviderProperties mProperties; 52 private boolean mEnabled = false; 53 private ProviderRequest mRequest = null; 54 private WorkSource mWorksource = new WorkSource(); 55 56 public static LocationProviderProxy createAndBind(Context context, String name, String action, 57 List<String> initialPackageNames, Handler handler) { 58 LocationProviderProxy proxy = new LocationProviderProxy(context, name, action, 59 initialPackageNames, handler); 60 if (proxy.bind()) { 61 return proxy; 62 } else { 63 return null; 64 } 65 } 66 67 private LocationProviderProxy(Context context, String name, String action, 68 List<String> initialPackageNames, Handler handler) { 69 mContext = context; 70 mName = name; 71 mServiceWatcher = new ServiceWatcher(mContext, TAG, action, initialPackageNames, 72 mNewServiceWork, handler); 73 } 74 75 private boolean bind () { 76 return mServiceWatcher.start(); 77 } 78 79 private ILocationProvider getService() { 80 return ILocationProvider.Stub.asInterface(mServiceWatcher.getBinder()); 81 } 82 83 public String getConnectedPackageName() { 84 return mServiceWatcher.getBestPackageName(); 85 } 86 87 /** 88 * Work to apply current state to a newly connected provider. 89 * Remember we can switch the service that implements a providers 90 * at run-time, so need to apply current state. 91 */ 92 private Runnable mNewServiceWork = new Runnable() { 93 @Override 94 public void run() { 95 if (D) Log.d(TAG, "applying state to connected service"); 96 97 boolean enabled; 98 ProviderProperties properties = null; 99 ProviderRequest request; 100 WorkSource source; 101 ILocationProvider service; 102 synchronized (mLock) { 103 enabled = mEnabled; 104 request = mRequest; 105 source = mWorksource; 106 service = getService(); 107 } 108 109 if (service == null) return; 110 111 try { 112 // load properties from provider 113 properties = service.getProperties(); 114 if (properties == null) { 115 Log.e(TAG, mServiceWatcher.getBestPackageName() + 116 " has invalid locatino provider properties"); 117 } 118 119 // apply current state to new service 120 if (enabled) { 121 service.enable(); 122 if (request != null) { 123 service.setRequest(request, source); 124 } 125 } 126 } catch (RemoteException e) { 127 Log.w(TAG, e); 128 } catch (Exception e) { 129 // never let remote service crash system server 130 Log.e(TAG, "Exception from " + mServiceWatcher.getBestPackageName(), e); 131 } 132 133 synchronized (mLock) { 134 mProperties = properties; 135 } 136 } 137 }; 138 139 @Override 140 public String getName() { 141 return mName; 142 } 143 144 @Override 145 public ProviderProperties getProperties() { 146 synchronized (mLock) { 147 return mProperties; 148 } 149 } 150 151 @Override 152 public void enable() { 153 synchronized (mLock) { 154 mEnabled = true; 155 } 156 ILocationProvider service = getService(); 157 if (service == null) return; 158 159 try { 160 service.enable(); 161 } catch (RemoteException e) { 162 Log.w(TAG, e); 163 } catch (Exception e) { 164 // never let remote service crash system server 165 Log.e(TAG, "Exception from " + mServiceWatcher.getBestPackageName(), e); 166 } 167 } 168 169 @Override 170 public void disable() { 171 synchronized (mLock) { 172 mEnabled = false; 173 } 174 ILocationProvider service = getService(); 175 if (service == null) return; 176 177 try { 178 service.disable(); 179 } catch (RemoteException e) { 180 Log.w(TAG, e); 181 } catch (Exception e) { 182 // never let remote service crash system server 183 Log.e(TAG, "Exception from " + mServiceWatcher.getBestPackageName(), e); 184 } 185 } 186 187 @Override 188 public boolean isEnabled() { 189 synchronized (mLock) { 190 return mEnabled; 191 } 192 } 193 194 @Override 195 public void setRequest(ProviderRequest request, WorkSource source) { 196 synchronized (mLock) { 197 mRequest = request; 198 mWorksource = source; 199 } 200 ILocationProvider service = getService(); 201 if (service == null) return; 202 203 try { 204 service.setRequest(request, source); 205 } catch (RemoteException e) { 206 Log.w(TAG, e); 207 } catch (Exception e) { 208 // never let remote service crash system server 209 Log.e(TAG, "Exception from " + mServiceWatcher.getBestPackageName(), e); 210 } 211 } 212 213 @Override 214 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 215 pw.append("REMOTE SERVICE"); 216 pw.append(" name=").append(mName); 217 pw.append(" pkg=").append(mServiceWatcher.getBestPackageName()); 218 pw.append(" version=").append("" + mServiceWatcher.getBestVersion()); 219 pw.append('\n'); 220 221 ILocationProvider service = getService(); 222 if (service == null) { 223 pw.println("service down (null)"); 224 return; 225 } 226 pw.flush(); 227 228 try { 229 service.asBinder().dump(fd, args); 230 } catch (RemoteException e) { 231 pw.println("service down (RemoteException)"); 232 Log.w(TAG, e); 233 } catch (Exception e) { 234 pw.println("service down (Exception)"); 235 // never let remote service crash system server 236 Log.e(TAG, "Exception from " + mServiceWatcher.getBestPackageName(), e); 237 } 238 } 239 240 @Override 241 public int getStatus(Bundle extras) { 242 ILocationProvider service = getService(); 243 if (service == null) return LocationProvider.TEMPORARILY_UNAVAILABLE; 244 245 try { 246 return service.getStatus(extras); 247 } catch (RemoteException e) { 248 Log.w(TAG, e); 249 } catch (Exception e) { 250 // never let remote service crash system server 251 Log.e(TAG, "Exception from " + mServiceWatcher.getBestPackageName(), e); 252 } 253 return LocationProvider.TEMPORARILY_UNAVAILABLE; 254 } 255 256 @Override 257 public long getStatusUpdateTime() { 258 ILocationProvider service = getService(); 259 if (service == null) return 0; 260 261 try { 262 return service.getStatusUpdateTime(); 263 } catch (RemoteException e) { 264 Log.w(TAG, e); 265 } catch (Exception e) { 266 // never let remote service crash system server 267 Log.e(TAG, "Exception from " + mServiceWatcher.getBestPackageName(), e); 268 } 269 return 0; 270 } 271 272 @Override 273 public boolean sendExtraCommand(String command, Bundle extras) { 274 ILocationProvider service = getService(); 275 if (service == null) return false; 276 277 try { 278 return service.sendExtraCommand(command, extras); 279 } catch (RemoteException e) { 280 Log.w(TAG, e); 281 } catch (Exception e) { 282 // never let remote service crash system server 283 Log.e(TAG, "Exception from " + mServiceWatcher.getBestPackageName(), e); 284 } 285 return false; 286 } 287 } 288