/*
   * Load the spinner
   */
  void loadSpinner() {

    // If loadingDialog property, then show the App loading dialog for first page of app
    String loading = null;
    if ((this.appView == null) || !this.appView.canGoBack()) {
      loading = preferences.getString("LoadingDialog", null);
    } else {
      loading = preferences.getString("LoadingPageDialog", null);
    }
    if (loading != null) {

      String title = "";
      String message = "Loading Application...";

      if (loading.length() > 0) {
        int comma = loading.indexOf(',');
        if (comma > 0) {
          title = loading.substring(0, comma);
          message = loading.substring(comma + 1);
        } else {
          title = "";
          message = loading;
        }
      }
      this.spinnerStart(title, message);
    }
  }
  /**
   * Report an error to the host application. These errors are unrecoverable (i.e. the main resource
   * is unavailable). The errorCode parameter corresponds to one of the ERROR_* constants.
   *
   * @param errorCode The error code corresponding to an ERROR_* value.
   * @param description A String describing the error.
   * @param failingUrl The url that failed to load.
   */
  public void onReceivedError(
      final int errorCode, final String description, final String failingUrl) {
    final CordovaActivity me = this;

    // If errorUrl specified, then load it
    final String errorUrl = preferences.getString("errorUrl", null);
    if ((errorUrl != null)
        && (errorUrl.startsWith("file://") || whitelist.isUrlWhiteListed(errorUrl))
        && (!failingUrl.equals(errorUrl))) {

      // Load URL on UI thread
      me.runOnUiThread(
          new Runnable() {
            public void run() {
              // Stop "app loading" spinner if showing
              me.spinnerStop();
              me.appView.showWebPage(errorUrl, false, true, null);
            }
          });
    }
    // If not, then display error dialog
    else {
      final boolean exit = !(errorCode == WebViewClient.ERROR_HOST_LOOKUP);
      me.runOnUiThread(
          new Runnable() {
            public void run() {
              if (exit) {
                me.appView.setVisibility(View.GONE);
                me.displayError(
                    "Application Error", description + " (" + failingUrl + ")", "OK", exit);
              }
            }
          });
    }
  }
  /** Load the url into the webview. */
  public void loadUrl(String url) {
    if (appView == null) {
      init();
    }
    this.splashscreenTime = preferences.getInteger("SplashScreenDelay", this.splashscreenTime);
    String splash = preferences.getString("SplashScreen", null);
    if (this.splashscreenTime > 0 && splash != null) {
      this.splashscreen =
          getResources().getIdentifier(splash, "drawable", getClass().getPackage().getName());
      ;
      if (this.splashscreen != 0) {
        this.showSplashScreen(this.splashscreenTime);
      }
    }

    // If keepRunning
    this.keepRunning = preferences.getBoolean("KeepRunning", true);

    // Check if the view is attached to anything
    if (appView.getParent() != null) {
      // Then load the spinner
      this.loadSpinner();
    }
    // Load the correct splashscreen

    if (this.splashscreen != 0) {
      this.appView.loadUrl(url, this.splashscreenTime);
    } else {
      this.appView.loadUrl(url);
    }
  }
  /**
   * Called when a message is sent to plugin.
   *
   * @param id The message id
   * @param data The message data
   * @return Object or null
   */
  public Object onMessage(String id, Object data) {
    if (!"onScrollChanged".equals(id)) {
      LOG.d(TAG, "onMessage(" + id + "," + data + ")");
    }

    if ("splashscreen".equals(id)) {
      if ("hide".equals(data.toString())) {
        this.removeSplashScreen();
      } else {
        // If the splash dialog is showing don't try to show it again
        if (this.splashDialog == null || !this.splashDialog.isShowing()) {
          String splashResource = preferences.getString("SplashScreen", null);
          if (splashResource != null) {
            splashscreen =
                getResources()
                    .getIdentifier(splashResource, "drawable", getClass().getPackage().getName());
          }
          this.showSplashScreen(this.splashscreenTime);
        }
      }
    } else if ("spinner".equals(id)) {
      if ("stop".equals(data.toString())) {
        this.spinnerStop();
        this.appView.setVisibility(View.VISIBLE);
      }
    } else if ("onReceivedError".equals(id)) {
      JSONObject d = (JSONObject) data;
      try {
        this.onReceivedError(d.getInt("errorCode"), d.getString("description"), d.getString("url"));
      } catch (JSONException e) {
        e.printStackTrace();
      }
    } else if ("exit".equals(id)) {
      this.endActivity();
    }
    return null;
  }
 /** Get string property for activity. */
 @Deprecated // Call method on preferences directly.
 public String getStringProperty(String name, String defaultValue) {
   return preferences.getString(name, defaultValue);
 }