Android Open Source - RealtimeMessaging-Android Main Activity






From Project

Back to project page RealtimeMessaging-Android.

License

The source code is released under:

MIT License

If you think the Android project RealtimeMessaging-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ibt.ortc.ortclib;
/*w w  w.  j ava 2 s  .c o  m*/
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import ibt.ortc.api.ChannelPermissions;
import ibt.ortc.api.OnDisablePresence;
import ibt.ortc.api.OnEnablePresence;
import ibt.ortc.api.OnPresence;
import ibt.ortc.api.Ortc;
import ibt.ortc.api.Presence;
import ibt.ortc.extensibility.OnConnected;
import ibt.ortc.extensibility.OnDisconnected;
import ibt.ortc.extensibility.OnException;
import ibt.ortc.extensibility.OnMessage;
import ibt.ortc.extensibility.OnReconnected;
import ibt.ortc.extensibility.OnReconnecting;
import ibt.ortc.extensibility.OnSubscribed;
import ibt.ortc.extensibility.OnUnsubscribed;
import ibt.ortc.extensibility.OrtcClient;
import ibt.ortc.extensibility.OrtcFactory;


public class MainActivity extends ActionBarActivity {

    private static final boolean defaultNeedsAuthentication = false;

    private OrtcClient client;
    private int reconnectingTries = 0;
    private static final int RESULT_SETTINGS = 1;

    private String server;
    private String appKey;
    private String privateKey;
    private String authToken;
    private String connectionMetadata;
    private boolean isCluster = true;
    private String channel;
    private String message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

        try {
            Ortc ortc = new Ortc();

            OrtcFactory factory;

            factory = ortc.loadOrtcFactory("IbtRealtimeSJ");

            client = factory.createClient();

            //client.setApplicationContext(getApplicationContext());
            //client.setGoogleProjectId("your_google_project_id");

        } 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()));
            }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            startActivityForResult(new Intent(this,SettingsActivity.class),RESULT_SETTINGS);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_SETTINGS:
                updateUserSettings();
                break;

        }
    }

    private void init(){

        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(this);

        server = sharedPrefs.getString("server",getString(R.string.DefaultServer));
        isCluster = sharedPrefs.getBoolean("isCluster",true);
        authToken = sharedPrefs.getString("authToken",getString(R.string.DefaultAuthenticationToken));
        appKey = sharedPrefs.getString("appKey",getString(R.string.DefaultApplicationKey));
        privateKey = sharedPrefs.getString("privateKey",getString(R.string.DefaultPrivateKey));
        connectionMetadata = sharedPrefs.getString("server",getString(R.string.DefaultConnectionMetadata));
        channel = sharedPrefs.getString("channel",getString(R.string.DefaultChannel));
        message = sharedPrefs.getString("message",getString(R.string.DefaultText));
    }

    private void updateUserSettings() {

        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(this);

        server = sharedPrefs.getString("server",getString(R.string.DefaultServer));
        isCluster = sharedPrefs.getBoolean("isCluster",true);
        authToken = sharedPrefs.getString("authToken",getString(R.string.DefaultAuthenticationToken));
        appKey = sharedPrefs.getString("appKey",getString(R.string.DefaultApplicationKey));
        privateKey = sharedPrefs.getString("privateKey",getString(R.string.DefaultPrivateKey));
        connectionMetadata = sharedPrefs.getString("connMetada",getString(R.string.DefaultConnectionMetadata));
        channel = sharedPrefs.getString("channel",getString(R.string.DefaultChannel));
        message = sharedPrefs.getString("message",getString(R.string.DefaultText));

    }


    public void clearClickEventHandler(View v) {
        TextView textViewLog = (TextView) findViewById(R.id.TextViewLog);
        textViewLog.setText("");
        textViewLog.scrollTo(0, 0);
    }

    private void subscribe() {
        log("Subscribing...");

        client.subscribe(channel, true,
                new OnMessage() {
                    public void run(OrtcClient sender, String channel,
                                    String message) {
                        final String subscribedChannel = channel;
                        final String messageReceived = message;
                        runOnUiThread(new Runnable() {
                            public void run() {
                                log(String.format("Message on channel %s: %s", subscribedChannel, messageReceived));
                            }
                        });
                    }
                });
    }

    private void unsubscribe() {
        log("Unsubscribing...");
        client.unsubscribe(channel);
    }

    public void sendClickEventHandler(View v) {

        log(String.format("Sending %s to %s...", message, channel));

        client.send(channel, message);
    }

    private void connect() {


        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(server, isCluster, authToken, false, appKey, 14000, privateKey, permissions)) {
                    log("Unable to authenticate");
                }
                else {
                    log("Authentication successfull");
                }
            } catch (Exception e) {
                log(String.format("ORTC AUTHENTICATION ERROR: %s", e.toString()));
            }
        }

        if (isCluster) {
            client.setClusterUrl(server);
        }
        else {
            client.setUrl(server);
        }

        client.setConnectionMetadata(connectionMetadata);

        log("Connecting...");
        client.connect(appKey, authToken);
    }

    private void disconnect() {
        log("Disconnecting...");
        client.disconnect();
    }

    public void presenceClickEventHandler(View v) {

        Ortc.presence(
                server,
                isCluster,
                appKey,
                authToken, channel, 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());
                                }
                            }
                        });
                    }
                });
    }

    private void enablePresence() {

        Ortc.enablePresence(
                server,
                isCluster,
                appKey,
                privateKey,
                channel,
                true,
                new OnEnablePresence() {
                    @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);
                                }
                            }
                        });
                    }
                });
    }

    private void disablePresence() {

        Ortc.disablePresence(
                server,
                isCluster,
                appKey,
                privateKey,
                channel,
                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);
                                }
                            }
                        });
                    }
                });
    }


    private void log(String text) {
        TextView t = ((TextView) findViewById(R.id.TextViewLog));
        t.setText(String.format("%s - %s\n%s", DateFormat.format("HH:mm:ss", new java.util.Date()), text, t.getText()));
    }


    public void connect(View view) {
        connect();
    }

    public void disconnect(View view) {
        disconnect();
    }

    public void subscribe(View view) {
        subscribe();
    }

    public void unsubscribe(View view) {
        unsubscribe();
    }

    public void enablePresence(View view) {
        enablePresence();
    }

    public void disablePresence(View view) {
        disablePresence();
    }
}




