11package pubsub .io .android .example ;
22
3+ import org .json .JSONException ;
4+ import org .json .JSONObject ;
5+
36import pubsub .io .android .Pubsub ;
47import android .app .Activity ;
58import android .content .ComponentName ;
69import android .content .Context ;
710import android .content .Intent ;
811import android .content .ServiceConnection ;
12+ import android .hardware .Sensor ;
13+ import android .hardware .SensorEvent ;
14+ import android .hardware .SensorEventListener ;
15+ import android .hardware .SensorManager ;
916import android .os .Bundle ;
1017import android .os .Handler ;
1118import android .os .IBinder ;
1219import android .os .Message ;
20+ import android .util .Log ;
21+ import android .view .View ;
22+ import android .view .View .OnClickListener ;
23+ import android .widget .Button ;
1324
1425/**
1526 * Example pubsub.io - Android client.
1627 *
17- * @author Andreas Göransson
28+ * @author Andreas G�ransson
1829 *
1930 */
20- public class Pubsub_exampleActivity extends Activity {
31+ public class Pubsub_exampleActivity extends Activity implements
32+ SensorEventListener {
33+
34+ protected static final String TAG = "Pubsub_exampleActivity" ;
35+
36+ protected static final int MY_HANDLER = 1241 ;
2137
2238 // Pubsub object
2339 private Pubsub mPubsub ;
2440
41+ private SensorManager mSensorManager ;
42+ private Sensor mAccelerometer ;
43+
2544 /** Called when the activity is first created. */
2645 @ Override
2746 public void onCreate (Bundle savedInstanceState ) {
2847 super .onCreate (savedInstanceState );
2948 setContentView (R .layout .main );
49+
50+ mSensorManager = (SensorManager ) getSystemService (SENSOR_SERVICE );
51+ mAccelerometer = mSensorManager
52+ .getDefaultSensor (Sensor .TYPE_ACCELEROMETER );
53+
54+ Button publish = (Button ) findViewById (R .id .button1 );
55+ publish .setOnClickListener (new OnClickListener () {
56+
57+ @ Override
58+ public void onClick (View v ) {
59+
60+ JSONObject doc = new JSONObject ();
61+ try {
62+ doc .put ("where" , "android" );
63+ } catch (JSONException e ) {
64+ // TODO Auto-generated catch block
65+ e .printStackTrace ();
66+ }
67+ mPubsub .publish (doc );
68+ }
69+ });
3070 }
3171
3272 @ Override
3373 protected void onPause () {
3474 // Disconnect from the service
3575 unbindService (serviceConnection );
3676
77+ mSensorManager .unregisterListener (this );
78+
3779 super .onPause ();
3880 }
3981
@@ -44,19 +86,32 @@ protected void onResume() {
4486 startService (intent );
4587 bindService (intent , serviceConnection , Context .BIND_AUTO_CREATE );
4688
89+ mSensorManager .registerListener (this , mAccelerometer ,
90+ SensorManager .SENSOR_DELAY_NORMAL );
91+
4792 super .onResume ();
4893 }
4994
5095 private ServiceConnection serviceConnection = new ServiceConnection () {
96+
5197 @ Override
5298 public void onServiceConnected (ComponentName className , IBinder service ) {
5399 mPubsub = ((Pubsub .LocalBinder ) service ).getService ();
54100 mPubsub .setHandler (mHandler );
55- mPubsub .connect ();
101+ mPubsub .connect ("10.3.3.11" , "10547" );
102+
103+ // In other clients the "connect" and the "sub" method are done at
104+ // the same time... this should probably be changed!
105+ mPubsub .sub ("android" );
106+
107+ // Subscribe to everything?
108+ JSONObject json_filter = new JSONObject ();
109+ mPubsub .subscribe (json_filter , MY_HANDLER );
56110 }
57111
58112 @ Override
59113 public void onServiceDisconnected (ComponentName className ) {
114+ mPubsub .disconnect ();
60115 mPubsub = null ;
61116 mPubsub .setHandler (null );
62117 }
@@ -78,7 +133,34 @@ public void handleMessage(Message msg) {
78133 case Pubsub .ERROR :
79134 // TODO get error message...
80135 break ;
136+ case MY_HANDLER :
137+ Log .i (TAG , "MY_HANDLER message: " + (String ) msg .obj );
138+ break ;
81139 }
82140 }
83141 };
142+
143+ @ Override
144+ public void onAccuracyChanged (Sensor arg0 , int arg1 ) {
145+ }
146+
147+ @ Override
148+ public void onSensorChanged (SensorEvent event ) {
149+ // We're just making sure that
150+ if (mPubsub != null && mPubsub .isSubscribed ()) {
151+ float [] vals = event .values ;
152+
153+ JSONObject doc = new JSONObject ();
154+ try {
155+ doc .put ("x" , vals [0 ]);
156+ doc .put ("y" , vals [1 ]);
157+ doc .put ("z" , vals [2 ]);
158+ } catch (JSONException e ) {
159+ // TODO Auto-generated catch block
160+ e .printStackTrace ();
161+ }
162+
163+ // mPubsub.publish(doc);
164+ }
165+ }
84166}
0 commit comments