-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurrentActivity.cs
105 lines (93 loc) · 4.27 KB
/
CurrentActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//-----------------------------------------------------------------------
// <copyright file="CurrentActivity.cs" company="In The Hand Ltd">
// Copyright (c) 2023 In The Hand Ltd, All rights reserved.
// This source code is licensed under the MIT License
// </copyright>
//-----------------------------------------------------------------------
using Android.App;
using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
namespace InTheHand
{
/// <summary>
/// Provides a central point to query the current Android activity.
/// </summary>
public static class AndroidActivity
{
private static ActivityLifecycleCallbacks _callbacks = new ActivityLifecycleCallbacks();
static AndroidActivity()
{
((Application)Application.Context).RegisterActivityLifecycleCallbacks(_callbacks);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public static void Init()
{
}
private static void TryGetActivity()
{
// when used by a cross-platform UI framework like MAUI or Uno we need to get the current Activity in order to launch the picker UI
// for a "native" app you can use the Android specific RequestDevice overload which accepts a Context
// This is a backup if Init wasn't passed an Activity or lifecycle callbacks didn't pickup the activity.
#if NET6_0_OR_GREATER
// check for Uno without taking a hard dependency
var t = Type.GetType("Uno.UI.ContextHelper, Uno, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null", false, true);
if (t != null)
{
CurrentActivity = (Activity)t.GetProperty("Current", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).GetValue(null);
}
else
{
// try Maui Essentials if not
t = Type.GetType("Microsoft.Maui.ApplicationModel.Platform, Microsoft.Maui.Essentials, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", false, true);
if (t != null)
{
CurrentActivity = (Activity)t.GetProperty("CurrentActivity", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).GetValue(null);
}
}
#else
// check for Xamarin.Essentials without taking a hard dependency
var t = Type.GetType("Xamarin.Essentials.Platform, Xamarin.Essentials, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", false, true);
if (t != null)
{
try
{
var task = (Task<Activity>)t.GetMethod("WaitForActivityAsync", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).Invoke(null, new object[] { null});
task.Wait();
CurrentActivity = (Activity)t.GetProperty("CurrentActivity", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).GetValue(null);
}
catch { }
}
#endif
if (currentActivity == null)
System.Diagnostics.Debug.WriteLine("CurrentActivity:Unknown");
else
System.Diagnostics.Debug.WriteLine($"CurrentActivity:{CurrentActivity.GetType().FullName}");
}
private static Activity currentActivity;
/// <summary>
/// The current Android activity, or null if not set.
/// </summary>
/// <remarks>
/// Set this property from your MainActivity OnCreate method to ensure that all libraries which depend on a reference to the activity will work as expected.
/// </remarks>
/// <value>The main activity for your application. This will be the MainActivity in your Xamarin/.NET application.</value>
public static Activity CurrentActivity
{
get
{
if (currentActivity == null)
TryGetActivity();
return currentActivity;
}
set
{
if (value != null && currentActivity == null)
{
currentActivity = value;
}
}
}
}
}