This documentation is for WSO2 Enterprise Mobility Manager 2.2.0. View documentation for the latest release.
Listening to Events Not Broadcasted by the Android OS - Enterprise Mobility Manager 2.2.0 - WSO2 Documentation
||
Skip to end of metadata
Go to start of metadata

This section describes how WSO2 EMM is configured to check the status of the events that are not broadcasted by the OS. This is done by continuously polling the relevant APIs. 

Follow the steps given below to check the status of the events via the Android API when the alarm is triggered.

  1. Configure the startListening() method of the AlertEventListener interface, for the API to poll continuously, and check if any events have triggered the alarm.

    The startListening() method needs to be configured in the new class you created. This class is in the org.wso2.emm.agent.events.listeners  package.

    @Override
    public void startListening() {
     RuntimeInfo runtimeInfo = new RuntimeInfo(EventRegistry.context, new String[] {
      "top",
      "-n",
      "1",
      "-m",
      "1",
      "-s",
      "cpu"
     });
     Application application = runtimeInfo.getHighestCPU();
     if (application.getCpu() > CPU_THRESHOLD) {
      try {
       String appState = CommonUtils.toJSON(application);
       publishEvent(appState, Constants.EventListners.RUNTIME_STATE);
       if (Constants.DEBUG_MODE_ENABLED) {
        Log.d(TAG, appState);
       }
      } catch (AndroidAgentException e) {
       Log.i(TAG, "Could not convert to JSON");
      }
     }
    }
  2. When an event is received, the onReceive method that is in the org.wso2.emm.agent.events.listeners package is notified. At that point you need to extract the data from the intent and populate the bean with the data.

    The Bean classes are located in the org.wso2.emm.agent.events.beans package.

  3. Convert the data populated by the bean to a JSON string using the CommonUtils.toJSON(beanInstance).
    Example:

    ApplicationStatus applicationState = new ApplicationStatus();
    applicationState.setState(status);
    applicationState.setPackageName(packageName);
    String appState = CommonUtils.toJSON(applicationState);
  4. Publish the data to WSO2 EMM by calling the publishEvent method. For this you need to configure the publish method so that it calls the HttpDataPublisher.
    Example:

    @Override
    public void publishEvent(String payload, String type) {
     EventPayload eventPayload = new EventPayload();
     eventPayload.setPayload(payload);
     eventPayload.setType(type);
     HttpDataPublisher httpDataPublisher = new HttpDataPublisher();
     httpDataPublisher.publish(eventPayload);
    }
  5. If you wish to periodically poll the APIs, you need to schedule the AlarmManager. This can be done via two options, depending on your requirement.

    For more information on the AlarmManager concept, see AlarmManager.

    • Option 1: Setting up the default alarm
      Use this option, if you wish to use the existing DEFAULT_START_TIME and DEFAULT_INTERVAL values. 

      1. In this method you don't have to do any changes to the default EventRegistry.startDefaultAlarm method that is in the org.wso2.emm.agent.events package.

        • DEFAULT_START_TIME: The time the alarm should first go off, in milliseconds. The default value is 1000.
        • DEFAULT_INTERVAL: The interval between subsequent repeats of the alarm, in milliseconds. The default value is 30000.
        • For more information on the default configuration, see EventRegistry.java file.
      2. Register the new listener by adding an or condition to the existing if condition.

      Example:

      if (Constants.EventListners.RUNTIME_STATE_LISTENER || Constants.EventListners.NEWLY_ADDED_LISTENER) {
          // If the event is running on a scheduled polling, it is only necessary to schedule
          // the alarm manager. If the same DEFAULT_START_TIME and DEFAULT_INTERVAL
          // can be used for any new event, there is no need to create a new
          // scheduled alarm here.
          EventRegistry.startDefaultAlarm(Constants.EventListners.DEFAULT_LISTENER_CODE,
                                          Constants.EventListners.DEFAULT_START_TIME,
                                          Constants.EventListners.DEFAULT_INTERVAL);
      }
    • Option 2: Setting up a new alarm

      Use this method, if you wish to change the DEFAULT_START_TIME and DEFAULT_INTERVAL values. 

      You will be using the same EventAlarmReceiver class, which is in the org.wso2.emm.agent.events package, in both option 1 and option 2.

      1. Configure the DEFAULT_LISTENER_CODEDEFAULT_START_TIME, and DEFAULT_INTERVAL values.

        EventRegistry.startDefaultAlarm(Constants.EventListners.DEFAULT_LISTENER_CODE,
                                            Constants.EventListners.DEFAULT_START_TIME,
                                            Constants.EventListners.DEFAULT_INTERVAL);

        Example:

        EventRegistry.startDefaultAlarm(Constants.EventListners.NEWLY_ADDED_LISTENER_CODE,
                                            Constants.EventListners.3000,
                                            Constants.EventListners.20000);
        • DEFAULT_LISTENER_CODE: The code used by the EventAlarmReceiver to identify the event listeners. Make sure to change this field to a preferred value.

        • DEFAULT_START_TIME: Define the time the alarm should first go off, in milliseconds.
        • DEFAULT_INTERVAL: Define the interval between subsequent repeats of the alarm, in milliseconds.
        • For more information on the default configuration, see EventRegistry.java file.
      2. Configure the EventAlarmReceiver class that is in the org.wso2.emm.agent.events package.
        1. Configure the Constants.EventListners.DEFAULT_LISTENER_CODE  property by updating it with the new code you defined in step a.

          if (requestCode == Constants.EventListners.DEFAULT_LISTENER_CODE)

          Example:

          if (requestCode == Constants.EventListners.NEWLY_ADDED_LISTENER_CODE)
        2. Write a logic inside the startListening() to periodically poll the API.

          if (requestCode == Constants.EventListners.DEFAULT_LISTENER_CODE) {
              RuntimeStateListener runtimeStateListener = new RuntimeStateListener();
            runtimeStateListener.startListening();
          }
  • No labels