Java tutorial
/** * This file was auto-generated by the Titanium Module SDK helper for Android * Appcelerator Titanium Mobile * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved. * Licensed under the terms of the Apache Public License * Please see the LICENSE included with this distribution for details. * */ package co.realtime.ortc; import ibt.ortc.api.Ortc; 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; import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.appcelerator.kroll.KrollDict; import org.appcelerator.kroll.KrollModule; import org.appcelerator.kroll.annotations.Kroll; import org.appcelerator.kroll.common.Log; import org.appcelerator.titanium.TiApplication; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.content.SharedPreferences; import android.os.IBinder; //import android.app.Activity; //import org.appcelerator.kroll.common.TiConfig; import android.content.Context; //import java.util.*; @SuppressWarnings("deprecation") @Kroll.module(name = "Ortc", id = "co.realtime.ortc") public class OrtcModule extends KrollModule { public static int USE_CACHE_VERSION = 21; public static boolean USE_CACHE = true; // Standard Debugging variables private static final String LCAT = "OrtcModule"; //private static final boolean DBG = TiConfig.LOGD; // You can define constants with @Kroll.constant, for example: // @Kroll.constant public static final String EXTERNAL_NAME = value; static OrtcClient client; private static String aKey; private static String aToken; @Kroll.method public void setNotificationTitle(String title) { TiApplication appContext = TiApplication.getInstance(); SharedPreferences settings = appContext.getSharedPreferences("ortcInfo", 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("pushTitle", title); editor.commit(); } @Kroll.method public void setGoogleProjectId(String id) { client.setGoogleProjectId(id); } public void sendPush(String channel, String message) { Log.i("PUSH", String.format("Push notification on channel %s: %s", channel, message)); KrollDict kd = new KrollDict(); kd.put("channel", channel); kd.put("message", message); fireEvent("onNotification", kd); } public OrtcModule() { super(); try { Ortc ortc = new Ortc(); OrtcFactory factory; factory = ortc.loadOrtcFactory("IbtRealtimeSJ"); client = factory.createClient(); Ortc.setOnPushNotification(new OnMessage() { public void run(OrtcClient sender, final String channel, final String message, String payload) { Log.i("PUSH", String.format("Push notification on channel %s: %s", channel, message)); KrollDict kd = new KrollDict(); kd.put("channel", channel); kd.put("message", message); kd.put("payload", payload); fireEvent("onNotification", kd); } }); } catch (Exception e) { fireException(e.getMessage()); } if (client != null) { try { client.onConnected = new OnConnected() { public void run(OrtcClient arg0) { KrollDict kd = new KrollDict(); fireEvent("onConnected", kd); } }; client.onDisconnected = new OnDisconnected() { public void run(OrtcClient arg0) { KrollDict kd = new KrollDict(); fireEvent("onDisconnected", kd); } }; client.onSubscribed = new OnSubscribed() { public void run(OrtcClient arg0, String arg1) { KrollDict kd = new KrollDict(); kd.put("channel", arg1); fireEvent("onSubscribed", kd); } }; client.onUnsubscribed = new OnUnsubscribed() { public void run(OrtcClient arg0, String arg1) { KrollDict kd = new KrollDict(); kd.put("channel", arg1); fireEvent("onUnsubscribed", kd); } }; client.onException = new OnException() { public void run(OrtcClient arg0, Exception arg1) { fireException(arg1.getMessage()); } }; client.onReconnected = new OnReconnected() { public void run(OrtcClient arg0) { KrollDict kd = new KrollDict(); fireEvent("onReconnected", kd); } }; client.onReconnecting = new OnReconnecting() { public void run(OrtcClient arg0) { KrollDict kd = new KrollDict(); fireEvent("onReconnecting", kd); } }; } catch (Exception e) { fireException(e.getMessage()); } } } @Kroll.onAppCreate public static void onAppCreate(TiApplication app) { Log.d(LCAT, "inside onAppCreate"); // put module init code that needs to run when the application is created } //Properties @Kroll.getProperty public String getClusterUrl() { return client.getClusterUrl(); } @Kroll.setProperty public void setClusterUrl(String clusterUrl) { client.setClusterUrl(clusterUrl); } @Kroll.getProperty public String getUrl() { return client.getUrl(); } @Kroll.setProperty public void setUrl(String url) { client.setUrl(url); } @Kroll.getProperty public String getConnectionMetadata() { return client.getConnectionMetadata(); } @Kroll.setProperty public void setConnectionMetadata(String connectionMetadata) { client.setConnectionMetadata(connectionMetadata); } @Kroll.getProperty public String getAnnouncementSubchannel() { return client.getAnnouncementSubChannel(); } @Kroll.setProperty public void setAnnouncementSubchannel(String announcementSubChannel) { client.setAnnouncementSubChannel(announcementSubChannel); } /* @Kroll.setProperty public void setGoogleProjectId(String googleProjectId){ client.setGoogleProjectId(googleProjectId); Activity act = TiApplication.getAppRootOrCurrentActivity(); client.setApplicationContext(act.getApplicationContext()); }*/ //Methods /* @Kroll.method public void connect(String appKey) { aKey = appKey; aToken = "PM.Anonymous"; client.connect(appKey, aToken); }*/ BackgroundService backgroundService; boolean isBound = false; private ServiceConnection serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { BackgroundService.ServiceBinder binder = (BackgroundService.ServiceBinder) service; backgroundService = binder.getService(); isBound = true; Log.i("ServiceConnected", "onServiceConnected"); } public void onServiceDisconnected(ComponentName name) { backgroundService = null; isBound = false; Log.i("ServiceConnected", "onServiceDisconnected"); } }; @Kroll.method public void connect(String appKey, @Kroll.argument(optional = true) String authToken) { TiApplication appContext = TiApplication.getInstance(); client.setApplicationContext(appContext.getApplicationContext()); aKey = appKey; if (authToken == "" || authToken == null) authToken = "PM.Anonymous"; aToken = authToken; Intent intent = new Intent(appContext, BackgroundService.class); appContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); appContext.startService(intent); client.connect(appKey, aToken); } @Kroll.method public void disconnect() { client.disconnect(); } @Kroll.method public void subscribe(String channel, boolean subscribeOnReconnect) { client.subscribe(channel, subscribeOnReconnect, new OnMessage() { public void run(OrtcClient sender, String channel, String message, String payload) { KrollDict kd = new KrollDict(); kd.put("channel", channel); kd.put("message", message); kd.put("payload", payload); fireEvent("onMessage", kd); } }); } @Kroll.method public void subscribeWithNotifications(String channel, boolean subscribeOnReconnect) { client.subscribeWithNotifications(channel, subscribeOnReconnect, new OnMessage() { public void run(OrtcClient sender, String channel, String message, String payload) { KrollDict kd = new KrollDict(); kd.put("channel", channel); kd.put("message", message); kd.put("payload", payload); fireEvent("onMessage", kd); } }); } @Kroll.method public void unsubscribe(String channel) { client.unsubscribe(channel); } @Kroll.method public void send(String channel, String message) { client.send(channel, message); } @Kroll.method public boolean isSubscribed(String channel) { return client.isSubscribed(channel); } @Kroll.method public boolean isConnected() { return client.getIsConnected(); } @Kroll.method public void presence(String channel) { getPresence(channel); } private void getPresence(final String channel) { Runnable task = new Runnable() { private HttpClient hc; public void run() { if (!client.getIsConnected()) { fireException("Not connected"); } else { KrollDict kd = new KrollDict(); kd.put("channel", channel); String result = ""; String error = ""; String sUrl = client.getUrl(); sUrl += "/presence/" + aKey + "/" + aToken + "/" + channel; try { hc = new DefaultHttpClient(); hc.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); HttpGet hg = new HttpGet(sUrl); ResponseHandler<String> rh = new BasicResponseHandler(); result = hc.execute(hg, rh); } catch (Exception e) { error = e.getMessage(); } kd.put("result", result); kd.put("error", error); fireEvent("onPresence", kd); } } }; new Thread(task).start(); } private void fireException(String e) { KrollDict kd = new KrollDict(); kd.put("info", e); fireEvent("onException", kd); } }