1+ package com .pubnub .examples .blackberry ;
2+
3+ import java .util .Hashtable ;
4+
5+ import net .rim .device .api .command .Command ;
6+ import net .rim .device .api .command .CommandHandler ;
7+ import net .rim .device .api .command .ReadOnlyCommandMetadata ;
8+ import net .rim .device .api .ui .MenuItem ;
9+ import net .rim .device .api .ui .component .LabelField ;
10+ import net .rim .device .api .ui .container .MainScreen ;
11+ import net .rim .device .api .util .StringProvider ;
12+
13+ import com .pubnub .api .Callback ;
14+ import com .pubnub .api .Pubnub ;
15+ import com .pubnub .api .PubnubException ;
16+
17+ /**
18+ * A class extending the MainScreen class, which provides default standard
19+ * behavior for BlackBerry GUI applications.
20+ */
21+ public final class PubnubExampleScreen extends MainScreen
22+ {
23+ String channel = "hello_world" ;
24+ String [] channels = { "hello_world1" , "hello_world2" , "hello_world3" ,
25+ "hello_world4" };
26+ Pubnub _pubnub = new Pubnub ("demo" ,"demo" ,"demo" , false );
27+ /**
28+ * Creates a new PubnubExampleScreen object
29+ */
30+ public PubnubExampleScreen ()
31+ {
32+ // Set the displayed title of the screen
33+ setTitle ("PubnubExample" );
34+ add (new LabelField ("Please select an item from the menu" ));
35+ add (new LabelField ("Subscribe will listen on following channels: " ));
36+ add (new LabelField ("hello_world1, hello_world2, hello_world3, hello_world4" ));
37+ add (new LabelField ("Publish, history, detailedHistory, hereNow, Presence use hello_world" ));
38+
39+
40+ addMenuItem (new TimeMenuItem ());
41+ addMenuItem (new PublishMenuItem ());
42+ addMenuItem (new HereNowMenuItem ());
43+ addMenuItem (new HistoryMenuItem ());
44+ addMenuItem (new DetailedHistoryMenuItem ());
45+ addMenuItem (new SubscribeMenuItem ());
46+ addMenuItem (new UnsubscribeMenuItem ());
47+ addMenuItem (new PresenceMenuItem ());
48+ addMenuItem (new ToggleRoRMenuItem ());
49+ addMenuItem (new DisconnectAndResubMenuItem ());
50+ }
51+
52+
53+ private class TimeMenuItem extends MenuItem
54+ {
55+ public TimeMenuItem ()
56+ {
57+ super (new StringProvider ("Time" ), 0x230010 , 0 );
58+ this .setCommand (new Command (new CommandHandler ()
59+ {
60+ public void execute (ReadOnlyCommandMetadata metadata , Object context )
61+ { _pubnub .time (new Callback () {
62+ public void successCallback (String channel , Object message ) {
63+ PubnubExample .alertDialog (message .toString ());
64+ }
65+
66+ public void errorCallback (String channel , Object message ) {
67+ PubnubExample .alertDialog (message .toString ());
68+ }
69+ });
70+ }
71+ }));
72+ }
73+ }
74+ private class DisconnectAndResubMenuItem extends MenuItem
75+ {
76+ public DisconnectAndResubMenuItem ()
77+ {
78+ super (new StringProvider ("Disconnect and Resubscribe" ), 0x230010 , 0 );
79+ this .setCommand (new Command (new CommandHandler ()
80+ {
81+ public void execute (ReadOnlyCommandMetadata metadata , Object context )
82+ { _pubnub .disconnectAndResubscribe ();
83+ }
84+ }));
85+ }
86+ }
87+ private class ToggleRoRMenuItem extends MenuItem
88+ {
89+ public ToggleRoRMenuItem ()
90+ {
91+ super (new StringProvider ("Toggle Resume on Reconnect" ), 0x230010 , 0 );
92+ this .setCommand (new Command (new CommandHandler ()
93+ {
94+ public void execute (ReadOnlyCommandMetadata metadata , Object context )
95+ { _pubnub .setResumeOnReconnect (_pubnub .isResumeOnReconnect ()?false :true );
96+ }
97+ }));
98+ }
99+ }
100+ private class PublishMenuItem extends MenuItem
101+ {
102+ public PublishMenuItem ()
103+ {
104+ super (new StringProvider ("Publish" ), 0x230010 , 0 );
105+ this .setCommand (new Command (new CommandHandler ()
106+ {
107+ public void execute (ReadOnlyCommandMetadata metadata , Object context ) {
108+
109+ _pubnub .publish (channel , "Blackberry says hello world" , new Callback () {
110+ public void successCallback (String channel , Object message ) {
111+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
112+ }
113+
114+ public void errorCallback (String channel , Object message ) {
115+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
116+ }
117+ });
118+
119+ }}));
120+ }
121+ }
122+ private class HereNowMenuItem extends MenuItem
123+ {
124+ public HereNowMenuItem ()
125+ {
126+ super (new StringProvider ("HereNow" ), 0x230010 , 0 );
127+ this .setCommand (new Command (new CommandHandler ()
128+ {
129+ public void execute (ReadOnlyCommandMetadata metadata , Object context )
130+ {
131+ _pubnub .hereNow (channel , new Callback () {
132+ public void successCallback (String channel , Object message ) {
133+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
134+ }
135+
136+ public void errorCallback (String channel , Object message ) {
137+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
138+ }
139+ });
140+ }
141+ }));
142+ }
143+ }
144+ private class HistoryMenuItem extends MenuItem
145+ {
146+ public HistoryMenuItem ()
147+ {
148+ super (new StringProvider ("History" ), 0x230010 , 0 );
149+ this .setCommand (new Command (new CommandHandler ()
150+ {
151+ public void execute (ReadOnlyCommandMetadata metadata , Object context )
152+ {
153+ _pubnub .history (channel , 1 , new Callback () {
154+ public void successCallback (String channel , Object message ) {
155+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
156+ }
157+
158+ public void errorCallback (String channel , Object message ) {
159+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
160+ }
161+ });
162+ }
163+ }));
164+ }
165+ }
166+ private class DetailedHistoryMenuItem extends MenuItem
167+ {
168+ public DetailedHistoryMenuItem ()
169+ {
170+ super (new StringProvider ("DetailedHistory" ), 0x230010 , 0 );
171+ this .setCommand (new Command (new CommandHandler ()
172+ {
173+ public void execute (ReadOnlyCommandMetadata metadata , Object context )
174+ {
175+ _pubnub .detailedHistory (channel , 1 , new Callback () {
176+ public void successCallback (String channel , Object message ) {
177+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
178+ }
179+
180+ public void errorCallback (String channel , Object message ) {
181+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
182+ }
183+ });
184+ }
185+ }));
186+ }
187+ }
188+ private class SubscribeMenuItem extends MenuItem
189+ {
190+ public SubscribeMenuItem ()
191+ {
192+ super (new StringProvider ("Subscribe" ), 0x230010 , 0 );
193+ this .setCommand (new Command (new CommandHandler ()
194+ {
195+ public void execute (ReadOnlyCommandMetadata metadata , Object context )
196+ { Hashtable args = new Hashtable (6 );
197+ args .put ("channels" , channels );
198+
199+ try {
200+ _pubnub .subscribe (args , new Callback () {
201+ public void connectCallback (String channel ) {
202+ PubnubExample .alertDialog ("CONNECT on channel:" + channel );
203+ }
204+
205+ public void disconnectCallback (String channel ) {
206+ PubnubExample .alertDialog ("DISCONNECT on channel:" + channel );
207+ }
208+
209+ public void reconnectCallback (String channel ) {
210+ PubnubExample .alertDialog ("RECONNECT on channel:" + channel );
211+ }
212+
213+ public void successCallback (String channel , Object message ) {
214+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
215+ }
216+ });
217+
218+ } catch (Exception e ) {
219+
220+ }
221+ }
222+ }));
223+ }
224+ }
225+ private class PresenceMenuItem extends MenuItem
226+ {
227+ public PresenceMenuItem ()
228+ {
229+ super (new StringProvider ("Presence" ), 0x230010 , 0 );
230+ this .setCommand (new Command (new CommandHandler ()
231+ {
232+ public void execute (ReadOnlyCommandMetadata metadata , Object context )
233+ {
234+ try {
235+ _pubnub .presence (channel , new Callback () {
236+ public void successCallback (String channel , Object message ) {
237+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
238+ }
239+
240+ public void errorCallback (String channel , Object message ) {
241+ PubnubExample .alertDialog ("Channel : " + channel + ", " + message .toString ());
242+ }
243+ });
244+ } catch (PubnubException e ) {
245+
246+ }
247+ }
248+ }));
249+ }
250+ }
251+ private class UnsubscribeMenuItem extends MenuItem
252+ {
253+ public UnsubscribeMenuItem ()
254+ {
255+ super (new StringProvider ("Unsubscribe" ), 0x230010 , 0 );
256+ this .setCommand (new Command (new CommandHandler ()
257+ {
258+ public void execute (ReadOnlyCommandMetadata metadata , Object context )
259+ {
260+ _pubnub .unsubscribe (channels );
261+ }
262+ }));
263+ }
264+ }
265+ }
0 commit comments