4

I am trying to handle an event in BrowserField when the user actually clicks a link. I studied BrowserFieldListener, tried its documentCreated() method but that gives me a response when the page starts loading. I want a trigger the moment user clicks a link inside browserField.

What am i missing here?

Jazib
  • 1,180
  • 1
  • 16
  • 37

3 Answers3

6

Override handleNavigationRequest() of ProtocolController like

ProtocolController controller = new ProtocolController(browserField) {
    public void handleNavigationRequest(BrowserFieldRequest request) throws Exception {
         /* 
         Here you get the redirection link using 
           request.getURL() 
         and do what you want to do 
          */
      // to display url in browserfield use
      InputConnection inputConnection = handleResourceRequest(request);
      browserField.displayContent(inputConnection, request.getURL()); 

    }
};

browserField.getConfig().setProperty(BrowserFieldConfig.CONTROLLER, controller);
Ajmal M A
  • 1,506
  • 20
  • 43
  • Please could you include what to do inside the method? (in particular, the call to super.handleNavigationRequest or whatever has to be done to open the link as usual). I'm trying to intercept some URL patterns and always get a null error when calling super. – Mister Smith Aug 13 '12 at 07:25
  • Thanks for the edit. That was the code I was debugging too, and it is supposed to work (but not for me, for some reason I ignore) I gave up on it. – Mister Smith Aug 13 '12 at 10:00
3

Use the following class that was I used

public class CacheProtocolController extends ProtocolController{
    public CacheProtocolController() {
        super(browserField);     
    }

    /**
    * Handle navigation requests (e.g., link clicks)
    */
    public void handleNavigationRequest(final BrowserFieldRequest request) throws Exception {
        UiApplication.getUiApplication().invokeAndWait(new Runnable() {
            public void run() {
                // TODO Auto-generated method stub
                Logger.debug("*******URL*******",request.getURL() );
        });
    }

    /**
    * Handle resource request (e.g., images, external css/javascript resources)
    */
    public InputConnection handleResourceRequest(BrowserFieldRequest request) throws Exception {
        return super.handleResourceRequest(request);
    }
}
Nate
  • 30,589
  • 12
  • 76
  • 201
0

I have solved this problem using the following class:

public class CacheProtocolController extends ProtocolController{
    private SparseList sparseList = null;
    private int imageIndex ;
    private int click = 0;
    private BrowserField browserField = null;

    public CacheProtocolController(BrowserField browserField,SparseList sparseList,int imageIndex ) {
        super(browserField);
        this.sparseList = sparseList;
        this.imageIndex = imageIndex;
    }

    /**
    * Handle navigation requests (e.g., link clicks)
    */
    public void handleNavigationRequest(final BrowserFieldRequest request) throws Exception {
        UiApplication.getUiApplication().invokeAndWait(new Runnable() {
            public void run() {
                Logger.debug("*******URL*******",request.getURL() );
                String requestUrl = null;
                requestUrl = FileManipulations.replaceAll(request.getURL(), "file:///SDCard/BlackBerry/pictures/", "../");
                Logger.debug("*******requestUrl*******",requestUrl );
                Enumeration enumeration = sparseList.elements();
                while (enumeration.hasMoreElements()) {
                    final News news = (News) enumeration.nextElement();
                    if(news.getDetailsURL().equalsIgnoreCase(requestUrl)){
                        if(click == 1){
                            click = 0;
                            UiApplication.getUiApplication().pushScreen(new DetailedNewsScreen(news.getImageURL() , imageIndex));
                        } else
                            click++;
                    }
                }
            }
        });
    }

    /**
    * Handle resource request (e.g., images, external css/javascript resources)
    */
    public InputConnection handleResourceRequest(BrowserFieldRequest request) throws Exception {
        return super.handleResourceRequest(request);
    }

}

And in the MainScren use the following

browserField = new BrowserField();
browserField.getConfig().setProperty(BrowserFieldConfig.CONTROLLER, new CacheProtocolController(browserField,List,index));
Nate
  • 30,589
  • 12
  • 76
  • 201
pranavjayadev
  • 917
  • 7
  • 29