How Android Realizes Analog Positioning

  • 2021-12-12 09:56:50
  • OfStack

Directory 1. Android impersonation permission on configuration 1) Turn on analog positioning switch below Android 6.0 2) Select application of analog positioning for code configuration above Android 6.0
2. Implementation of Android Analog Positioning 1) Analog Position Switch Check
2) setTestProviderLocation call

In navigation test scenarios, positioning simulation and route playback are often needed, and the simulation status is realized by LocationManager. setTestProviderLocation () method. If the application to be tested does not support TestProviderLocation simulation position input, it can be considered to start with HAL layer, and the default GPS implementation of hook system.

1. Android impersonation permission opening configuration

In Android versions below 6.0, you need to check the switch of analog positioning in the settings, and change it to the application of selecting analog positioning above 6.0, and the corresponding opening configuration mode is different. The same is that the following two permissions need to be configured in AndroidManifest. xml:


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

1) Turn on analog positioning switch below Android 6.0


Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 1);

To open analog positioning in this way, the following system permissions need to be configured in AndroidManifest. xml, and the application needs to be signed by the system, which cannot be implemented in this way for non-system applications.


android:sharedUserId="android.uid.system"

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

This operation involved can be configured through the adb shell command to bypass the system permission configuration:


adb shell settings put secure mock_location 1

2) Application of Android 6.0 and above code configuration selection analog positioning

mock_location permission for the specified package name needs to be set to allow for Android versions above 6.0.


try {
    String mockLocationPkgName = getPackageName();
    PackageManager mPackageManager = getPackageManager();
    final ApplicationInfo ai = mPackageManager.getApplicationInfo(
            mockLocationPkgName, PackageManager.MATCH_DISABLED_COMPONENTS);
    AppOpsManager mAppsOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
    mAppsOpsManager.setMode(AppOpsManager.OPSTR_MOCK_LOCATION, ai.uid,
            mockLocationPkgName, AppOpsManager.MODE_ALLOWED);
} catch (PackageManager.NameNotFoundException e) {
    /* ignore */
}

At the same time, the following permissions need to be configured in AndroidManifest. xml. Unfortunately, this method also needs to be signed by the system and compiled by the source code, which can only be used by system-level applications.


<uses-permission android:name="android.permission.MANAGE_APP_OPS_MODES" />

The ACCESS_MOCK_LOCATION permission needs to be configured in order to display the application of analog positioning in the setting interface analog positioning options.


<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

The above configuration can also be realized by adb shell command. < package > Parameter is replaced with the package name that you apply.


adb shell appops set <package> android:mock_location allow

Turn off the impersonation location permission and use the following command:


adb shell appops set <package> android:mock_location deny

To find out which applications have emulated location permissions turned on, use the following command:


adb shell appops query-op android:mock_location allow

After execution, the applied package name list parameters will be output.

2. Implementation of Android simulation positioning

1) Analog Position Switch Check

First of all, it is judged whether the simulated positioning authority is turned on in the code, and those above 6.0 can only be judged by adding positioning monitoring to see if there is any abnormality.


Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 1);
0

You can look at the source code implementation of adding positioning monitoring, with the source code implementation of Android 7.0 as a reference. After LocationManager calls the interface, it is finally called to LocationManagerService, and there are calls to checkPackageName methods in requestLocationUpdates methods or getLastLocation methods


Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 1);
1

If the application calling requestLocationUpdates method does not have the authority to simulate positioning, it will report SecurityException exception. In addition, requestLocationUpdates needs to be called in the main thread. If it is called in the sub-thread, it needs to pass an looper parameter, otherwise it will report an error when instantiating ListenerTransport.

Look at the constructor of ListenerTransport. If listening is added in the sub-thread and Loop is not passed, an exception will be reported when mListenerHandler is initialized.


Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 1);
2

Then add the corresponding Provider, set the switch state to true, and configure the state to AVAILABLE.


LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
if (provider != null) {
    locationManager.addTestProvider(
        provider.getName()
        , provider.requiresNetwork()
        , provider.requiresSatellite()
        , provider.requiresCell()
        , provider.hasMonetaryCost()
        , provider.supportsAltitude()
        , provider.supportsSpeed()
        , provider.supportsBearing()
        , provider.getPowerRequirement()
        , provider.getAccuracy());
} else {
    locationManager.addTestProvider(LocationManager.GPS_PROVIDER, true, true, false, false, true, true,
        true, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
}
locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
locationManager.setTestProviderStatus(LocationManager.GPS_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis());

2) setTestProviderLocation call

The example of calling code is as follows. The parameters of longitude and latitude, vehicle speed, positioning accuracy, azimuth and altitude are set according to actual needs.


Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 1);
4

See why you need to set the elapsedRealtimeNanos and time parameters. Under SDK version 17, Location (Android 4.1. 1) does not have setElapsedRealtimeNanos. Starting from SDK version 17, Location (Android 4.2) adds this method. When calling setTestProviderLocation to set positioning information, Android SDK version 17 or above will check whether the positioning parameters are complete, and the versions below 17 will automatically make up.


Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 1);
5

Then look at the logical processing of makeComplete and isComplete in Location class. There are judgments of provider, Accuracy, mTime and mElapsedRealtimeNanos in isComplete.


Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 1);
6

The above is the realization of simulated positioning of a single point. If you want to realize route playback simulation, you only need to refresh and call setTestProviderLocation interface every 1 second after requesting the array data of route positioning points in the background.

After the simulated positioning operation is completed, it is necessary to remove the simulated positioning object, avoid positioning information or use the parameters of the simulated positioning interface. If the Provider with the same name is called when the next use is not removed, an exception will be thrown.


Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 1);
7

The above is Android how to achieve simulation positioning details, more about Android simulation positioning information please pay attention to other related articles on this site!


Related articles: