Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Mtp / Tizen.Network.Mtp / MtpManagerImpl.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 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
21
22 namespace Tizen.Network.Mtp
23 {
24     static internal class Globals
25     {
26         internal const string LogTag = "Tizen.Network.Mtp";
27     }
28
29     internal partial class MtpManagerImpl : IDisposable
30     {
31         private static readonly MtpManagerImpl _instance = new MtpManagerImpl();
32         private List<MtpDevice> _deviceList = new List<MtpDevice>();
33         private bool disposed = false;
34
35         internal static MtpManagerImpl Instance
36         {
37             get
38             {
39                 return _instance;
40             }
41         }
42
43         private MtpManagerImpl()
44         {
45             Initialize();
46         }
47
48         ~MtpManagerImpl()
49         {
50             Dispose(false);
51         }
52
53         public void Dispose()
54         {
55             Dispose(true);
56             GC.SuppressFinalize(this);
57         }
58
59         private void Dispose(bool disposing)
60         {
61             if (disposed)
62                 return;
63
64             if (disposing)
65             {
66                 // Free managed objects.
67             }
68             //Free unmanaged objects
69             Deinitialize();
70             disposed = true;
71         }
72
73         private void Initialize()
74         {
75             int ret = Interop.Mtp.Initialize();
76             if (ret != (int)MtpError.None)
77             {
78                 Log.Error(Globals.LogTag, "Failed to Initialize Mtp, Error - " + (MtpError)ret);
79                 MtpErrorFactory.ThrowMtpException(ret);
80             }
81         }
82
83         private void Deinitialize()
84         {
85             int ret = Interop.Mtp.Deinitialize();
86             if (ret != (int)MtpError.None)
87             {
88                 Log.Error(Globals.LogTag, "Failed to Deinitialize Mtp, Error - " + (MtpError)ret);
89             }
90         }
91
92         internal IEnumerable<MtpDevice> GetDevices()
93         {
94             IntPtr devicePtr;
95             int count = 0;
96
97             int ret = Interop.Mtp.GetDevices(out devicePtr, out count);
98             if (ret != (int)MtpError.None)
99             {
100                 Log.Error(Globals.LogTag, "Failed to get device list, Error - " + (MtpError)ret);
101                 MtpErrorFactory.ThrowMtpException(ret);
102             }
103
104             for (int i = 0; i < count; i++)
105             {
106                 int deviceID = Marshal.ReadInt32(devicePtr);
107
108                 MtpDevice deviceItem = new MtpDevice(deviceID);
109                 _deviceList.Add(deviceItem);
110                 devicePtr += sizeof(int);
111             }
112
113             return _deviceList;
114         }
115     }
116 }