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.RemoteException; 25 import android.os.UserHandle; 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 private IGeocodeProvider getService() { 68 return IGeocodeProvider.Stub.asInterface(mServiceWatcher.getBinder()); 69 } 70 71 public String getConnectedPackageName() { 72 return mServiceWatcher.getBestPackageName(); 73 } 74 75 public String getFromLocation(double latitude, double longitude, int maxResults, 76 GeocoderParams params, List<Address> addrs) { 77 IGeocodeProvider provider = getService(); 78 if (provider != null) { 79 try { 80 return provider.getFromLocation(latitude, longitude, maxResults, params, addrs); 81 } catch (RemoteException e) { 82 Log.w(TAG, e); 83 } 84 } 85 return "Service not Available"; 86 } 87 88 public String getFromLocationName(String locationName, 89 double lowerLeftLatitude, double lowerLeftLongitude, 90 double upperRightLatitude, double upperRightLongitude, int maxResults, 91 GeocoderParams params, List<Address> addrs) { 92 IGeocodeProvider provider = getService(); 93 if (provider != null) { 94 try { 95 return provider.getFromLocationName(locationName, lowerLeftLatitude, 96 lowerLeftLongitude, upperRightLatitude, upperRightLongitude, 97 maxResults, params, addrs); 98 } catch (RemoteException e) { 99 Log.w(TAG, e); 100 } 101 } 102 return "Service not Available"; 103 } 104 105 } 106