Optimize imports, Add code timeout bar

This commit is contained in:
MrLetsplay 2023-06-27 21:30:16 +02:00
parent 002475d1b3
commit 8e6bc903c1
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
17 changed files with 38 additions and 75 deletions

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<targetSelectedWithDropDown>
<Target>
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/Pixel_2_API_33.avd" />
</Key>
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-06-26T17:29:55.573199337Z" />
</component>
</project>

View File

@ -1,8 +1,6 @@
package com.cringe_studios.cringe_authenticator; package com.cringe_studios.cringe_authenticator;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;

View File

@ -18,8 +18,6 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricPrompt; import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; 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.ActivityMainBinding;
import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeChoiceBinding; 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 com.cringe_studios.cringe_authenticator_library.OTPType;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.function.Supplier;
public class MainActivity extends AppCompatActivity { 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.fabMenu.setOnClickListener(view -> NavigationUtil.navigate(this, MenuFragment.class, null));
binding.fabScan.setOnClickListener(view -> scanCode()); binding.fabScan.setOnClickListener(view -> scanCode());
binding.fabInput.setOnClickListener(view -> inputCode()); binding.fabInput.setOnClickListener(view -> inputCode());
NavigationUtil.navigate(this, HomeFragment.class, null);
} }
@Override @Override

View File

