Skip to content

Commit 6f7bf2e

Browse files
authored
[Tizen.Applications][Non-ACR] Add ApplicationManager internal apis (#7420)
* [Tizen.Applications][Non-ACR] Add ApplicationManager internal apis ApplicationLifecycleStateChanged EventHandler has been added. Adds: - ApplicationLifecycleStateChanged * Add exception description Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
1 parent 321b025 commit 6f7bf2e

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ internal enum AppInfoAppComponentType
7171
WatchApp = 3
7272
}
7373

74+
internal enum AppLifecycleState
75+
{
76+
Initialized = 0,
77+
Created = 1,
78+
Resumed = 2,
79+
Paused = 3,
80+
Destroyed = 4
81+
}
82+
83+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
84+
internal delegate void AppManagerLifecycleStateChangedCallback(string appId, int pid, AppLifecycleState state, bool hasFocus, IntPtr userData);
85+
//void (*app_manager_lifecycle_state_changed_cb)(const char *app_id, pid_t pid, app_manager_lifecycle_state_e state, bool has_focus, void *user_data)
86+
7487
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
7588
internal delegate void AppManagerEventCallback(string appType, string appId, AppManagerEventType eventType, AppManagerEventState eventState, IntPtr eventHandle, IntPtr userData);
7689
//void(* app_manager_event_cb)(const char *type, const char *app_id, app_manager_event_type_e event_type, app_manager_event_state_e event_state, app_manager_event_h handle, void *user_data)
@@ -111,6 +124,14 @@ internal enum AppInfoAppComponentType
111124
internal static extern void AppManagerUnSetAppContextEvent();
112125
//void app_manager_unset_app_context_event_cb (void);
113126

127+
[DllImport(Libraries.AppManager, EntryPoint = "app_manager_set_lifecycle_state_changed_cb")]
128+
internal static extern ErrorCode AppManagerSetLifecycleStateChangedCb(AppManagerLifecycleStateChangedCallback callback, IntPtr userData);
129+
//int app_manager_set_lifecycle_state_changed_cb(app_manager_lifecycle_state_changed_cb callback, void *user_data)
130+
131+
[DllImport(Libraries.AppManager, EntryPoint = "app_manager_unset_lifecycle_state_changed_cb")]
132+
internal static extern void AppManagerUnsetLifecycleStateChangedCb();
133+
//void app_manager_unset_lifecycle_state_changed_cb(void)
134+
114135
[DllImport(Libraries.AppManager, EntryPoint = "app_manager_foreach_running_app_context")]
115136
internal static extern ErrorCode AppManagerForeachRunningAppContext(AppManagerAppContextCallback callback, IntPtr userData);
116137
//int app_manager_foreach_running_app_context(app_manager_app_context_cb callback, void *user_data)

src/Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,51 @@
2222

2323
namespace Tizen.Applications
2424
{
25+
/// <summary>
26+
/// Enumeration for application lifecycle state.
27+
/// </summary>
28+
[EditorBrowsable(EditorBrowsableState.Never)]
29+
public enum ApplicationLifecycleState
30+
{
31+
/// <summary>The application is initialized.</summary>
32+
Initialized = 0,
33+
/// <summary>The application is created.</summary>
34+
Created = 1,
35+
/// <summary>The application is resumed.</summary>
36+
Resumed = 2,
37+
/// <summary>The application is paused.</summary>
38+
Paused = 3,
39+
/// <summary>The application is destroyed.</summary>
40+
Destroyed = 4
41+
}
42+
43+
/// <summary>
44+
/// Event arguments for application lifecycle state changed event.
45+
/// </summary>
46+
[EditorBrowsable(EditorBrowsableState.Never)]
47+
public class ApplicationLifecycleStateChangedEventArgs : EventArgs
48+
{
49+
/// <summary>
50+
/// Gets the application ID.
51+
/// </summary>
52+
public string ApplicationId { get; internal set; }
53+
54+
/// <summary>
55+
/// Gets the process ID.
56+
/// </summary>
57+
public int ProcessId { get; internal set; }
58+
59+
/// <summary>
60+
/// Gets the lifecycle state.
61+
/// </summary>
62+
public ApplicationLifecycleState State { get; internal set; }
63+
64+
/// <summary>
65+
/// Gets whether the application has focus.
66+
/// </summary>
67+
public bool HasFocus { get; internal set; }
68+
}
69+
2570
/// <summary>
2671
/// This class has the methods and events of the ApplicationManager.
2772
/// </summary>
@@ -38,6 +83,9 @@ public static class ApplicationManager
3883
private static IntPtr _eventHandle = IntPtr.Zero;
3984
private static readonly object s_eventLock = new object();
4085
private static readonly object s_applicationChangedEventLock = new object();
86+
private static EventHandler<ApplicationLifecycleStateChangedEventArgs> s_lifecycleStateChangedHandler;
87+
private static Interop.ApplicationManager.AppManagerLifecycleStateChangedCallback s_lifecycleStateChangedCallback;
88+
private static readonly object s_lifecycleStateChangedLock = new object();
4189

4290
/// <summary>
4391
/// Occurs whenever the installed application is enabled.
@@ -687,6 +735,85 @@ private static void UnRegisterApplicationEvent()
687735
}
688736
}
689737

