Home | History | Annotate | Download | only in location
      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 
     17 package com.android.server.location;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.ServiceConnection;
     23 import android.location.Address;
     24 import android.location.GeocoderParams;
     25 import android.location.IGeocodeProvider;
     26 import android.os.IBinder;
     27 import android.os.RemoteException;
     28 import android.os.SystemClock;
     29 import android.util.Log;
     30 
     31 import java.util.List;
     32 
     33 /**
     34  * A class for proxying IGeocodeProvider implementations.
     35  *
     36  * {@hide}
     37  */
     38 public class GeocoderProxy {
     39 
     40     private static final String TAG = "GeocoderProxy";
     41 
     42     private final Context mContext;
     43     private final Intent mIntent;
     44     private final Object mMutex = new Object();  // synchronizes access to mServiceConnection
     45     private Connection mServiceConnection = new Connection();  // never null
     46 
     47     public GeocoderProxy(Context context, String serviceName) {
     48         mContext = context;
     49         mIntent = new Intent(serviceName);
     50         mContext.bindService(mIntent, mServiceConnection,
     51                 Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND
     52                 | Context.BIND_ALLOW_OOM_MANAGEMENT);
     53     }
     54 
     55     /**
     56      * When unbundled NetworkLocationService package is updated, we
     57      * need to unbind from the old version and re-bind to the new one.
     58      */
     59     public void reconnect() {
     60         synchronized (mMutex) {
     61             mContext.unbindService(mServiceConnection);
     62             mServiceConnection = new Connection();
     63             mContext.bindService(mIntent, mServiceConnection,
     64                     Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND
     65                     | Context.BIND_ALLOW_OOM_MANAGEMENT);
     66         }
     67     }
     68 
     69     private class Connection implements ServiceConnection {
     70 
     71         private IGeocodeProvider mProvider;
     72 
     73         public void onServiceConnected(ComponentName className, IBinder service) {
     74             synchronized (this) {
     75                 mProvider = IGeocodeProvider.Stub.asInterface(service);
     76             }
     77         }
     78 
     79         public void onServiceDisconnected(ComponentName className) {
     80             synchronized (this) {
     81                 mProvider = null;
     82             }
     83         }
     84 
     85         public IGeocodeProvider getProvider() {
     86             synchronized (this) {
     87                 return mProvider;
     88             }
     89         }
     90     }
     91 
     92     public String getFromLocation(double latitude, double longitude, int maxResults,
     93             GeocoderParams params, List<Address> addrs) {
     94         IGeocodeProvider provider;
     95         synchronized (mMutex) {
     96             provider = mServiceConnection.getProvider();
     97         }
     98         if (provider != null) {
     99             try {
    100                 return provider.getFromLocation(latitude, longitude, maxResults,
    101                         params, addrs);
    102             } catch (RemoteException e) {
    103                 Log.e(TAG, "getFromLocation failed", e);
    104             }
    105         }
    106         return "Service not Available";
    107     }
    108 
    109     public String getFromLocationName(String locationName,
    110             double lowerLeftLatitude, double lowerLeftLongitude,
    111             double upperRightLatitude, double upperRightLongitude, int maxResults,
    112             GeocoderParams params, List<Address> addrs) {
    113         IGeocodeProvider provider;
    114         synchronized (mMutex) {
    115             provider = mServiceConnection.getProvider();
    116         }
    117         if (provider != null) {
    118             try {
    119                 return provider.getFromLocationName(locationName, lowerLeftLatitude,
    120                         lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
    121                         maxResults, params, addrs);
    122             } catch (RemoteException e) {
    123                 Log.e(TAG, "getFromLocationName failed", e);
    124             }
    125         }
    126         return "Service not Available";
    127     }
    128 }
    129