blob: 422b94b335f865cce23885f19bafcb7bd32d78f4 [file] [log] [blame]
Mike Lockwood628fd6d2010-01-25 22:46:13 -05001/*
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 Lockwood00b74272010-03-26 10:41:48 -040017package com.android.server.location;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050018
Mike Lockwood628fd6d2010-01-25 22:46:13 -050019import android.content.Context;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050020import android.location.Address;
21import android.location.GeocoderParams;
22import android.location.IGeocodeProvider;
Victoria Lease5cd731a2012-12-19 15:04:21 -080023import android.os.Handler;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050024import android.os.RemoteException;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050025import android.util.Log;
26
Nick Pelly6fa9ad42012-07-16 12:18:23 -070027import com.android.server.ServiceWatcher;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050028import java.util.List;
29
30/**
Nick Pelly6fa9ad42012-07-16 12:18:23 -070031 * Proxy for IGeocodeProvider implementations.
Mike Lockwood628fd6d2010-01-25 22:46:13 -050032 */
33public class GeocoderProxy {
Mike Lockwood628fd6d2010-01-25 22:46:13 -050034 private static final String TAG = "GeocoderProxy";
35
Nick Pelly6fa9ad42012-07-16 12:18:23 -070036 private static final String SERVICE_ACTION = "com.android.location.service.GeocodeProvider";
Nick Pelly00355d52012-05-27 16:12:45 -070037
Mike Lockwood628fd6d2010-01-25 22:46:13 -050038 private final Context mContext;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070039 private final ServiceWatcher mServiceWatcher;
Mike Lockwood628fd6d2010-01-25 22:46:13 -050040
Nick Pelly6fa9ad42012-07-16 12:18:23 -070041 public static GeocoderProxy createAndBind(Context context,
Zhentao Sunc5fc9982013-04-17 17:47:53 -070042 int overlaySwitchResId, int defaultServicePackageNameResId,
43 int initialPackageNamesResId, Handler handler) {
44 GeocoderProxy proxy = new GeocoderProxy(context, overlaySwitchResId,
45 defaultServicePackageNameResId, initialPackageNamesResId, handler);
Nick Pelly6fa9ad42012-07-16 12:18:23 -070046 if (proxy.bind()) {
47 return proxy;
48 } else {
49 return null;
50 }
51 }
52
Zhentao Sunc5fc9982013-04-17 17:47:53 -070053 private GeocoderProxy(Context context,
54 int overlaySwitchResId, int defaultServicePackageNameResId,
55 int initialPackageNamesResId, Handler handler) {
Mike Lockwood628fd6d2010-01-25 22:46:13 -050056 mContext = context;
Nick Pelly6fa9ad42012-07-16 12:18:23 -070057
Zhentao Sunc5fc9982013-04-17 17:47:53 -070058 mServiceWatcher = new ServiceWatcher(mContext, TAG, SERVICE_ACTION, overlaySwitchResId,
59 defaultServicePackageNameResId, initialPackageNamesResId, null, handler);
Mike Lockwood628fd6d2010-01-25 22:46:13 -050060 }
61
Nick Pelly6fa9ad42012-07-16 12:18:23 -070062 private boolean bind () {
63 return mServiceWatcher.start();
Mike Lockwoode97ae402010-09-29 15:23:46 -040064 }
65
Nick Pelly6fa9ad42012-07-16 12:18:23 -070066 private IGeocodeProvider getService() {
67 return IGeocodeProvider.Stub.asInterface(mServiceWatcher.getBinder());
68 }
Mark Vandevoorde8863c432010-10-04 14:23:24 -070069
Nick Pelly6fa9ad42012-07-16 12:18:23 -070070 public String getConnectedPackageName() {
71 return mServiceWatcher.getBestPackageName();
Mike Lockwood628fd6d2010-01-25 22:46:13 -050072 }
73
74 public String getFromLocation(double latitude, double longitude, int maxResults,
75 GeocoderParams params, List<Address> addrs) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -070076 IGeocodeProvider provider = getService();
Mike Lockwood628fd6d2010-01-25 22:46:13 -050077 if (provider != null) {
78 try {
Nick Pelly6fa9ad42012-07-16 12:18:23 -070079 return provider.getFromLocation(latitude, longitude, maxResults, params, addrs);
Mike Lockwood628fd6d2010-01-25 22:46:13 -050080 } catch (RemoteException e) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -070081 Log.w(TAG, e);
Mike Lockwood628fd6d2010-01-25 22:46:13 -050082 }
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 Pelly6fa9ad42012-07-16 12:18:23 -070091 IGeocodeProvider provider = getService();
Mike Lockwood628fd6d2010-01-25 22:46:13 -050092 if (provider != null) {
93 try {
94 return provider.getFromLocationName(locationName, lowerLeftLatitude,
95 lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
96 maxResults, params, addrs);
97 } catch (RemoteException e) {
Nick Pelly6fa9ad42012-07-16 12:18:23 -070098 Log.w(TAG, e);
Mike Lockwood628fd6d2010-01-25 22:46:13 -050099 }
100 }
101 return "Service not Available";
102 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700103
Mike Lockwood628fd6d2010-01-25 22:46:13 -0500104}