Intercepting Annotation Handling in Tools

By default, the Tools package will respond to user actions such as a click event on a link or form widget, or a property change event on a selected annotation. If you would like to customize the behavior executed (prior to handling by the Tools package) in response to user actions, it is possible to do so via the BasicAnnotationListener.onInterceptAnnotationHandling(Annot, Bundle, int) API. If this method returns true, then it will intercept executing the default logic.

In BasicAnnotationListener.onInterceptAnnotationHandling(Annot, Bundle, int), the second parameter, Bundle, contains the name of the action intercepted by this function, as well as some extra information. You can get the name of the intercepted action by calling bundle.getString(Tool.METHOD_FROM):

public boolean onInterceptAnnotationHandling(Annot annot, Bundle extra, ToolMode toolMode) {
    if (extra != null && extra.containsKey(Tool.METHOD_FROM)) {
        String methodCalling = extra.getString(Tool.METHOD_FROM);
    }
}

To get extra information, first obtain the information keys by calling bundle.getStringArray(Tool.KEYS):

String[] paramKeys = bundle.getStringArray(Tool.KEYS)

Then, you can get the information values by looping on each key obtained above:

public boolean onInterceptAnnotationHandling(Annot annot, Bundle extra, ToolMode toolMode) {
    if (extra != null && extra.containsKey(Tool.KEYS)) {
        String[] paramKeys = extra.getStringArray(Tool.KEYS);
        if (paramKeys != null) {
            for (String key : paramKeys) {
                // Gets the information value
                Object param = extra.get(key);
            }
        }
    }
}

For example, the following code demonstrates several scenarios and how you can get notified before each: clicking on a radio button, changing the annotation opacity, and clicking on a link. See the comments in the code for details.

mToolManager.setBasicAnnotationListener(new ToolManager.BasicAnnotationListener() {
    @Override
    public boolean onInterceptAnnotationHandling(Annot annot, Bundle extra, ToolMode toolMode) {
        /*
            For example, when a radio button is clicked, this code will print:
            calling method: handleWidget
            property: OTHER
        */
        if (extra != null && extra.containsKey(Tool.METHOD_FROM)) {
            String methodCalling = extra.getString(Tool.METHOD_FROM);
            Log.d("InterceptAnnot", "calling method " + methodCalling);
            AnnotationProperty.Property property = AnnotationProperty.getProperty(methodCalling);
            Log.d("InterceptAnnot", "property: " + property);
        }

        /*
            For example, when annotation opacity is about to be changed to 50%, this code will print:
            calling method: editOpacity
            property: OPACITY
            key: opacity
            value: 0.5
        */
        if (extra != null && extra.containsKey(Tool.KEYS)) {
            String[] paramKeys = extra.getStringArray(Tool.KEYS);
            if (paramKeys != null) {
                for (String key : paramKeys) {
                    Object param = extra.get(key);
                    Log.d("InterceptAnnot", "key: " + key);
                    Log.d("InterceptAnnot", "value: " + param.toString());
                }
            }
        }

        try {
            // Intercept radio button from clicking by return true
            if(annot.getType() == Annot.e_Widget) {
                Widget w = new Widget(annot);
                Field field = w.getField();
                if (field != null && field.isValid() && field.getType() == Field.e_radio) {
                    return true;
                }
            }

            // Intercept clicking link annotation by return true
            if (annot.getType() == Annot.e_Link) {
                Log.d("InterceptAnnot", "handling link annotation");
            }
        } catch (PDFNetException e) {
            e.printStackTrace();
        }
        // return false so the other events can continue executing
        return false;
    }
});

If you simply want to get notified after the action has happened, you can do so via the AnnotationModificationListener.onAnnotationsModified(Map<Annot, Integer>) API. This event callback is raised when an annotation change has occurred. For example, in order to get notified when the value of a checkbox has been changed and when annotation opacity has been changed, do the following:

ToolManager.AnnotationModificationListener annotationModificationListener = new ToolManager.AnnotationModificationListener() {
    @Override
    public void onAnnotationsModified(Map<Annot, Integer> annots, Bundle extra) {
        /*
            For example, when a checkbox's value has been changed, this code will print:
            calling method: handleWidget
            property: OTHER
        */
        if (extra != null && extra.containsKey(Tool.METHOD_FROM)) {
            String methodCalling = extra.getString(Tool.METHOD_FROM);
            Log.d("AnnotationsModified", "calling method " + methodCalling);
            AnnotationProperty.Property property = AnnotationProperty.getProperty(methodCalling);
            Log.d("AnnotationsModified", "property: " + property);
        }

        /*
            For example, when annotation opacity is changed to 50%, this code will print:
            calling method: editOpacity
            property: OPACITY
            key: opacity
            value: 0.5
        */
        if (extra != null && extra.containsKey(Tool.KEYS)) {
            String[] paramKeys = extra.getStringArray(Tool.KEYS);
            if (paramKeys != null) {
                for (String key : paramKeys) {
                    Object param = extra.get(key);
                    Log.d("AnnotationsModified", "key: " + key);
                    Log.d("AnnotationsModified", "value: " + param.toString());
                }
            }
        }
    }
};
mToolManager.addAnnotationModificationListener(annotationModificationListener);

When finished with the callback, do:

mToolManager.removeAnnotationModificationListener(annotationModificationListener);