11

Does anyone know how to provide your own LocationProvider and publish it to the system, so other apps may fetch a reference to it from the LocationManager?

Marcus Wolschon
  • 2,550
  • 2
  • 22
  • 28

4 Answers4

12

For development and testing purposes you can implement a mock location provider. You need to enable "Allow mock locations" in developer options in order for applications to see locations from that provider.

For a code example, look at GPX Playback:
https://github.com/johncarpenter/Android-GPX-Mock-Location-Provider

The code for registering the location provider starts in line 203 of file android/src/com/twolinessoftware/android/PlaybackService.java.

If you want a "real" location provider (e.g. to deploy your functionality as part of an end-user application), some sources say it cannot be done unless that provider is in a package signed with the system key, so you'd have to build your own ROM from source, sign it with your key, sign your location provider with the same key and install both on your phone.

However, Android at some point introduced "unbundled" location providers. Apparently they needed that in order to move their own NetworkLocationProvider from the Android core into the Google Apps package.

I haven't seen an unbundled location provider working in practice yet and I'm not aware of any open-source code using it, but it looks like recent version of Android have support for "aftermarket" location providers.

Source code for the respective library is at
https://github.com/CyanogenMod/android_frameworks_base/tree/cm-10.1/location/lib

A location provider would have to extend com.android.location.provider.LocationProviderBase.

Classes and methods are documented in the code.

Edit: Looking at
https://github.com/android/platform_frameworks_base/blob/master/core/res/AndroidManifest.xml
starting at line 607, it looks like the protection level for the INSTALL_LOCATION_PROVIDER permission is signatureOrSystem. This means that any app which uses this feature (i.e. installs a location provider, not necessarily the locaion provider itself) would either need to be signed with the same key as the platform (which would require you to build your entire Android system from source) or be installed as a system application (which might work on rooted devices).

There is some code at
https://github.com/microg/NetworkLocation
which seems to implement a LocationProvider. I haven't looked at it in detail (yet) or built it, so I cannot say if it works and what is requierd for it. But maybe it provides some pointers.

user149408
  • 5,385
  • 4
  • 33
  • 69
  • If I make my own LocationProvider, how do I register/install it? I can't find anything in the manifest... – JohnyTex Apr 23 '14 at 09:17
  • 1
    I've never done this myself, but there's a custom location provider out at https://github.com/microg/android_packages_apps_UnifiedNlp. This app should have all the code for installing a proper location provider. If you want to try the app, it needs to be installed as a system app (just install it and move it to the system app folder via Terminal or adb) and needs at least one backend, which can be a userspace app. – user149408 Apr 24 '14 at 19:18
  • Thanks! Though this lib seems to be missing: https://github.com/android/platform_frameworks_base/tree/master/location/lib Where can I find it? Do I really need to check out the entire repo? – JohnyTex Apr 25 '14 at 09:06
  • 2
    Apparently UnifiedNLP is intended to be built with the Android build system. This is the standard way to build components that are part of the ROM image, but it is possible to build just individual components (see build instructions on the Github repo's main page). You probably won't need to clone the entire AOSP source tree but just the relevant parts of it. The one you mention looks like a good start. You might want to look at Android build instructions (e.g. those for CyanogenMod) to find out how to set up your code tree locally. – user149408 Apr 27 '14 at 13:33
  • I get: 05-12 11:28:22.287 1420-1525/system_process W/LocationManagerService﹕ se.xxx.android.gpsprovider implements com.android.location.service.NetworkLocationProvider but its signatures don't match those in @0, ignoring. Any idea what that means? What is @0? – JohnyTex May 12 '14 at 09:34
  • I suppose this means the system expects location providers to be signed with the same key as the system. I don't think this is standard in Android – what ROM do you use? If Cyanogenmod is available on your device, try that. – user149408 May 12 '14 at 12:04
4

It cannot be done.

The problem is that only OEMs are allowed to install a new location provider. A third-party developer cannot grant to his/her application the permission required to install a new location provider (android.permission.INSTALL_LOCATION_PROVIDER).

See the answer of Dianne Hackborn (Android framework engineer) in this thread:

https://groups.google.com/d/topic/android-developers/OvCcdvO6jZY/discussion

To install a new location provider, you should develop a whole new firmware (like http://www.cyanogenmod.com/ ), insert your new location provider in this firmware and install all of this stuff o the user's phone.

See this SO thread, as well: Why are these permissions being refused?

All of this because of security concerns.

Community
  • 1
  • 1
AlexBottoni
  • 2,237
  • 20
  • 24
1

What Pentium10 suggested seems to be offline, check out http://pedroassuncao.com/2009/11/android-location-provider-mock/

Or the LocationManagerProximitryTest by google: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/location/LocationManagerProximityTest.java

Or here some other similar question with working solution/code: Android mock location on device?

Community
  • 1
  • 1
icyerasor
  • 4,973
  • 1
  • 43
  • 52
0

Read a tutorial about Android location provider mock

Pentium10
  • 204,586
  • 122
  • 423
  • 502