Invoking the Android System Service API
The Android system service application provides an API for the external applications to know the device firmware upgrade package status, download progress and to check if a new firmware upgrade is available. It also initiates new firmware upgrades and enables/disables Automatic Retry for firmware upgrades. To invoke these APIs, external applications should invoke the service exposed by the system service application and implement a BroadcastReceiver
to receive results. Let's take a look at how it's done by following the subsections.
Configure the external application
Follow the steps given below to configure the external application:
- In order to use this API, you need to sign the android application with the same key, which you have used to signed the EMM agent and the System service.
- Add the
uses-permission
entry to theorg.wso2.emm.system.service.permission.ACCESS
to themanifest.xml
file of the application. - Add the receiver to receive broadcasts from the Android system service application.
Take a look at the sample given below to understand how the above entries need to be added to your applications manifest.xml
file.
<manifest ...> ... <uses-permission android:name="org.wso2.emm.system.service.permission.ACCESS"/> ... <application ...> ... <receiver android:name=.UpgradeServiceResponseReceiver> <intent-filter> <action android:name="org.wso2.emm.system.service.MESSAGE_PROCESSED"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> ... </application> </manifest>
Receiving the Broadcasts
Follow the steps given below to receive broadcasts from system service application.
Configure the
BroadcastReceiver
.UpgradeServiceResponseReceiver.javapublic class UpgradeServiceResponseReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String operation = intent.getStringExtra(“operation”); //The operation result broadcast belongs String code = intent.getStringExtra(“code”); //Status code (200 for success, 300 for pending and 400 for failures) String status = intent.getStringExtra(“status”); //Sub status of the broadcast for more details. //Optional message or stringified JSON data related to the sub status. String payload = intent.getStringExtra(“payload”); //Your result manipulation code goes here } }
Register a receiver in your activity or service to listen to the service results.
IntentFilter filter = new IntentFilter(“org.wso2.emm.system.service.MESSAGE_PROCESSED”); filter.addCategory(Intent.CATEGORY_DEFAULT); UpgradeServiceResponseReceiver receiver = new UpgradeServiceResponseReceiver(); registerReceiver(receiver, filter);
Supported broadcasts
The Systems Service uses the following broadcast codes to indicate success, pending and failure status.
Code | Status of the result |
---|---|
200 | Success |
300 | Pending |
400 | Failure |
The following states are defined in the broadcasts to identify specific sub status related to the above status codes.
Status | Description |
---|---|
2000 | Successful |
2001 | Ongoing |
2002 | Request placed. |
3000 | User canceled. |
3001 | No data available. |
4000 | Malformed request. i.e. command is not in valid format |
4001 | Insufficient battery level to download firmware. |
4002 | Wi-Fi off. |
4003 | Unreachable network. |
4004 | No upgrade found. |
4005 | Upgrade info not readable. |
4006 | OTA download failed. |
4007 | OTA image invalid. |
4008 | Low disk space. |
4009 | Malformed OTA URL. |
4010 | Another pending upgrade in line. |
4011 | Insufficient battery level to install the firmware. |
4012 | OTA verification failed. |
4013 | Connection failed. |
4014 | File not found. |
5001 | Internal error |
Broadcast operations, code, and the status descriptions.
Operation | Code | Status | Payload | Description |
---|---|---|---|---|
FIRMWARE_UPGRADE_ | 200 | 2001 | { "progress":<PROGRESS VALUE> } | The Payload contains the firmware progress as a percentage. |
FIRMWARE_UPGRADE_ | 200 | 3001 | - | Indicates that the system service doesn't have any records related to the latest firmware download progress. This status is returned if a firmware upgrade has not yet being carried out. |
FIRMWARE_UPGRADE_ | 300 | 2002, 4001 to 4003, 4008 | - | The pending code with the status indicating that the operation is pending. (Please refer the status descriptions.) |
FIRMWARE_UPGRADE_ | 400 | 4000 to 4014, 5001 | <optional error message> | The failure code with the status indicating the root cause of the failure. (Please refer the status descriptions.) Optionally, an error message may be available in the payload. |
UPGRADE_FIRMWARE
| 200 | 2000 | - | The success code with the status indicating the completion of the firmware upgrade. |
UPGRADE_FIRMWARE
| 200 | 2001 | { "progress":<PROGRESS VALUE> } | The Payload contains the firmware upgrade progress as a percentage. |
| 200 | 2002 | - | The success code along with the request placed status. This indicates that the request entered the critical path of the execution. |
UPGRADE_FIRMWARE
| 300 | 2001 | - | The operation pending code along with ongoing OTA upgrade status. |
UPGRADE_FIRMWARE
| 400 | 3000, 4000 to 4014, 5001 | <optional error message> | The failure code along with the status indicating the root cause of the failure. (Please refer the status descriptions.) Optionally, an error message may be available in the payload. |
FIRMWARE_BUILD_ | 200 | 2000 | { "buildDate":<UTC DATE & TIME> } | The success code along with the value of the ro.build.date.utc property. |
FIRMWARE_BUILD_ | 400 | 5001 | <error message> | The failure code along with the status indicating the internal error. The error message is available in the payload. |
FIRMWARE_UPGRADE_ | 200 | 2000 | { "size":<UPGRADE PACKAGE SIZE>, "release":<RELEASE CODE>, "version":<VERSION CODE>, "description":<DESCRIPTION> } | The Success code along with the success status of the firmware upgrade. The payload contains a JSON string with the details of the upgrade. |
FIRMWARE_UPGRADE_ | 200 | 2002 | - | The success code along with the request placed status. This indicates that the request entered the critical path of the execution. |
FIRMWARE_UPGRADE_ | 200 | 4004 | - | The success status with the no upgrade found status. |
FIRMWARE_UPGRADE_ | 400 | 4000,4002, | <optional error message> | The failure code with the status that indicates the root cause of the failure. (Please refer the status descriptions.) Optionally, an error message may be available in the payload. |
Invoking the API
Follow the steps given below to invoke the API by starting the exposed service.
Create an explicit intent method.
public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) { //Retrieve all services that can match the given intent PackageManager pm = context.getPackageManager(); List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); //Make sure only one match was found if (resolveInfo == null || resolveInfo.size() != 1) { return null; } //Get component info and create ComponentName ResolveInfo serviceInfo = resolveInfo.get(0); String packageName = serviceInfo.serviceInfo.packageName; String className = serviceInfo.serviceInfo.name; ComponentName component = new ComponentName(packageName, className); //Create a new intent. Use the old one for extras and such reuse Intent explicitIntent = new Intent(implicitIntent); //Set the component to be explicit explicitIntent.setComponent(component); return explicitIntent; }
Call the service.
This should always happen after registering the receiver. Else, you will not receive the results of your API call from the systems service.
Intent intent = new Intent(“org.wso2.emm.system.service.START_SERVICE”); Intent explicitIntent = createExplicitFromImplicitIntent(context, intent); if (explicitIntent != null) { intent = explicitIntent; } intent.putExtra("operation", <Operation code>); //Operation code which need to perform intent.putExtra("command", <Commands for the operation>); //Optional command string which might required for the particular operation. startService(intent); //Start the service with intent
Supported operations by the API
After calling to below operations System Service will send a broadcast with the response. Please check the Broadcast operations, code, and the status descriptions section for the corresponding responses.
Operation Code | Command format | Description |
---|---|---|
| "true" or "false" | Enable or disable the automatic retry on failure option for firmware upgrades. The value set by this operation will be overwritten by the The command should be specified as a string that contains |
UPGRADE_FIRMWARE | { "schedule":<ISO 8601 DATA & TIME>, "server":<OTA SERVER URL>, "autoRetry":<TRUE or FALSE> } | The firmware upgrade has the following 3 parameters by default.
Let's look at how this operation works:
|
FIRMWARE_UPGRADE_ | { "server": <OTA server URL> } | Check the availability of the firmware upgrade. The server parameter is optional and if you don't define a value the default value will be used. |
FIRMWARE_BUILD_DATE | - | Get the build date of the current firmware. |
FIRMWARE_UPGRADE_ | - | Get the download progress of the current firmware upgrade. |