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.Context; 20 import android.location.Address; 21 import android.location.GeocoderParams; 22 import android.location.IGeocodeProvider; 23 import android.os.Handler; 24 import android.os.IBinder; 25 import android.os.RemoteException; 26 import android.util.Log; 27 28 import com.android.server.ServiceWatcher; 29 import java.util.List; 30 31 /** 32 * Proxy for IGeocodeProvider implementations. 33 */ 34 public class GeocoderProxy { 35 private static final String TAG = "GeocoderProxy"; 36 37 private static final String SERVICE_ACTION = "com.android.location.service.GeocodeProvider"; 38 39 private final Context mContext; 40 private final ServiceWatcher mServiceWatcher; 41 42 public static GeocoderProxy createAndBind(Context context, 43 int overlaySwitchResId, int defaultServicePackageNameResId, 44 int initialPackageNamesResId, Handler handler) { 45 GeocoderProxy proxy = new GeocoderProxy(context, overlaySwitchResId, 46 defaultServicePackageNameResId, initialPackageNamesResId, handler); 47 if (proxy.bind()) { 48 return proxy; 49 } else { 50 return null; 51 } 52 } 53 54 private GeocoderProxy(Context context, 55 int overlaySwitchResId, int defaultServicePackageNameResId, 56 int initialPackageNamesResId, Handler handler) { 57 mContext = context; 58 59 mServiceWatcher = new ServiceWatcher(mContext, TAG, SERVICE_ACTION, overlaySwitchResId, 60 defaultServicePackageNameResId, initialPackageNamesResId, null, handler); 61 } 62 63 private boolean bind () { 64 return mServiceWatcher.start(); 65 } 66 67 public String getConnectedPackageName() { 68 return mServiceWatcher.getBestPackageName(); 69 } 70 71 public String getFromLocation(double latitude, double longitude, int maxResults, 72 GeocoderParams params, List<Address> addrs) { 73 final String[] result = new String[] {"Service not Available"}; 74 mServiceWatcher.runOnBinder(new ServiceWatcher.BinderRunner() { 75 @Override 76 public void run(IBinder binder) { 77 IGeocodeProvider provider = IGeocodeProvider.Stub.asInterface(binder); 78 try { 79 result[0] = provider.getFromLocation( 80 latitude, longitude, maxResults, params, addrs); 81 } catch (RemoteException e) { 82 Log.w(TAG, e); 83 } 84 } 85 }); 86 return result[0]; 87 } 88 89 public String getFromLocationName(String locationName, 90 double lowerLeftLatitude, double lowerLeftLongitude, 91 double upperRightLatitude, double upperRightLongitude, int maxResults, 92 GeocoderParams params, List<Address> addrs) { 93 final String[] result = new String[] {"Service not Available"}; 94 mServiceWatcher.runOnBinder(new ServiceWatcher.BinderRunner() { 95 @Override 96 public void run(IBinder binder) { 97 IGeocodeProvider provider = IGeocodeProvider.Stub.asInterface(binder); 98 try { 99 result[0] = provider.getFromLocationName(locationName, lowerLeftLatitude, 100 lowerLeftLongitude, upperRightLatitude, upperRightLongitude, 101 maxResults, params, addrs); 102 } catch (RemoteException e) { 103 Log.w(TAG, e); 104 } 105 } 106 }); 107 return result[0]; 108 } 109 110 } 111