@ -72,6 +72,10 @@ public class OTPData implements Serializable {
this.counter = getOTP().getCounter(); this.counter = getOTP().getCounter();
} }
public long getNextDueTime() {
return getOTP().getNextDueTime();
}
public String validate() { public String validate() {
try { try {
getOTP(); getOTP();

View File

@ -1,7 +1,5 @@
package com.cringe_studios.cringe_authenticator.fragment; package com.cringe_studios.cringe_authenticator.fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
@ -11,7 +9,6 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.cringe_studios.cringe_authenticator.OTPData; import com.cringe_studios.cringe_authenticator.OTPData;
import com.cringe_studios.cringe_authenticator.databinding.FragmentDynamicBinding; 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.otplist.OTPListItem;
import com.cringe_studios.cringe_authenticator.util.FabUtil; import com.cringe_studios.cringe_authenticator.util.FabUtil;
import com.cringe_studios.cringe_authenticator.util.SettingsUtil; import com.cringe_studios.cringe_authenticator.util.SettingsUtil;
import com.cringe_studios.cringe_authenticator_library.OTPType;
import java.util.List; import java.util.List;
@ -62,12 +60,7 @@ public class DynamicFragment extends NamedFragment {
handler = new Handler(Looper.getMainLooper()); handler = new Handler(Looper.getMainLooper());
refreshCodes = () -> { refreshCodes = () -> {
for(int i = 0; i < binding.itemList.getChildCount(); i++) { refreshCodes();
OTPListItem vh = (OTPListItem) binding.itemList.findViewHolderForAdapterPosition(i);
if(vh == null) continue;
vh.getBinding().otpCode.setText(vh.getOTPData().getPin());
}
handler.postDelayed(refreshCodes, 1000L); handler.postDelayed(refreshCodes, 1000L);
}; };
@ -89,6 +82,19 @@ public class DynamicFragment extends NamedFragment {
otpListAdapter.add(data); 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 @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();

View File

@ -6,7 +6,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.cringe_studios.cringe_authenticator.databinding.FragmentHomeBinding; import com.cringe_studios.cringe_authenticator.databinding.FragmentHomeBinding;
import com.cringe_studios.cringe_authenticator.util.FabUtil; import com.cringe_studios.cringe_authenticator.util.FabUtil;

View File

@ -1,7 +1,5 @@
package com.cringe_studios.cringe_authenticator.fragment; package com.cringe_studios.cringe_authenticator.fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -10,14 +8,9 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; 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.FragmentMenuBinding;
import com.cringe_studios.cringe_authenticator.databinding.MenuItemBinding;
import com.cringe_studios.cringe_authenticator.grouplist.GroupListAdapter; 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.FabUtil;
import com.cringe_studios.cringe_authenticator.util.NavigationUtil; import com.cringe_studios.cringe_authenticator.util.NavigationUtil;
import com.cringe_studios.cringe_authenticator.util.SettingsUtil; import com.cringe_studios.cringe_authenticator.util.SettingsUtil;

View File

@ -1,8 +1,6 @@
package com.cringe_studios.cringe_authenticator.fragment; package com.cringe_studios.cringe_authenticator.fragment;
import android.content.res.TypedArray;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -11,22 +9,11 @@ import android.widget.ArrayAdapter;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; 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.databinding.FragmentSettingsBinding;
import com.cringe_studios.cringe_authenticator.util.FabUtil; import com.cringe_studios.cringe_authenticator.util.FabUtil;
import com.cringe_studios.cringe_authenticator.util.SettingsUtil; 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 { public class SettingsFragment extends NamedFragment {
private FragmentSettingsBinding binding; private FragmentSettingsBinding binding;

View File

@ -1,24 +1,17 @@
package com.cringe_studios.cringe_authenticator.grouplist; package com.cringe_studios.cringe_authenticator.grouplist;
import android.content.Context; import android.content.Context;
import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.util.Consumer; import androidx.core.util.Consumer;
import androidx.recyclerview.widget.RecyclerView; 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.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.ArrayList;
import java.util.List; import java.util.List;

View File

@ -1,8 +1,5 @@
package com.cringe_studios.cringe_authenticator.grouplist; package com.cringe_studios.cringe_authenticator.grouplist;
import android.view.MenuItem;
import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Toast; import android.widget.Toast;
@ -44,6 +45,7 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
holder.setOTPData(data); holder.setOTPData(data);
holder.getBinding().label.setText(holder.getOTPData().getName()); 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 -> { holder.getBinding().getRoot().setOnClickListener(view -> {
if(data.getType() != OTPType.HOTP) return; if(data.getType() != OTPType.HOTP) return;
@ -52,6 +54,7 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
view.setClickable(false); view.setClickable(false);
Toast.makeText(view.getContext(), "Generated new code", Toast.LENGTH_LONG).show(); Toast.makeText(view.getContext(), "Generated new code", Toast.LENGTH_LONG).show();
data.incrementCounter(); data.incrementCounter();
holder.getBinding().otpCode.setText(data.getPin());
handler.postDelayed(() -> view.setClickable(true), 5000); handler.postDelayed(() -> view.setClickable(true), 5000);
}); });
} }

View File

@ -29,8 +29,6 @@ import androidx.core.content.ContextCompat;
import com.cringe_studios.cringe_authenticator.OTPData; import com.cringe_studios.cringe_authenticator.OTPData;
import com.cringe_studios.cringe_authenticator.databinding.ActivityQrScannerBinding; import com.cringe_studios.cringe_authenticator.databinding.ActivityQrScannerBinding;
import com.cringe_studios.cringe_authenticator.util.OTPParser; 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.common.util.concurrent.ListenableFuture;
import com.google.mlkit.vision.barcode.BarcodeScanner; import com.google.mlkit.vision.barcode.BarcodeScanner;
import com.google.mlkit.vision.barcode.BarcodeScanning; import com.google.mlkit.vision.barcode.BarcodeScanning;

View File

@ -2,7 +2,6 @@ package com.cringe_studios.cringe_authenticator.urihandler;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;

View File

@ -1,6 +1,5 @@
package com.cringe_studios.cringe_authenticator.util; package com.cringe_studios.cringe_authenticator.util;
import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import androidx.appcompat.app.ActionBar; 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.R;
import com.cringe_studios.cringe_authenticator.fragment.NamedFragment; import com.cringe_studios.cringe_authenticator.fragment.NamedFragment;
import kotlin.Suppress;
public class NavigationUtil { public class NavigationUtil {
// TODO: check if this still works after changes
public static void navigate(AppCompatActivity activity, Class<? extends NamedFragment> fragmentClass, Bundle args) { public static void navigate(AppCompatActivity activity, Class<? extends NamedFragment> fragmentClass, Bundle args) {
FragmentManager manager = activity.getSupportFragmentManager().getPrimaryNavigationFragment().getChildFragmentManager(); FragmentManager manager = activity.getSupportFragmentManager();
NamedFragment fragment = instantiateFragment(manager, fragmentClass, args); NamedFragment fragment = instantiateFragment(manager, fragmentClass, args);
ActionBar bar = activity.getSupportActionBar(); ActionBar bar = activity.getSupportActionBar();
@ -45,7 +44,7 @@ public class NavigationUtil {
} }
public static Fragment getCurrentFragment(AppCompatActivity activity) { public static Fragment getCurrentFragment(AppCompatActivity activity) {
return getCurrentFragment(activity.getSupportFragmentManager().getPrimaryNavigationFragment().getChildFragmentManager()); return getCurrentFragment(activity.getSupportFragmentManager());
} }
public static Fragment getCurrentFragment(Fragment currentFragment) { public static Fragment getCurrentFragment(Fragment currentFragment) {

View File

@ -16,8 +16,6 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import kotlinx.coroutines.flow.SharedFlow;
public class SettingsUtil { public class SettingsUtil {
public static final Map<String, Integer> THEMES; public static final Map<String, Integer> THEMES;

View File

@ -5,7 +5,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"> app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment <androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment_content_main" android:id="@+id/nav_host_fragment_content_main"
android:name="androidx.navigation.fragment.NavHostFragment" android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp" android:layout_width="0dp"
@ -14,6 +14,5 @@
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent" />
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -21,4 +21,12 @@
android:text="000000" android:text="000000"
android:textAlignment="center" android:textAlignment="center"
android:textSize="20sp" /> android:textSize="20sp" />
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100" />
</LinearLayout> </LinearLayout>