Update some theme stuff, Add more error checking

This commit is contained in:
MrLetsplay 2023-06-24 16:44:48 +02:00
parent 553d04df8e
commit 9fecf18a8e
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
16 changed files with 109 additions and 157 deletions

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="R38N50464FV" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-06-23T14:25:11.126839955Z" />
</component>
</project>

View File

@ -33,10 +33,10 @@
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.CringeAuthenticator">
android:theme="@style/Theme.CringeAuthenticator.None">
</activity>
<activity android:name=".scanner.QRScannerActivity"
android:theme="@style/Theme.CringeAuthenticator">
android:theme="@style/Theme.CringeAuthenticator.None">
</activity>
</application>

View File

@ -52,6 +52,9 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: load configured theme
setTheme(R.style.Theme_CringeAuthenticator_Blue_Green);
Executor executor = ContextCompat.getMainExecutor(this);
BiometricPrompt prompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {
@Override
@ -140,7 +143,6 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onBackPressed() {
Log.i("AMOGUS", "navigateUp");
if(!(NavigationUtil.getCurrentFragment(this) instanceof HomeFragment)) {
NavigationUtil.navigate(this, HomeFragment.class, null);
}
@ -184,7 +186,7 @@ public class MainActivity extends AppCompatActivity {
showHOTPDialog();
break;
}
showTOTPDialog();
dialog.dismiss();
});
@ -197,6 +199,27 @@ public class MainActivity extends AppCompatActivity {
binding.inputDigits.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new Integer[]{6, 7, 8, 9, 10, 11, 12}));
showCodeDialog(binding.getRoot(), () -> {
// TODO: handle input
Fragment fragment = NavigationUtil.getCurrentFragment(this);
if(!(fragment instanceof DynamicFragment)) return;
try {
String name = binding.inputName.getText().toString();
String secret = binding.inputSecret.getText().toString();
OTPAlgorithm algorithm = (OTPAlgorithm) binding.inputAlgorithm.getSelectedItem();
int digits = (int) binding.inputDigits.getSelectedItem();
int period = Integer.parseInt(binding.inputPeriod.getText().toString());
OTPData data = new OTPData(name, OTPType.TOTP, secret, algorithm, digits, period, 0);
if(!data.validate()) {
// TODO: error
return;
}
((DynamicFragment) fragment).addOTP(data);
}catch(NumberFormatException e) {
// TODO: error
return;
}
});
}

View File