738+
/// <summary>
739+
/// Occurs when the lifecycle state of any application changes.
740+
/// </summary>
741+
/// <remarks>
742+
/// This event is raised whenever the lifecycle state of any application changes.
743+
/// It provides information about the application whose lifecycle state has changed.
744+
/// </remarks>
745+
/// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
746+
/// <exception cref="InvalidOperationException">Thrown when failed to register the lifecycle state changed event.</exception>
747+
/// <example>
748+
/// The following code snippet demonstrates how to subscribe to the ApplicationLifecycleStateChanged event:
749+
///
750+
/// <code>
751+
/// ApplicationManager.ApplicationLifecycleStateChanged += (sender, e) =>
752+
/// {
753+
/// Console.WriteLine($"Application {e.ApplicationId} (PID: {e.ProcessId}) state changed to {e.State}");
754+
/// Console.WriteLine($"Has focus: {e.HasFocus}");
755+
/// };
756+
/// </code>
757+
/// </example>
758+
/// <since_tizen> 13 </since_tizen>
759+
[EditorBrowsable(EditorBrowsableState.Never)]
760+
public static event EventHandler<ApplicationLifecycleStateChangedEventArgs> ApplicationLifecycleStateChanged
761+
{
762+
add
763+
{
764+
lock (s_lifecycleStateChangedLock)
765+
{
766+
if (s_lifecycleStateChangedCallback == null)
767+
{
768+
RegisterLifecycleStateChangedEvent();
769+
}
770+
s_lifecycleStateChangedHandler += value;
771+
}
772+
}
773+
remove
774+
{
775+
lock (s_lifecycleStateChangedLock)
776+
{
777+
s_lifecycleStateChangedHandler -= value;
778+
if (s_lifecycleStateChangedHandler == null && s_lifecycleStateChangedCallback != null)
779+
{
780+
UnRegisterLifecycleStateChangedEvent();
781+
s_lifecycleStateChangedCallback = null;
782+
}
783+
}
784+
}
785+
}
786+
787+
private static void RegisterLifecycleStateChangedEvent()
788+
{
789+
s_lifecycleStateChangedCallback = (string appId, int pid,
790+
Interop.ApplicationManager.AppLifecycleState state, bool hasFocus, IntPtr userData) =>
791+
{
792+
lock (s_lifecycleStateChangedLock)
793+
{
794+
s_lifecycleStateChangedHandler?.Invoke(null, new ApplicationLifecycleStateChangedEventArgs
795+
{
796+
ApplicationId = appId,
797+
ProcessId = pid,
798+
State = (ApplicationLifecycleState)state,
799+
HasFocus = hasFocus
800+
});
801+
}
802+
};
803+
804+
Interop.ApplicationManager.ErrorCode err =
805+
Interop.ApplicationManager.AppManagerSetLifecycleStateChangedCb(s_lifecycleStateChangedCallback, IntPtr.Zero);
806+
if (err != Interop.ApplicationManager.ErrorCode.None)
807+
{
808+
throw ApplicationManagerErrorFactory.GetException(err, "Failed to register the lifecycle state changed event.");
809+
}
810+
}
811+
812+
private static void UnRegisterLifecycleStateChangedEvent()
813+
{
814+
Interop.ApplicationManager.AppManagerUnsetLifecycleStateChangedCb();
815+
}
816+
690817
/// <summary>
691818
/// Gets the information of the recent applications.
692819
/// </summary>

0 commit comments

Comments
 (0)