GeocoderProxy.java revision 9158825f9c41869689d6b1786d7c7aa8bdd524ce
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
17package com.android.server.location;
18
19import android.content.Context;
20import android.location.Address;
21import android.location.GeocoderParams;
22import android.location.IGeocodeProvider;
23import android.os.Handler;
24import android.os.RemoteException;
25import android.os.UserHandle;
26import android.util.Log;
27
28import com.android.server.ServiceWatcher;
29import java.util.List;
30
31/**
32 * Proxy for IGeocodeProvider implementations.
33 */
34public 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