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.os.UserHandle;
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            List<String> initialPackageNames, int userId) {
43        GeocoderProxy proxy = new GeocoderProxy(context, initialPackageNames, userId);
44        if (proxy.bind()) {
45            return proxy;
46        } else {
47            return null;
48        }
49    }
50
51    public GeocoderProxy(Context context, List<String> initialPackageNames, int userId) {
52        mContext = context;
53
54        mServiceWatcher = new ServiceWatcher(mContext, TAG, SERVICE_ACTION, initialPackageNames,
55                null, null, userId);
56    }
57
58    private boolean bind () {
59        return mServiceWatcher.start();
60    }
61
62    private IGeocodeProvider getService() {
63        return IGeocodeProvider.Stub.asInterface(mServiceWatcher.getBinder());
64    }
65
66    public String getConnectedPackageName() {
67        return mServiceWatcher.getBestPackageName();
68    }
69
70    public String getFromLocation(double latitude, double longitude, int maxResults,
71            GeocoderParams params, List<Address> addrs) {
72        IGeocodeProvider provider = getService();
73        if (provider != null) {
74            try {
75                return provider.getFromLocation(latitude, longitude, maxResults, params, addrs);
76            } catch (RemoteException e) {
77                Log.w(TAG, e);
78            }
79        }
80        return "Service not Available";
81    }
82
83    public String getFromLocationName(String locationName,
84            double lowerLeftLatitude, double lowerLeftLongitude,
85            double upperRightLatitude, double upperRightLongitude, int maxResults,
86            GeocoderParams params, List<Address> addrs) {
87        IGeocodeProvider provider = getService();
88        if (provider != null) {
89            try {
90                return provider.getFromLocationName(locationName, lowerLeftLatitude,
91                        lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
92                        maxResults, params, addrs);
93            } catch (RemoteException e) {
94                Log.w(TAG, e);
95            }
96        }
97        return "Service not Available";
98    }
99
100}
101