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