summaryrefslogtreecommitdiff
path: root/Tizen.NUI/src/internal/NUICoreBackend.cs
blob: 81c04671f59c7843deb573ab9c4cff35fb413c10 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

using System;
using System.Collections.Generic;

using Tizen.Applications.CoreBackend;
using Tizen.Applications;
using Tizen.NUI;

namespace Tizen.NUI
{
    class NUICoreBackend : ICoreBackend
    {
        /// <summary>
        /// The Application instance to connect event.
        /// </summary>
        protected Application _application;
        private string _stylesheet = "";
        private NUIApplication.WindowMode _windowMode = NUIApplication.WindowMode.Opaque;

        /// <summary>
        /// The Dictionary to contain each type of event callback.
        /// </summary>
        protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();

        /// <summary>
        /// The default Constructor.
        /// </summary>
        public NUICoreBackend()
        {
        }

        /// <summary>
        /// The constructor with stylesheet.
        /// </summary>
        public NUICoreBackend(string stylesheet)
        {
            _stylesheet = stylesheet;
        }

        /// <summary>
        /// The constructor with stylesheet and window mode.
        /// </summary>
        public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
        {
            _stylesheet = stylesheet;
            _windowMode = windowMode;
        }

        /// <summary>
        /// Adds NUIApplication event to Application.
        /// Puts each type of event callback in Dictionary.
        /// </summary>
        /// <param name="evType">The type of event.</param>
        /// <param name="handler">The event callback.</param>
        public void AddEventHandler(EventType evType, Action handler)
        {
            Handlers.Add(evType, handler);
        }

        /// <summary>
        /// Adds NUIApplication event to Application.
        /// Puts each type of event callback in Dictionary.
        /// </summary>
        /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
        /// <param name="evType">The type of event.</param>
        /// <param name="handler">The event callback.</param>
        public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
        {
            Handlers.Add(evType, handler);
        }


        /// <summary>
        /// The Dispose function.
        /// </summary>
        public void Dispose()
        {
            if(_application != null)
            {
                _application.Dispose();
            }
        }

        /// <summary>
        /// The Exit application.
        /// </summary>
        public void Exit()
        {
            if(_application != null)
            {
                _application.Quit();
            }
        }

        /// <summary>
        /// Ensures that the function passed in is called from the main loop when it is idle.
        /// </summary>
        /// <param name="func">The function to call</param>
        /// <returns>true if added successfully, false otherwise</returns>
        public bool AddIdle(System.Delegate func)
        {
            return _application.AddIdle(func);
        }

        /// <summary>
        /// The Run application.
        /// </summary>
        /// <param name="args">The arguments from commandline.</param>
        public void Run(string[] args)
        {
            TizenSynchronizationContext.Initialize();

            args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
            if (args.Length == 1)
            {
                _application = Application.NewApplication();
            }
            else if (args.Length > 1)
            {
                _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode);
            }

            _application.BatteryLow += OnBatteryLow;
            _application.LanguageChanged += OnLanguageChanged;
            _application.MemoryLow += OnMemoryLow;
            _application.RegionChanged += OnRegionChanged;

            _application.Initialized += OnInitialized;
            _application.Resumed += OnResumed;
            _application.Terminating += OnTerminated;
            _application.Paused += OnPaused;
            _application.AppControl += OnAppControl;

            _application.MainLoop();
        }

        /// <summary>
        /// The Region changed event callback function.
        /// </summary>
        /// <param name="source">The application instance.</param>
        /// <param name="e">The event argument for RegionChanged.</param>
        private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
        {
            Log.Debug("NUI", "NUICorebackend OnRegionChanged Called");
            var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
            // Need to make new signal return in native to return right value.
            handler?.Invoke( new RegionFormatChangedEventArgs(e.Application.GetRegion()));
        }

