Ejemplo n.º 1
0
  /** Create a new GPSDemoAdvanced object */
  public GPSDemoAdvanced() {
    final MainScreen screen = new MainScreen();

    // Initialize UI components
    screen.setTitle("GPS Advanced Demo");
    final LabelField apiChoiceMessage =
        new LabelField("Please select an API to test:", Field.USE_ALL_WIDTH | Field.FIELD_HCENTER);
    _coreTestButton =
        new ButtonField("Core GPS API Test", ButtonField.NEVER_DIRTY | ButtonField.CONSUME_CLICK);
    _extendedTestButton =
        new ButtonField(
            "Extended GPS API Test", ButtonField.NEVER_DIRTY | ButtonField.CONSUME_CLICK);
    _coreTestButton.setChangeListener(this);
    _extendedTestButton.setChangeListener(this);

    screen.add(apiChoiceMessage);
    screen.add(_coreTestButton);
    screen.add(_extendedTestButton);
    pushScreen(screen);
  }
  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;
  }