This documentation is for WSO2 Enterprise Mobility Manager 2.2.0. View documentation for the latest release.
Listening to Events 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 provides information on how to listen and capture events that are broadcasted by the Android OS.

Prerequsite

Prior knowledge on the Android Broadcast Receivers will help you understand the concept clearly, as WSO2 EMM uses it to listen to the events. For more information, see BroadcastReceiver.

Follow the steps given below to configure WSO2 EMM, to listen to the broadcasted events:

  1. Register the receiver in the startListening() method of the AlertEventListener interface, in the class you created in the org.wso2.emm.agent.events.listeners package.

    Example: 

    @Override
    public void startListening() {
      EventRegistry.context.registerReceiver(this, intentFilter);
    }
  2. When an event is received the onReceive method that is in the org.wso2.emm.agent.events.listeners package will be 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. Optionally, if you wish to publish data to a different server other than WSO2 EMM or use a different protocol, you can do so by writing a new data publisher.
    Implement the DataPublisher interface that is in the package org.wso2.emm.agent.events.publisher, to write the new data publisher.

     Click here to see a sample that is written to listen to application changes on the device, such as application installs, uninstalls, upgrades, and data clearing.
    /**
     * Listening to application state changes such as an app getting installed, uninstalled,
     * upgraded and data cleared.
     */
    public class ApplicationStateListener extends BroadcastReceiver implements AlertEventListener {
        private static final String TAG = ApplicationStateListener.class.getName();
    
        @Override
        public void startListening() {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
            intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
            intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
            intentFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
            intentFilter.addDataScheme("package");
            EventRegistry.context.registerReceiver(this, intentFilter);
        }
    
        @Override
        public void stopListening() {
            if (EventRegistry.context != null) {
                EventRegistry.context.unregisterReceiver(this);
            }
        }
    
            @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);
            }
    
        @Override
        public void onReceive(Context context, final Intent intent) {
            String status = null;
            ApplicationStatus applicationState;
            switch (intent.getAction()) {
                case Intent.ACTION_PACKAGE_ADDED:
                    status = "added";
                    break;
                case Intent.ACTION_PACKAGE_REMOVED:
                    status = "removed";
                    break;
                case Intent.ACTION_PACKAGE_REPLACED:
                    status = "upgraded";
                    break;
                case Intent.ACTION_PACKAGE_DATA_CLEARED:
                    status = "dataCleared";
                    break;
                default:
                    Log.i(TAG, "Invalid intent received");
            }
            if (status != null) {
                String packageName = intent.getData().getEncodedSchemeSpecificPart();
                applicationState = new ApplicationStatus();
                applicationState.setState(status);
                applicationState.setPackageName(packageName);
                try {
                    String appState = CommonUtils.toJSON(applicationState);
                    publishEvent(appState, Constants.EventListners.APPLICATION_STATE);
                    if (Constants.DEBUG_MODE_ENABLED) {
                        Log.d(TAG, appState);
                    }
                } catch (AndroidAgentException e) {
                    Log.e(TAG, "Could not convert to JSON");
                }
            }
        }
    }
    
  6. Open the AndroidManifest.xml file that is in the <EMM_Android_agent>/client/src/main folder, and  add a new receiver. The name of the receiver should be the same as the listener class you created in step 1.
    Example:

    Receiver
    <receiver android:name=".events.listeners.ApplicationStateListener">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <action android:name="android.intent.action.PACKAGE_DATA_CLEARED" />
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>

    Now you have set up the listener.

  7. Call the startListening method from the register method, to register the listener. The register method is in the EventRegistry class.

    For more information on the default configuration, see EventRegistry.java file.

    Example:

    public void register() {
        // First, check if event listening is enabled. If yes, check each event that is enabled and
        // start event listening.
        if (Constants.EventListners.EVENT_LISTENING_ENABLED) {
            if (Constants.EventListners.APPLICATION_STATE_LISTENER) {
                // If the listener is implementing broadcast listener, calling start listener
                // should start listening for events.
                ApplicationStateListener applicationState = new ApplicationStateListener();
                applicationState.startListening();
            }
            if (Constants.EventListners.RUNTIME_STATE_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);
            }
        }
    }
  • No labels