        /// <summary>
        /// The Memory Low event callback function.
        /// </summary>
        /// <param name="source">The application instance.</param>
        /// <param name="e">The event argument for MemoryLow.</param>
        private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
        {
            Log.Debug("NUI", "NUICorebackend OnMemoryLow Called");
            var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
            // Need to make new signal return in native to return right value.
            switch( e.MemoryStatus )
            {
                case Application.MemoryStatus.Normal:
                {
                    handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
                    break;
                }
                case Application.MemoryStatus.SoftWarning:
                {
                    handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
                    break;
                }
                case Application.MemoryStatus.HardWarning:
                {
                    handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
                    break;
                }
            }
        }

        /// <summary>
        /// The Language changed event callback function.
        /// </summary>
        /// <param name="source">The application instance.</param>
        /// <param name="e">The event argument for LanguageChanged.</param>
        private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
        {
            Log.Debug("NUI", "NUICorebackend OnLanguageChanged Called");
            var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
            // Need to make new signal return in native to return right value.
            handler?.Invoke( new LocaleChangedEventArgs(e.Application.GetLanguage()));
        }

        /// <summary>
        /// The Battery Low event callback function.
        /// </summary>
        /// <param name="source">The application instance.</param>
        /// <param name="e">The event argument for BatteryLow.</param>
        private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
        {
            Log.Debug("NUI", "NUICorebackend OnBatteryLow Called");
            var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
            // Need to make new signal return in native to return right value.
            switch( e.BatteryStatus )
            {
                case Application.BatteryStatus.Normal:
                {
                    handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
                    break;
                }
                case Application.BatteryStatus.CriticalLow:
                {
                    handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
                    break;
                }
                case Application.BatteryStatus.PowerOff:
                {
                    handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
                    break;
                }
            }
        }

        /// <summary>
        /// The Initialized event callback function.
        /// </summary>
        /// <param name="source">The application instance.</param>
        /// <param name="e">The event argument for Initialized.</param>
        private void OnInitialized(object source, NUIApplicationInitEventArgs e)
        {
            Log.Debug("NUI", "NUICorebackend OnPreCreated Called");
            var preCreateHandler = Handlers[EventType.PreCreated] as Action;
            preCreateHandler?.Invoke();

            Log.Debug("NUI", "NUICorebackend OnCreate Called");
            var createHandler = Handlers[EventType.Created] as Action;
            createHandler?.Invoke();
        }

        /// <summary>
        /// The Terminated event callback function.
        /// </summary>
        /// <param name="source">The application instance.</param>
        /// <param name="e">The event argument for Terminated.</param>
        private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
        {
            Log.Debug("NUI", "NUICorebackend OnTerminated Called");
            var handler = Handlers[EventType.Terminated] as Action;
            handler?.Invoke();
        }

        /// <summary>
        /// The Resumed event callback function.
        /// </summary>
        /// <param name="source">The application instance.</param>
        /// <param name="e">The event argument for Resumed.</param>
        private void OnResumed(object source, NUIApplicationResumedEventArgs e)
        {
            Log.Debug("NUI", "NUICorebackend OnResumed Called");
            var handler = Handlers[EventType.Resumed] as Action;
            handler?.Invoke();
        }

        /// <summary>
        /// The App control event callback function.
        /// </summary>
        /// <param name="source">The application instance.</param>
        /// <param name="e">The event argument for AppControl.</param>
        private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
        {
            Log.Debug("NUI", "NUICorebackend OnAppControl Called");
            var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
            SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false);
            handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) );
        }

        /// <summary>
        /// The Paused event callback function.
        /// </summary>
        /// <param name="source">The application instance.</param>
        /// <param name="e">The event argument for Paused.</param>
        private void OnPaused(object source, NUIApplicationPausedEventArgs e)
        {
            Log.Debug("NUI", "NUICorebackend OnPaused Called");
            var handler = Handlers[EventType.Paused] as Action;
            handler?.Invoke();
        }


        internal Application ApplicationHandle
        {
            get
            {
                return _application;
            }
        }

    }
}