Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3534dddc authored by Soonil Nagarkar's avatar Soonil Nagarkar Committed by Android (Google) Code Review
Browse files

Merge "Make calls for geocoder one way"

parents f765b458 c178f80d
Loading
Loading
Loading
Loading
+56 −40
Original line number Diff line number Diff line
@@ -17,16 +17,16 @@
package android.location;

import android.content.Context;
import android.location.Address;
import android.os.RemoteException;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

import java.io.IOException;
import java.util.Locale;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * A class for handling geocoding and reverse geocoding.  Geocoding is
@@ -45,10 +45,11 @@ import java.util.List;
 * exists.
 */
public final class Geocoder {
    private static final String TAG = "Geocoder";

    private GeocoderParams mParams;
    private ILocationManager mService;
    private static final long TIMEOUT_MS = 60000;

    private final GeocoderParams mParams;
    private final ILocationManager mService;

    /**
     * Returns true if the Geocoder methods getFromLocation and
@@ -62,8 +63,7 @@ public final class Geocoder {
        try {
            return lm.geocoderIsPresent();
        } catch (RemoteException e) {
            Log.e(TAG, "isPresent: got RemoteException", e);
            return false;
            throw e.rethrowFromSystemServer();
        }
    }

@@ -129,17 +129,11 @@ public final class Geocoder {
            throw new IllegalArgumentException("longitude == " + longitude);
        }
        try {
            List<Address> results = new ArrayList<Address>();
            String ex =  mService.getFromLocation(latitude, longitude, maxResults,
                mParams, results);
            if (ex != null) {
                throw new IOException(ex);
            } else {
                return results;
            }
            GeocodeListener listener = new GeocodeListener();
            mService.getFromLocation(latitude, longitude, maxResults, mParams, listener);
            return listener.getResults();
        } catch (RemoteException e) {
            Log.e(TAG, "getFromLocation: got RemoteException", e);
            return null;
            throw e.rethrowFromSystemServer();
        }
    }

@@ -170,18 +164,13 @@ public final class Geocoder {
        if (locationName == null) {
            throw new IllegalArgumentException("locationName == null");
        }

        try {
            List<Address> results = new ArrayList<Address>();
            String ex = mService.getFromLocationName(locationName,
                0, 0, 0, 0, maxResults, mParams, results);
            if (ex != null) {
                throw new IOException(ex);
            } else {
                return results;
            }
            GeocodeListener listener = new GeocodeListener();
            mService.getFromLocationName(locationName, 0, 0, 0, 0, maxResults, mParams, listener);
            return listener.getResults();
        } catch (RemoteException e) {
            Log.e(TAG, "getFromLocationName: got RemoteException", e);
            return null;
            throw e.rethrowFromSystemServer();
        }
    }

@@ -242,19 +231,46 @@ public final class Geocoder {
            throw new IllegalArgumentException("upperRightLongitude == "
                + upperRightLongitude);
        }

        try {
            ArrayList<Address> result = new ArrayList<Address>();
            String ex =  mService.getFromLocationName(locationName,
                lowerLeftLatitude, lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
                maxResults, mParams, result);
            if (ex != null) {
                throw new IOException(ex);
            GeocodeListener listener = new GeocodeListener();
            mService.getFromLocationName(locationName, lowerLeftLatitude, lowerLeftLongitude,
                    upperRightLatitude, upperRightLongitude, maxResults, mParams, listener);
            return listener.getResults();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    private static class GeocodeListener extends IGeocodeListener.Stub {
        private final CountDownLatch mLatch = new CountDownLatch(1);

        private String mError = null;
        private List<Address> mResults = Collections.emptyList();

        GeocodeListener() {}

        @Override
        public void onResults(String error, List<Address> results) {
            mError = error;
            mResults = results;
            mLatch.countDown();
        }

        public List<Address> getResults() throws IOException {
            try {
                if (!mLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
                    mError = "Service not Available";
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            if (mError != null) {
                throw new IOException(mError);
            } else {
                return result;
                return mResults;
            }
        } catch (RemoteException e) {
            Log.e(TAG, "getFromLocationName: got RemoteException", e);
            return null;
        }
    }
}
+29 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.location;

import android.location.Address;

/**
 * An interface for returning geocode results.
 *
 * {@hide}
 */
interface IGeocodeListener {

    oneway void onResults(String error, in List<Address> results);
}
+4 −9
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.location;

import android.location.Address;
import android.location.IGeocodeListener;
import android.location.GeocoderParams;

/**
@@ -26,13 +27,7 @@ import android.location.GeocoderParams;
 */
interface IGeocodeProvider {

    @UnsupportedAppUsage
    String getFromLocation(double latitude, double longitude, int maxResults,
        in GeocoderParams params, out List<Address> addrs);

    @UnsupportedAppUsage
    String getFromLocationName(String locationName,
        double lowerLeftLatitude, double lowerLeftLongitude,
        double upperRightLatitude, double upperRightLongitude, int maxResults,
        in GeocoderParams params, out List<Address> addrs);
    oneway void getFromLocation(double latitude, double longitude, int maxResults, in GeocoderParams params, in IGeocodeListener listener);
    oneway void getFromLocationName(String locationName, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude,
        double upperRightLongitude, int maxResults, in GeocoderParams params, in IGeocodeListener listener);
}
+5 −4
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.location.Geofence;
import android.location.GnssMeasurementCorrections;
import android.location.GnssRequest;
import android.location.IBatchedLocationCallback;
import android.location.IGeocodeListener;
import android.location.IGnssAntennaInfoListener;
import android.location.IGnssMeasurementsListener;
import android.location.IGnssStatusListener;
@@ -58,12 +59,12 @@ interface ILocationManager
    void removeGeofence(in Geofence fence, in PendingIntent intent, String packageName);

    boolean geocoderIsPresent();
    String getFromLocation(double latitude, double longitude, int maxResults,
        in GeocoderParams params, out List<Address> addrs);
    String getFromLocationName(String locationName,
    void getFromLocation(double latitude, double longitude, int maxResults,
        in GeocoderParams params, in IGeocodeListener listener);
    void getFromLocationName(String locationName,
        double lowerLeftLatitude, double lowerLeftLongitude,
        double upperRightLatitude, double upperRightLongitude, int maxResults,
        in GeocoderParams params, out List<Address> addrs);
        in GeocoderParams params, in IGeocodeListener listener);

    long getGnssCapabilities();
    int getGnssYearOfHardware();
+25 −10
Original line number Diff line number Diff line
@@ -16,12 +16,14 @@

package com.android.location.provider;

import android.os.IBinder;

import android.location.Address;
import android.location.GeocoderParams;
import android.location.IGeocodeListener;
import android.location.IGeocodeProvider;
import android.os.IBinder;
import android.os.RemoteException;

import java.util.ArrayList;
import java.util.List;

/**
@@ -38,19 +40,32 @@ import java.util.List;
public abstract class GeocodeProvider {

    private IGeocodeProvider.Stub mProvider = new IGeocodeProvider.Stub() {
        public String getFromLocation(double latitude, double longitude, int maxResults,
                GeocoderParams params, List<Address> addrs) {
            return GeocodeProvider.this.onGetFromLocation(latitude, longitude, maxResults,
                    params, addrs);
        @Override
        public void getFromLocation(double latitude, double longitude, int maxResults,
                GeocoderParams params, IGeocodeListener listener) {
            List<Address> results = new ArrayList<>();
            String error = onGetFromLocation(latitude, longitude, maxResults, params, results);
            try {
                listener.onResults(error, results);
            } catch (RemoteException e) {
                // ignore
            }
        }

        public String getFromLocationName(String locationName,
        @Override
        public void getFromLocationName(String locationName,
                double lowerLeftLatitude, double lowerLeftLongitude,
                double upperRightLatitude, double upperRightLongitude, int maxResults,
                GeocoderParams params, List<Address> addrs) {
            return GeocodeProvider.this.onGetFromLocationName(locationName, lowerLeftLatitude,
                GeocoderParams params, IGeocodeListener listener) {
            List<Address> results = new ArrayList<>();
            String error = onGetFromLocationName(locationName, lowerLeftLatitude,
                    lowerLeftLongitude, upperRightLatitude, upperRightLongitude,
                    maxResults, params, addrs);
                    maxResults, params, results);
            try {
                listener.onResults(error, results);
            } catch (RemoteException e) {
                // ignore
            }
        }
    };

Loading