Java Source Code List

ibt.ortc.api.ApplicationTest.java
ibt.ortc.api.Authentication.java
ibt.ortc.api.Balancer.java
ibt.ortc.api.ChannelPermissions.java
ibt.ortc.api.InvalidBalancerServerException.java
ibt.ortc.api.OnDisablePresence.java
ibt.ortc.api.OnEnablePresence.java
ibt.ortc.api.OnPresence.java
ibt.ortc.api.OnRestWebserviceResponse.java
ibt.ortc.api.OrtcAuthenticationNotAuthorizedException.java
ibt.ortc.api.Ortc.java
ibt.ortc.api.Pair.java
ibt.ortc.api.Presence.java
ibt.ortc.api.RestWebservice.java
ibt.ortc.api.SecureWebConnections.java
ibt.ortc.api.Strings.java
ibt.ortc.extensibility.ChannelSubscription.java
ibt.ortc.extensibility.CharEscaper.java
ibt.ortc.extensibility.ConnectionProtocol.java
ibt.ortc.extensibility.DispatchedMessages.java
ibt.ortc.extensibility.EventEnum.java
ibt.ortc.extensibility.GcmOrtcBroadcastReceiver.java
ibt.ortc.extensibility.GcmOrtcIntentService.java
ibt.ortc.extensibility.GcmRegistration.java
ibt.ortc.extensibility.HeartbeatSender.java
ibt.ortc.extensibility.OnConnected.java
ibt.ortc.extensibility.OnDisconnected.java
ibt.ortc.extensibility.OnException.java
ibt.ortc.extensibility.OnMessageWithPayload.java
ibt.ortc.extensibility.OnMessage.java
ibt.ortc.extensibility.OnReconnected.java
ibt.ortc.extensibility.OnReconnecting.java
ibt.ortc.extensibility.OnSubscribed.java
ibt.ortc.extensibility.OnUnsubscribed.java
ibt.ortc.extensibility.OrtcClient.java
ibt.ortc.extensibility.OrtcFactory.java
ibt.ortc.extensibility.exception.OrtcAlreadyConnectedException.java
ibt.ortc.extensibility.exception.OrtcDoesNotHavePermissionException.java
ibt.ortc.extensibility.exception.OrtcEmptyFieldException.java
ibt.ortc.extensibility.exception.OrtcGcmException.java
ibt.ortc.extensibility.exception.OrtcInvalidCharactersException.java
ibt.ortc.extensibility.exception.OrtcMaxLengthException.java
ibt.ortc.extensibility.exception.OrtcNotConnectedException.java
ibt.ortc.extensibility.exception.OrtcNotSubscribedException.java
ibt.ortc.extensibility.exception.OrtcSubscribedException.java
ibt.ortc.ortclib.ApplicationTest.java
ibt.ortc.ortclib.MainActivity.java
ibt.ortc.ortclib.SettingsActivity.java
ibt.ortc.plugins.IbtRealtimeSJ.IbtRealtimeSJClient.java
ibt.ortc.plugins.IbtRealtimeSJ.IbtRealtimeSJFactory.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcMessage.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcOperation.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcServerErrorException.java
ibt.ortc.plugins.websocket.WebSocketConnection.java
ibt.ortc.plugins.websocket.WebSocketEventHandler.java
ibt.ortc.plugins.websocket.WebSocketException.java
ibt.ortc.plugins.websocket.WebSocketHandshake.java
ibt.ortc.plugins.websocket.WebSocketMessage.java
ibt.ortc.plugins.websocket.WebSocketReceiver.java
ibt.ortc.plugins.websocket.WebSocketSender.java
ibt.ortc.plugins.websocket.WebSocket.java