1111import android .hardware .usb .UsbDevice ;
1212import android .hardware .usb .UsbManager ;
1313import android .os .Bundle ;
14+ import android .os .Handler ;
1415import android .util .Log ;
1516import android .view .GestureDetector ;
1617import android .view .MotionEvent ;
@@ -110,13 +111,15 @@ public void onClick(View v) {
110111 setupGestureDetectors ();
111112
112113 mUsbMaskConnection = new UsbMaskConnection ();
113- mVideoReader = new VideoReaderExoplayer (fpvView , overlayView , this );
114+ Handler videoReaderEventListener = new Handler (this .getMainLooper (), msg -> onVideoReaderEvent ((VideoReaderExoplayer .VideoReaderEventMessageCode ) msg .obj ));
115+
116+ mVideoReader = new VideoReaderExoplayer (fpvView , this , videoReaderEventListener );
114117
115118 if (!usbConnected ) {
116119 if (searchDevice ()) {
117120 connect ();
118121 } else {
119- overlayView . showOpaque (R .string .waiting_for_usb_device , OverlayStatus .Disconnected );
122+ showOverlay (R .string .waiting_for_usb_device , OverlayStatus .Disconnected );
120123 }
121124 }
122125 }
@@ -157,6 +160,11 @@ public boolean onTouchEvent(MotionEvent event) {
157160 }
158161
159162 private void updateWatermark () {
163+ if (overlayView .getVisibility () == View .VISIBLE ) {
164+ watermarkView .setAlpha (0 );
165+ return ;
166+ }
167+
160168 if (sharedPreferences .getBoolean (ShowWatermark , true )) {
161169 watermarkView .setAlpha (0.3F );
162170 } else {
@@ -172,9 +180,27 @@ private void updateVideoZoom() {
172180 }
173181 }
174182
183+ private void cancelButtonAnimation () {
184+ Handler handler = settingsButton .getHandler ();
185+ if (handler != null ) {
186+ settingsButton .getHandler ().removeCallbacksAndMessages (null );
187+ }
188+ }
189+
190+ private void showSettingsButton () {
191+ cancelButtonAnimation ();
192+
193+ if (overlayView .getVisibility () == View .VISIBLE ) {
194+ buttonAlpha = 1 ;
195+ settingsButton .setAlpha (1 );
196+ }
197+ }
198+
175199 private void toggleSettingsButton () {
200+ if (buttonAlpha == 1 && overlayView .getVisibility () == View .VISIBLE ) return ;
201+
176202 // cancel any pending delayed animations first
177- settingsButton . getHandler (). removeCallbacksAndMessages ( null );
203+ cancelButtonAnimation ( );
178204
179205 if (buttonAlpha == 1 ) {
180206 buttonAlpha = 0 ;
@@ -194,6 +220,7 @@ public void onAnimationEnd(Animator animation) {
194220 }
195221
196222 private void autoHideSettingsButton () {
223+ if (overlayView .getVisibility () == View .VISIBLE ) return ;
197224 if (buttonAlpha == 0 ) return ;
198225
199226 settingsButton .postDelayed (new Runnable () {
@@ -211,14 +238,14 @@ public void run() {
211238 public void usbDeviceApproved (UsbDevice device ) {
212239 Log .i (TAG , "USB - usbDevice approved" );
213240 usbDevice = device ;
214- overlayView . showOpaque (R .string .usb_device_approved , OverlayStatus .Connected );
241+ showOverlay (R .string .usb_device_approved , OverlayStatus .Connected );
215242 connect ();
216243 }
217244
218245 @ Override
219246 public void usbDeviceDetached () {
220247 Log .i (TAG , "USB - usbDevice detached" );
221- overlayView . showOpaque (R .string .usb_device_detached_waiting , OverlayStatus .Disconnected );
248+ showOverlay (R .string .usb_device_detached_waiting , OverlayStatus .Disconnected );
222249 this .onStop ();
223250 }
224251
@@ -233,7 +260,7 @@ private boolean searchDevice() {
233260 if (device .getVendorId () == VENDOR_ID && device .getProductId () == PRODUCT_ID ) {
234261 if (usbManager .hasPermission (device )) {
235262 Log .i (TAG , "USB - usbDevice attached" );
236- overlayView . showOpaque (R .string .usb_device_found , OverlayStatus .Connected );
263+ showOverlay (R .string .usb_device_found , OverlayStatus .Connected );
237264 usbDevice = device ;
238265 return true ;
239266 }
@@ -251,6 +278,9 @@ private void connect() {
251278 mVideoReader .setUsbMaskConnection (mUsbMaskConnection );
252279 overlayView .hide ();
253280 mVideoReader .start ();
281+ updateWatermark ();
282+ autoHideSettingsButton ();
283+ showOverlay (R .string .waiting_for_video , OverlayStatus .Connected );
254284 }
255285
256286 @ Override
@@ -275,7 +305,7 @@ public void onResume() {
275305 Log .d (TAG , "APP - On Resume usbDevice device found" );
276306 connect ();
277307 } else {
278- overlayView . showOpaque (R .string .waiting_for_usb_device , OverlayStatus .Disconnected );
308+ showOverlay (R .string .waiting_for_usb_device , OverlayStatus .Connected );
279309 }
280310 }
281311
@@ -285,6 +315,30 @@ public void onResume() {
285315 updateVideoZoom ();
286316 }
287317
318+ private boolean onVideoReaderEvent (VideoReaderExoplayer .VideoReaderEventMessageCode m ) {
319+ if (VideoReaderExoplayer .VideoReaderEventMessageCode .WAITING_FOR_VIDEO .equals (m )) {
320+ Log .d (TAG , "event: WAITING_FOR_VIDEO" );
321+ showOverlay (R .string .waiting_for_video , OverlayStatus .Connected );
322+ } else if (VideoReaderExoplayer .VideoReaderEventMessageCode .VIDEO_PLAYING .equals (m )) {
323+ Log .d (TAG , "event: VIDEO_PLAYING" );
324+ hideOverlay ();
325+ }
326+ return false ; // false to continue listening
327+ }
328+
329+ private void showOverlay (int textId , OverlayStatus connected ) {
330+ overlayView .show (textId , connected );
331+ updateWatermark ();
332+ showSettingsButton ();
333+ }
334+
335+ private void hideOverlay () {
336+ overlayView .hide ();
337+ updateWatermark ();
338+ showSettingsButton ();
339+ autoHideSettingsButton ();
340+ }
341+
288342 @ Override
289343 protected void onStop () {
290344 super .onStop ();
@@ -319,7 +373,7 @@ protected void onDestroy() {
319373 protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
320374 super .onActivityResult (requestCode , resultCode , data );
321375
322- SharedPreferences preferences = getApplicationContext (). getSharedPreferences ( "com.fpvout.digiview" , Context . MODE_PRIVATE );
376+ SharedPreferences preferences = PreferenceManager . getDefaultSharedPreferences ( getApplicationContext ());
323377 boolean dataCollectionAccepted = preferences .getBoolean ("dataCollectionAccepted" , false );
324378
325379 if (requestCode == 1 ) { // Data Collection agreement Activity
@@ -336,7 +390,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
336390 } //onActivityResult
337391
338392 private void checkDataCollectionAgreement () {
339- SharedPreferences preferences = getApplicationContext (). getSharedPreferences ( "com.fpvout.digiview" , Context . MODE_PRIVATE );
393+ SharedPreferences preferences = PreferenceManager . getDefaultSharedPreferences ( getApplicationContext ());
340394 boolean dataCollectionAccepted = preferences .getBoolean ("dataCollectionAccepted" , false );
341395 boolean dataCollectionReplied = preferences .getBoolean ("dataCollectionReplied" , false );
342396 if (!dataCollectionReplied ) {
0 commit comments