From 5925674826e3071fce8fd4714c4a45345b5de8fe Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Thu, 22 Jun 2023 19:19:48 +0200 Subject: [PATCH] Move some files --- .../cringe_authenticator/MainActivity.java | 19 ++++---- .../fragment/DynamicFragment.java | 44 +++++++++++++++---- .../fragment/MenuFragment.java | 6 +-- .../{ => scanner}/QRScannerActivity.java | 10 +++-- .../{ => scanner}/QRScannerContract.java | 6 ++- .../util/SettingsUtil.java | 4 +- .../{authenticate_totp.xml => otp_code.xml} | 6 +-- 7 files changed, 64 insertions(+), 31 deletions(-) rename app/src/main/java/com/cringe_studios/cringe_authenticator/{ => scanner}/QRScannerActivity.java (96%) rename app/src/main/java/com/cringe_studios/cringe_authenticator/{ => scanner}/QRScannerContract.java (77%) rename app/src/main/res/layout/{authenticate_totp.xml => otp_code.xml} (89%) 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 15e9657..fce4ac8 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 @@ -1,7 +1,5 @@ package com.cringe_studios.cringe_authenticator; -import android.app.Activity; -import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; @@ -9,12 +7,10 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; +import android.widget.Toast; import androidx.activity.result.ActivityResultLauncher; -import androidx.activity.result.contract.ActivityResultContract; -import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.biometric.BiometricPrompt; @@ -26,15 +22,14 @@ import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import com.cringe_studios.cringe_authenticator.databinding.ActivityMainBinding; -import com.cringe_studios.cringe_authenticator.databinding.FragmentDynamicBinding; import com.cringe_studios.cringe_authenticator.fragment.DynamicFragment; import com.cringe_studios.cringe_authenticator.fragment.HomeFragment; import com.cringe_studios.cringe_authenticator.fragment.MenuFragment; import com.cringe_studios.cringe_authenticator.fragment.SettingsFragment; +import com.cringe_studios.cringe_authenticator.scanner.QRScannerActivity; +import com.cringe_studios.cringe_authenticator.scanner.QRScannerContract; import com.cringe_studios.cringe_authenticator.util.NavigationUtil; import com.cringe_studios.cringe_authenticator.util.SettingsUtil; -import com.cringe_studios.cringe_authenticator_library.OTPType; -import com.google.android.material.color.utilities.DynamicColor; import java.util.concurrent.Executor; @@ -73,10 +68,16 @@ public class MainActivity extends AppCompatActivity { launchApp(); startQRCodeScan = registerForActivityResult(new QRScannerContract(), obj -> { + if(obj == null) { // Got some error TODO: show error message + Toast.makeText(this, "Failed to scan code", Toast.LENGTH_LONG).show(); + return; + } + Fragment fragment = NavigationUtil.getCurrentFragment(this); if(fragment instanceof DynamicFragment) { DynamicFragment frag = (DynamicFragment) fragment; - SettingsUtil.addOTP(getSharedPreferences("groups", MODE_PRIVATE), frag.getGroupName(), obj); + SettingsUtil.addOTP(getSharedPreferences(DynamicFragment.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 4de2596..bdfac0a 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 @@ -3,6 +3,7 @@ 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.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -13,22 +14,28 @@ import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import com.cringe_studios.cringe_authenticator.OTPData; -import com.cringe_studios.cringe_authenticator.R; -import com.cringe_studios.cringe_authenticator.databinding.AuthenticateTotpBinding; import com.cringe_studios.cringe_authenticator.databinding.FragmentDynamicBinding; +import com.cringe_studios.cringe_authenticator.databinding.OtpCodeBinding; import com.cringe_studios.cringe_authenticator.util.FabUtil; -import com.cringe_studios.cringe_authenticator.util.NavigationUtil; import com.cringe_studios.cringe_authenticator.util.SettingsUtil; -import com.google.android.material.floatingactionbutton.FloatingActionButton; +import com.cringe_studios.cringe_authenticator_library.OTP; import java.util.List; public class DynamicFragment extends Fragment { + public static final String + GROUPS_PREFS_NAME = "groups", + BUNDLE_GROUP = "group"; + private String groupName; private FragmentDynamicBinding binding; + private Handler handler; + + private Runnable refreshCodes; + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -39,7 +46,7 @@ public class DynamicFragment extends Fragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { binding = FragmentDynamicBinding.inflate(inflater, container, false); - groupName = requireArguments().getString("group"); + groupName = requireArguments().getString(DynamicFragment.BUNDLE_GROUP); /*String[] totps = new String[]{"Code 1", "Code 2", groupName}; for(String totp : totps) { @@ -52,18 +59,31 @@ public class DynamicFragment extends Fragment { FabUtil.showFabs(getActivity()); + refreshCodes = () -> { + for(int i = 0; i < binding.itemList.getChildCount(); i++) { + View v = binding.itemList.getChildAt(i); + OTP otp = (OTP) v.getTag(); + otp.getPin(); + } + handler.postDelayed(refreshCodes, 1000L); + }; + + handler.post(refreshCodes); + return binding.getRoot(); } public void loadOTPs() { - SharedPreferences prefs = getActivity().getSharedPreferences("groups", Context.MODE_PRIVATE); + SharedPreferences prefs = getActivity().getSharedPreferences(DynamicFragment.GROUPS_PREFS_NAME, Context.MODE_PRIVATE); List data = SettingsUtil.getOTPs(prefs, groupName); Log.i("AMOGUS", "OTPS: " + data); + binding.itemList.removeAllViews(); for(OTPData otp : data) { - AuthenticateTotpBinding itemBinding = AuthenticateTotpBinding.inflate(getLayoutInflater()); - itemBinding.displayName.setText(otp.getName()); - itemBinding.totpCode.setText(otp.toOTP().getPin()); + OtpCodeBinding itemBinding = OtpCodeBinding.inflate(getLayoutInflater()); + itemBinding.label.setText(otp.getName()); + itemBinding.otpCode.setText(otp.toOTP().getPin()); + itemBinding.getRoot().setTag(otp.toOTP()); binding.itemList.addView(itemBinding.getRoot()); } } @@ -74,6 +94,12 @@ public class DynamicFragment extends Fragment { this.binding = null; } + @Override + public void onDestroy() { + handler.removeCallbacks(refreshCodes); + super.onDestroy(); + } + public String getGroupName() { return groupName; } 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 023b4e5..efb5198 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 @@ -29,16 +29,16 @@ public class MenuFragment extends Fragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { binding = FragmentMenuBinding.inflate(inflater); - String[] items = {"a", "b"}; - SharedPreferences pr = getContext().getSharedPreferences("menu", Context.MODE_PRIVATE); + String[] items = {"a", "b"}; + for(String item : items) { MenuItemBinding itemBinding = MenuItemBinding.inflate(inflater); itemBinding.button.setText(item); itemBinding.button.setOnClickListener(view -> { Bundle bundle = new Bundle(); - bundle.putString("tab", item); + bundle.putString(DynamicFragment.BUNDLE_GROUP, item); NavigationUtil.navigate(this, DynamicFragment.class, bundle); }); itemBinding.button.setOnLongClickListener(view -> { diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/QRScannerActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/scanner/QRScannerActivity.java similarity index 96% rename from app/src/main/java/com/cringe_studios/cringe_authenticator/QRScannerActivity.java rename to app/src/main/java/com/cringe_studios/cringe_authenticator/scanner/QRScannerActivity.java index ecd1b97..38f3083 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/QRScannerActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/scanner/QRScannerActivity.java @@ -1,4 +1,4 @@ -package com.cringe_studios.cringe_authenticator; +package com.cringe_studios.cringe_authenticator.scanner; import android.Manifest; import android.app.Activity; @@ -24,6 +24,7 @@ import androidx.camera.lifecycle.ProcessCameraProvider; import androidx.core.content.ContextCompat; import androidx.lifecycle.LifecycleOwner; +import com.cringe_studios.cringe_authenticator.OTPData; import com.cringe_studios.cringe_authenticator.databinding.ActivityQrScannerBinding; import com.cringe_studios.cringe_authenticator_library.OTP; import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm; @@ -120,6 +121,8 @@ public class QRScannerActivity extends AppCompatActivity { } } + if(code == null) return; + Uri uri = Uri.parse(code.getRawValue()); Log.i("AMOGUS", code.getRawValue()); Log.i("AMOGUS", uri.getHost()); @@ -180,7 +183,7 @@ public class QRScannerActivity extends AppCompatActivity { } } - private void success(OTPData data) { + private void success(@NonNull OTPData data) { Intent result = new Intent(); result.putExtra("data", data); setResult(Activity.RESULT_OK, result); @@ -188,8 +191,7 @@ public class QRScannerActivity extends AppCompatActivity { } private void error(String errorMessage) { - Intent result = new Intent(); - setResult(Activity.RESULT_CANCELED, result); + setResult(Activity.RESULT_CANCELED, null); finish(); } diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/QRScannerContract.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/scanner/QRScannerContract.java similarity index 77% rename from app/src/main/java/com/cringe_studios/cringe_authenticator/QRScannerContract.java rename to app/src/main/java/com/cringe_studios/cringe_authenticator/scanner/QRScannerContract.java index 71e741f..2645112 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/QRScannerContract.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/scanner/QRScannerContract.java @@ -1,4 +1,4 @@ -package com.cringe_studios.cringe_authenticator; +package com.cringe_studios.cringe_authenticator.scanner; import android.app.Activity; import android.content.Context; @@ -8,6 +8,8 @@ import androidx.activity.result.contract.ActivityResultContract; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import com.cringe_studios.cringe_authenticator.OTPData; + public class QRScannerContract extends ActivityResultContract { @NonNull @@ -17,7 +19,7 @@ public class QRScannerContract extends ActivityResultContract { } @Override - public OTPData parseResult(int result, @Nullable Intent intent) { + public @Nullable OTPData parseResult(int result, @Nullable Intent intent) { if(result != Activity.RESULT_OK || intent == null) { return null; } 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 2f2dad9..4d32e90 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 @@ -2,6 +2,8 @@ package com.cringe_studios.cringe_authenticator.util; import android.content.SharedPreferences; +import androidx.annotation.NonNull; + import com.cringe_studios.cringe_authenticator.OTPData; import com.google.gson.Gson; @@ -18,7 +20,7 @@ public class SettingsUtil { return Arrays.asList(GSON.fromJson(currentOTPs, OTPData[].class)); } - public static void addOTP(SharedPreferences prefs, String group, OTPData data) { + public static void addOTP(SharedPreferences prefs, String group, @NonNull OTPData data) { List otps = new ArrayList<>(getOTPs(prefs, group)); otps.add(data); diff --git a/app/src/main/res/layout/authenticate_totp.xml b/app/src/main/res/layout/otp_code.xml similarity index 89% rename from app/src/main/res/layout/authenticate_totp.xml rename to app/src/main/res/layout/otp_code.xml index ea28f90..a592a06 100644 --- a/app/src/main/res/layout/authenticate_totp.xml +++ b/app/src/main/res/layout/otp_code.xml @@ -11,15 +11,15 @@ android:padding="5dp" android:orientation="vertical">