public class

LocationPolicy

extends Object
java.lang.Object
   ↳ android.app.enterprise.LocationPolicy

Class Overview

This class provides APIs to control the settings related to location services.

Since
MDM 2.0

Summary

Public Methods
List<String> getAllLocationProviders()
API to get all available location services on the device.
boolean getLocationProviderState(String provider)
API to get the enabled or disabled state of a given location provider.
boolean isGPSOn()
API to check whether GPS is ON or OFF on the device.
boolean isGPSStateChangeAllowed()
API to check whether the user is allowed to change the state of GPS on the device.
boolean setGPSStateChangeAllowed(boolean allow)
API to allow or disallow the user changing the state of GPS on the device.
boolean setLocationProviderState(String provider, boolean enable)
API to enable or disable a given location provider.
boolean startGPS(boolean start)
API to start or stop GPS on the device.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public List<String> getAllLocationProviders ()

API to get all available location services on the device.

Returns
  • A list containing the names of the location services if successful, else false.
Throws
SecurityException If caller does not have required permissions
Usage
An administrator can use this API to get the different location services available (e.g., gps, network, passive) on the device.

 EnterpriseDeviceManager edm = (EnterpriseDeviceManager) getSystemService(EnterpriseDeviceManager.ENTERPRISE_POLICY_SERVICE);
  LocationPolicy locationPolicy = edm.getLocationPolicy();
  try {
      List list = locationPolicy.getAllLocationProviders();
      if (list != null) {
          // getting all available location provider has succeeded
      }else {
         // getting all available location provider has failed
      }
  } catch(SecurityException e) {
      Log.w(TAG,"SecurityException: "+e);
  }
  // Sample Output
  // gps
  // network
  // passive
 
Permission
The use of this API requires the caller to have the "android.permission.sec.MDM_LOCATION" permission which has a protection level of signature

Since
MDM 2.0
Multiuser Environment
User Scope

public boolean getLocationProviderState (String provider)

API to get the enabled or disabled state of a given location provider.

Parameters
provider The case sensitive location provider name. Sample values are gps, network, passive.
Returns
  • true if the location provider is enabled, false if the location provider is disabled.
Throws
SecurityException If caller does not have required permissions
Usage
An administrator can use this API to get the enabled or disabled state of a location provider. All available location providers on the device can be obtained by using getAllLocationProviders().

 EnterpriseDeviceManager edm = (EnterpriseDeviceManager) getSystemService(EnterpriseDeviceManager.ENTERPRISE_POLICY_SERVICE);
  LocationPolicy locationPolicy = edm.getLocationPolicy();
  String gpsProvider  = "gps";//case sensitive
  try {
      boolean result = locationPolicy.getLocationProviderState(gpsProvider);
      if(result == true){
          Log.d(TAG,"GPS location provider is not blocked, and user is
          allowed to modify.");
      }else{
          Log.d(TAG,"GPS location provider is disabled by administrator. User not
          allowed to modify.");
      }
  }catch(SecurityException e) {
      Log.w(TAG,"SecurityException: "+e);
  }
 
Permission
The use of this API requires the caller to have the "android.permission.sec.MDM_LOCATION" permission which has a protection level of signature

Since
MDM 2.0
Multiuser Environment
User Scope

public boolean isGPSOn ()

API to check whether GPS is ON or OFF on the device.

Returns
  • true if GPS is ON, else false.
Throws
SecurityException If caller does not have required permissions
Permission
The use of this API requires the caller to have the "android.permission.sec.MDM_LOCATION" permission which has a protection level of signature
Note: Since MDM 5.4, caller does not need to have "android.permission.sec.MDM_LOCATION" permission to call this API.
Since
MDM 3.0
Multiuser Environment
User Scope

public boolean isGPSStateChangeAllowed ()

API to check whether the user is allowed to change the state of GPS on the device.

Returns
  • true if allowed, else false.
Throws
SecurityException If caller does not have required permissions
Usage
An administrator can use this API check whether the user is allowed to change the GPS state. If not allowed, the user cannot change GPS UI settings.

Permission
The use of this API requires the caller to have the "android.permission.sec.MDM_LOCATION" permission which has a protection level of signature.
Note: Since MDM 5.4, caller does not need to have "android.permission.sec.MDM_LOCATION" permission to call this API.
Since
MDM 3.0
Multiuser Environment
User Scope

public boolean setGPSStateChangeAllowed (boolean allow)

API to allow or disallow the user changing the state of GPS on the device.

Parameters
allow true to allow user to change state of GPS, false to disallow it.
Returns
  • true if successful, else false.
Throws
SecurityException If caller does not have required permissions
Usage
An administrator can use this API to allow or disallow the user changing the state of GPS. If not allowed, the user cannot change GPS UI settings.

Note:
There is no difference between existing "setLocationProviderState("gps", true)" and "setGPSStateChangeAllowed(true)".
There is one difference between existing "setLocationProviderState("gps", false)" and "setGPSStateChangeAllowed(false)".
When GPS in ON in device.
a) calling "setLocationProviderState("gps", false)" stops GPS and disables it from user change.
b) calling "setGPSStateChangeAllowed(false)" disables GPS to user change but does not stop GPS.

 EnterpriseDeviceManager edm = (EnterpriseDeviceManager) getSystemService(EnterpriseDeviceManager.ENTERPRISE_POLICY_SERVICE);
  LocationPolicy locationPolicy = edm.getLocationPolicy();
  try {
      boolean result = locationPolicy.setGPSStateChangeAllowed(false);
      if(result == true) {
          Log.d(TAG, "User is not allowed to change state of GPS");
      }
  }catch(SecurityException e) {
      Log.w(TAG,"SecurityException: "+e);
  }
 
