LocationManager.java revision 6fa9ad4afcd762aea519ff61811386c23d18ddb2
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.location;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.PendingIntent;
20e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pellyimport android.content.Context;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Intent;
222eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pellyimport android.os.Build;
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Bundle;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Looper;
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.RemoteException;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Handler;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Message;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3079762a3ee34eb8be5549bcb183af844b6f19c266Mike Lockwood
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.ArrayList;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.HashMap;
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.List;
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport com.android.internal.location.ProviderProperties;
366fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * This class provides access to the system location services.  These
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * services allow applications to obtain periodic updates of the
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * device's geographical location, or to fire an application-specified
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link Intent} when the device enters the proximity of a given
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * geographical location.
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>You do not
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * instantiate this class directly; instead, retrieve it through
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link android.content.Context#getSystemService
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Context.getSystemService(Context.LOCATION_SERVICE)}.
483aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez *
493aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
503aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
513aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about using location services, read the
523aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/location/index.html">Location and Maps</a>
533aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * developer guide.</p>
543aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class LocationManager {
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final String TAG = "LocationManager";
58e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly
59e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    private final Context mContext;
60e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    private final ILocationManager mService;
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final HashMap<GpsStatus.Listener, GpsStatusListenerTransport> mGpsStatusListeners =
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            new HashMap<GpsStatus.Listener, GpsStatusListenerTransport>();
63b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood    private final HashMap<GpsStatus.NmeaListener, GpsStatusListenerTransport> mNmeaListeners =
64b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            new HashMap<GpsStatus.NmeaListener, GpsStatusListenerTransport>();
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final GpsStatus mGpsStatus = new GpsStatus();
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Name of the network location provider.  This provider determines location based on
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * availability of cell tower and WiFi access points. Results are retrieved
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * by means of a network lookup.
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or android.permission.ACCESS_FINE_LOCATION.
746fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link Criteria} class instead
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
766fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String NETWORK_PROVIDER = "network";
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Name of the GPS location provider. This provider determines location using
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * satellites. Depending on conditions, this provider may take a while to return
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a location fix.
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
8455ed4914de69f30c1067bf1894b193f89dfd53b3Kenny Root     * Requires the permission android.permission.ACCESS_FINE_LOCATION.
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> The extras Bundle for the GPS location provider can contain the
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * following key/value pairs:
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <ul>
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <li> satellites - the number of satellites used to derive the fix
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * </ul>
926fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link Criteria} class instead
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
946fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String GPS_PROVIDER = "gps";
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
98a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood     * A special location provider for receiving locations without actually initiating
99a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood     * a location fix. This provider can be used to passively receive location updates
100a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood     * when other applications or services request them without actually requesting
101a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood     * the locations yourself.  This provider will return locations generated by other
102a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood     * providers.  You can query the {@link Location#getProvider()} method to determine
103a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood     * the origin of the location update.
104a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood     *
105a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood     * Requires the permission android.permission.ACCESS_FINE_LOCATION, although if the GPS
106a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood     * is not enabled this provider might only return coarse fixes.
1076fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link Criteria} class instead
108a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood     */
1096fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
110a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood    public static final String PASSIVE_PROVIDER = "passive";
111a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood
112a4903f254b4711c8fc0ac5f7e3d605f4dce34f35Mike Lockwood    /**
1136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Name of the Fused location provider.<p>
1146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * This provider combines inputs for all possible location sources
1156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * to provide the best possible Location fix.<p>
1166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     *
1176fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Requires the permission android.permission.ACCESS_FINE_LOCATION.
1186fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @hide
1196fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     */
1206fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public static final String FUSED_PROVIDER = "fused";
1216fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1226fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    /**
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Key used for the Bundle extra holding a boolean indicating whether
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a proximity alert is entering (true) or exiting (false)..
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String KEY_PROXIMITY_ENTERING = "entering";
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Key used for a Bundle extra holding an Integer status value
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * when a status change is broadcast using a PendingIntent.
1316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link Criteria} class instead
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String KEY_STATUS_CHANGED = "status";
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Key used for a Bundle extra holding an Boolean status value
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * when a provider enabled/disabled event is broadcast using a PendingIntent.
1396fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link Criteria} class instead
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1416fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String KEY_PROVIDER_ENABLED = "providerEnabled";
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Key used for a Bundle extra holding a Location value
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * when a location change is broadcast using a PendingIntent.
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String KEY_LOCATION_CHANGED = "location";
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15000b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood    /**
15100b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * Broadcast intent action indicating that the GPS has either been
15200b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * enabled or disabled. An intent extra provides this state as a boolean,
15300b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * where {@code true} means enabled.
15400b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * @see #EXTRA_GPS_ENABLED
15500b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     *
15600b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * {@hide}
15700b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     */
15800b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood    public static final String GPS_ENABLED_CHANGE_ACTION =
15900b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood        "android.location.GPS_ENABLED_CHANGE";
16000b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood
16100b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood    /**
1620c5a04014d3833c9a82772a832d3bc6410fc52acBrad Fitzpatrick     * Broadcast intent action when the configured location providers
1630c5a04014d3833c9a82772a832d3bc6410fc52acBrad Fitzpatrick     * change.
1646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link Criteria} class instead
1650c5a04014d3833c9a82772a832d3bc6410fc52acBrad Fitzpatrick     */
1666fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
1670c5a04014d3833c9a82772a832d3bc6410fc52acBrad Fitzpatrick    public static final String PROVIDERS_CHANGED_ACTION =
1680c5a04014d3833c9a82772a832d3bc6410fc52acBrad Fitzpatrick        "android.location.PROVIDERS_CHANGED";
1690c5a04014d3833c9a82772a832d3bc6410fc52acBrad Fitzpatrick
1700c5a04014d3833c9a82772a832d3bc6410fc52acBrad Fitzpatrick    /**
17100b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * Broadcast intent action indicating that the GPS has either started or
17200b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * stopped receiving GPS fixes. An intent extra provides this state as a
17300b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * boolean, where {@code true} means that the GPS is actively receiving fixes.
17400b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * @see #EXTRA_GPS_ENABLED
17500b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     *
17600b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * {@hide}
17700b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     */
17800b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood    public static final String GPS_FIX_CHANGE_ACTION =
17900b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood        "android.location.GPS_FIX_CHANGE";
18000b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood
18100b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood    /**
18200b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * The lookup key for a boolean that indicates whether GPS is enabled or
18300b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * disabled. {@code true} means GPS is enabled. Retrieve it with
18400b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * {@link android.content.Intent#getBooleanExtra(String,boolean)}.
18500b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     *
18600b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     * {@hide}
18700b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood     */
18800b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood    public static final String EXTRA_GPS_ENABLED = "enabled";
18900b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Map from LocationListeners to their associated ListenerTransport objects
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private HashMap<LocationListener,ListenerTransport> mListeners =
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        new HashMap<LocationListener,ListenerTransport>();
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private class ListenerTransport extends ILocationListener.Stub {
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        private static final int TYPE_LOCATION_CHANGED = 1;
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        private static final int TYPE_STATUS_CHANGED = 2;
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        private static final int TYPE_PROVIDER_ENABLED = 3;
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        private static final int TYPE_PROVIDER_DISABLED = 4;
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        private LocationListener mListener;
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        private final Handler mListenerHandler;
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ListenerTransport(LocationListener listener, Looper looper) {
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mListener = listener;
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (looper == null) {
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mListenerHandler = new Handler() {
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    @Override
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    public void handleMessage(Message msg) {
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        _handleMessage(msg);
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                };
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mListenerHandler = new Handler(looper) {
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    @Override
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    public void handleMessage(Message msg) {
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        _handleMessage(msg);
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                };
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
223e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        @Override
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onLocationChanged(Location location) {
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Message msg = Message.obtain();
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            msg.what = TYPE_LOCATION_CHANGED;
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            msg.obj = location;
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mListenerHandler.sendMessage(msg);
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
231e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        @Override
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onStatusChanged(String provider, int status, Bundle extras) {
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Message msg = Message.obtain();
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            msg.what = TYPE_STATUS_CHANGED;
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Bundle b = new Bundle();
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            b.putString("provider", provider);
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            b.putInt("status", status);
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (extras != null) {
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                b.putBundle("extras", extras);
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            msg.obj = b;
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mListenerHandler.sendMessage(msg);
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
245e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        @Override
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onProviderEnabled(String provider) {
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Message msg = Message.obtain();
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            msg.what = TYPE_PROVIDER_ENABLED;
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            msg.obj = provider;
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mListenerHandler.sendMessage(msg);
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
253e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        @Override
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onProviderDisabled(String provider) {
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Message msg = Message.obtain();
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            msg.what = TYPE_PROVIDER_DISABLED;
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            msg.obj = provider;
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mListenerHandler.sendMessage(msg);
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        private void _handleMessage(Message msg) {
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            switch (msg.what) {
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                case TYPE_LOCATION_CHANGED:
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    Location location = new Location((Location) msg.obj);
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    mListener.onLocationChanged(location);
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    break;
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                case TYPE_STATUS_CHANGED:
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    Bundle b = (Bundle) msg.obj;
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    String provider = b.getString("provider");
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    int status = b.getInt("status");
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    Bundle extras = b.getBundle("extras");
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    mListener.onStatusChanged(provider, status, extras);
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    break;
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                case TYPE_PROVIDER_ENABLED:
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    mListener.onProviderEnabled((String) msg.obj);
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    break;
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                case TYPE_PROVIDER_DISABLED:
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    mListener.onProviderDisabled((String) msg.obj);
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    break;
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
28148f17518223a2917fd82195004fbb206cb9a05a3Mike Lockwood            try {
28248f17518223a2917fd82195004fbb206cb9a05a3Mike Lockwood                mService.locationCallbackFinished(this);
28348f17518223a2917fd82195004fbb206cb9a05a3Mike Lockwood            } catch (RemoteException e) {
28448f17518223a2917fd82195004fbb206cb9a05a3Mike Lockwood                Log.e(TAG, "locationCallbackFinished: RemoteException", e);
28548f17518223a2917fd82195004fbb206cb9a05a3Mike Lockwood            }
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide - hide this constructor because it has a parameter
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * of type ILocationManager, which is a system private class. The
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * right way to create an instance of this class is using the
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * factory Context.getSystemService.
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
294e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly    public LocationManager(Context context, ILocationManager service) {
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mService = service;
296e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        mContext = context;
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2996fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private LocationProvider createProvider(String name, ProviderProperties properties) {
3006fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        return new LocationProvider(name, properties);
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a list of the names of all known location providers.  All
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * providers are returned, including ones that are not permitted to be
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * accessed by the calling activity or are currently disabled.
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return list of Strings containing names of the providers
3096fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link Criteria} class instead
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public List<String> getAllProviders() {
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return mService.getAllProviders();
3156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
3166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return null;
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a list of the names of location providers.  Only providers that
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * are permitted to be accessed by the calling activity will be returned.
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param enabledOnly if true then only the providers which are currently
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * enabled are returned.
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return list of Strings containing names of the providers
3286fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated The {@link LocationProvider} class is deprecated. So
3296fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * use the {@link Criteria} class to request location instead of
3306fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * enumerating providers.
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public List<String> getProviders(boolean enabledOnly) {
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
33503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood            return mService.getProviders(null, enabledOnly);
3366fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
3376fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return null;
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the information associated with the location provider of the
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * given name, or null if no provider exists by that name.
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param name the provider name
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return a LocationProvider, or null
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3496fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @throws IllegalArgumentException if name is null or does not exisit
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if the caller is not permitted to access the
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * given provider.
3526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated The {@link LocationProvider} class is deprecated. So
3536fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * use the {@link Criteria} class to request location instead of
3546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * enumerating providers.
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public LocationProvider getProvider(String name) {
3586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkProvider(name);
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
3606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            ProviderProperties properties = mService.getProviderProperties(name);
3616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            if (properties == null) {
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return null;
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return createProvider(name, properties);
3656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
3666fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return null;
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a list of the names of LocationProviders that satisfy the given
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * criteria, or null if none do.  Only providers that are permitted to be
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * accessed by the calling activity will be returned.
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param criteria the criteria that the returned providers must match
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param enabledOnly if true then only the providers which are currently
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * enabled are returned.
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return list of Strings containing names of the providers
3806fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated The {@link LocationProvider} class is deprecated. So
3816fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * use the {@link Criteria} class to request location instead of
3826fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * enumerating providers.
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3846fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
3866fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkCriteria(criteria);
38703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood        try {
38803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood            return mService.getProviders(criteria, enabledOnly);
3896fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
3906fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
39203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood        return null;
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the name of the provider that best meets the given criteria. Only providers
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * that are permitted to be accessed by the calling activity will be
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * returned.  If several providers meet the criteria, the one with the best
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * accuracy is returned.  If no provider meets the criteria,
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the criteria are loosened in the following sequence:
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <ul>
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <li> power requirement
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <li> accuracy
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <li> bearing
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <li> speed
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <li> altitude
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * </ul>
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> Note that the requirement on monetary cost is not removed
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * in this process.
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param criteria the criteria that need to be matched
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param enabledOnly if true then only a provider that is currently enabled is returned
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return name of the provider that best matches the requirements
4166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated using an explicit provider doesn't allow fused location
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4186fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public String getBestProvider(Criteria criteria, boolean enabledOnly) {
4206fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkCriteria(criteria);
42103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood        try {
42203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood            return mService.getBestProvider(criteria, enabledOnly);
4236fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
4246fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return null;
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Registers the current activity to be notified periodically by
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the named provider.  Periodically, the supplied LocationListener will
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * be called with the current Location or with status updates.
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
434f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> It may take a while to receive the first location update. If
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * an immediate location is required, applications may use the
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #getLastKnownLocation(String)} method.
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> In case the provider is disabled by the user, updates will stop,
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and the {@link LocationListener#onProviderDisabled(String)}
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * method will be called. As soon as the provider is enabled again,
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the {@link LocationListener#onProviderEnabled(String)} method will
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * be called and location updates will start again.
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
444f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> The update interval can be controlled using the minTime parameter.
445f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * The elapsed time between location updates will never be less than
446f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * minTime, although it can be more depending on the Location Provider
447f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * implementation and the update interval requested by other applications.
448f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
449f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> Choosing a sensible value for minTime is important to conserve
450f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * battery life. Each location update requires power from
451f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * GPS, WIFI, Cell and other radios. Select a minTime value as high as
452f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * possible while still providing a reasonable user experience.
453f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * If your application is not in the foreground and showing
454f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location to the user then your application should avoid using an active
455f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * provider (such as {@link #NETWORK_PROVIDER} or {@link #GPS_PROVIDER}),
456f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * but if you insist then select a minTime of 5 * 60 * 1000 (5 minutes)
457f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * or greater. If your application is in the foreground and showing
458f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location to the user then it is appropriate to select a faster
459f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * update interval.
460f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
461f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> The minDistance parameter can also be used to control the
462f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * frequency of location updates. If it is greater than 0 then the
463f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location provider will only send your application an update when
464f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * the location has changed by at least minDistance meters, AND
465f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * at least minTime milliseconds have passed. However it is more
466f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * difficult for location providers to save power using the minDistance
467f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * parameter, so minTime should be the primary tool to conserving battery
468f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * life.
469f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
470f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> If your application wants to passively observe location
471f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * updates triggered by other applications, but not consume
472f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * any additional power otherwise, then use the {@link #PASSIVE_PROVIDER}
473f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * This provider does not actively turn on or modify active location
474f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * providers, so you do not need to be as careful about minTime and
475f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * minDistance. However if your application performs heavy work
476f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * on a location update (such as network activity) then you should
477f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * select non-zero values for minTime and/or minDistance to rate-limit
478f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * your update frequency in the case another application enables a
479f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location provider with extremely fast updates.
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> The calling thread must be a {@link android.os.Looper} thread such as
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the main thread of the calling Activity.
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
484f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p class="note"> Prior to Jellybean, the minTime parameter was
485f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * only a hint, and some location provider implementations ignored it.
486f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * From Jellybean and onwards it is mandatory for Android compatible
487f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * devices to observe both the minTime and minDistance parameters.
488f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the name of the provider with which to register
490f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @param minTime minimum time interval between location updates, in milliseconds
491f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @param minDistance minimum distance between location updates, in meters
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param listener a {#link LocationListener} whose
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link LocationListener#onLocationChanged} method will be called for
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * each location update
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
496f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @throws IllegalArgumentException if provider is null or doesn't exist
497f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * on this device
498f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @throws IllegalArgumentException if listener is null
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws RuntimeException if the calling thread has no Looper
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if no suitable permission is present for the provider.
5016fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5036fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
5046fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void requestLocationUpdates(String provider, long minTime, float minDistance,
5056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            LocationListener listener) {
5066fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkProvider(provider);
5076fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkListener(listener);
5086fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
5096fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedProvider(
5106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                provider, minTime, minDistance, false);
5116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, listener, null, null);
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Registers the current activity to be notified periodically by
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the named provider.  Periodically, the supplied LocationListener will
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * be called with the current Location or with status updates.
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
519f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> It may take a while to receive the first location update. If
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * an immediate location is required, applications may use the
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #getLastKnownLocation(String)} method.
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> In case the provider is disabled by the user, updates will stop,
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and the {@link LocationListener#onProviderDisabled(String)}
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * method will be called. As soon as the provider is enabled again,
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the {@link LocationListener#onProviderEnabled(String)} method will
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * be called and location updates will start again.
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
529f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> The update interval can be controlled using the minTime parameter.
530f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * The elapsed time between location updates will never be less than
531f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * minTime, although it can be more depending on the Location Provider
532f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * implementation and the update interval requested by other applications.
533f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
534f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> Choosing a sensible value for minTime is important to conserve
535f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * battery life. Each location update requires power from
536f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * GPS, WIFI, Cell and other radios. Select a minTime value as high as
537f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * possible while still providing a reasonable user experience.
538f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * If your application is not in the foreground and showing
539f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location to the user then your application should avoid using an active
540f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * provider (such as {@link #NETWORK_PROVIDER} or {@link #GPS_PROVIDER}),
541f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * but if you insist then select a minTime of 5 * 60 * 1000 (5 minutes)
542f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * or greater. If your application is in the foreground and showing
543f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location to the user then it is appropriate to select a faster
544f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * update interval.
545f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
546f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> The minDistance parameter can also be used to control the
547f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * frequency of location updates. If it is greater than 0 then the
548f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location provider will only send your application an update when
549f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * the location has changed by at least minDistance meters, AND
550f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * at least minTime milliseconds have passed. However it is more
551f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * difficult for location providers to save power using the minDistance
552f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * parameter, so minTime should be the primary tool to conserving battery
553f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * life.
554f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
555f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> If your application wants to passively observe location
556f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * updates triggered by other applications, but not consume
557f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * any additional power otherwise, then use the {@link #PASSIVE_PROVIDER}
558f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * This provider does not actively turn on or modify active location
559f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * providers, so you do not need to be as careful about minTime and
560f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * minDistance. However if your application performs heavy work
561f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * on a location update (such as network activity) then you should
562f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * select non-zero values for minTime and/or minDistance to rate-limit
563f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * your update frequency in the case another application enables a
564f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location provider with extremely fast updates.
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> The supplied Looper is used to implement the callback mechanism.
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
568f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p class="note"> Prior to Jellybean, the minTime parameter was
569f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * only a hint, and some location provider implementations ignored it.
570f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * From Jellybean and onwards it is mandatory for Android compatible
571f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * devices to observe both the minTime and minDistance parameters.
572f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the name of the provider with which to register
574f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @param minTime minimum time interval between location updates, in milliseconds
575f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @param minDistance minimum distance between location updates, in meters
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param listener a {#link LocationListener} whose
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link LocationListener#onLocationChanged} method will be called for
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * each location update
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param looper a Looper object whose message queue will be used to
580f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * implement the callback mechanism, or null to make callbacks on the
581f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * main thread
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if provider is null or doesn't exist
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if listener is null
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if no suitable permission is present for the provider.
5866fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5886fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
5896fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void requestLocationUpdates(String provider, long minTime, float minDistance,
5906fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            LocationListener listener, Looper looper) {
5916fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkProvider(provider);
5926fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkListener(listener);
5936fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
5946fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedProvider(
5956fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                provider, minTime, minDistance, false);
5966fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, listener, looper, null);
59703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    }
59803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood
59903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    /**
60003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * Registers the current activity to be notified periodically based on
601f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * the supplied criteria.  Periodically, the supplied LocationListener will
60203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * be called with the current Location or with status updates.
60303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
604f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> It may take a while to receive the first location update. If
60503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * an immediate location is required, applications may use the
60603ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * {@link #getLastKnownLocation(String)} method.
60703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
60803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * <p> In case the provider is disabled by the user, updates will stop,
60903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * and the {@link LocationListener#onProviderDisabled(String)}
61003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * method will be called. As soon as the provider is enabled again,
61103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * the {@link LocationListener#onProviderEnabled(String)} method will
61203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * be called and location updates will start again.
61303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
614f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> The update interval can be controlled using the minTime parameter.
615f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * The elapsed time between location updates will never be less than
616f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * minTime, although it can be more depending on the Location Provider
617f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * implementation and the update interval requested by other applications.
618f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
619f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> Choosing a sensible value for minTime is important to conserve
620f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * battery life. Each location update requires power from
621f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * GPS, WIFI, Cell and other radios. Select a minTime value as high as
622f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * possible while still providing a reasonable user experience.
623f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * If your application is not in the foreground and showing
624f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location to the user then your application should avoid using an active
625f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * provider (such as {@link #NETWORK_PROVIDER} or {@link #GPS_PROVIDER}),
626f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * but if you insist then select a minTime of 5 * 60 * 1000 (5 minutes)
627f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * or greater. If your application is in the foreground and showing
628f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location to the user then it is appropriate to select a faster
629f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * update interval.
630f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
631f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> The minDistance parameter can also be used to control the
632f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * frequency of location updates. If it is greater than 0 then the
633f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location provider will only send your application an update when
634f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * the location has changed by at least minDistance meters, AND
635f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * at least minTime milliseconds have passed. However it is more
636f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * difficult for location providers to save power using the minDistance
637f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * parameter, so minTime should be the primary tool to conserving battery
638f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * life.
63903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
64003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * <p> The supplied Looper is used to implement the callback mechanism.
64103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
642f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p class="note"> Prior to Jellybean, the minTime parameter was
643f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * only a hint, and some location provider implementations ignored it.
644f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * From Jellybean and onwards it is mandatory for Android compatible
645f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * devices to observe both the minTime and minDistance parameters.
646f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
647f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @param minTime minimum time interval between location updates, in milliseconds
648f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @param minDistance minimum distance between location updates, in meters
64903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param criteria contains parameters for the location manager to choose the
65003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * appropriate provider and parameters to compute the location
65103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param listener a {#link LocationListener} whose
65203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * {@link LocationListener#onLocationChanged} method will be called for
65303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * each location update
65403ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param looper a Looper object whose message queue will be used to
655f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * implement the callback mechanism, or null to make callbacks on the
656f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * main thread.
65703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
65803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if criteria is null
65903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if listener is null
660f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @throws SecurityException if no suitable permission is present for the provider.
6616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
66203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     */
6636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
6646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void requestLocationUpdates(long minTime, float minDistance, Criteria criteria,
6656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            LocationListener listener, Looper looper) {
6666fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkCriteria(criteria);
6676fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkListener(listener);
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6696fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedCriteria(
6706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                criteria, minTime, minDistance, false);
6716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, listener, looper, null);
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Registers the current activity to be notified periodically by
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the named provider.  Periodically, the supplied PendingIntent will
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * be broadcast with the current Location or with status updates.
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
679f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> Location updates are sent with a key of
680f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * {@link #KEY_LOCATION_CHANGED} and a {@link android.location.Location} value.
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
682f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> It may take a while to receive the first location update. If
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * an immediate location is required, applications may use the
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #getLastKnownLocation(String)} method.
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
686f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> The update interval can be controlled using the minTime parameter.
687f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * The elapsed time between location updates will never be less than
688f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * minTime, although it can be more depending on the Location Provider
689f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * implementation and the update interval requested by other applications.
690f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
691f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> Choosing a sensible value for minTime is important to conserve
692f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * battery life. Each location update requires power from
693f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * GPS, WIFI, Cell and other radios. Select a minTime value as high as
694f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * possible while still providing a reasonable user experience.
695f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * If your application is not in the foreground and showing
696f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location to the user then your application should avoid using an active
697f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * provider (such as {@link #NETWORK_PROVIDER} or {@link #GPS_PROVIDER}),
698f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * but if you insist then select a minTime of 5 * 60 * 1000 (5 minutes)
699f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * or greater. If your application is in the foreground and showing
700f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location to the user then it is appropriate to select a faster
701f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * update interval.
702f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
703f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> The minDistance parameter can also be used to control the
704f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * frequency of location updates. If it is greater than 0 then the
705f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location provider will only send your application an update when
706f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * the location has changed by at least minDistance meters, AND
707f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * at least minTime milliseconds have passed. However it is more
708f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * difficult for location providers to save power using the minDistance
709f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * parameter, so minTime should be the primary tool to conserving battery
710f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * life.
711f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
712f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> If your application wants to passively observe location
713f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * updates triggered by other applications, but not consume
714f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * any additional power otherwise, then use the {@link #PASSIVE_PROVIDER}
715f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * This provider does not actively turn on or modify active location
716f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * providers, so you do not need to be as careful about minTime and
717f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * minDistance. However if your application performs heavy work
718f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * on a location update (such as network activity) then you should
719f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * select non-zero values for minTime and/or minDistance to rate-limit
720f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * your update frequency in the case another application enables a
721f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location provider with extremely fast updates.
722f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
723f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> If the provider is disabled by the user, updates will stop,
724f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * and an intent will be sent with an extra with key
725f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * {@link #KEY_PROVIDER_ENABLED} and a boolean value of false.
726f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * If the provider is re-enabled, an intent will be sent with an
727f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * extra with key {@link #KEY_PROVIDER_ENABLED} and a boolean value of
728f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * true and location updates will start again.
729f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
730f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> If the provider's status changes, an intent will be sent with
731f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * an extra with key {@link #KEY_STATUS_CHANGED} and an integer value
732f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * indicating the new status.  Any extras associated with the status
733f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * update will be sent as well.
734f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
735f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p class="note"> Prior to Jellybean, the minTime parameter was
736f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * only a hint, and some location provider implementations ignored it.
737f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * From Jellybean and onwards it is mandatory for Android compatible
738f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * devices to observe both the minTime and minDistance parameters.
7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
7409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the name of the provider with which to register
741f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @param minTime minimum time interval between location updates, in milliseconds
742f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @param minDistance minimum distance between location updates, in meters
74303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param intent a {#link PendingIntent} to be sent for each location update
7449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
7459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if provider is null or doesn't exist
746f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * on this device
7479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if intent is null
7489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if no suitable permission is present for the provider.
7496fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7516fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
7526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void requestLocationUpdates(String provider, long minTime, float minDistance,
7536fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            PendingIntent intent) {
7546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkProvider(provider);
7556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
7566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
7576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedProvider(
7586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                provider, minTime, minDistance, false);
7596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, null, null, intent);
7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
76203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    /**
76303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * Registers the current activity to be notified periodically based on
764f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * the supplied criteria.  Periodically, the supplied PendingIntent will
76503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * be broadcast with the current Location or with status updates.
76603ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
767f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> Location updates are sent with a key of
768f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * {@link #KEY_LOCATION_CHANGED} and a {@link android.location.Location} value.
76903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
770f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> It may take a while to receive the first location update. If
77103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * an immediate location is required, applications may use the
77203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * {@link #getLastKnownLocation(String)} method.
77303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
774f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> The update interval can be controlled using the minTime parameter.
775f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * The elapsed time between location updates will never be less than
776f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * minTime, although it can be more depending on the Location Provider
777f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * implementation and the update interval requested by other applications.
778f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
779f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> Choosing a sensible value for minTime is important to conserve
780f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * battery life. Each location update requires power from
781f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * GPS, WIFI, Cell and other radios. Select a minTime value as high as
782f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * possible while still providing a reasonable user experience.
783f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * If your application is not in the foreground and showing
784f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location to the user then your application should avoid using an active
785f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * provider (such as {@link #NETWORK_PROVIDER} or {@link #GPS_PROVIDER}),
786f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * but if you insist then select a minTime of 5 * 60 * 1000 (5 minutes)
787f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * or greater. If your application is in the foreground and showing
788f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location to the user then it is appropriate to select a faster
789f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * update interval.
790f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
791f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> The minDistance parameter can also be used to control the
792f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * frequency of location updates. If it is greater than 0 then the
793f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * location provider will only send your application an update when
794f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * the location has changed by at least minDistance meters, AND
795f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * at least minTime milliseconds have passed. However it is more
796f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * difficult for location providers to save power using the minDistance
797f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * parameter, so minTime should be the primary tool to conserving battery
798f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * life.
799f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
800f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> If the provider is disabled by the user, updates will stop,
801f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * and an intent will be sent with an extra with key
802f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * {@link #KEY_PROVIDER_ENABLED} and a boolean value of false.
803f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * If the provider is re-enabled, an intent will be sent with an
804f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * extra with key {@link #KEY_PROVIDER_ENABLED} and a boolean value of
805f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * true and location updates will start again.
806f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
807f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> If the provider's status changes, an intent will be sent with
808f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * an extra with key {@link #KEY_STATUS_CHANGED} and an integer value
809f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * indicating the new status.  Any extras associated with the status
810f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * update will be sent as well.
811f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
812f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p class="note"> Prior to Jellybean, the minTime parameter was
813f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * only a hint, and some location provider implementations ignored it.
814f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * From Jellybean and onwards it is mandatory for Android compatible
815f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * devices to observe both the minTime and minDistance parameters.
816f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     *
817f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @param minTime minimum time interval between location updates, in milliseconds
818f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @param minDistance minimum distance between location updates, in meters
81903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param criteria contains parameters for the location manager to choose the
82003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * appropriate provider and parameters to compute the location
82103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param intent a {#link PendingIntent} to be sent for each location update
82203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
823f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @throws IllegalArgumentException if criteria is null
82403ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if intent is null
82503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws SecurityException if no suitable permission is present for the provider.
8266fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
82703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     */
8286fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
829f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly    public void requestLocationUpdates(long minTime, float minDistance, Criteria criteria,
830f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly            PendingIntent intent) {
8316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkCriteria(criteria);
8326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
8339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedCriteria(
8356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                criteria, minTime, minDistance, false);
8366fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, null, null, intent);
8379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
840e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * Requests a single location update from the named provider.
84103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
84203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * <p> It may take a while to receive the most recent location. If
84303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * an immediate location is required, applications may use the
84403ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * {@link #getLastKnownLocation(String)} method.
84503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
846e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * <p> In case the provider is disabled by the user, the update will not be received,
84703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * and the {@link LocationListener#onProviderDisabled(String)}
84803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * method will be called. As soon as the provider is enabled again,
84903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * the {@link LocationListener#onProviderEnabled(String)} method will
85003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * be called and location updates will start again.
85103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
85203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * <p> The supplied Looper is used to implement the callback mechanism.
85303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
85403ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param provider the name of the provider with which to register
85503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param listener a {#link LocationListener} whose
856e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * {@link LocationListener#onLocationChanged} method will be called when
857e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * the location update is available
85803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param looper a Looper object whose message queue will be used to
859f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * implement the callback mechanism, or null to make callbacks on the
860f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * main thread
86103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
86203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if provider is null or doesn't exist
86303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if listener is null
864f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @throws SecurityException if no suitable permission is present for the provider
8656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
86603ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     */
8676fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
86803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    public void requestSingleUpdate(String provider, LocationListener listener, Looper looper) {
8696fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkProvider(provider);
8706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkListener(listener);
8716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
8726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedProvider(
8736fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                provider, 0, 0, true);
8746fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, listener, looper, null);
87503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    }
87603ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood
87703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    /**
878e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * Requests a single location update based on the specified criteria.
87903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
88003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * <p> It may take a while to receive the most recent location. If
88103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * an immediate location is required, applications may use the
88203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * {@link #getLastKnownLocation(String)} method.
88303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
884e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * <p> In case the provider is disabled by the user, the update will not be received,
88503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * and the {@link LocationListener#onProviderDisabled(String)}
88603ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * method will be called. As soon as the provider is enabled again,
88703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * the {@link LocationListener#onProviderEnabled(String)} method will
88803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * be called and location updates will start again.
88903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
89003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * <p> The supplied Looper is used to implement the callback mechanism.
89103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
89203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param criteria contains parameters for the location manager to choose the
89303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * appropriate provider and parameters to compute the location
89403ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param listener a {#link LocationListener} whose
895e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * {@link LocationListener#onLocationChanged} method will be called when
896e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * the location update is available
89703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param looper a Looper object whose message queue will be used to
898f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * implement the callback mechanism, or null to make callbacks on the
899f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * main thread
90003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
90103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if criteria is null
90203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if listener is null
90303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws SecurityException if no suitable permission is present to access
904f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * the location services
9056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
90603ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     */
9076fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
90803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    public void requestSingleUpdate(Criteria criteria, LocationListener listener, Looper looper) {
9096fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkCriteria(criteria);
9106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkListener(listener);
9116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
9126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedCriteria(
9136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                criteria, 0, 0, true);
9146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, listener, looper, null);
91503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    }
91603ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood
91703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    /**
918e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * Requests a single location update from the named provider.
91903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
92003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * <p> It may take a while to receive the most recent location. If
92103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * an immediate location is required, applications may use the
92203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * {@link #getLastKnownLocation(String)} method.
92303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
924f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> Location updates are sent with a key of
925f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * {@link #KEY_LOCATION_CHANGED} and a {@link android.location.Location} value.
926e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     *
927e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * <p> In case the provider is disabled by the user, the update will not be received,
928e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * and the {@link LocationListener#onProviderDisabled(String)}
929e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * method will be called. As soon as the provider is enabled again,
930e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * the {@link LocationListener#onProviderEnabled(String)} method will
931e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * be called and location updates will start again.
932e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     *
93303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param provider the name of the provider with which to register
93403ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param intent a {#link PendingIntent} to be sent for the location update
93503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
93603ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if provider is null or doesn't exist
93703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if intent is null
938f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @throws SecurityException if no suitable permission is present for the provider
9396fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
94003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     */
9416fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
94203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    public void requestSingleUpdate(String provider, PendingIntent intent) {
9436fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkProvider(provider);
9446fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
9456fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
9466fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedProvider(
9476fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                provider, 0, 0, true);
9486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, null, null, intent);
94903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    }
95003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood
95103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    /**
952e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     * Requests a single location update based on the specified criteria.
95303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
95403ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * <p> It may take a while to receive the most recent location. If
95503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * an immediate location is required, applications may use the
95603ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * {@link #getLastKnownLocation(String)} method.
95703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
958f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> Location updates are sent with a key of
959f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * {@link #KEY_LOCATION_CHANGED} and a {@link android.location.Location} value.
960e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     *
961f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * <p> If the provider is disabled by the user, an update will not be
962f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * received, and an intent will be sent with an extra with key
963f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * {@link #KEY_PROVIDER_ENABLED} and a boolean value of false.
964f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * If the provider is re-enabled, an intent will be sent with an
965f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * extra with key {@link #KEY_PROVIDER_ENABLED} and a boolean value of
966f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * true and the location update will occur.
967e23fc8ee13605dace4a7abcdf85caec639407786Amith Yamasani     *
96803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param criteria contains parameters for the location manager to choose the
96903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * appropriate provider and parameters to compute the location
97003ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @param intent a {#link PendingIntent} to be sent for the location update
97103ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     *
97203ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if provider is null or doesn't exist
97303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     * @throws IllegalArgumentException if intent is null
974f1be6861daea57ba0a1595739a899d05229d6d15Nick Pelly     * @throws SecurityException if no suitable permission is present for the provider
9756fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
97603ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood     */
9776fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
97803ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    public void requestSingleUpdate(Criteria criteria, PendingIntent intent) {
9796fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkCriteria(criteria);
9806fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
9816fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
9826fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedCriteria(
9836fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                criteria, 0, 0, true);
9846fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, null, null, intent);
9856fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
9866fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
9876fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void requestLocationUpdates(LocationRequest request, LocationListener listener,
9886fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Looper looper) {
9896fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkListener(listener);
9906fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, listener, looper, null);
9916fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
9926fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
9936fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void requestLocationUpdates(LocationRequest request, PendingIntent intent) {
9946fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
9956fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        requestLocationUpdates(request, null, null, intent);
9966fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
9976fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
9986fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private ListenerTransport wrapListener(LocationListener listener, Looper looper) {
9996fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (listener == null) return null;
10006fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        synchronized (mListeners) {
10016fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            ListenerTransport transport = mListeners.get(listener);
10026fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            if (transport == null) {
10036fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                transport = new ListenerTransport(listener, looper);
10046fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            }
10056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mListeners.put(listener, transport);
10066fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return transport;
100703ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood        }
10086fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
10096fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
10106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private void requestLocationUpdates(LocationRequest request, LocationListener listener,
10116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Looper looper, PendingIntent intent) {
10126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
10136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        String packageName = mContext.getPackageName();
10146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
10156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        // wrap the listener class
10166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        ListenerTransport transport = wrapListener(listener, looper);
10176fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
10186fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        try {
10196fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mService.requestLocationUpdates(request, transport, intent, packageName);
10206fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly       } catch (RemoteException e) {
10216fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly           Log.e(TAG, "RemoteException", e);
10226fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly       }
102303ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    }
102403ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood
102503ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwood    /**
10269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Removes any current registration for location updates of the current activity
10279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * with the given LocationListener.  Following this call, updates will no longer
10289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * occur for this listener.
10299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param listener {#link LocationListener} object that no longer needs location updates
10319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if listener is null
10329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void removeUpdates(LocationListener listener) {
10346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkListener(listener);
10356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        String packageName = mContext.getPackageName();
10366fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
10376fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        ListenerTransport transport;
10386fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        synchronized (mListeners) {
10396fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            transport = mListeners.remove(listener);
10409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10416fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (transport == null) return;
10426fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
10439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
10446fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mService.removeUpdates(transport, null, packageName);
10456fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
10466fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
10479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Removes any current registration for location updates of the current activity
10529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * with the given PendingIntent.  Following this call, updates will no longer
10539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * occur for this intent.
10549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param intent {#link PendingIntent} object that no longer needs location updates
10569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if intent is null
10579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void removeUpdates(PendingIntent intent) {
10596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
10606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        String packageName = mContext.getPackageName();
10616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
10629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
10636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mService.removeUpdates(null, intent, packageName);
10646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
10656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
10669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets a proximity alert for the location given by the position
10719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * (latitude, longitude) and the given radius.  When the device
10729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * detects that it has entered or exited the area surrounding the
10739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * location, the given PendingIntent will be used to create an Intent
10749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * to be fired.
10759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> The fired Intent will have a boolean extra added with key
10779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #KEY_PROXIMITY_ENTERING}. If the value is true, the device is
10789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * entering the proximity region; if false, it is exiting.
10799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> Due to the approximate nature of position estimation, if the
10819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * device passes through the given area briefly, it is possible
10829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * that no Intent will be fired.  Similarly, an Intent could be
10839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * fired if the device passes very close to the given area but
10849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * does not actually enter it.
10859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> After the number of milliseconds given by the expiration
10879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * parameter, the location manager will delete this proximity
10889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * alert and no longer monitor it.  A value of -1 indicates that
10899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * there should be no expiration time.
10909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> In case the screen goes to sleep, checks for proximity alerts
10929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * happen only once every 4 minutes. This conserves battery life by
10939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * ensuring that the device isn't perpetually awake.
10949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> Internally, this method uses both {@link #NETWORK_PROVIDER}
10969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and {@link #GPS_PROVIDER}.
10979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param latitude the latitude of the central point of the
10999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * alert region
11009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param longitude the longitude of the central point of the
11019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * alert region
11029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param radius the radius of the central point of the
11039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * alert region, in meters
11049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param expiration time for this proximity alert, in milliseconds,
11059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or -1 to indicate no expiration
11069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param intent a PendingIntent that will be used to generate an Intent to
11079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * fire when entry to or exit from the alert region is detected
11089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if no permission exists for the required
11109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * providers.
11116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
11129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
11146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void addProximityAlert(double latitude, double longitude, float radius, long expiration,
11156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            PendingIntent intent) {
11166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
11176fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (expiration < 0) expiration = Long.MAX_VALUE;
11186fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
11196fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        Geofence fence = Geofence.createCircle(latitude, longitude, radius);
11206fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = new LocationRequest().setExpireIn(expiration);
11216fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        try {
11226fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mService.requestGeofence(request, fence, intent, mContext.getPackageName());
11236fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
11246fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
11259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11266fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
11276fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
11286fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void requestGeofence(LocationRequest request, Geofence fence, PendingIntent intent) {
11296fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
11306fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkGeofence(fence);
11316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
11329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
11336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mService.requestGeofence(request, fence, intent, mContext.getPackageName());
11346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
11356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
11369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
11409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Removes the proximity alert with the given PendingIntent.
11419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param intent the PendingIntent that no longer needs to be notified of
11439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * proximity alerts
11446fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
11459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11466fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
11479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void removeProximityAlert(PendingIntent intent) {
11486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
11496fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        String packageName = mContext.getPackageName();
11506fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
11516fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        try {
11526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mService.removeGeofence(null, intent, packageName);
11536fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
11546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
11559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
11576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
11586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void removeGeofence(Geofence fence, PendingIntent intent) {
11596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
11606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkGeofence(fence);
11616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        String packageName = mContext.getPackageName();
11626fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
11639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
11646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mService.removeGeofence(fence, intent, packageName);
11656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
11666fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
11676fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
11686fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
11696fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
11706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void removeAllGeofences(PendingIntent intent) {
11716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkPendingIntent(intent);
11726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        String packageName = mContext.getPackageName();
11736fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
11746fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        try {
11756fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mService.removeGeofence(null, intent, packageName);
11766fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
11776fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
11789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
11829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the current enabled/disabled status of the given provider. If the
11839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * user has enabled this provider in the Settings menu, true is returned
11849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * otherwise false is returned
11859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the name of the provider
11879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if the provider is enabled
11889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if no suitable permission is present for the provider.
1190f4d207b1c2212d6e9adc3e7954a8d14c6b9abc51Mike Lockwood     * @throws IllegalArgumentException if provider is null
11916fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
11929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11936fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
11949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean isProviderEnabled(String provider) {
11956fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkProvider(provider);
11966fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
11979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
11989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return mService.isProviderEnabled(provider);
11996fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
12006fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
12019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
12029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public Location getLastLocation(LocationRequest request) {
12066fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        try {
12076fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return mService.getLastLocation(request);
12086fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
12096fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
12106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return null;
12116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
12126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
12136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
12146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
12159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
12169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a Location indicating the data from the last known
12179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * location fix obtained from the given provider.  This can be done
12189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * without starting the provider.  Note that this location could
12199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * be out-of-date, for example if the device was turned off and
12209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * moved to another location.
12219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
12229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p> If the provider is currently disabled, null is returned.
12239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
12249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the name of the provider
12259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the last known location for the provider, or null
12269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
12279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if no suitable permission is present for the provider.
12289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if provider is null or doesn't exist
12296fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
12309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
12329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Location getLastKnownLocation(String provider) {
12336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkProvider(provider);
12346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
12356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedProvider(
12366fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                provider, 0, 0, true);
12376fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
12386fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        try {
12396fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return mService.getLastLocation(request);
12406fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
12416fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
12426fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return null;
12439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12446fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
12456fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
12466fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    /**
12476fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Return the last know Location that satisfies the given
12486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * criteria. This can be done without starting the provider.
12496fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Note that this location could
12506fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * be out-of-date, for example if the device was turned off and
12516fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * moved to another location.
12526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     *
12536fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * <p> If no location is found that satisfies the criteria, null is returned
12546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     *
12556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @param criteria location criteria
12566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @return the last known location that satisfies criteria, or null
12576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     *
12586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @throws SecurityException if no suitable permission is present
12596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @throws IllegalArgumentException if criteria is null
12606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
12616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     */
12626fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
12636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public Location getLastKnownLocation(Criteria criteria) {
12646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        checkCriteria(criteria);
12656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
12666fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        LocationRequest request = LocationRequest.createFromDeprecatedCriteria(
12676fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                criteria, 0, 0, true);
12689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
12696fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return mService.getLastLocation(request);
12706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
12716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
12729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
12739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Mock provider support
12779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
12799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Creates a mock location provider and adds it to the set of active providers.
12809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
12819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param name the provider name
12829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param requiresNetwork
12839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param requiresSatellite
12849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param requiresCell
12859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param hasMonetaryCost
12869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param supportsAltitude
12879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param supportsSpeed
12889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param supportsBearing
12899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param powerRequirement
12909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param accuracy
12919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
12929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if the ACCESS_MOCK_LOCATION permission is not present
12939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or the {@link android.provider.Settings.Secure#ALLOW_MOCK_LOCATION
12949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Settings.Secure.ALLOW_MOCK_LOCATION} system setting is not enabled
12959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if a provider with the given name already exists
12966fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
12979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12986fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
12999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite,
13006fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude,
13016fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) {
13026fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        ProviderProperties properties = new ProviderProperties(requiresNetwork,
13036fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                requiresSatellite, requiresCell, hasMonetaryCost, supportsAltitude, supportsSpeed,
13046fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                supportsBearing, powerRequirement, accuracy);
13056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (name.matches(LocationProvider.BAD_CHARS_REGEX)) {
13066fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            throw new IllegalArgumentException("provider name contains illegal character: " + name);
13076fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
13086fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
13099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
13106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mService.addTestProvider(name, properties);
13116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
13126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
13139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Removes the mock location provider with the given name.
13189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the provider name
13209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if the ACCESS_MOCK_LOCATION permission is not present
13229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or the {@link android.provider.Settings.Secure#ALLOW_MOCK_LOCATION
13239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Settings.Secure.ALLOW_MOCK_LOCATION}} system setting is not enabled
13249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if no provider with the given name exists
13256fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
13269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
13276fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
13289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void removeTestProvider(String provider) {
13299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
13309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mService.removeTestProvider(provider);
13316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
13326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
13339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13372eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly     * Sets a mock location for the given provider.
13382eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly     * <p>This location will be used in place of any actual location from the provider.
13392eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly     * The location object must have a minimum number of fields set to be
13402eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly     * considered a valid LocationProvider Location, as per documentation
13412eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly     * on {@link Location} class.
13429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the provider name
13449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param loc the mock location
13459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if the ACCESS_MOCK_LOCATION permission is not present
13479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or the {@link android.provider.Settings.Secure#ALLOW_MOCK_LOCATION
13489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Settings.Secure.ALLOW_MOCK_LOCATION}} system setting is not enabled
13499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if no provider with the given name exists
13502eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly     * @throws IllegalArgumentException if the location is incomplete
13516fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
13529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
13536fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
13549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setTestProviderLocation(String provider, Location loc) {
13552eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly        if (!loc.isComplete()) {
13566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            IllegalArgumentException e = new IllegalArgumentException(
13576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                    "Incomplete location object, missing timestamp or accuracy? " + loc);
13582eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly            if (mContext.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN) {
13596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                // just log on old platform (for backwards compatibility)
13606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                Log.w(TAG, e);
13612eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly                loc.makeComplete();
13622eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly            } else {
13636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                // really throw it!
13646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                throw e;
13652eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly            }
13662eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly        }
13672eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly
13689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
13699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mService.setTestProviderLocation(provider, loc);
13706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
13716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
13729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Removes any mock location associated with the given provider.
13779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the provider name
13799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if the ACCESS_MOCK_LOCATION permission is not present
13819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or the {@link android.provider.Settings.Secure#ALLOW_MOCK_LOCATION
13829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Settings.Secure.ALLOW_MOCK_LOCATION}} system setting is not enabled
13839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if no provider with the given name exists
13846fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
13859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
13866fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
13879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void clearTestProviderLocation(String provider) {
13889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
13899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mService.clearTestProviderLocation(provider);
13906fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
13916fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
13929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets a mock enabled value for the given provider.  This value will be used in place
13979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * of any actual value from the provider.
13989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the provider name
14009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param enabled the mock enabled value
14019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if the ACCESS_MOCK_LOCATION permission is not present
14039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or the {@link android.provider.Settings.Secure#ALLOW_MOCK_LOCATION
14049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Settings.Secure.ALLOW_MOCK_LOCATION}} system setting is not enabled
14059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if no provider with the given name exists
14066fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
14079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
14086fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
14099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setTestProviderEnabled(String provider, boolean enabled) {
14109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
14119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mService.setTestProviderEnabled(provider, enabled);
14126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
14136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
14149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
14189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Removes any mock enabled value associated with the given provider.
14199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the provider name
14219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if the ACCESS_MOCK_LOCATION permission is not present
14239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or the {@link android.provider.Settings.Secure#ALLOW_MOCK_LOCATION
14249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Settings.Secure.ALLOW_MOCK_LOCATION}} system setting is not enabled
14259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if no provider with the given name exists
14266fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
14279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
14286fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
14299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void clearTestProviderEnabled(String provider) {
14309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
14319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mService.clearTestProviderEnabled(provider);
14326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
14336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
14349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
14389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets mock status values for the given provider.  These values will be used in place
14399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * of any actual values from the provider.
14409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the provider name
14429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param status the mock status
14439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param extras a Bundle containing mock extras
14449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param updateTime the mock update time
14459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if the ACCESS_MOCK_LOCATION permission is not present
14479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or the {@link android.provider.Settings.Secure#ALLOW_MOCK_LOCATION
14489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Settings.Secure.ALLOW_MOCK_LOCATION}} system setting is not enabled
14499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if no provider with the given name exists
14506fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
14519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
14526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
14539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setTestProviderStatus(String provider, int status, Bundle extras, long updateTime) {
14549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
14559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mService.setTestProviderStatus(provider, status, extras, updateTime);
14566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
14576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
14589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
14629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Removes any mock status values associated with the given provider.
14639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider the provider name
14659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if the ACCESS_MOCK_LOCATION permission is not present
14679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or the {@link android.provider.Settings.Secure#ALLOW_MOCK_LOCATION
14689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Settings.Secure.ALLOW_MOCK_LOCATION}} system setting is not enabled
14699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if no provider with the given name exists
14706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
14719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
14726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
14739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void clearTestProviderStatus(String provider) {
14749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
14759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mService.clearTestProviderStatus(provider);
14766fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
14776fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
14789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // GPS-specific support
14829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // This class is used to send GPS status events to the client's main thread.
14849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private class GpsStatusListenerTransport extends IGpsStatusListener.Stub {
14859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        private final GpsStatus.Listener mListener;
1487b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        private final GpsStatus.NmeaListener mNmeaListener;
1488b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood
1489b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        // This must not equal any of the GpsStatus event IDs
1490b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        private static final int NMEA_RECEIVED = 1000;
1491b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood
1492b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        private class Nmea {
1493b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            long mTimestamp;
1494b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            String mNmea;
1495b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood
1496b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            Nmea(long timestamp, String nmea) {
1497b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mTimestamp = timestamp;
1498b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mNmea = nmea;
1499b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            }
1500b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        }
1501b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        private ArrayList<Nmea> mNmeaBuffer;
15029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GpsStatusListenerTransport(GpsStatus.Listener listener) {
15049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mListener = listener;
1505b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            mNmeaListener = null;
1506b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        }
1507b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood
1508b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        GpsStatusListenerTransport(GpsStatus.NmeaListener listener) {
1509b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            mNmeaListener = listener;
1510b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            mListener = null;
1511b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            mNmeaBuffer = new ArrayList<Nmea>();
15129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1514e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        @Override
15159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onGpsStarted() {
1516b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            if (mListener != null) {
1517b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                Message msg = Message.obtain();
1518b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                msg.what = GpsStatus.GPS_EVENT_STARTED;
1519b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mGpsHandler.sendMessage(msg);
1520b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            }
15219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1523e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        @Override
15249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onGpsStopped() {
1525b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            if (mListener != null) {
1526b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                Message msg = Message.obtain();
1527b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                msg.what = GpsStatus.GPS_EVENT_STOPPED;
1528b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mGpsHandler.sendMessage(msg);
1529b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            }
15309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1532e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        @Override
15339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onFirstFix(int ttff) {
1534b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            if (mListener != null) {
1535b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mGpsStatus.setTimeToFirstFix(ttff);
1536b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                Message msg = Message.obtain();
1537b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                msg.what = GpsStatus.GPS_EVENT_FIRST_FIX;
1538b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mGpsHandler.sendMessage(msg);
1539b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            }
15409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1542e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        @Override
15439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onSvStatusChanged(int svCount, int[] prns, float[] snrs,
15449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                float[] elevations, float[] azimuths, int ephemerisMask,
15459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                int almanacMask, int usedInFixMask) {
1546b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            if (mListener != null) {
1547b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mGpsStatus.setStatus(svCount, prns, snrs, elevations, azimuths,
1548b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                        ephemerisMask, almanacMask, usedInFixMask);
1549b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood
1550b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                Message msg = Message.obtain();
1551b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                msg.what = GpsStatus.GPS_EVENT_SATELLITE_STATUS;
1552b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                // remove any SV status messages already in the queue
1553b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mGpsHandler.removeMessages(GpsStatus.GPS_EVENT_SATELLITE_STATUS);
1554b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mGpsHandler.sendMessage(msg);
1555b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            }
1556b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        }
15579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1558e0fd693c6098f59004f9e96ad75c058e26c337b0Nick Pelly        @Override
1559b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        public void onNmeaReceived(long timestamp, String nmea) {
1560b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            if (mNmeaListener != null) {
1561b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                synchronized (mNmeaBuffer) {
1562b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                    mNmeaBuffer.add(new Nmea(timestamp, nmea));
1563b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                }
1564b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                Message msg = Message.obtain();
1565b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                msg.what = NMEA_RECEIVED;
1566b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                // remove any NMEA_RECEIVED messages already in the queue
1567b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mGpsHandler.removeMessages(NMEA_RECEIVED);
1568b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mGpsHandler.sendMessage(msg);
1569b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            }
15709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        private final Handler mGpsHandler = new Handler() {
15739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            @Override
15749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            public void handleMessage(Message msg) {
1575b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                if (msg.what == NMEA_RECEIVED) {
1576b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                    synchronized (mNmeaBuffer) {
1577b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                        int length = mNmeaBuffer.size();
1578b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                        for (int i = 0; i < length; i++) {
1579b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                            Nmea nmea = mNmeaBuffer.get(i);
1580b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                            mNmeaListener.onNmeaReceived(nmea.mTimestamp, nmea.mNmea);
1581b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                        }
1582b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                        mNmeaBuffer.clear();
1583b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                    }
1584b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                } else {
1585b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                    // synchronize on mGpsStatus to ensure the data is copied atomically.
1586b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                    synchronized(mGpsStatus) {
1587b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                        mListener.onGpsStatusChanged(msg.what);
1588b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                    }
15899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
15909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
15919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        };
15929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Adds a GPS status listener.
15969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param listener GPS status listener object to register
15989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if the listener was successfully added
1600a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa     *
16019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present
16029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean addGpsStatusListener(GpsStatus.Listener listener) {
16049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean result;
16059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mGpsStatusListeners.get(listener) != null) {
16079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // listener is already registered
16089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return true;
16099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
16119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            GpsStatusListenerTransport transport = new GpsStatusListenerTransport(listener);
16129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            result = mService.addGpsStatusListener(transport);
16139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (result) {
16149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mGpsStatusListeners.put(listener, transport);
16159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
16169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
16179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "RemoteException in registerGpsStatusListener: ", e);
16189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            result = false;
16199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return result;
16229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Removes a GPS status listener.
16269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
16279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param listener GPS status listener object to remove
16289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void removeGpsStatusListener(GpsStatus.Listener listener) {
16309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
16319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            GpsStatusListenerTransport transport = mGpsStatusListeners.remove(listener);
16329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (transport != null) {
16339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mService.removeGpsStatusListener(transport);
16349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
16359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
16369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "RemoteException in unregisterGpsStatusListener: ", e);
16379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1640b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood    /**
1641b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood     * Adds an NMEA listener.
1642b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood     *
1643640992dec96fa88f0bbd42c6a02902640a000b35Mike Lockwood     * @param listener a {#link GpsStatus.NmeaListener} object to register
1644b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood     *
1645b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood     * @return true if the listener was successfully added
1646b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood     *
1647b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood     * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present
1648b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood     */
1649b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood    public boolean addNmeaListener(GpsStatus.NmeaListener listener) {
1650b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        boolean result;
1651b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood
1652b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        if (mNmeaListeners.get(listener) != null) {
1653b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            // listener is already registered
1654b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            return true;
1655b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        }
1656b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        try {
1657b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            GpsStatusListenerTransport transport = new GpsStatusListenerTransport(listener);
1658b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            result = mService.addGpsStatusListener(transport);
1659b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            if (result) {
1660b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mNmeaListeners.put(listener, transport);
1661b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            }
1662b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        } catch (RemoteException e) {
1663b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            Log.e(TAG, "RemoteException in registerGpsStatusListener: ", e);
1664b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            result = false;
1665b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        }
1666b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood
1667b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        return result;
1668b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood    }
1669b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood
1670b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood    /**
1671b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood     * Removes an NMEA listener.
1672b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood     *
1673640992dec96fa88f0bbd42c6a02902640a000b35Mike Lockwood     * @param listener a {#link GpsStatus.NmeaListener} object to remove
1674b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood     */
1675b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood    public void removeNmeaListener(GpsStatus.NmeaListener listener) {
1676b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        try {
1677b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            GpsStatusListenerTransport transport = mNmeaListeners.remove(listener);
1678b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            if (transport != null) {
1679b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood                mService.removeGpsStatusListener(transport);
1680b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            }
1681b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        } catch (RemoteException e) {
1682b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood            Log.e(TAG, "RemoteException in unregisterGpsStatusListener: ", e);
1683b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood        }
1684b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood    }
1685b16e7800be4f879135f239f1f8f586f3712df01eMike Lockwood
16869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     /**
16879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Retrieves information about the current status of the GPS engine.
16889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This should only be called from the {@link GpsStatus.Listener#onGpsStatusChanged}
16899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * callback to ensure that the data is copied atomically.
16909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
16919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The caller may either pass in a {@link GpsStatus} object to set with the latest
16929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * status information, or pass null to create a new {@link GpsStatus} object.
16939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
16949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param status object containing GPS status details, or null.
16959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return status object containing updated GPS status.
16969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public GpsStatus getGpsStatus(GpsStatus status) {
16989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (status == null) {
16999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            status = new GpsStatus();
17009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project       }
17019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project       status.setStatus(mGpsStatus);
17029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project       return status;
17039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
17049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
17069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sends additional commands to a location provider.
17079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Can be used to support provider specific extensions to the Location Manager API
17089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param provider name of the location provider.
17109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param command name of the command to send to the provider.
17119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param extras optional arguments for the command (or null).
17129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The provider may optionally fill the extras Bundle with results from the command.
17139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if the command succeeds.
17156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @deprecated use the {@link LocationRequest} class instead
17169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
17176fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Deprecated
17189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean sendExtraCommand(String provider, String command, Bundle extras) {
17199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
17209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return mService.sendExtraCommand(provider, command, extras);
17219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
17229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "RemoteException in sendExtraCommand: ", e);
17239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
17249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
17259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1726a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa
172722d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie    /**
172822d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie     * Used by NetInitiatedActivity to report user response
172922d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie     * for network initiated GPS fix requests.
173022d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie     *
173122d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie     * {@hide}
173222d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie     */
173322d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie    public boolean sendNiResponse(int notifId, int userResponse) {
173422d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie    	try {
173522d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie            return mService.sendNiResponse(notifId, userResponse);
173622d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie        } catch (RemoteException e) {
173722d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie            Log.e(TAG, "RemoteException in sendNiResponse: ", e);
173822d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie            return false;
173922d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie        }
174022d1f9fb23015471de6af1a70e40fb5c82ecb665Danke Xie    }
1741a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa
17426fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private static void checkProvider(String provider) {
17436fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (provider == null) {
17446fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            throw new IllegalArgumentException("invalid provider: " + provider);
17456fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
17466fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
17476fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
17486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private static void checkCriteria(Criteria criteria) {
17496fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (criteria == null) {
17506fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            throw new IllegalArgumentException("invalid criteria: " + criteria);
17516fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
17526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
17536fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private static void checkListener(LocationListener listener) {
17546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (listener == null) {
17556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            throw new IllegalArgumentException("invalid listener: " + listener);
17566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
17576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
17586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
17596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private void checkPendingIntent(PendingIntent intent) {
17606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (intent == null) {
17616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            throw new IllegalArgumentException("invalid pending intent: " + intent);
17626fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
17636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (!intent.isTargetedToPackage()) {
17646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            IllegalArgumentException e = new IllegalArgumentException(
17656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                    "pending intent msut be targeted to package");
17666fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.JELLY_BEAN) {
17676fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                throw e;
17686fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            } else {
17696fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                Log.w(TAG, e);
17706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            }
17716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
17726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
17736fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
17746fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private static void checkGeofence(Geofence fence) {
17756fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (fence == null) {
17766fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            throw new IllegalArgumentException("invalid geofence: " + fence);
17776fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
17786fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
17799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1780