1 /* 2 * Copyright (C) 2010 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.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.ServiceConnection; 23 import android.location.Address; 24 import android.location.GeocoderParams; 25 import android.location.IGeocodeProvider; 26 import android.os.IBinder; 27 import android.os.RemoteException; 28 import android.os.SystemClock; 29 import android.util.Log; 30 31 import java.util.List; 32 33 /** 34 * A class for proxying IGeocodeProvider implementations. 35 * 36 * {@hide} 37 */ 38 public class GeocoderProxy { 39 40 private static final String TAG = "GeocoderProxy"; 41 42 private final Context mContext; 43 private final Intent mIntent; 44 private final Object mMutex = new Object(); // synchronizes access to mServiceConnection 45 private Connection mServiceConnection = new Connection(); // never null 46 GeocoderProxy(Context context, String serviceName)47 public GeocoderProxy(Context context, String serviceName) { 48 mContext = context; 49 mIntent = new Intent(serviceName); 50 mContext.bindService(mIntent, mServiceConnection, Context.BIND_AUTO_CREATE); 51 } 52 53 /** 54 * When unbundled NetworkLocationService package is updated, we 55 * need to unbind from the old version and re-bind to the new one. 56 */ reconnect()57 public void reconnect() { 58 synchronized (mMutex) { 59 mContext.unbindService(mServiceConnection); 60 mServiceConnection = new Connection(); 61 mContext.bindService(mIntent, mServiceConnection, Context.BIND_AUTO_CREATE); 62 } 63 } 64 65 private class Connection implements ServiceConnection { 66 67 private IGeocodeProvider mProvider; 68 onServiceConnected(ComponentName className, IBinder service)69 public void onServiceConnected(ComponentName className, IBinder service) { 70 Log.d(TAG, "onServiceConnected " + className); 71 synchronized (this) { 72 mProvider = IGeocodeProvider.Stub.asInterface(service); 73 } 74 } 75 onServiceDisconnected(ComponentName className)76 public void onServiceDisconnected(ComponentName className) { 77 Log.d(TAG, "onServiceDisconnected " + className); 78 synchronized (this) { 79 mProvider = null; 80 } 81 } 82 getProvider()83 public IGeocodeProvider getProvider() { 84 synchronized (this) { 85 return mProvider; 86 } 87 } 88 } 89 getFromLocation(double latitude, double longitude, int maxResults, GeocoderParams params, List<Address> addrs)90 public String getFromLocation(double latitude, double longitude, int maxResults, 91 GeocoderParams params, List<Address> addrs) { 92 IGeocodeProvider provider; 93 synchronized (mMutex) { 94 provider = mServiceConnection.getProvider(); 95 } 96 if (provider != null) { 97 try { 98 return provider.getFromLocation(latitude, longitude, maxResults, 99 params, addrs); 100 } catch (RemoteException e) { 101 Log.e(TAG, "getFromLocation failed", e); 102 } 103 } 104 return "Service not Available"; 105 } 106 getFromLocationName(String locationName, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude, int maxResults, GeocoderParams params, List<Address> addrs)107 public String getFromLocationName(String locationName, 108 double lowerLeftLatitude, double lowerLeftLongitude, 109 double upperRightLatitude, double upperRightLongitude, int maxResults, 110 GeocoderParams params, List<Address> addrs) { 111 IGeocodeProvider provider; 112 synchronized (mMutex) { 113 provider = mServiceConnection.getProvider(); 114 } 115 if (provider != null) { 116 try { 117 return provider.getFromLocationName(locationName, lowerLeftLatitude, 118 lowerLeftLongitude, upperRightLatitude, upperRightLongitude, 119 maxResults, params, addrs); 120 } catch (RemoteException e) { 121 Log.e(TAG, "getFromLocationName failed", e); 122 } 123 } 124 return "Service not Available"; 125 } 126 } 127