/**
  * Processes a new HttpConnection object to instantiate a new browser Field (aka WebView) object,
  * and then resets the screen to the newly-created Field.
  *
  * @param connection
  * @param e
  */
 public void processConnection(HttpConnection connection, Event e) {
   // Cancel previous request.
   if (_currentConnection != null) {
     try {
       _currentConnection.close();
     } catch (IOException e1) {
     }
   }
   // Clear out pending responses.
   synchronized (pendingResponses) {
     pendingResponses.removeAllElements();
   }
   // Cancel any XHRs happening.
   commandManager.stopXHR();
   _currentConnection = connection;
   BrowserContent browserContent = null;
   Field field = null;
   try {
     browserContent = _renderingSession.getBrowserContent(connection, this, e);
     if (browserContent != null) {
       field = browserContent.getDisplayableContent();
       if (field != null) {
         synchronized (Application.getEventLock()) {
           _mainScreen.deleteAll();
           _mainScreen.add(field);
         }
       }
       browserContent.finishLoading();
     }
   } catch (RenderingException re) {
   } finally {
     browserContent = null;
     field = null;
     // Manually call the garbage collector to clean up all of leftover objects and free up the
     // nulled object handles.
     System.gc();
   }
 }
  public Object eventOccurred(final Event event) {
    int eventId = event.getUID();
    switch (eventId) {
      case Event.EVENT_REDIRECT:
        {
          RedirectEvent e = (RedirectEvent) event;
          String referrer = e.getSourceURL();
          switch (e.getType()) {
            case RedirectEvent.TYPE_SINGLE_FRAME_REDIRECT:
              // Show redirect message.
              Application.getApplication()
                  .invokeAndWait(
                      new Runnable() {
                        public void run() {
                          Status.show(REDIRECT_MSG);
                        }
                      });
              break;

            case RedirectEvent.TYPE_JAVASCRIPT:
              break;

            case RedirectEvent.TYPE_META:
              // MSIE and Mozilla don't send a Referer for META Refresh.
              referrer = null;
              break;

            case RedirectEvent.TYPE_300_REDIRECT:
              // MSIE, Mozilla, and Opera all send the original request's Referer as the Referer for
              // the new request.
              Object eventSource = e.getSource();
              if (eventSource instanceof HttpConnection) {
                referrer = ((HttpConnection) eventSource).getRequestProperty(REFERER);
              }
              eventSource = null;
              break;
          }
          // Create the request, populate header with referrer and fire off the request.
          HttpHeaders requestHeaders = new HttpHeaders();
          requestHeaders.setProperty(REFERER, referrer);
          PrimaryResourceFetchThread thread =
              new PrimaryResourceFetchThread(e.getLocation(), requestHeaders, null, event, this);
          thread.start();
          e = null;
          referrer = null;
          requestHeaders = null;
          break;
        }
      case Event.EVENT_URL_REQUESTED:
        {
          UrlRequestedEvent urlRequestedEvent = (UrlRequestedEvent) event;
          String url = urlRequestedEvent.getURL();
          HttpHeaders header = urlRequestedEvent.getHeaders();
          PrimaryResourceFetchThread thread =
              new PrimaryResourceFetchThread(
                  url, header, urlRequestedEvent.getPostData(), event, this);
          thread.start();
          urlRequestedEvent = null;
          url = null;
          header = null;
          break;
        }
      case Event.EVENT_BROWSER_CONTENT_CHANGED:
        {
          // Browser field title might have changed update title.
          BrowserContentChangedEvent browserContentChangedEvent =
              (BrowserContentChangedEvent) event;
          if (browserContentChangedEvent.getSource() instanceof BrowserContent) {
            BrowserContent browserField = (BrowserContent) browserContentChangedEvent.getSource();
            String newTitle = browserField.getTitle();
            if (newTitle != null) {
              synchronized (getAppEventLock()) {
                _mainScreen.setTitle(newTitle);
              }
            }
            browserField = null;
            newTitle = null;
          }
          browserContentChangedEvent = null;
          break;
        }
      case Event.EVENT_CLOSE:
        // TODO: close the application
        break;

      case Event.EVENT_SET_HEADER: // No cache support.
      case Event.EVENT_SET_HTTP_COOKIE:
        String cookie = ((SetHttpCookieEvent) event).getCookie();
        if (cookie.startsWith(PHONEGAP_PROTOCOL)) {
          String response = commandManager.processInstruction(cookie);
          if ((response != null) && (response.trim().length() > 0)) {
            pendingResponses.addElement(response);
          }
          response = null;
        }
        cookie = null;
        break;
      case Event.EVENT_HISTORY: // TODO: No history support.. but we added our own history stack
        // implementation in ConnectionManager. Can we hook it up - then we'd have
        // access to window.history :o
      case Event.EVENT_EXECUTING_SCRIPT: // No progress bar is supported.
      case Event.EVENT_FULL_WINDOW: // No full window support.
      case Event.EVENT_STOP: // No stop loading support.
      default:
    }

    return null;
  }