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.internal.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 Connection mServiceConnection = new Connection(); 45 private IGeocodeProvider mProvider; 46 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 private class Connection implements ServiceConnection { 54 public void onServiceConnected(ComponentName className, IBinder service) { 55 Log.d(TAG, "onServiceConnected " + className); 56 synchronized (this) { 57 mProvider = IGeocodeProvider.Stub.asInterface(service); 58 } 59 } 60 61 public void onServiceDisconnected(ComponentName className) { 62 Log.d(TAG, "onServiceDisconnected " + className); 63 synchronized (this) { 64 mProvider = null; 65 } 66 } 67 } 68 69 public String getFromLocation(double latitude, double longitude, int maxResults, 70 GeocoderParams params, List<Address> addrs) { 71 IGeocodeProvider provider; 72 synchronized (mServiceConnection) { 73 provider = mProvider; 74 } 75 if (provider != null) { 76 try { 77 return provider.getFromLocation(latitude, longitude, maxResults, 78 params, addrs); 79 } catch (RemoteException e) { 80 Log.e(TAG, "getFromLocation failed", e); 81 } 82 } 83 return "Service not Available"; 84 } 85 86 public String getFromLocationName(String locationName, 87 double lowerLeftLatitude, double lowerLeftLongitude, 88 double upperRightLatitude, double upperRightLongitude, int maxResults, 89 GeocoderParams params, List<Address> addrs) { 90 IGeocodeProvider provider; 91 synchronized (mServiceConnection) { 92 provider = mProvider; 93 } 94 if (provider != null) { 95 try { 96 return provider.getFromLocationName(locationName, lowerLeftLatitude, 97 lowerLeftLongitude, upperRightLatitude, upperRightLongitude, 98 maxResults, params, addrs); 99 } catch (RemoteException e) { 100 Log.e(TAG, "getFromLocationName failed", e); 101 } 102 } 103 return "Service not Available"; 104 } 105 } 106