d497adf2401b6b4c4d8d21f59e194609eac5c4a6
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.WatchfaceComplication / Tizen.Applications / ComplicationData.cs
1 /*
2  * Copyright (c) 2018 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 using System;
17 using System.Collections.Generic;
18 using System.Text;
19
20 namespace Tizen.Applications.WatchfaceComplication
21 {
22     /// <summary>
23     /// Represents the Complication data class.
24     /// </summary>
25     /// <since_tizen> 6 </since_tizen>
26     public abstract class ComplicationData
27     {
28         private long _timestamp;
29         private string _imagePath;
30         private double _currentValue;
31         private double _minValue;
32         private double _maxValue;
33         private string _iconPath;
34         private string _longText;
35         private string _shortText;
36         private string _extraData;
37         private string _screenReaderText;
38         private string _title;
39         private ComplicationTypes _type;
40         private const string LogTag = "WatchfaceComplication";
41
42         internal string ShortText
43         {
44             get
45             {
46                 return _shortText;
47             }
48             set
49             {
50                 _shortText = value;
51             }
52         }
53
54         internal string LongText
55         {
56             get
57             {
58                 return _longText;
59             }
60             set
61             {
62                 _longText = value;
63             }
64         }
65
66         internal string IconPath
67         {
68             get
69             {
70                 return _iconPath;
71             }
72             set
73             {
74                 _iconPath = value;
75             }
76         }
77
78         internal string ImagePath
79         {
80             get
81             {
82                 return _imagePath;
83             }
84             set
85             {
86                 _imagePath = value;
87             }
88         }
89
90         internal string Title
91         {
92             get
93             {
94                 return _title;
95             }
96             set
97             {
98                 _title = value;
99             }
100         }
101
102         internal string ExtraData
103         {
104             get
105             {
106                 return _extraData;
107             }
108             set
109             {
110                 _extraData = value;
111             }
112         }
113
114         internal string ScreenReaderText
115         {
116             get
117             {
118                 return _screenReaderText;
119             }
120             set
121             {
122                 _screenReaderText = value;
123             }
124         }
125
126         internal long Timestamp
127         {
128             get
129             {
130                 return _timestamp;
131             }
132             set
133             {
134                 _timestamp = value;
135             }
136         }
137
138         internal double RangeCurrent
139         {
140             get
141             {
142                 return _currentValue;
143             }
144             set
145             {
146                 _currentValue = value;
147             }
148         }
149
150         internal double RangeMin
151         {
152             get
153             {
154                 return _minValue;
155             }
156             set
157             {
158                 _minValue = value;
159             }
160         }
161
162         internal double RangeMax
163         {
164             get
165             {
166                 return _maxValue;
167             }
168             set
169             {
170                 _maxValue = value;
171             }
172         }
173
174         internal ComplicationTypes Type
175         {
176             get
177             {
178                 return _type;
179             }
180             set
181             {
182                 _type = value;
183             }
184         }
185
186         private bool IsDataValid(IntPtr sharedData)
187         {
188             bool isValid = false;
189             ComplicationError err = Interop.WatchfaceComplication.ProviderSharedDataIsValid(sharedData, out isValid);
190             if (err != ComplicationError.None)
191                 ErrorFactory.ThrowException(err, "fail to check shared data validation");
192             return isValid;
193         }
194
195         internal ComplicationError UpdateSharedData(IntPtr sharedData)
196         {
197             ComplicationError err = ComplicationError.None;
198             switch (_type)
199             {
200                 case ComplicationTypes.ShortText:
201                     err = Interop.WatchfaceComplication.ProviderSetShortText(sharedData, _shortText);
202                     Interop.WatchfaceComplication.ProviderSetIconPath(sharedData, _iconPath);
203                     Interop.WatchfaceComplication.ProviderSetTitle(sharedData, _title);
204                     Interop.WatchfaceComplication.ProviderSetExtraData(sharedData, _extraData);
205                     break;
206                 case ComplicationTypes.LongText:
207                     err = Interop.WatchfaceComplication.ProviderSetLongText(sharedData, _longText);
208                     Interop.WatchfaceComplication.ProviderSetIconPath(sharedData, _iconPath);
209                     Interop.WatchfaceComplication.ProviderSetTitle(sharedData, _title);
210                     Interop.WatchfaceComplication.ProviderSetExtraData(sharedData, _extraData);
211                     break;
212                 case ComplicationTypes.RangedValue:
213                     Interop.WatchfaceComplication.ProviderSetLongText(sharedData, _shortText);
214                     Interop.WatchfaceComplication.ProviderSetIconPath(sharedData, _iconPath);
215                     Interop.WatchfaceComplication.ProviderSetTitle(sharedData, _title);
216                     err = Interop.WatchfaceComplication.ProviderSetRangedValue(sharedData, _currentValue, _minValue, _maxValue);
217                     Interop.WatchfaceComplication.ProviderSetExtraData(sharedData, _extraData);
218                     break;
219                 case ComplicationTypes.Time:
220                     err = Interop.WatchfaceComplication.ProviderSetTimestamp(sharedData, _timestamp);
221                     Interop.WatchfaceComplication.ProviderSetIconPath(sharedData, _iconPath);
222                     Interop.WatchfaceComplication.ProviderSetExtraData(sharedData, _extraData);
223                     break;
224                 case ComplicationTypes.Icon:
225                     err = Interop.WatchfaceComplication.ProviderSetIconPath(sharedData, _iconPath);
226                     Interop.WatchfaceComplication.ProviderSetExtraData(sharedData, _extraData);
227                     break;
228                 case ComplicationTypes.Image:
229                     err = Interop.WatchfaceComplication.ProviderSetImagePath(sharedData, _imagePath);
230                     Interop.WatchfaceComplication.ProviderSetExtraData(sharedData, _extraData);
231                     break;
232             }
233             Interop.WatchfaceComplication.ProviderSetScreenReaderText(sharedData, _screenReaderText);
234             try
235             {
236                 if (!IsDataValid(sharedData))
237                     return ComplicationError.IO;
238             }
239             catch (Exception ex)
240             {
241                 Log.Error(LogTag, "valid check fail : " + ex);
242                 return ComplicationError.IO;
243             }
244
245             return err;
246         }
247     }
248 }