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