From ccfaf87210d6b8c2d35e3f3f07d3dcb8b140f1bc Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Thu, 22 Jun 2023 21:03:36 +0200 Subject: [PATCH] Update Intro stuff --- app/src/main/AndroidManifest.xml | 7 +- .../cringe_authenticator/IntroActivity.java | 75 +++++++++-- .../cringe_authenticator/MainActivity.java | 2 +- .../fragment/DynamicFragment.java | 6 +- .../cringe_authenticator/fragment/Intro.java | 123 ------------------ .../util/SettingsUtil.java | 4 + app/src/main/res/layout/activity_intro.xml | 22 ++-- app/src/main/res/layout/fragment_intro.xml | 18 --- app/src/main/res/values/strings.xml | 2 - 9 files changed, 82 insertions(+), 177 deletions(-) delete mode 100644 app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/Intro.java delete mode 100644 app/src/main/res/layout/fragment_intro.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 58d1b7c..b8ea110 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -34,13 +34,8 @@ android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.CringeAuthenticator"> - - - - - - diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java index 552f06b..aeb1631 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java @@ -1,17 +1,32 @@ package com.cringe_studios.cringe_authenticator; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.media.MediaPlayer; +import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Looper; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.VideoView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import com.cringe_studios.cringe_authenticator.databinding.ActivityIntroBinding; + public class IntroActivity extends AppCompatActivity { public static boolean show_logoanimation = false; + private static ActivityIntroBinding binding; + + private MediaPlayer mMediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { @@ -20,21 +35,55 @@ public class IntroActivity extends AppCompatActivity { super.onCreate(savedInstanceState); if (show_logoanimation) { setContentView(R.layout.activity_intro); - final Handler handler = new Handler(Looper.getMainLooper()); - handler.postDelayed(new Runnable() { - @Override - public void run() { - //Do something after 4000ms - Intent m = new Intent(getApplicationContext(), MainActivity.class); - m.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION); - startActivity(m); - } - }, 4000); } else { - Intent m = new Intent(getApplicationContext(), MainActivity.class); - m.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION); - startActivity(m); } + + binding = ActivityIntroBinding.inflate(getLayoutInflater()); + + Uri uri = Uri.parse(String.format("android.resource://%s/%s", getPackageName(), R.raw.intro)); + binding.videoView.setVideoURI(uri); + binding.videoView.start(); + + binding.videoView.setOnPreparedListener(mediaPlayer -> { + mMediaPlayer = mediaPlayer; + setDimension(); + }); + + binding.videoView.setOnCompletionListener(mp -> openMainActivity()); + + setContentView(binding.getRoot()); + } + + public void openMainActivity() { + Intent m = new Intent(getApplicationContext(), MainActivity.class); + m.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION); + startActivity(m); + finish(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + // When the Activity is destroyed, release our MediaPlayer and set it to null. + mMediaPlayer.release(); + mMediaPlayer = null; + } + + private void setDimension() { + float videoProportion = (float) mMediaPlayer.getVideoHeight() / mMediaPlayer.getVideoWidth(); + int screenWidth = getResources().getDisplayMetrics().widthPixels; + int screenHeight = getResources().getDisplayMetrics().heightPixels; + float screenProportion = (float) screenHeight / (float) screenWidth; + ViewGroup.LayoutParams lp = binding.videoView.getLayoutParams(); + + if (videoProportion < screenProportion) { + lp.height= screenHeight; + lp.width = (int) ((float) screenHeight / videoProportion); + } else { + lp.width = screenWidth; + lp.height = (int) ((float) screenWidth * videoProportion); + } + binding.videoView.setLayoutParams(lp); } diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java index fce4ac8..b6952b7 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java @@ -76,7 +76,7 @@ public class MainActivity extends AppCompatActivity { Fragment fragment = NavigationUtil.getCurrentFragment(this); if(fragment instanceof DynamicFragment) { DynamicFragment frag = (DynamicFragment) fragment; - SettingsUtil.addOTP(getSharedPreferences(DynamicFragment.GROUPS_PREFS_NAME, MODE_PRIVATE), frag.getGroupName(), obj); + SettingsUtil.addOTP(getSharedPreferences(SettingsUtil.GROUPS_PREFS_NAME, MODE_PRIVATE), frag.getGroupName(), obj); frag.loadOTPs(); } Log.i("AMOGUS", "Actually got something bruh" + obj); diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/DynamicFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/DynamicFragment.java index bdfac0a..284e1b1 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/DynamicFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/DynamicFragment.java @@ -24,9 +24,7 @@ import java.util.List; public class DynamicFragment extends Fragment { - public static final String - GROUPS_PREFS_NAME = "groups", - BUNDLE_GROUP = "group"; + public static final String BUNDLE_GROUP = "group"; private String groupName; @@ -74,7 +72,7 @@ public class DynamicFragment extends Fragment { } public void loadOTPs() { - SharedPreferences prefs = getActivity().getSharedPreferences(DynamicFragment.GROUPS_PREFS_NAME, Context.MODE_PRIVATE); + SharedPreferences prefs = getActivity().getSharedPreferences(SettingsUtil.GROUPS_PREFS_NAME, Context.MODE_PRIVATE); List data = SettingsUtil.getOTPs(prefs, groupName); Log.i("AMOGUS", "OTPS: " + data); diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/Intro.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/Intro.java deleted file mode 100644 index 7117438..0000000 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/Intro.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.cringe_studios.cringe_authenticator.fragment; - -import android.media.MediaPlayer; -import android.net.Uri; -import android.os.Bundle; - -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; - -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.VideoView; - -import com.cringe_studios.cringe_authenticator.R; - -/** - * A simple {@link Fragment} subclass. - * Use the {@link Intro#newInstance} factory method to - * create an instance of this fragment. - */ -public class Intro extends Fragment { - - // Create a VideoView variable, a MediaPlayer variable, and an int to hold the current - // video position. - private VideoView videoBG; - MediaPlayer mMediaPlayer; - int mCurrentVideoPosition; - - - public View onCreateView(@NonNull LayoutInflater inflater, - ViewGroup container, Bundle savedInstanceState) { - - View root = inflater.inflate(R.layout.fragment_intro, container, false); - - // Hook up the VideoView to our UI. - videoBG = (VideoView) root.findViewById(R.id.videoView); - - // Build your video Uri - Uri uri = Uri.parse("android.resource://" // First start with this, - + requireContext().getPackageName() // then retrieve your package name, - + "/" // add a slash, - + R.raw.intro); // and then finally add your video resource. Make sure it is stored - // in the raw folder. - - // Set the new Uri to our VideoView - System.out.println(uri); - videoBG.setVideoURI(uri); - // Start the VideoView - videoBG.start(); - - // Set an OnPreparedListener for our VideoView. For more information about VideoViews, - // check out the Android Docs: https://developer.android.com/reference/android/widget/VideoView.html - videoBG.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { - @Override - public void onPrepared(MediaPlayer mediaPlayer) { - setDimension(); - mMediaPlayer = mediaPlayer; - // We want our video to play over and over so we set looping to true. - mMediaPlayer.setLooping(true); - // We then seek to the current position if it has been set and play the video. - if (mCurrentVideoPosition != 0) { - mMediaPlayer.seekTo(mCurrentVideoPosition); - mMediaPlayer.start(); - } - } - }); - return root; - } - /*================================ Important Section! ================================ - We must override onPause(), onResume(), and onDestroy() to properly handle our - VideoView. - */ - - @Override - public void onPause() { - super.onPause(); - // Capture the current video position and pause the video. - mCurrentVideoPosition = mMediaPlayer.getCurrentPosition(); - videoBG.pause(); - } - - @Override - public void onResume() { - super.onResume(); - // Restart the video when resuming the Activity - videoBG.start(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - // When the Activity is destroyed, release our MediaPlayer and set it to null. - mMediaPlayer.release(); - mMediaPlayer = null; - } - private void setDimension() { - // Adjust the size of the video - // so it fits on the screen - float videoProportion = getVideoProportion(); - int screenWidth = getResources().getDisplayMetrics().widthPixels; - int screenHeight = getResources().getDisplayMetrics().heightPixels; - float screenProportion = (float) screenHeight / (float) screenWidth; - android.view.ViewGroup.LayoutParams lp = videoBG.getLayoutParams(); - - if (videoProportion < screenProportion) { - lp.height= screenHeight; - lp.width = (int) ((float) screenHeight / videoProportion); - } else { - lp.width = screenWidth; - lp.height = (int) ((float) screenWidth * videoProportion); - } - videoBG.setLayoutParams(lp); - } - - // This method gets the proportion of the video that you want to display. -// I already know this ratio since my video is hardcoded, you can get the -// height and width of your video and appropriately generate the proportion -// as :height/width - private float getVideoProportion(){ - return 2.22f; - } -} \ No newline at end of file diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java index 4d32e90..840a3b2 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java @@ -13,6 +13,10 @@ import java.util.List; public class SettingsUtil { + public static String + GROUPS_PREFS_NAME = "groups", + GENERAL_PREFS_NAME = "general"; + private static final Gson GSON = new Gson(); public static List getOTPs(SharedPreferences prefs, String group) { diff --git a/app/src/main/res/layout/activity_intro.xml b/app/src/main/res/layout/activity_intro.xml index c6fde91..b4f7419 100644 --- a/app/src/main/res/layout/activity_intro.xml +++ b/app/src/main/res/layout/activity_intro.xml @@ -1,16 +1,18 @@ - + android:background="#000000"> - - + + diff --git a/app/src/main/res/layout/fragment_intro.xml b/app/src/main/res/layout/fragment_intro.xml deleted file mode 100644 index 07e70d5..0000000 --- a/app/src/main/res/layout/fragment_intro.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bcd4a55..abf7123 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -43,6 +43,4 @@ libero vel nunc consequat, quis tincidunt nisl eleifend. Cras bibendum enim a justo luctus vestibulum. Fusce dictum libero quis erat maximus, vitae volutpat diam dignissim. - - Hello blank fragment \ No newline at end of file