Refactor AppFW
[platform/core/csapi/tizenfx.git] / Tizen.Applications / Tizen.Applications / Application.cs
1 /// Copyright 2016 by Samsung Electronics, Inc.,
2 ///
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
8
9
10 using System;
11
12 namespace Tizen.Applications
13 {
14     /// <summary>
15     /// The Application handles an application state change or system events and provides mechanisms that launch other applications.
16     /// </summary>
17     public abstract class Application
18     {
19         private static Application s_CurrentApplication = null;
20
21         private Interop.AppEvent.SafeAppEventHandle _lowMemoryNativeHandle;
22         private Interop.AppEvent.SafeAppEventHandle _localeChangedNativeHandle;
23
24         /// <summary>
25         /// The low memory event.
26         /// </summary>
27         public event EventHandler<LowMemoryEventArgs> LowMemory;
28
29         /// <summary>
30         /// The system language changed event.
31         /// </summary>
32         public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
33
34         /// <summary>
35         /// 
36         /// </summary>
37         public static Application Current { get { return s_CurrentApplication; } }
38
39         /// <summary>
40         /// 
41         /// </summary>
42         public ApplicationInfo ApplicationInfo { get; internal set; }
43
44         /// <summary>
45         /// Runs the application's main loop.
46         /// </summary>
47         /// <param name="args"></param>
48         public virtual void Run(string[] args)
49         {
50             s_CurrentApplication = this;
51
52             Interop.AppEvent.AddEventHandler(Interop.AppEvent.EventNames.LowMemory, HandleAppEvent, IntPtr.Zero, out _lowMemoryNativeHandle);
53             Interop.AppEvent.AddEventHandler(Interop.AppEvent.EventNames.LanguageSet, HandleAppEvent, IntPtr.Zero, out _localeChangedNativeHandle);
54         }
55
56         /// <summary>
57         /// Exits the main loop of application. 
58         /// </summary>
59         public abstract void Exit();
60
61         /// <summary>
62         /// 
63         /// </summary>
64         protected virtual void OnCreate()
65         {
66         }
67
68         /// <summary>
69         /// 
70         /// </summary>
71         protected virtual void OnTerminate()
72         {
73         }
74
75         /// <summary>
76         /// 
77         /// </summary>
78         /// <param name="control"></param>
79         protected virtual void OnAppControlReceived(ReceivedAppControl control)
80         {
81         }
82
83         /// <summary>
84         /// 
85         /// </summary>
86         /// <param name="e"></param>
87         protected virtual void OnLowMemory(LowMemoryEventArgs e)
88         {
89             EventHandler<LowMemoryEventArgs> eh = LowMemory;
90             if (eh != null)
91             {
92                 eh(this, e);
93             }
94         }
95
96         /// <summary>
97         /// 
98         /// </summary>
99         /// <param name="e"></param>
100         protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
101         {
102             EventHandler<LocaleChangedEventArgs> eh = LocaleChanged;
103             if (eh != null)
104             {
105                 eh(this, e);
106             }
107         }
108
109         internal void SendCreate()
110         {
111             ApplicationInfo = new ApplicationInfo();
112             OnCreate();
113         }
114
115         private void HandleAppEvent(string eventName, IntPtr eventData, IntPtr data)
116         {
117             Console.WriteLine("HandleAppEvent!! eventName={0}, eventData={1}", eventName, eventData);
118             Bundle b = new Bundle(eventData);
119             if (eventName == Interop.AppEvent.EventNames.LowMemory)
120             {
121                 string value = b.GetItem<string>(Interop.AppEvent.EventKeys.LowMemory);
122                 LowMemoryStatus status = LowMemoryStatus.Normal;
123                 if (value == Interop.AppEvent.EventValues.MemorySoftWarning)
124                 {
125                     status = LowMemoryStatus.SoftWarning;
126                 }
127                 else if (value == Interop.AppEvent.EventValues.MemoryHardWarning)
128                 {
129                     status = LowMemoryStatus.HardWarning;
130                 }
131                 OnLowMemory(new LowMemoryEventArgs { LowMemoryStatus = status });
132             }
133             else if (eventName == Interop.AppEvent.EventNames.LanguageSet)
134             {
135                 string value = b.GetItem<string>(Interop.AppEvent.EventKeys.LanguageSet);
136                 OnLocaleChanged(new LocaleChangedEventArgs { Locale = value });
137             }
138         }
139     }
140 }