1616
1717import android .content .pm .PackageInfo ;
1818import android .content .pm .PackageManager ;
19+ import android .graphics .PixelFormat ;
20+ import android .graphics .SurfaceTexture ;
1921import android .util .Log ;
2022import java .net .MalformedURLException ;
2123import java .net .URL ;
7173import android .widget .TextView ;
7274import org .vast .util .DateTimeFormat ;
7375
76+ import javax .microedition .khronos .egl .EGL10 ;
7477
75- public class MainActivity extends Activity implements SurfaceHolder .Callback , IEventListener
78+
79+ public class MainActivity extends Activity implements TextureView .SurfaceTextureListener , IEventListener
7680{
7781 TextView textArea ;
7882 SensorHubService boundService ;
7983 IModuleConfigRepository sensorhubConfig ;
8084 Handler displayHandler ;
8185 Runnable displayCallback ;
8286 StringBuffer displayText = new StringBuffer ();
83- SurfaceHolder camPreviewSurfaceHolder ;
8487 boolean oshStarted = false ;
8588 ArrayList <SOSTClient > sostClients = new ArrayList <SOSTClient >();
8689 URL sosUrl = null ;
@@ -144,7 +147,7 @@ protected void updateConfig(SharedPreferences prefs, String runName)
144147 showVideo = true ;
145148 sensorsConfig .videoCodec = prefs .getString ("video_codec" , AndroidSensorsConfig .JPEG_CODEC );
146149 sensorsConfig .androidContext = this .getApplicationContext ();
147- sensorsConfig .camPreviewSurfaceHolder = this . camPreviewSurfaceHolder ;
150+ sensorsConfig .camPreviewTexture = boundService . getVideoTexture () ;
148151 sensorsConfig .runName = runName ;
149152 sensorhubConfig .add (sensorsConfig );
150153 addSosTConfig (sensorsConfig , sosUser , sosPwd );
@@ -201,7 +204,7 @@ protected void updateConfig(SharedPreferences prefs, String runName)
201204 flironeConfig .name = "FLIR One Camera [" + deviceName + "]" ;
202205 flironeConfig .autoStart = true ;
203206 flironeConfig .androidContext = this .getApplicationContext ();
204- flironeConfig .camPreviewSurfaceHolder = this . camPreviewSurfaceHolder ;
207+ flironeConfig .camPreviewTexture = boundService . getVideoTexture () ;
205208 sensorhubConfig .add (flironeConfig );
206209 addSosTConfig (flironeConfig , sosUser , sosPwd );
207210 }
@@ -238,8 +241,6 @@ protected void onCreate(Bundle savedInstanceState)
238241 super .onCreate (savedInstanceState );
239242 setContentView (R .layout .activity_main );
240243 textArea = (TextView ) findViewById (R .id .text );
241- SurfaceView camPreview = (SurfaceView ) findViewById (R .id .textureView1 );
242- camPreview .getHolder ().addCallback (this );
243244
244245 // bind to SensorHub service
245246 Intent intent = new Intent (this , SensorHubService .class );
@@ -318,12 +319,17 @@ public void onClick(DialogInterface dialog, int whichButton)
318319 {
319320 getWindow ().addFlags (WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON );
320321 String runName = input .getText ().toString ();
321- updateConfig (PreferenceManager .getDefaultSharedPreferences (MainActivity .this ), runName );
322322 newStatusMessage ("Starting SensorHub..." );
323+
324+ updateConfig (PreferenceManager .getDefaultSharedPreferences (MainActivity .this ), runName );
323325 sostClients .clear ();
324326 boundService .startSensorHub (sensorhubConfig , MainActivity .this );
327+
325328 if (showVideo )
329+ {
330+ showVideo ();
326331 textArea .setBackgroundColor (0x80FFFFFF );
332+ }
327333 }
328334 });
329335
@@ -350,7 +356,7 @@ protected void showAboutPopup()
350356 }
351357
352358 String message = "A software platform for building smart sensor networks and the Internet of Things\n \n " ;
353- message += "Version: " + version ;
359+ message += "Version: " + version + " \n " ;
354360
355361 AlertDialog .Builder alert = new AlertDialog .Builder (this );
356362 alert .setTitle ("OpenSensorHub" );
@@ -399,7 +405,6 @@ public void run()
399405 {
400406 displayStatus ();
401407 textArea .setText (Html .fromHtml (displayText .toString ()));
402- //textArea.setText(new DateTimeFormat().formatIso(System.currentTimeMillis()/1000., 0));
403408 displayHandler .postDelayed (this , 1000 );
404409 }
405410 };
@@ -524,6 +529,25 @@ protected void stopListeningForEvents()
524529 }
525530
526531
532+ protected void showVideo ()
533+ {
534+ if (boundService .getVideoTexture () != null )
535+ {
536+ TextureView textureView = (TextureView ) findViewById (R .id .video );
537+ if (textureView .getSurfaceTexture () != boundService .getVideoTexture ())
538+ {
539+ textureView .setSurfaceTexture (boundService .getVideoTexture ());
540+ textureView .setSurfaceTextureListener (this );
541+ }
542+ }
543+ }
544+
545+
546+ protected void hideVideo ()
547+ {
548+ }
549+
550+
527551 @ Override
528552 protected void onStart ()
529553 {
@@ -540,6 +564,7 @@ protected void onResume()
540564 {
541565 startListeningForEvents ();
542566 startRefreshingStatus ();
567+ showVideo ();
543568 }
544569 }
545570
@@ -549,6 +574,7 @@ protected void onPause()
549574 {
550575 stopListeningForEvents ();
551576 stopRefreshingStatus ();
577+ hideVideo ();
552578 super .onPause ();
553579 }
554580
@@ -571,20 +597,25 @@ protected void onDestroy()
571597
572598
573599 @ Override
574- public void surfaceCreated ( SurfaceHolder holder )
600+ public void onSurfaceTextureAvailable ( SurfaceTexture surfaceTexture , int i , int i1 )
575601 {
576- this .camPreviewSurfaceHolder = holder ;
577602 }
578603
579604
580605 @ Override
581- public void surfaceChanged ( SurfaceHolder holder , int format , int width , int height )
606+ public void onSurfaceTextureSizeChanged ( SurfaceTexture surfaceTexture , int i , int i1 )
582607 {
583608 }
584609
585610
586611 @ Override
587- public void surfaceDestroyed (SurfaceHolder holder )
612+ public boolean onSurfaceTextureDestroyed (SurfaceTexture surfaceTexture )
613+ {
614+ return false ;
615+ }
616+
617+ @ Override
618+ public void onSurfaceTextureUpdated (SurfaceTexture surfaceTexture )
588619 {
589- }
620+ }
590621}
0 commit comments