public void disablePresenceClickEventHandler(View v) {
    EditText editTextServer = (EditText) findViewById(R.id.EditTextServer);
    EditText editTextApplicationKey = (EditText) findViewById(R.id.EditTextApplicationKey);
    EditText editTextChannel = (EditText) findViewById(R.id.EditTextChannel);
    CheckBox checkBoxIsCluster = (CheckBox) findViewById(R.id.CheckBoxIsCluster);

    Ortc.disablePresence(
        editTextServer.getText().toString(),
        checkBoxIsCluster.isChecked(),
        editTextApplicationKey.getText().toString(),
        defaultPrivateKey,
        editTextChannel.getText().toString(),
        new OnDisablePresence() {
          @Override
          public void run(Exception error, String result) {
            final Exception exception = error;
            final String resultText = result;
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    if (exception != null) {
                      log(String.format("Error: %s", exception.getMessage()));
                    } else {
                      log(resultText);
                    }
                  }
                });
          }
        });
  }
  public void connectClickEventHandler(View v) {
    EditText editTextServer = (EditText) findViewById(R.id.EditTextServer);
    EditText editTextApplicationKey = (EditText) findViewById(R.id.EditTextApplicationKey);
    EditText editTextAuthenticationToken =
        (EditText) findViewById(R.id.EditTextAuthenticationToken);
    EditText editTextConnectionMetadata = (EditText) findViewById(R.id.EditTextConnectionMetadata);
    CheckBox checkBoxIsCluster = (CheckBox) findViewById(R.id.CheckBoxIsCluster);

    if (defaultNeedsAuthentication) {
      try {
        TextView textViewLog = (TextView) findViewById(R.id.TextViewLog);
        textViewLog.setMovementMethod(new ScrollingMovementMethod());

        HashMap<String, ChannelPermissions> permissions = new HashMap<String, ChannelPermissions>();

        permissions.put("yellow:*", ChannelPermissions.Write);
        permissions.put("yellow", ChannelPermissions.Write);
        permissions.put("test:*", ChannelPermissions.Write);
        permissions.put("test", ChannelPermissions.Write);

        log("Authenticating...");

        if (!Ortc.saveAuthentication(
            editTextServer.getText().toString(),
            checkBoxIsCluster.isChecked(),
            editTextAuthenticationToken.getText().toString(),
            false,
            editTextApplicationKey.getText().toString(),
            14000,
            defaultPrivateKey,
            permissions)) {
          log("Unable to authenticate");
        } else {
          log("Authentication successfull");
        }
      } catch (Exception e) {
        log(String.format("ORTC AUTHENTICATION ERROR: %s", e.toString()));
      }
    }

    if (checkBoxIsCluster.isChecked()) {
      client.setClusterUrl(editTextServer.getText().toString());
    } else {
      client.setUrl(editTextServer.getText().toString());
    }

    client.setConnectionMetadata(editTextConnectionMetadata.getText().toString());

    log("Connecting...");
    client.connect(
        editTextApplicationKey.getText().toString(),
        editTextAuthenticationToken.getEditableText().toString());
  }
  @Override
  protected void onStart() {
    super.onStart();

    try {
      Ortc ortc = new Ortc();

      OrtcFactory factory;

      factory = ortc.loadOrtcFactory("IbtRealtimeSJ");

      client = factory.createClient();

    } catch (Exception e) {
      log(String.format("ORTC CREATE ERROR: %s", e.toString()));
    }

    if (client != null) {
      try {
        client.onConnected =
            new OnConnected() {

              public void run(final OrtcClient sender) {
                runOnUiThread(
                    new Runnable() {

                      public void run() {
                        log(String.format("Connected to: %s", ((OrtcClient) sender).getUrl()));
                      }
                    });
              }
            };

        client.onDisconnected =
            new OnDisconnected() {

              public void run(OrtcClient arg0) {
                runOnUiThread(
                    new Runnable() {

                      public void run() {
                        log("Disconnected");
                      }
                    });
              }
            };

        client.onSubscribed =
            new OnSubscribed() {

              public void run(OrtcClient sender, String channel) {
                final String subscribedChannel = channel;
                runOnUiThread(
                    new Runnable() {

                      public void run() {
                        log(String.format("Subscribed to channel: %s", subscribedChannel));
                      }
                    });
              }
            };

        client.onUnsubscribed =
            new OnUnsubscribed() {

              public void run(OrtcClient sender, String channel) {
                final String subscribedChannel = channel;
                runOnUiThread(
                    new Runnable() {

                      public void run() {
                        log(String.format("Unsubscribed from channel: %s", subscribedChannel));
                      }
                    });
              }
            };

        client.onException =
            new OnException() {

              public void run(OrtcClient send, Exception ex) {
                final Exception exception = ex;
                runOnUiThread(
                    new Runnable() {

                      public void run() {
                        log(String.format("Error: %s", exception.getMessage()));
                      }
                    });
              }
            };

        client.onReconnected =
            new OnReconnected() {

              public void run(final OrtcClient sender) {
                runOnUiThread(
                    new Runnable() {

                      public void run() {
                        reconnectingTries = 0;

                        log(String.format("Reconnected to: %s", ((OrtcClient) sender).getUrl()));
                      }
                    });
              }
            };

        client.onReconnecting =
            new OnReconnecting() {

              public void run(OrtcClient sender) {
                runOnUiThread(
                    new Runnable() {

                      public void run() {
                        reconnectingTries++;

                        log(String.format("Reconnecting %s...", reconnectingTries));
                      }
                    });
              }
            };
      } catch (Exception e) {
        log(String.format("ORTC EXCEPTION: %s", e.toString()));
      }
    }
  }
  public void presenceClickEventHandler(View v) {
    EditText editTextServer = (EditText) findViewById(R.id.EditTextServer);
    EditText editTextApplicationKey = (EditText) findViewById(R.id.EditTextApplicationKey);
    EditText editTextAuthenticationToken =
        (EditText) findViewById(R.id.EditTextAuthenticationToken);
    EditText editTextChannel = (EditText) findViewById(R.id.EditTextChannel);
    CheckBox checkBoxIsCluster = (CheckBox) findViewById(R.id.CheckBoxIsCluster);

    if (client != null && client.getIsConnected()) {
      try {
        client.presence(
            editTextChannel.getText().toString(),
            new OnPresence() {
              @Override
              public void run(Exception error, Presence presence) {
                final Exception exception = error;
                final Presence presenceData = presence;
                runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        if (exception != null) {
                          log(String.format("Error: %s", exception.getMessage()));
                        } else {
                          Iterator<?> metadataIterator =
                              presenceData.getMetadata().entrySet().iterator();
                          while (metadataIterator.hasNext()) {
                            @SuppressWarnings("unchecked")
                            Map.Entry<String, Long> entry =
                                (Map.Entry<String, Long>) metadataIterator.next();
                            log(entry.getKey() + " - " + entry.getValue());
                          }
                          log("Subscriptions - " + presenceData.getSubscriptions());
                        }
                      }
                    });
              }
            });
      } catch (OrtcNotConnectedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    } else {
      Ortc.presence(
          editTextServer.getText().toString(),
          checkBoxIsCluster.isChecked(),
          editTextApplicationKey.getText().toString(),
          editTextAuthenticationToken.getText().toString(),
          editTextChannel.getText().toString(),
          new OnPresence() {
            @Override
            public void run(Exception error, Presence presence) {
              final Exception exception = error;
              final Presence presenceData = presence;
              runOnUiThread(
                  new Runnable() {
                    @Override
                    public void run() {
                      if (exception != null) {
                        log(String.format("Error: %s", exception.getMessage()));
                      } else {
                        Iterator<?> metadataIterator =
                            presenceData.getMetadata().entrySet().iterator();
                        while (metadataIterator.hasNext()) {
                          @SuppressWarnings("unchecked")
                          Map.Entry<String, Long> entry =
                              (Map.Entry<String, Long>) metadataIterator.next();
                          log(entry.getKey() + " - " + entry.getValue());
                        }
                        log("Subscriptions - " + presenceData.getSubscriptions());
                      }
                    }
                  });
            }
          });
    }
  }