From 8e6bc903c16e65e2fa548bee20da4031b6e83845 Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Tue, 27 Jun 2023 21:30:16 +0200 Subject: [PATCH] Optimize imports, Add code timeout bar --- .idea/deploymentTargetDropDown.xml | 17 ------------- .../cringe_authenticator/IntroActivity.java | 2 -- .../cringe_authenticator/MainActivity.java | 5 ++-- .../cringe_authenticator/OTPData.java | 4 ++++ .../fragment/DynamicFragment.java | 24 ++++++++++++------- .../fragment/HomeFragment.java | 1 - .../fragment/MenuFragment.java | 7 ------ .../fragment/SettingsFragment.java | 13 ---------- .../grouplist/GroupListAdapter.java | 7 ------ .../grouplist/GroupListItem.java | 3 --- .../otplist/OTPListAdapter.java | 3 +++ .../scanner/QRScannerActivity.java | 2 -- .../urihandler/URIHandlerActivity.java | 1 - .../util/NavigationUtil.java | 9 ++++--- .../util/SettingsUtil.java | 2 -- app/src/main/res/layout/content_main.xml | 5 ++-- app/src/main/res/layout/otp_code.xml | 8 +++++++ 17 files changed, 38 insertions(+), 75 deletions(-) delete mode 100644 .idea/deploymentTargetDropDown.xml diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml deleted file mode 100644 index ee7629a..0000000 --- a/.idea/deploymentTargetDropDown.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file 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 bc899c3..65588bc 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,8 +1,6 @@ package com.cringe_studios.cringe_authenticator; -import android.app.Activity; import android.content.Intent; -import android.content.SharedPreferences; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; 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 0cda957..c17405d 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 @@ -18,8 +18,6 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.biometric.BiometricPrompt; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; -import androidx.navigation.Navigation; -import androidx.navigation.ui.NavigationUI; import com.cringe_studios.cringe_authenticator.databinding.ActivityMainBinding; import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeChoiceBinding; @@ -37,7 +35,6 @@ import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm; import com.cringe_studios.cringe_authenticator_library.OTPType; import java.util.concurrent.Executor; -import java.util.function.Supplier; public class MainActivity extends AppCompatActivity { @@ -105,6 +102,8 @@ public class MainActivity extends AppCompatActivity { binding.fabMenu.setOnClickListener(view -> NavigationUtil.navigate(this, MenuFragment.class, null)); binding.fabScan.setOnClickListener(view -> scanCode()); binding.fabInput.setOnClickListener(view -> inputCode()); + + NavigationUtil.navigate(this, HomeFragment.class, null); } @Override diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/OTPData.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/OTPData.java index dc6bb96..e969b40 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/OTPData.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/OTPData.java @@ -72,6 +72,10 @@ public class OTPData implements Serializable { this.counter = getOTP().getCounter(); } + public long getNextDueTime() { + return getOTP().getNextDueTime(); + } + public String validate() { try { getOTP(); 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 4ae1d46..af20563 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 @@ -1,7 +1,5 @@ package com.cringe_studios.cringe_authenticator.fragment; -import android.content.Context; -import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -11,7 +9,6 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; import com.cringe_studios.cringe_authenticator.OTPData; import com.cringe_studios.cringe_authenticator.databinding.FragmentDynamicBinding; @@ -19,6 +16,7 @@ import com.cringe_studios.cringe_authenticator.otplist.OTPListAdapter; import com.cringe_studios.cringe_authenticator.otplist.OTPListItem; import com.cringe_studios.cringe_authenticator.util.FabUtil; import com.cringe_studios.cringe_authenticator.util.SettingsUtil; +import com.cringe_studios.cringe_authenticator_library.OTPType; import java.util.List; @@ -62,12 +60,7 @@ public class DynamicFragment extends NamedFragment { handler = new Handler(Looper.getMainLooper()); refreshCodes = () -> { - for(int i = 0; i < binding.itemList.getChildCount(); i++) { - OTPListItem vh = (OTPListItem) binding.itemList.findViewHolderForAdapterPosition(i); - if(vh == null) continue; - vh.getBinding().otpCode.setText(vh.getOTPData().getPin()); - } - + refreshCodes(); handler.postDelayed(refreshCodes, 1000L); }; @@ -89,6 +82,19 @@ public class DynamicFragment extends NamedFragment { otpListAdapter.add(data); } + public void refreshCodes() { + for(int i = 0; i < binding.itemList.getChildCount(); i++) { + OTPListItem vh = (OTPListItem) binding.itemList.findViewHolderForAdapterPosition(i); + if(vh == null) continue; + vh.getBinding().otpCode.setText(vh.getOTPData().getPin()); + + if(vh.getOTPData().getType() == OTPType.TOTP) { + long timeDiff = vh.getOTPData().getNextDueTime() - System.currentTimeMillis() / 1000; + vh.getBinding().progress.setProgress((int) ((1 - ((double) timeDiff / vh.getOTPData().getPeriod())) * 100)); + } + } + } + @Override public void onDestroyView() { super.onDestroyView(); diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/HomeFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/HomeFragment.java index 16b267d..d093341 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/HomeFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/HomeFragment.java @@ -6,7 +6,6 @@ import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; import com.cringe_studios.cringe_authenticator.databinding.FragmentHomeBinding; import com.cringe_studios.cringe_authenticator.util.FabUtil; diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java index 5f639b8..938bec8 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java @@ -1,7 +1,5 @@ package com.cringe_studios.cringe_authenticator.fragment; -import android.content.Context; -import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; @@ -10,14 +8,9 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; -import androidx.fragment.app.Fragment; -import com.cringe_studios.cringe_authenticator.OTPData; import com.cringe_studios.cringe_authenticator.databinding.FragmentMenuBinding; -import com.cringe_studios.cringe_authenticator.databinding.MenuItemBinding; import com.cringe_studios.cringe_authenticator.grouplist.GroupListAdapter; -import com.cringe_studios.cringe_authenticator.grouplist.GroupListItem; import com.cringe_studios.cringe_authenticator.util.FabUtil; import com.cringe_studios.cringe_authenticator.util.NavigationUtil; import com.cringe_studios.cringe_authenticator.util.SettingsUtil; diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java index 9ec41dc..cddc2ed 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java @@ -1,8 +1,6 @@ package com.cringe_studios.cringe_authenticator.fragment; -import android.content.res.TypedArray; import android.os.Bundle; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -11,22 +9,11 @@ import android.widget.ArrayAdapter; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; -import androidx.navigation.NavController; -import androidx.navigation.Navigation; -import com.cringe_studios.cringe_authenticator.R; import com.cringe_studios.cringe_authenticator.databinding.FragmentSettingsBinding; import com.cringe_studios.cringe_authenticator.util.FabUtil; import com.cringe_studios.cringe_authenticator.util.SettingsUtil; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; - public class SettingsFragment extends NamedFragment { private FragmentSettingsBinding binding; diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/grouplist/GroupListAdapter.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/grouplist/GroupListAdapter.java index d5771eb..9f3ec24 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/grouplist/GroupListAdapter.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/grouplist/GroupListAdapter.java @@ -1,24 +1,17 @@ package com.cringe_studios.cringe_authenticator.grouplist; import android.content.Context; -import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.view.LayoutInflater; -import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; import androidx.core.util.Consumer; import androidx.recyclerview.widget.RecyclerView; -import com.cringe_studios.cringe_authenticator.OTPData; import com.cringe_studios.cringe_authenticator.databinding.MenuItemBinding; -import com.cringe_studios.cringe_authenticator.fragment.DynamicFragment; -import com.cringe_studios.cringe_authenticator.util.NavigationUtil; -import com.cringe_studios.cringe_authenticator.util.SettingsUtil; import java.util.ArrayList; import java.util.List; diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/grouplist/GroupListItem.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/grouplist/GroupListItem.java index 3e44f33..cad9058 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/grouplist/GroupListItem.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/grouplist/GroupListItem.java @@ -1,8 +1,5 @@ package com.cringe_studios.cringe_authenticator.grouplist; -import android.view.MenuItem; -import android.view.View; - import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/otplist/OTPListAdapter.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/otplist/OTPListAdapter.java index f446448..e42adff 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/otplist/OTPListAdapter.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/otplist/OTPListAdapter.java @@ -4,6 +4,7 @@ import android.content.Context; import android.os.Handler; import android.os.Looper; import android.view.LayoutInflater; +import android.view.View; import android.view.ViewGroup; import android.widget.Toast; @@ -44,6 +45,7 @@ public class OTPListAdapter extends RecyclerView.Adapter { holder.setOTPData(data); holder.getBinding().label.setText(holder.getOTPData().getName()); + holder.getBinding().progress.setVisibility(holder.getOTPData().getType() == OTPType.TOTP ? View.VISIBLE : View.GONE); holder.getBinding().getRoot().setOnClickListener(view -> { if(data.getType() != OTPType.HOTP) return; @@ -52,6 +54,7 @@ public class OTPListAdapter extends RecyclerView.Adapter { view.setClickable(false); Toast.makeText(view.getContext(), "Generated new code", Toast.LENGTH_LONG).show(); data.incrementCounter(); + holder.getBinding().otpCode.setText(data.getPin()); handler.postDelayed(() -> view.setClickable(true), 5000); }); } diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/scanner/QRScannerActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/scanner/QRScannerActivity.java index 114fc50..bb0dff2 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/scanner/QRScannerActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/scanner/QRScannerActivity.java @@ -29,8 +29,6 @@ import androidx.core.content.ContextCompat; import com.cringe_studios.cringe_authenticator.OTPData; import com.cringe_studios.cringe_authenticator.databinding.ActivityQrScannerBinding; import com.cringe_studios.cringe_authenticator.util.OTPParser; -import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm; -import com.cringe_studios.cringe_authenticator_library.OTPType; import com.google.common.util.concurrent.ListenableFuture; import com.google.mlkit.vision.barcode.BarcodeScanner; import com.google.mlkit.vision.barcode.BarcodeScanning; diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/urihandler/URIHandlerActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/urihandler/URIHandlerActivity.java index 4cb32e9..5422398 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/urihandler/URIHandlerActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/urihandler/URIHandlerActivity.java @@ -2,7 +2,6 @@ package com.cringe_studios.cringe_authenticator.urihandler; import android.content.Intent; import android.os.Bundle; -import android.util.Log; import android.widget.Toast; import androidx.annotation.Nullable; diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/NavigationUtil.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/NavigationUtil.java index 555ef70..6d2a33a 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/NavigationUtil.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/NavigationUtil.java @@ -1,6 +1,5 @@ package com.cringe_studios.cringe_authenticator.util; -import android.content.Context; import android.os.Bundle; import androidx.appcompat.app.ActionBar; @@ -11,12 +10,12 @@ import androidx.fragment.app.FragmentManager; import com.cringe_studios.cringe_authenticator.R; import com.cringe_studios.cringe_authenticator.fragment.NamedFragment; -import kotlin.Suppress; - public class NavigationUtil { + // TODO: check if this still works after changes + public static void navigate(AppCompatActivity activity, Class fragmentClass, Bundle args) { - FragmentManager manager = activity.getSupportFragmentManager().getPrimaryNavigationFragment().getChildFragmentManager(); + FragmentManager manager = activity.getSupportFragmentManager(); NamedFragment fragment = instantiateFragment(manager, fragmentClass, args); ActionBar bar = activity.getSupportActionBar(); @@ -45,7 +44,7 @@ public class NavigationUtil { } public static Fragment getCurrentFragment(AppCompatActivity activity) { - return getCurrentFragment(activity.getSupportFragmentManager().getPrimaryNavigationFragment().getChildFragmentManager()); + return getCurrentFragment(activity.getSupportFragmentManager()); } public static Fragment getCurrentFragment(Fragment currentFragment) { 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 2c4f3e4..2dbbc92 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 @@ -16,8 +16,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import kotlinx.coroutines.flow.SharedFlow; - public class SettingsUtil { public static final Map THEMES; diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index e416e1c..42d777f 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -5,7 +5,7 @@ android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> - + app:layout_constraintTop_toTopOf="parent" /> \ No newline at end of file diff --git a/app/src/main/res/layout/otp_code.xml b/app/src/main/res/layout/otp_code.xml index 4f9e324..51ff1c2 100644 --- a/app/src/main/res/layout/otp_code.xml +++ b/app/src/main/res/layout/otp_code.xml @@ -21,4 +21,12 @@ android:text="000000" android:textAlignment="center" android:textSize="20sp" /> + + + \ No newline at end of file