Added theme

This commit is contained in:
Maurice 2024-06-19 19:20:50 +02:00
parent 4f21ecb859
commit dceb5b49d8
6 changed files with 77 additions and 56 deletions

View File

@ -7,37 +7,7 @@ using Java.Lang;
namespace DemoAndroid; namespace DemoAndroid;
class BiometricCallback : BiometricPrompt.AuthenticationCallback, IDialogInterfaceOnClickListener [Activity(Label = "WakeOnLanActivity", MainLauncher = true, Theme = "@style/NoActionBar")]
{
private Context _context;
public BiometricCallback(Context context)
{
_context = context;
}
public override void OnAuthenticationError(BiometricErrorCode errorCode, ICharSequence? errString)
{
Toast.MakeText(_context, $"Authentication error: {errorCode}, {errString?.ToString()}", ToastLength.Long).Show();
}
public override void OnAuthenticationSucceeded(BiometricPrompt.AuthenticationResult? result)
{
Toast.MakeText(_context, "Authentication succeeded!", ToastLength.Long).Show();
}
public override void OnAuthenticationFailed()
{
Toast.MakeText(_context, "Authentication failed!", ToastLength.Long).Show();
}
public void OnClick(IDialogInterface? dialog, int which)
{
Toast.MakeText(_context, "Cancel clicked!", ToastLength.Long).Show();
}
}
[Activity(Label = "WakeOnLanActivity")]
public class WakeOnLanActivity : Activity public class WakeOnLanActivity : Activity
{ {
private const int BiometricStrong = 0x0000000f; private const int BiometricStrong = 0x0000000f;
@ -56,30 +26,34 @@ public class WakeOnLanActivity : Activity
var button = FindViewById<Button>(Resource.Id.btnWakeOnLan); var button = FindViewById<Button>(Resource.Id.btnWakeOnLan);
button.Click += (sender, args) => button.Click += (sender, args) =>
{ {
if (!_authenticated) OnlyAuthenticated(WakePc);
{
Authenticate();
return;
}
WakePc();
}; };
} }
private void Authenticate() private void OnlyAuthenticated(Action action)
{ {
var callback = new BiometricCallback(this); if (!_authenticated)
{
var callback = new BiometricCallback(this, () =>
{
_authenticated = true;
action();
});
var prompt = new BiometricPrompt.Builder(this) var prompt = new BiometricPrompt.Builder(this)
.SetNegativeButton("Negative", MainExecutor, callback) .SetNegativeButton("Annuleren", MainExecutor, callback)
.SetTitle("Bewijs jezelf met je vingerafdruk") .SetTitle("Bewijs jezelf met je vingerafdruk")
.SetSubtitle("We willen zeker weten dat je niet Colinde bent") .SetSubtitle("We willen zeker weten dat je niet Colinde bent")
.SetAllowedAuthenticators(BiometricStrong) .SetAllowedAuthenticators(BiometricStrong)
.Build(); .Build();
var cancellation = new CancellationSignal(); var cancellation = new CancellationSignal();
prompt.Authenticate(cancellation, MainExecutor, callback); prompt.Authenticate(cancellation, MainExecutor, callback);
return;
}
action();
} }
private void WakePc() private void WakePc()
@ -112,6 +86,43 @@ public class WakeOnLanActivity : Activity
client.Send(magicPacket, magicPacket.Length, endpoint); client.Send(magicPacket, magicPacket.Length, endpoint);
} }
Toast.MakeText(this, "Computer started!", ToastLength.Long).Show(); Toast.MakeText(this, "Computer started!", ToastLength.Long)!.Show();
}
}
/// <summary>
/// Biometric authentication callbacks
/// https://developer.android.com/identity/sign-in/biometric-auth#java
/// </summary>
class BiometricCallback : BiometricPrompt.AuthenticationCallback, IDialogInterfaceOnClickListener
{
private Context _context;
private readonly Action _onSuccess;
public BiometricCallback(Context context, Action onSuccess)
{
_context = context;
_onSuccess = onSuccess;
}
public override void OnAuthenticationError(BiometricErrorCode errorCode, ICharSequence? errString)
{
Toast.MakeText(_context, $"Authenticatie mislukt: {errorCode}, {errString?.ToString()}", ToastLength.Long)!.Show();
}
public override void OnAuthenticationSucceeded(BiometricPrompt.AuthenticationResult? result)
{
Toast.MakeText(_context, "Authentication succeeded!", ToastLength.Short)!.Show();
_onSuccess();
}
public override void OnAuthenticationFailed()
{
Toast.MakeText(_context, "Authenticatie mislukt!", ToastLength.Long)!.Show();
}
public void OnClick(IDialogInterface? dialog, int which)
{
Toast.MakeText(_context, "Geannuleerd!", ToastLength.Long)!.Show();
} }
} }

View File

@ -4,7 +4,7 @@ using QRCoder; // QRCoder-ImageSharp else it crashes
namespace DemoAndroid; namespace DemoAndroid;
[Activity(Label = "@string/app_name", MainLauncher = true)] [Activity(Label = "@string/app_name", MainLauncher = false)]
public class MainActivity : Activity public class MainActivity : Activity
{ {
private string _text = ""; private string _text = "";

View File

@ -2,18 +2,19 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:layout_margin="5pt">
<TextView android:layout_width="match_parent" <TextView android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Start PC by tapping button below" /> android:text="Start de computer met Wake-On-Lan door op de knop hieronder te klikken" />
<Button android:id="@+id/btnWakeOnLan" <Button android:id="@+id/btnWakeOnLan"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Start PC" android:text="Start de computer"
android:textColor="@color/white" android:textColor="@color/white"
android:background="@color/blue" android:background="@color/green"
android:height="15pt" android:height="20pt"
android:layout_margin="10pt"/> android:layout_margin="10pt"/>
</LinearLayout> </LinearLayout>

View File

@ -2,4 +2,5 @@
<resources> <resources>
<color name="blue">#4287f5</color> <color name="blue">#4287f5</color>
<color name="white">#FFFFFF</color> <color name="white">#FFFFFF</color>
<color name="green">#2abf39</color>
</resources> </resources>

View File

@ -1,4 +1,4 @@
<resources> <resources>
<string name="app_name">DemoAndroid</string> <string name="app_name">Start computer</string>
<string name="app_text">Hello, Android!</string> <string name="app_text">Hello, Android!</string>
</resources> </resources>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NoActionBar" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>