@ -4,6 +4,7 @@ import androidx.annotation.NonNull;
import com.cringe_studios.cringe_authenticator_library.OTP;
import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm;
import com.cringe_studios.cringe_authenticator_library.OTPException;
import com.cringe_studios.cringe_authenticator_library.OTPType;
import java.io.Serializable;
@ -17,12 +18,12 @@ public class OTPData implements Serializable {
private OTPAlgorithm algorithm;
private int digits;
private int period;
private int counter;
private long counter;
// Cached
private OTP otp;
private transient OTP otp;
public OTPData(String name, OTPType type, String secret, OTPAlgorithm algorithm, int digits, int period, int counter) {
public OTPData(String name, OTPType type, String secret, OTPAlgorithm algorithm, int digits, int period, long counter) {
this.name = name;
this.type = type;
this.secret = secret;
@ -56,11 +57,29 @@ public class OTPData implements Serializable {
return period;
}
public int getCounter() {
public long getCounter() {
return counter;
}
public OTP toOTP() {
public String getPin() {
return getOTP().getPin();
}
public void incrementCounter() {
getOTP().incrementCounter();
this.counter = getOTP().getCounter();
}
public boolean validate() {
try {
getOTP();
return true;
}catch(IllegalArgumentException | OTPException e) {
return false;
}
}
private OTP getOTP() {
// TODO: checksum
if(otp != null) return otp;
return otp = OTP.createNewOTP(type, secret, algorithm, digits, counter, period, false);

View File

@ -60,7 +60,7 @@ public class DynamicFragment extends Fragment {
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().toOTP().getPin());
vh.getBinding().otpCode.setText(vh.getOTPData().getPin());
}
handler.postDelayed(refreshCodes, 1000L);

View File

@ -31,7 +31,7 @@ public class MenuFragment extends Fragment {
String[] items = {"a", "b"};
for(String item : items) {
MenuItemBinding itemBinding = MenuItemBinding.inflate(inflater);
MenuItemBinding itemBinding = MenuItemBinding.inflate(inflater, binding.menuItems, false);
itemBinding.button.setText(item);
itemBinding.button.setOnClickListener(view -> {
Bundle bundle = new Bundle();

View File

@ -1,14 +1,18 @@
package com.cringe_studios.cringe_authenticator.otplist;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.cringe_studios.cringe_authenticator.OTPData;
import com.cringe_studios.cringe_authenticator.databinding.OtpCodeBinding;
import com.cringe_studios.cringe_authenticator_library.OTPType;
import java.util.ArrayList;
import java.util.List;
@ -19,9 +23,12 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
private List<OTPData> items;
private Handler handler;
public OTPListAdapter(Context context) {
this.inflater = LayoutInflater.from(context);
this.items = new ArrayList<>();
this.handler = new Handler(Looper.getMainLooper());
}
@NonNull
@ -33,8 +40,20 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
@Override
public void onBindViewHolder(@NonNull OTPListItem holder, int position) {
holder.setOTPData(items.get(position));
OTPData data = items.get(position);
holder.setOTPData(data);
holder.getBinding().label.setText(holder.getOTPData().getName());
holder.getBinding().getRoot().setOnClickListener(view -> {
if(data.getType() != OTPType.HOTP) return;
// Click delay for HOTP
view.setClickable(false);
Toast.makeText(view.getContext(), "Generated new code", Toast.LENGTH_LONG).show();
data.incrementCounter();
handler.postDelayed(() -> view.setClickable(true), 5000);
});
}
@Override

View File

@ -1,39 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="0dp"
android:color="#FFFFFF" />
<gradient
android:angle="180"
android:endColor="#FF7700"
android:startColor="#00FFF7" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="0dp"
android:color="#FFFFFF" />
<gradient
android:angle="180"
android:endColor="?attr/colorOnBackground"
android:startColor="?attr/colorOnBackground" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
</item>
</layer-list>

View File

@ -1,39 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="0dp"
android:color="#FFFFFF" />
<gradient
android:angle="180"
android:endColor="#D900FF"
android:startColor="#89FF00" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="0dp"
android:color="#FFFFFF" />
<gradient
android:angle="180"
android:endColor="?attr/colorOnBackground"
android:startColor="?attr/colorOnBackground" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
</item>
</layer-list>

View File

@ -1,39 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="0dp"
android:color="#FFFFFF" />
<gradient
android:angle="180"
android:endColor="@color/colorPrimary"
android:startColor="@color/colorSecondary" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="0dp"
android:color="#FFFFFF" />
<gradient
android:angle="180"
android:endColor="?attr/colorOnBackground"
android:startColor="?attr/colorOnBackground" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
</item>
</layer-list>

View File

@ -1,13 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp"
android:background="@drawable/button_themed" >
<Button
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:background="@drawable/button_theme_red_blue"/>
android:background="@drawable/button_themed" />
</LinearLayout>

View File

@ -3,7 +3,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/rectangle"
android:background="@drawable/button_themed"
android:layout_margin="5dp"
android:padding="5dp">
<TextView

View File

@ -1,7 +1,8 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.CringeAuthenticator" parent="Theme.Material3.DayNight.NoActionBar">
<style name="Base.Theme.CringeAuthenticator" parent="Base.Theme.CringeAuthenticator_Base">
<!-- Customize your dark theme here. -->
<!-- <item name="colorPrimary">@color/my_dark_primary</item> -->
<item name="android:textColor">@color/white</item>
<item name="colorOnBackground">#393740</item>
</style>
</resources>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="colorTheme1" format="color" />
<attr name="colorTheme2" format="color" />
</resources>

View File

@ -4,4 +4,8 @@
<color name="white">#FFFFFFFF</color>
<color name="colorPrimary">#008BFF</color>
<color name="colorSecondary">#90D14C</color>
<!-- Theme colors -->
<color name="color_blue_green_1">#008BFF</color>
<color name="color_blue_green_2">#90D14C</color>
</resources>

View File

@ -1,9 +1,21 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.CringeAuthenticator" parent="Theme.Material3.DayNight.NoActionBar">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
<style name="Base.Theme.CringeAuthenticator_Base" parent="Theme.Material3.DayNight.NoActionBar">
<item name="colorTheme1">#FF00FF</item>
<item name="colorTheme2">#000000</item>
</style>
<style name="Theme.CringeAuthenticator" parent="Base.Theme.CringeAuthenticator" />
<style name="Base.Theme.CringeAuthenticator" parent="Base.Theme.CringeAuthenticator_Base">
<!--<item name="android:textColor">@color/black</item>
<item name="colorOnBackground">@color/white</item>-->
<!--<item name="colorTheme1">@color/color_blue_green_1</item>
<item name="colorTheme2">@color/color_blue_green_2</item>-->
</style>
<style name="Theme.CringeAuthenticator.None" parent="Base.Theme.CringeAuthenticator" />
<style name="Theme.CringeAuthenticator.Blue_Green" parent="Base.Theme.CringeAuthenticator">
<item name="colorTheme1">@color/color_blue_green_1</item>
<item name="colorTheme2">@color/color_blue_green_2</item>
</style>
</resources>