Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | 00b7427 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 17 | package com.android.server.location; |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 18 | |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 19 | import android.content.Context; |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 20 | import android.location.Address; |
| 21 | import android.location.GeocoderParams; |
| 22 | import android.location.IGeocodeProvider; |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 23 | import android.os.Handler; |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 24 | import android.os.RemoteException; |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 27 | import com.android.server.ServiceWatcher; |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 28 | import java.util.List; |
| 29 | |
| 30 | /** |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 31 | * Proxy for IGeocodeProvider implementations. |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 32 | */ |
| 33 | public class GeocoderProxy { |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 34 | private static final String TAG = "GeocoderProxy"; |
| 35 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 36 | private static final String SERVICE_ACTION = "com.android.location.service.GeocodeProvider"; |
Nick Pelly | 00355d5 | 2012-05-27 16:12:45 -0700 | [diff] [blame] | 37 | |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 38 | private final Context mContext; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 39 | private final ServiceWatcher mServiceWatcher; |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 40 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 41 | public static GeocoderProxy createAndBind(Context context, |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 42 | int overlaySwitchResId, int defaultServicePackageNameResId, |
| 43 | int initialPackageNamesResId, Handler handler) { |
| 44 | GeocoderProxy proxy = new GeocoderProxy(context, overlaySwitchResId, |
| 45 | defaultServicePackageNameResId, initialPackageNamesResId, handler); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 46 | if (proxy.bind()) { |
| 47 | return proxy; |
| 48 | } else { |
| 49 | return null; |
| 50 | } |
| 51 | } |
| 52 | |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 53 | private GeocoderProxy(Context context, |
| 54 | int overlaySwitchResId, int defaultServicePackageNameResId, |
| 55 | int initialPackageNamesResId, Handler handler) { |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 56 | mContext = context; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 57 | |
Zhentao Sun | c5fc998 | 2013-04-17 17:47:53 -0700 | [diff] [blame] | 58 | mServiceWatcher = new ServiceWatcher(mContext, TAG, SERVICE_ACTION, overlaySwitchResId, |
| 59 | defaultServicePackageNameResId, initialPackageNamesResId, null, handler); |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 62 | private boolean bind () { |
| 63 | return mServiceWatcher.start(); |
Mike Lockwood | e97ae40 | 2010-09-29 15:23:46 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 66 | private IGeocodeProvider getService() { |
| 67 | return IGeocodeProvider.Stub.asInterface(mServiceWatcher.getBinder()); |
| 68 | } |
Mark Vandevoorde | 8863c43 | 2010-10-04 14:23:24 -0700 | [diff] [blame] | 69 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 70 | public String getConnectedPackageName() { |
| 71 | return mServiceWatcher.getBestPackageName(); |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public String getFromLocation(double latitude, double longitude, int maxResults, |
| 75 | GeocoderParams params, List<Address> addrs) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 76 | IGeocodeProvider provider = getService(); |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 77 | if (provider != null) { |
| 78 | try { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 79 | return provider.getFromLocation(latitude, longitude, maxResults, params, addrs); |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 80 | } catch (RemoteException e) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 81 | Log.w(TAG, e); |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | return "Service not Available"; |
| 85 | } |
| 86 | |
| 87 | public String getFromLocationName(String locationName, |
| 88 | double lowerLeftLatitude, double lowerLeftLongitude, |
| 89 | double upperRightLatitude, double upperRightLongitude, int maxResults, |
| 90 | GeocoderParams params, List<Address> addrs) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 91 | IGeocodeProvider provider = getService(); |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 92 | if (provider != null) { |
| 93 | try { |
| 94 | return provider.getFromLocationName(locationName, lowerLeftLatitude, |
| 95 | lowerLeftLongitude, upperRightLatitude, upperRightLongitude, |
| 96 | maxResults, params, addrs); |
| 97 | } catch (RemoteException e) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 98 | Log.w(TAG, e); |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | return "Service not Available"; |
| 102 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 103 | |
Mike Lockwood | 628fd6d | 2010-01-25 22:46:13 -0500 | [diff] [blame] | 104 | } |