Permission
The use of this API requires the caller to have the "android.permission.sec.MDM_LOCATION" permission which has a protection level of signature

Since
MDM 3.0
Multiuser Environment
User Scope

public boolean setLocationProviderState (String provider, boolean enable)

API to enable or disable a given location provider.

Parameters
provider The case sensitive location provider name. Sample values are gps, network, passive.
enable true to enable the location provider, false to disable the provider.
Returns
  • true if set location service is successful, else false.
Throws
SecurityException If caller does not have required permissions
IllegalArgumentException If provider name is invalid or empty
Usage
An administrator can use this API to enable or disable a location provider. If set to false, given location provider control is disabled, and the user cannot turn it on until the administrator enables it again. If set to true, given location provider control is enabled. Enabling a given location provider enables the corresponding UI setting for user change but does not enable the location provider itself. Please note some UI settings may require more than one location provider enabled so they can be available for user changes. All available location providers on the device can be obtained by using getAllLocationProviders().

 EnterpriseDeviceManager edm = (EnterpriseDeviceManager) getSystemService(EnterpriseDeviceManager.ENTERPRISE_POLICY_SERVICE);
  LocationPolicy locationPolicy = edm.getLocationPolicy();
  String gpsProvider  = "gps";//case sensitive
  try {
      boolean result = locationPolicy.setLocationProviderState(gpsProvider,
                       false);
      if(result == true){
          Log.d(TAG," GPS location provider is disabled !!!");
      }else{
          Log.d(TAG,"GPS location provider disable failed !!!");
      }
  }catch(SecurityException e) {
      Log.w(TAG,"SecurityException: "+e);
  }
 
Permission
The use of this API requires the caller to have the "android.permission.sec.MDM_LOCATION" permission which has a protection level of signature

Since
MDM 2.0
Multiuser Environment
User Scope

public boolean startGPS (boolean start)

API to start or stop GPS on the device.

Parameters
start true to start GPS, false to stop.
Returns
  • true if start or stop GPS is successful, else false.
Throws
SecurityException If caller does not have required permissions
Usage
An administrator can use this API to start or stop GPS on the device. If set to true, GPS is started. If set to false, GPS is stopped.

 EnterpriseDeviceManager edm = (EnterpriseDeviceManager) getSystemService(EnterpriseDeviceManager.ENTERPRISE_POLICY_SERVICE);
  LocationPolicy locationPolicy = edm.getLocationPolicy();
  try {
      boolean result = locationPolicy.startGPS(true);
      if(result == true){
          Log.d(TAG, "Start GPS success !!!");
      }else{
          Log.d(TAG, "Start GPS success fail !!!");
      }
  }catch(SecurityException e) {
      Log.w(TAG,"SecurityException: "+e);
  }
 
Permission
The use of this API requires the caller to have the "android.permission.sec.MDM_LOCATION" permission which has a protection level of signature

Since
MDM 3.0
Multiuser Environment
User Scope
See Also