Skip to content

Commit 799bd59

Browse files
committed
Better overlay toggle
1 parent 69dc92b commit 799bd59

File tree

3 files changed

+99
-113
lines changed

3 files changed

+99
-113
lines changed

app/src/main/java/com/fpvout/digiview/MainActivity.java

Lines changed: 90 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import android.view.WindowManager;
2626

2727
import androidx.annotation.NonNull;
28+
import androidx.annotation.Nullable;
2829
import androidx.appcompat.app.ActionBar;
2930
import androidx.appcompat.app.AppCompatActivity;
3031
import androidx.core.app.ActivityCompat;
@@ -41,8 +42,6 @@
4142
import io.sentry.SentryLevel;
4243
import io.sentry.android.core.SentryAndroid;
4344

44-
import static com.fpvout.digiview.VideoReaderExoplayer.VideoZoomedIn;
45-
4645
public class MainActivity extends AppCompatActivity implements UsbDeviceListener, ConnectCheckerRtmp, ActivityCompat.OnRequestPermissionsResultCallback {
4746
private static final String TAG = "DIGIVIEW";
4847
private static final String ACTION_USB_PERMISSION = "com.fpvout.digiview.USB_PERMISSION";
@@ -69,6 +68,18 @@ public class MainActivity extends AppCompatActivity implements UsbDeviceListener
6968
private ScaleGestureDetector scaleGestureDetector;
7069
private SharedPreferences sharedPreferences;
7170
private static final String ShowWatermark = "ShowWatermark";
71+
private Runnable hideSettingsButtonRunnable = new Runnable() {
72+
@Override
73+
public void run() {
74+
toggleView(settingsButton, false);
75+
}
76+
};
77+
private Runnable hideLiveButtonRunnable = new Runnable() {
78+
@Override
79+
public void run() {
80+
toggleView(liveButton, false);
81+
}
82+
};
7283

7384
@Override
7485
protected void onCreate(Bundle savedInstanceState) {
@@ -151,11 +162,84 @@ protected void onCreate(Bundle savedInstanceState) {
151162
}
152163
}
153164

165+
private void toggleFullOverlay() {
166+
if (overlayView.getAlpha() > 0.0f) return;
167+
168+
if (sharedPreferences.getBoolean(ShowWatermark, true)) {
169+
toggleView(watermarkView, 0.3f);
170+
}
171+
172+
settingsButton.removeCallbacks(hideSettingsButtonRunnable);
173+
toggleView(settingsButton, new AnimatorListenerAdapter() {
174+
@Override
175+
public void onAnimationEnd(Animator animation) {
176+
settingsButton.postDelayed(hideSettingsButtonRunnable, 3000);
177+
}
178+
});
179+
liveButton.removeCallbacks(hideLiveButtonRunnable);
180+
toggleView(liveButton, new AnimatorListenerAdapter() {
181+
@Override
182+
public void onAnimationEnd(Animator animation) {
183+
liveButton.postDelayed(hideLiveButtonRunnable, 3000);
184+
}
185+
});
186+
}
187+
188+
private void hideFullOverlay() {
189+
toggleView(watermarkView, sharedPreferences.getBoolean(ShowWatermark, true), 0.3f);
190+
191+
toggleView(settingsButton, false);
192+
toggleView(liveButton, false);
193+
toggleView(overlayView, false);
194+
}
195+
196+
private void showFullOverlay() {
197+
toggleView(watermarkView, false);
198+
199+
settingsButton.removeCallbacks(hideSettingsButtonRunnable);
200+
toggleView(settingsButton, true);
201+
202+
liveButton.removeCallbacks(hideLiveButtonRunnable);
203+
toggleView(liveButton, true);
204+
}
205+
206+
private void toggleView(View view, @Nullable AnimatorListenerAdapter animatorListener) {
207+
toggleView(view, view.getAlpha() == 0.0f, 1.0f, animatorListener);
208+
}
209+
210+
private void toggleView(View view, float visibleAlpha) {
211+
toggleView(view, view.getAlpha() == 0.0f, visibleAlpha);
212+
}
213+
214+
private void toggleView(View view, boolean visible) {
215+
toggleView(view, visible, 1.0f, null);
216+
}
217+
218+
private void toggleView(View view, boolean visible, float visibleAlpha) {
219+
toggleView(view, visible, visibleAlpha, null);
220+
}
221+
222+
private void toggleView(View view, boolean visible, float visibleAlpha, @Nullable AnimatorListenerAdapter animatorListener) {
223+
if (!visible) {
224+
view.animate().cancel();
225+
view.animate()
226+
.alpha(0)
227+
.setDuration(shortAnimationDuration)
228+
.setListener(null);
229+
} else {
230+
view.animate().cancel();
231+
view.animate()
232+
.alpha(visibleAlpha)
233+
.setDuration(shortAnimationDuration)
234+
.setListener(animatorListener);
235+
}
236+
}
237+
154238
private void setupGestureDetectors() {
155239
gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
156240
@Override
157241
public boolean onSingleTapConfirmed(MotionEvent e) {
158-
toggleSettingsButton();
242+
toggleFullOverlay();
159243
return super.onSingleTapConfirmed(e);
160244
}
161245

@@ -186,81 +270,6 @@ public boolean onTouchEvent(MotionEvent event) {
186270
return super.onTouchEvent(event);
187271
}
188272

189-
private void updateWatermark() {
190-
if (overlayView.getVisibility() == View.VISIBLE) {
191-
watermarkView.setAlpha(0);
192-
return;
193-
}
194-
195-
if (sharedPreferences.getBoolean(ShowWatermark, true)) {
196-
watermarkView.setAlpha(0.3F);
197-
} else {
198-
watermarkView.setAlpha(0F);
199-
}
200-
}
201-
202-
private void updateVideoZoom() {
203-
if (sharedPreferences.getBoolean(VideoZoomedIn, true)) {
204-
mVideoReader.zoomIn();
205-
} else {
206-
mVideoReader.zoomOut();
207-
}
208-
}
209-
210-
private void cancelButtonAnimation() {
211-
Handler handler = settingsButton.getHandler();
212-
if (handler != null) {
213-
settingsButton.getHandler().removeCallbacksAndMessages(null);
214-
}
215-
}
216-
217-
private void showSettingsButton() {
218-
cancelButtonAnimation();
219-
220-
if (overlayView.getVisibility() == View.VISIBLE) {
221-
buttonAlpha = 1;
222-
settingsButton.setAlpha(1);
223-
}
224-
}
225-
226-
private void toggleSettingsButton() {
227-
if (buttonAlpha == 1 && overlayView.getVisibility() == View.VISIBLE) return;
228-
229-
// cancel any pending delayed animations first
230-
cancelButtonAnimation();
231-
232-
if (buttonAlpha == 1) {
233-
buttonAlpha = 0;
234-
} else {
235-
buttonAlpha = 1;
236-
}
237-
238-
settingsButton.animate()
239-
.alpha(buttonAlpha)
240-
.setDuration(shortAnimationDuration)
241-
.setListener(new AnimatorListenerAdapter() {
242-
@Override
243-
public void onAnimationEnd(Animator animation) {
244-
autoHideSettingsButton();
245-
}
246-
});
247-
}
248-
249-
private void autoHideSettingsButton() {
250-
if (overlayView.getVisibility() == View.VISIBLE) return;
251-
if (buttonAlpha == 0) return;
252-
253-
settingsButton.postDelayed(new Runnable() {
254-
@Override
255-
public void run() {
256-
buttonAlpha = 0;
257-
settingsButton.animate()
258-
.alpha(0)
259-
.setDuration(shortAnimationDuration);
260-
}
261-
}, 3000);
262-
}
263-
264273
@Override
265274
public void usbDeviceApproved(UsbDevice device) {
266275
Log.i(TAG, "USB - usbDevice approved");
@@ -303,10 +312,7 @@ private void connect() {
303312
usbConnected = true;
304313
mUsbMaskConnection.setUsbDevice(usbManager.openDevice(usbDevice), usbDevice);
305314
mVideoReader.setUsbMaskConnection(mUsbMaskConnection);
306-
overlayView.hide();
307315
mVideoReader.start();
308-
updateWatermark();
309-
autoHideSettingsButton();
310316
showOverlay(R.string.waiting_for_video, OverlayStatus.Connected);
311317
}
312318

@@ -335,11 +341,6 @@ public void onResume() {
335341
showOverlay(R.string.waiting_for_usb_device, OverlayStatus.Connected);
336342
}
337343
}
338-
339-
settingsButton.setAlpha(1);
340-
autoHideSettingsButton();
341-
updateWatermark();
342-
updateVideoZoom();
343344
}
344345

345346
private boolean onVideoReaderEvent(VideoReaderExoplayer.VideoReaderEventMessageCode m) {
@@ -348,22 +349,15 @@ private boolean onVideoReaderEvent(VideoReaderExoplayer.VideoReaderEventMessageC
348349
showOverlay(R.string.waiting_for_video, OverlayStatus.Connected);
349350
} else if (VideoReaderExoplayer.VideoReaderEventMessageCode.VIDEO_PLAYING.equals(m)) {
350351
Log.d(TAG, "event: VIDEO_PLAYING");
351-
hideOverlay();
352+
hideFullOverlay();
352353
}
353354
return false; // false to continue listening
354355
}
355356

356357
private void showOverlay(int textId, OverlayStatus connected) {
358+
toggleView(overlayView, true);
359+
showFullOverlay();
357360
overlayView.show(textId, connected);
358-
updateWatermark();
359-
showSettingsButton();
360-
}
361-
362-
private void hideOverlay() {
363-
overlayView.hide();
364-
updateWatermark();
365-
showSettingsButton();
366-
autoHideSettingsButton();
367361
}
368362

369363
@Override

app/src/main/java/com/fpvout/digiview/OverlayView.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.content.Context;
44
import android.util.AttributeSet;
5-
import android.view.View;
65
import android.widget.ImageView;
76
import android.widget.TextView;
87

@@ -22,18 +21,11 @@ public OverlayView(Context context, AttributeSet attrs) {
2221
imageView = findViewById(R.id.backdrop_image);
2322
}
2423

25-
public void hide(){
26-
setVisibility(View.GONE);
27-
}
28-
2924
public void show(int textResourceId, OverlayStatus status){
3025
showInfo(getContext().getString(textResourceId), status);
3126
}
3227

3328
private void showInfo(String text, OverlayStatus status){
34-
35-
setVisibility(View.VISIBLE);
36-
3729
textView.setText(text);
3830

3931
int image = R.drawable.ic_goggles_white;

app/src/main/res/layout/activity_main.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
app:layout_constraintTop_toTopOf="parent">
2020
</SurfaceView>
2121

22+
<com.fpvout.digiview.OverlayView
23+
android:id="@+id/overlayView"
24+
android:layout_width="match_parent"
25+
android:layout_height="match_parent"
26+
app:layout_constraintBottom_toTopOf="parent"
27+
app:layout_constraintEnd_toEndOf="parent"
28+
app:layout_constraintStart_toStartOf="parent"
29+
app:layout_constraintTop_toTopOf="parent" />
30+
2231
<TextView
2332
android:id="@+id/watermarkView"
2433
android:layout_width="wrap_content"
@@ -33,15 +42,6 @@
3342
app:layout_constraintStart_toStartOf="@id/fpvView"
3443
style="@style/text_logo" />
3544

36-
<com.fpvout.digiview.OverlayView
37-
android:id="@+id/overlayView"
38-
android:layout_width="match_parent"
39-
android:layout_height="match_parent"
40-
app:layout_constraintBottom_toTopOf="parent"
41-
app:layout_constraintEnd_toEndOf="parent"
42-
app:layout_constraintStart_toStartOf="parent"
43-
app:layout_constraintTop_toTopOf="parent" />
44-
4545
<com.google.android.material.floatingactionbutton.FloatingActionButton
4646
android:id="@+id/settingsButton"
4747
android:layout_width="wrap_content"

0 commit comments

Comments
 (0)