Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / Application.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18
19 namespace Tizen.Applications
20 {
21     /// <summary>
22     /// Class that represents a Tizen application.
23     /// </summary>
24     public abstract class Application : IDisposable
25     {
26         internal const string LogTag = "Tizen.Applications";
27
28         private static Application s_CurrentApplication = null;
29
30         private object _lock = new object();
31
32         private DirectoryInfo _directoryInfo;
33         private ApplicationInfo _applicationInfo;
34
35         /// <summary>
36         /// Gets the instance of current application.
37         /// </summary>
38         public static Application Current { get { return s_CurrentApplication; } }
39
40         /// <summary>
41         /// Gets the class representing directory information of current application.
42         /// </summary>
43         public DirectoryInfo DirectoryInfo
44         {
45             get
46             {
47                 lock (_lock)
48                 {
49                     if (_directoryInfo == null)
50                     {
51                         _directoryInfo = new DirectoryInfo();
52                     }
53                 }
54                 return _directoryInfo;
55             }
56         }
57
58         /// <summary>
59         /// Gets the class representing information of current application.
60         /// </summary>
61         public ApplicationInfo ApplicationInfo
62         {
63             get
64             {
65                 lock (_lock)
66                 {
67                     if (_applicationInfo == null)
68                     {
69                         string appId = string.Empty;
70                         Interop.AppCommon.AppGetId(out appId);
71                         if (!string.IsNullOrEmpty(appId))
72                         {
73                             _applicationInfo = new ApplicationInfo(appId);
74                         }
75                     }
76                 }
77                 return _applicationInfo;
78             }
79         }
80
81         /// <summary>
82         /// Runs the application's main loop.
83         /// </summary>
84         /// <param name="args">Arguments from commandline.</param>
85         public virtual void Run(string[] args)
86         {
87             if (args == null)
88             {
89                 throw new ArgumentNullException("args");
90             }
91             s_CurrentApplication = this;
92         }
93
94         /// <summary>
95         /// Exits the main loop of application. 
96         /// </summary>
97         public abstract void Exit();
98
99         #region IDisposable Support
100         private bool _disposedValue = false; // To detect redundant calls
101
102         /// <summary>
103         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
104         /// </summary>
105         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
106         protected virtual void Dispose(bool disposing)
107         {
108             if (!_disposedValue)
109             {
110                 if (disposing)
111                 {
112                     if (_applicationInfo != null)
113                     {
114                         _applicationInfo.Dispose();
115                     }
116                 }
117
118                 _disposedValue = true;
119             }
120         }
121
122         /// <summary>
123         /// Finalizer of the Application class.
124         /// </summary>
125         ~Application()
126         {
127             Dispose(false);
128         }
129
130         /// <summary>
131         /// Releases all resources used by the Application class.
132         /// </summary>
133         public void Dispose()
134         {
135             Dispose(true);
136             GC.SuppressFinalize(this);
137         }
138         #endregion
139     }
140 }