Skip to content

Commit

Permalink
Fix some dumb bugs I created 2 hours ago
Browse files Browse the repository at this point in the history
  • Loading branch information
astarivi committed Jun 12, 2023
1 parent db15c9d commit 3b14571
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ public ExtendedProperties(Context context, String filename) {

localFile = new File(context.getFilesDir(), subFolder + filename);
if (!localFile.exists()) {
//noinspection ResultOfMethodCallIgnored
localFile.mkdirs();
ensureDirectory();
return;
}

try (FileInputStream fileInputStream = new FileInputStream(localFile)){
this.load(fileInputStream);
} catch(FileNotFoundException e) {
//noinspection ResultOfMethodCallIgnored
localFile.mkdirs();
ensureDirectory();
} catch(IllegalArgumentException | IOException e) {
Logger.debug(e);
if (!localFile.delete()) localFile.deleteOnExit();
Expand All @@ -41,8 +39,7 @@ public ExtendedProperties(Context context, String filename) {
}

public synchronized void save(){
//noinspection ResultOfMethodCallIgnored
localFile.mkdirs();
ensureDirectory();

try (FileOutputStream fileOutput = new FileOutputStream(localFile)){
this.store(fileOutput, filename);
Expand Down Expand Up @@ -103,4 +100,13 @@ private boolean parseBoolean(String value) {
throw new IllegalArgumentException("Value cannot be converted to boolean");
}
}

private void ensureDirectory() {
try {
//noinspection ResultOfMethodCallIgnored
Objects.requireNonNull(localFile.getParentFile()).mkdirs();
} catch(NullPointerException ignored) {

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.GestureDetectorCompat;
import androidx.core.view.WindowCompat;
Expand All @@ -19,7 +21,7 @@
import com.astarivi.kaizoyu.core.models.Result;
import com.astarivi.kaizoyu.core.theme.AppCompatActivityTheme;
import com.astarivi.kaizoyu.databinding.ActivityVideoPlayerBinding;
import com.astarivi.kaizoyu.video.gui.PlayerView;
import com.astarivi.kaizoyu.video.gui.PlayerSkipView;
import com.astarivi.kaizoyu.video.utils.AnimeEpisodeManager;
import com.astarivi.kaizoyu.video.utils.BundleUtils;
import com.google.android.material.snackbar.Snackbar;
Expand Down Expand Up @@ -78,7 +80,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

// Everything else
binding.downloadStatus.setText(getResources().getString(R.string.executing_irc_handshake));
gestureDetector = new GestureDetectorCompat(this, new PlayerView.PlayerGestureListener(binding.mainPlayer.getBinding()));
gestureDetector = new GestureDetectorCompat(this, new PlayerGestureListener());
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);

binding.mainPlayer.initialize(
Expand Down Expand Up @@ -169,8 +171,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
viewModel.getDownloadFile().observe(this, file -> {
if (isDestroyed()) return;

isPlaying = true;

if (spark != null) spark.stopAnimation();
binding.getRoot().setBackgroundColor(Color.BLACK);
binding.initialDownloadProgress.setVisibility(View.GONE);
Expand All @@ -179,6 +179,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

binding.mainPlayer.play(file);
binding.mainPlayer.setVisibility(View.VISIBLE);
isPlaying = true;
});

viewModel.startDownload(this, result);
Expand Down Expand Up @@ -247,10 +248,56 @@ public boolean onTouchEvent(MotionEvent event) {
private void hideSystemUI(){
WindowInsetsControllerCompat windowInsetsController = WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());

View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}

private void delayedExit(){
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(this::finish, 5000);
}

public class PlayerGestureListener extends GestureDetector.SimpleOnGestureListener {

@Override
public boolean onSingleTapConfirmed(@NonNull MotionEvent event) {
if (!isPlaying) return true;
binding.mainPlayer.showPlayerBar();

return super.onSingleTapConfirmed(event);
}

@Override
public boolean onDoubleTap(@NonNull MotionEvent event) {
if (!isPlaying) return true;

PlayerSkipView skipView = binding.mainPlayer.getSkipManager();

if (skipView == null) return true;

int eventX = (int)event.getX();
int eventY = (int)event.getY();
int height = binding.getRoot().getHeight();
int width = binding.getRoot().getWidth();
int halfWidth = width / 2;

if (eventX > halfWidth) {
skipView.skipAhead();
} else {
skipView.skipBack();
}

return super.onDoubleTap(event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public PlayerBarView(Context context, @Nullable AttributeSet attrs, int defStyle

// endregion

private void inflateView(Context context) {
binding = PlayerBarBinding.inflate(
LayoutInflater.from(context),
this,
true
);
}

@Override
public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
Expand Down Expand Up @@ -93,20 +101,12 @@ public void onRestoreInstanceState(Parcelable state) {
return bundle.getParcelable(State.STATE);
}

private void inflateView(Context context) {
binding = PlayerBarBinding.inflate(
LayoutInflater.from(context),
this,
true
);
}

@NotNull
private String verboseMillisecondsToDuration(long duration) {
if (duration < 0) return "??:??:??";


long seconds = (duration / 1000) % 60 ;
long seconds = (duration / 1000) % 60;
long minutes = (duration / (1000*60)) % 60;
long hours = (duration / (1000*60*60)) % 24;

Expand Down
37 changes: 5 additions & 32 deletions app/src/main/java/com/astarivi/kaizoyu/video/gui/PlayerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.astarivi.kaizoyu.databinding.PlayerBinding;
Expand Down Expand Up @@ -131,36 +128,12 @@ public void destroy() {
mediaPlayer = null;
}

public static class PlayerGestureListener extends GestureDetector.SimpleOnGestureListener {
private final PlayerBinding binding;

public PlayerGestureListener (PlayerBinding binding) {
this.binding = binding;
}
@Override
public boolean onSingleTapConfirmed(@NonNull MotionEvent event) {
binding.playerBar.show();

return super.onSingleTapConfirmed(event);
}

@Override
public boolean onDoubleTap(@NonNull MotionEvent event) {

int eventX = (int)event.getX();
int eventY = (int)event.getY();
int height = binding.getRoot().getHeight();
int width = binding.getRoot().getWidth();
int halfWidth = width / 2;

if (eventX > halfWidth) {
binding.skipManager.skipAhead();
} else {
binding.skipManager.skipBack();
}
public void showPlayerBar() {
binding.playerBar.show();
}

return super.onDoubleTap(event);
}
public PlayerSkipView getSkipManager() {
return binding.skipManager;
}

public interface PlayerEventListener {
Expand Down

0 comments on commit 3b14571

Please sign in to comment.