Skip to content

From feature/refacto - better UI visibility management #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: feature/refacto
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ plugins {
android {
signingConfigs {
digiview {
try{
storeFile file(digiviewStoreFile)
storePassword digiviewStorePassword
keyPassword digiviewKeyPassword
keyAlias digiviewKeyAlias
}
catch (ex) {
println("You should define mStoreFile, mStorePassword, mKeyPassword and mKeyAlias in ~/.gradle/gradle.properties : "+ ex.message)
}
try{
storeFile file(digiviewStoreFile)
storePassword digiviewStorePassword
keyPassword digiviewKeyPassword
keyAlias digiviewKeyAlias
}
catch (ex) {
println("You should define mStoreFile, mStorePassword, mKeyPassword and mKeyAlias in ~/.gradle/gradle.properties : "+ ex.message)
}
}
}

Expand Down Expand Up @@ -65,7 +65,6 @@ android {
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
Expand Down
149 changes: 64 additions & 85 deletions app/src/main/java/com/fpvout/digiview/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.fpvout.digiview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.LayoutTransition;
import android.content.Context;
import android.content.Intent;
Expand All @@ -10,7 +8,6 @@
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
Expand All @@ -24,17 +21,19 @@
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.preference.PreferenceManager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import io.sentry.SentryLevel;
import io.sentry.android.core.SentryAndroid;

import static com.fpvout.digiview.UsbMaskConnection.ACTION_USB_PERMISSION;
import static com.fpvout.digiview.VideoReaderExoplayer.VideoZoomedIn;

public class MainActivity extends AppCompatActivity implements UsbDeviceListener {
private static final String TAG = "DIGIVIEW";
Expand All @@ -47,13 +46,18 @@ public class MainActivity extends AppCompatActivity implements UsbDeviceListener
boolean usbConnected = false;
SurfaceView fpvView;
private int shortAnimationDuration;
private float buttonAlpha = 1;
private View settingsButton;
private FloatingActionButton settingsButton;
private View watermarkView;
private OverlayView overlayView;
private GestureDetector gestureDetector;
private ScaleGestureDetector scaleGestureDetector;
private SharedPreferences sharedPreferences;
private final Runnable hideButtonsRunnable = new Runnable() {
@Override
public void run() {
toggleView(settingsButton, false);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -97,7 +101,7 @@ protected void onCreate(Bundle savedInstanceState) {
});

mVideoReader = new VideoReaderExoplayer(fpvView, this);
mVideoReader.setVideoPlayingEventListener(this::hideOverlay);
mVideoReader.setVideoPlayingEventListener(this::hideFullOverlay);
mVideoReader.setVideoWaitingEventListener(() -> showOverlay(R.string.waiting_for_video, OverlayStatus.Connected));

mUsbMaskConnection = new UsbMaskConnection();
Expand Down Expand Up @@ -132,7 +136,7 @@ private void setupGestureDetectors() {
gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
toggleSettingsButton();
toggleFullOverlay();
return super.onSingleTapConfirmed(e);
}

Expand Down Expand Up @@ -163,94 +167,78 @@ public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}

private void updateWatermark() {
if (overlayView.getVisibility() == View.VISIBLE) {
watermarkView.setAlpha(0);
return;
}
private void toggleFullOverlay() {
if (overlayView.getAlpha() > 0.0f) return;

if (sharedPreferences.getBoolean(ShowWatermark, true)) {
watermarkView.setAlpha(0.3F);
} else {
watermarkView.setAlpha(0F);
}
toggleView(settingsButton, hideButtonsRunnable);
}

private void updateVideoZoom() {
if (sharedPreferences.getBoolean(VideoZoomedIn, true)) {
mVideoReader.zoomIn();
} else {
mVideoReader.zoomOut();
}
}
private void hideFullOverlay() {
toggleView(watermarkView, sharedPreferences.getBoolean(ShowWatermark, true), 0.3f);

private void cancelButtonAnimation() {
Handler handler = settingsButton.getHandler();
if (handler != null) {
handler.removeCallbacksAndMessages(null);
}
toggleView(settingsButton, false);
toggleView(overlayView, false);
}

// private void showSettingsButton() {
// cancelButtonAnimation();
//
// if (overlayView.getVisibility() == View.VISIBLE) {
// buttonAlpha = 1;
// settingsButton.setAlpha(1);
// }
// }
private void showFullOverlay() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe moving this to OverlayView would make it cleaner.
and the toggleViews function to a CustomView class, that Overlay could inherit from ?

toggleView(watermarkView, false);

private void toggleSettingsButton() {
if (buttonAlpha == 1 && overlayView.getVisibility() == View.VISIBLE) return;
toggleView(settingsButton, true);
}

// cancel any pending delayed animations first
cancelButtonAnimation();
private void showOverlay(int textId, OverlayStatus connected) {
toggleView(overlayView, true);
showFullOverlay();
overlayView.show(textId, connected);
}

if (buttonAlpha == 1) {
buttonAlpha = 0;
} else {
buttonAlpha = 1;
}
private void toggleView(FloatingActionButton view, @Nullable Runnable runnable) {
toggleView(view, view.getVisibility() != View.VISIBLE, runnable);
}

settingsButton.animate()
.alpha(buttonAlpha)
.setDuration(shortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
autoHideSettingsButton();
}
});
private void toggleView(View view, boolean visible) {
toggleView(view, visible, 1.0f, null);
}

private void autoHideSettingsButton() {
if (overlayView.getVisibility() == View.VISIBLE) return;
if (buttonAlpha == 0) return;
private void toggleView(FloatingActionButton view, boolean visible) {
toggleView(view, visible, null);
}

private void toggleView(View view, boolean visible, float visibleAlpha) {
toggleView(view, visible, visibleAlpha, null);
}

settingsButton.postDelayed(() -> {
buttonAlpha = 0;
settingsButton.animate()
private void toggleView(View view, boolean visible, float visibleAlpha, @Nullable Runnable runnable) {
if (!visible) {
view.removeCallbacks(runnable);
view.animate().cancel();
view.animate()
.alpha(0)
.setDuration(shortAnimationDuration)
.setListener(null);
} else {
view.removeCallbacks(runnable);
view.animate().cancel();
view.animate()
.alpha(visibleAlpha)
.setDuration(shortAnimationDuration);
}, 3000);
view.postDelayed(runnable, 3000);
}
}

private void showOverlay(int textId, OverlayStatus connected) {
overlayView.show(textId, connected);
updateWatermark();
autoHideSettingsButton();
updateVideoZoom();

}
private void toggleView(FloatingActionButton view, boolean visible, @Nullable Runnable runnable) {
if (view.getHandler() != null) {
view.getHandler().removeCallbacksAndMessages(null);
}

private void hideOverlay() {
overlayView.hide();
updateWatermark();
autoHideSettingsButton();
updateVideoZoom();
if (!visible) {
view.hide();
} else {
view.show();
view.postDelayed(runnable, 3000);
}
}


@Override
public void usbDeviceApproved(UsbDevice device) {
Log.i(TAG, "USB - usbDevice approved");
Expand All @@ -266,15 +254,11 @@ public void usbDeviceDetached() {
this.onStop();
}


private void connect() {
usbConnected = true;
mUsbMaskConnection.setUsbDevice(usbManager, usbDevice);
mVideoReader.setUsbMaskConnection(mUsbMaskConnection);
overlayView.hide();
mVideoReader.start();
updateWatermark();
autoHideSettingsButton();
showOverlay(R.string.waiting_for_video, OverlayStatus.Connected);
}

Expand All @@ -295,11 +279,6 @@ public void onResume() {
showOverlay(R.string.waiting_for_usb_device, OverlayStatus.Disconnected);
}
}

settingsButton.setAlpha(1);
autoHideSettingsButton();
updateWatermark();
updateVideoZoom();
}


Expand Down
8 changes: 0 additions & 8 deletions app/src/main/java/com/fpvout/digiview/OverlayView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

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

public void hide(){
setVisibility(View.GONE);
}

public void show(int textResourceId, OverlayStatus status){
showInfo(getContext().getString(textResourceId), status);
}

private void showInfo(String text, OverlayStatus status){

setVisibility(View.VISIBLE);

textView.setText(text);

int image = R.drawable.ic_goggles_white;
Expand Down
Loading