16fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/*
26fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * Copyright (C) 2010 The Android Open Source Project
36fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly *
46fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
56fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * you may not use this file except in compliance with the License.
66fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * You may obtain a copy of the License at
76fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly *
86fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
96fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly *
106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * Unless required by applicable law or agreed to in writing, software
116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * See the License for the specific language governing permissions and
146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * limitations under the License.
156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly */
166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
176fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellypackage com.android.location.provider;
186fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
196fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport java.io.FileDescriptor;
206fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport java.io.FileOutputStream;
216fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport java.io.PrintWriter;
226fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
236fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.content.Context;
246fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.location.ILocationManager;
256fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.location.Location;
26779b77455fc51382ecafa210b8a805d2a616da55Victoria Leaseimport android.location.LocationManager;
27779b77455fc51382ecafa210b8a805d2a616da55Victoria Leaseimport android.location.LocationRequest;
286fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.os.Bundle;
296fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.os.IBinder;
306fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.os.RemoteException;
316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.os.ServiceManager;
326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.os.WorkSource;
336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.util.Log;
346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport com.android.internal.location.ILocationProvider;
366fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport com.android.internal.location.ProviderProperties;
376fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport com.android.internal.location.ProviderRequest;
386fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
396fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/**
40b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * Base class for location providers implemented as unbundled services.
41b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly *
42b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * <p>The network location provider must export a service with action
43b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * "com.android.location.service.v2.NetworkLocationProvider"
44b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * and a valid minor version in a meta-data field on the service, and
45b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * then return the result of {@link #getBinder()} on service binding.
46b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly *
47b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * <p>The fused location provider must export a service with action
48b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * "com.android.location.service.FusedLocationProvider"
49b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * and a valid minor version in a meta-data field on the service, and
50b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * then return the result of {@link #getBinder()} on service binding.
51b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly *
52b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * <p>IMPORTANT: This class is effectively a public API for unbundled
53b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * applications, and must remain API stable. See README.txt in the root
54b03c8c508dcbbef364e624ad5bc0ab6fa6733dc7Nick Pelly * of this package for more information.
556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly */
566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellypublic abstract class LocationProviderBase {
576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private final String TAG;
586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    protected final ILocationManager mLocationManager;
606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private final ProviderProperties mProperties;
616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private final IBinder mBinder;
626fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
63779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease    /**
64779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease     * Bundle key for a version of the location containing no GPS data.
65779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease     * Allows location providers to flag locations as being safe to
66779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease     * feed to LocationFudger.
67779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease     */
68779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease    public static final String EXTRA_NO_GPS_LOCATION = Location.EXTRA_NO_GPS_LOCATION;
69779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease
70779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease    /**
71779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease     * Name of the Fused location provider.
72779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease     *
73779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease     * <p>This provider combines inputs for all possible location sources
74779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease     * to provide the best possible Location fix.
75779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease     */
76779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease    public static final String FUSED_PROVIDER = LocationManager.FUSED_PROVIDER;
77779b77455fc51382ecafa210b8a805d2a616da55Victoria Lease
786fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private final class Service extends ILocationProvider.Stub {
796fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        @Override
806fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        public void enable() {
816fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            onEnable();
826fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
836fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        @Override
846fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        public void disable() {
856fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            onDisable();
866fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
876fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        @Override
886fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        public void setRequest(ProviderRequest request, WorkSource ws) {
896fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            onSetRequest(new ProviderRequestUnbundled(request), ws);
906fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
916fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        @Override
926fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        public ProviderProperties getProperties() {
936fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return mProperties;
946fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
956fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        @Override
966fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        public int getStatus(Bundle extras) {
976fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return onGetStatus(extras);
986fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
996fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        @Override
1006fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        public long getStatusUpdateTime() {
1016fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return onGetStatusUpdateTime();
1026fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
1036fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        @Override
1046fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        public boolean sendExtraCommand(String command, Bundle extras) {
1056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            return onSendExtraCommand(command, extras);
1066fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
1076fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        @Override
1086fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        public void dump(FileDescriptor fd, String[] args) {
1096fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            onDump(fd, pw, args);
1116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            pw.flush();
1126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
1136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
1146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public LocationProviderBase(String tag, ProviderPropertiesUnbundled properties) {
1166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        TAG = tag;
1176fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        IBinder b = ServiceManager.getService(Context.LOCATION_SERVICE);
1186fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        mLocationManager = ILocationManager.Stub.asInterface(b);
1196fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        mProperties = properties.getProviderProperties();
1206fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        mBinder = new Service();
1216fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
1226fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1236fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public IBinder getBinder() {
1246fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        return mBinder;
1256fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
1266fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1276fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    /**
1286fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Used by the location provider to report new locations.
1296fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     *
1306fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @param location new Location to report
1316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     *
1326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Requires the android.permission.INSTALL_LOCATION_PROVIDER permission.
1336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     */
1346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public final void reportLocation(Location location) {
1356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        try {
1366fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            mLocationManager.reportLocation(location, false);
1376fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (RemoteException e) {
1386fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "RemoteException", e);
1396fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        } catch (Exception e) {
1406fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            // never crash provider, might be running in a system process
1416fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            Log.e(TAG, "Exception", e);
1426fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        }
1436fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
1446fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1456fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    /**
1466fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Enable the location provider.
1476fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * <p>The provider may initialize resources, but does
1486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * not yet need to report locations.
1496fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     */
1506fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public abstract void onEnable();
1516fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    /**
1536fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Disable the location provider.
1546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * <p>The provider must release resources, and stop
1556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * performing work. It may no longer report locations.
1566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     */
1576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public abstract void onDisable();
1586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    /**
1606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Set the {@link ProviderRequest} requirements for this provider.
1616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * <p>Each call to this method overrides all previous requests.
1626fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * <p>This method might trigger the provider to start returning
1636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * locations, or to stop returning locations, depending on the
1646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * parameters in the request.
1656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     */
1666fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public abstract void onSetRequest(ProviderRequestUnbundled request, WorkSource source);
1676fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1686fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    /**
1696fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Dump debug information.
1706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     */
1716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void onDump(FileDescriptor fd, PrintWriter pw, String[] args) {
1726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
1736fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1746fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    /**
1756fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Returns a information on the status of this provider.
1766fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * <p>{@link android.location.LocationProvider#OUT_OF_SERVICE} is returned if the provider is
1776fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * out of service, and this is not expected to change in the near
1786fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * future; {@link android.location.LocationProvider#TEMPORARILY_UNAVAILABLE} is returned if
1796fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * the provider is temporarily unavailable but is expected to be
1806fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * available shortly; and {@link android.location.LocationProvider#AVAILABLE} is returned
1816fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * if the provider is currently available.
1826fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     *
1836fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * <p>If extras is non-null, additional status information may be
1846fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * added to it in the form of provider-specific key/value pairs.
1856fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     */
1866fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public abstract int onGetStatus(Bundle extras);
1876fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1886fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    /**
1896fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Returns the time at which the status was last updated. It is the
1906fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * responsibility of the provider to appropriately set this value using
1916fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * {@link android.os.SystemClock#elapsedRealtime SystemClock.elapsedRealtime()}.
1926fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * there is a status update that it wishes to broadcast to all its
1936fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * listeners. The provider should be careful not to broadcast
1946fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * the same status again.
1956fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     *
1966fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @return time of last status update in millis since last reboot
1976fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     */
1986fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public abstract long onGetStatusUpdateTime();
1996fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
2006fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    /**
2016fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * Implements addditional location provider specific additional commands.
2026fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     *
2036fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @param command name of the command to send to the provider.
2046fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @param extras optional arguments for the command (or null).
2056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * The provider may optionally fill the extras Bundle with results from the command.
2066fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     *
2076fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     * @return true if the command succeeds.
2086fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly     */
2096fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public boolean onSendExtraCommand(String command, Bundle extras) {
2106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        // default implementation
2116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        return false;
2126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
2136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly}
214