Release 4.0.0-preview1-00304
[platform/core/csapi/tizenfx.git] / src / Tizen.Context / Tizen.Context.AppHistory / BatteryStatistics.cs
1 /*
2  * Copyright (c) 2017 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
20 namespace Tizen.Context.AppHistory
21 {
22     /// <summary>
23     /// This class provides APIs to query the battery consumption per application.
24     /// </summary>
25     /// <since_tizen> 4 </since_tizen>
26     public class BatteryStatistics : AppStatistics
27     {
28         private const string AppStatsConsumption = "TotalAmount";
29
30         /// <summary>
31         /// The default constructor of BatteryStatistics class.
32         /// </summary>
33         /// <since_tizen> 4 </since_tizen>
34         /// <feature>http://tizen.org/feature/app_history</feature>
35         /// <feature>http://tizen.org/feature/battery</feature>
36         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an internal error.</exception>
37         /// <exception cref="NotSupportedException">Thrown when the features are not supported.</exception>
38         public BatteryStatistics()
39         {
40             SortOrder = SortOrderType.ConsumptionMost;
41             Uri = ConvertSortOrderToString((int)SortOrder);
42
43             bool isSupported = false;
44             int error = Interop.CtxHistory.IsSupported(Uri, out isSupported);
45             if ((AppHistoryError)error != AppHistoryError.None)
46             {
47                 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri);
48             }
49
50             if (!isSupported)
51             {
52                 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.NotSupported, Uri);
53             }
54         }
55
56         /// <summary>
57         /// The constructor of BatteryStatistics class.
58         /// </summary>
59         /// <since_tizen> 4 </since_tizen>
60         /// <param name="order">The criteria of the battery statistics sorted by.</param>
61         /// <feature>http://tizen.org/feature/app_history</feature>
62         /// <feature>http://tizen.org/feature/battery</feature>
63         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
64         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an internal error.</exception>
65         /// <exception cref="NotSupportedException">Thrown when the features are not supported.</exception>
66         public BatteryStatistics(SortOrderType order)
67         {
68             SortOrder = order;
69             Uri = ConvertSortOrderToString((int)SortOrder);
70
71             if (Uri == null)
72             {
73                 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.InvalidParameter, "Invalid SortOrderType");
74             }
75
76             bool isSupported = false;
77             int error = Interop.CtxHistory.IsSupported(Uri, out isSupported);
78             if ((AppHistoryError)error != AppHistoryError.None)
79             {
80                 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri);
81             }
82
83             if (!isSupported)
84             {
85                 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.NotSupported, Uri);
86             }
87         }
88
89         /// <summary>
90         /// Retrieves a given type of battery statistics.
91         /// </summary>
92         /// <since_tizen> 4 </since_tizen>
93         /// <param name="startTime">The start time of the data to be aggregated.</param>
94         /// <param name="endTime">The end time of the data to be aggregated.</param>
95         /// <returns>Battery statistics data retrieved.</returns>
96         /// <privilege>http://tizen.org/privilege/apphistory.read</privilege>
97         /// <feature>http://tizen.org/feature/app_history</feature>
98         /// <feature>http://tizen.org/feature/battery</feature>
99         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
100         /// <exception cref="InvalidOperationException">Thrown when invalid operation occurs.</exception>
101         /// <exception cref="NotSupportedException">Thrown when the features are not supported.</exception>
102         /// <exception cref="UnauthorizedAccessException">Thrown when the application has no privilege to retrieve the application history.</exception>
103         public IReadOnlyList<BatteryStatisticsData> Query(DateTime startTime, DateTime endTime)
104         {
105             CheckTimeSpan(startTime, endTime);
106
107             return Query(startTime, endTime, DefaultResultSize);
108         }
109
110         /// <summary>
111         /// Retrieves a given type of battery statistics.
112         /// </summary>
113         /// <since_tizen> 4 </since_tizen>
114         /// <param name="startTime">The start time of the data to be aggregated.</param>
115         /// <param name="endTime">The end time of the data to be aggregated.</param>
116         /// <param name="resultSize">The number of data records to be retrieved.</param>
117         /// <returns>Battery statistics data retrieved.</returns>
118         /// <privilege>http://tizen.org/privilege/apphistory.read</privilege>
119         /// <feature>http://tizen.org/feature/app_history</feature>
120         /// <feature>http://tizen.org/feature/battery</feature>
121         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
122         /// <exception cref="InvalidOperationException">Thrown when an invalid operation occurs.</exception>
123         /// <exception cref="NotSupportedException">Thrown when the features are not supported.</exception>
124         /// <exception cref="UnauthorizedAccessException">Thrown when the application has no privilege to retrieve the application history.</exception>
125         public IReadOnlyList<BatteryStatisticsData> Query(DateTime startTime, DateTime endTime, uint resultSize)
126         {
127             CheckTimeSpan(startTime, endTime);
128             CheckResultSize(resultSize);
129
130             List<BatteryStatisticsData> result = new List<BatteryStatisticsData>();
131
132             IntPtr cursor = IntPtr.Zero;
133             int error = Interop.CtxHistory.Query(Uri, (int)ConvertDateTimeToUnixTimestamp(startTime), (int)ConvertDateTimeToUnixTimestamp(endTime), resultSize, out cursor);
134             if ((AppHistoryError)error == AppHistoryError.NoData)
135             {
136                 return result.AsReadOnly();
137             }
138             else if ((AppHistoryError)error != AppHistoryError.None)
139             {
140                 Log.Error(AppHistoryErrorFactory.LogTag, "Failed to request query battery statistics");
141                 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request query battery statistics");
142             }
143
144             int size;
145             error = Interop.CtxHistoryCursor.GetCount(cursor, out size);
146             Interop.CtxHistoryCursor.First(cursor);
147
148             for (int i = 0; i < size; i++)
149             {
150                 result.Add(ConvertOutputToStatsData(cursor));
151                 Interop.CtxHistoryCursor.Next(cursor);
152             }
153             Interop.CtxHistoryCursor.Destroy(cursor);
154
155             return result.AsReadOnly();
156         }
157
158         /// <summary>
159         /// Gets the last time when the device was fully charged.
160         /// </summary>
161         /// <since_tizen> 4 </since_tizen>
162         /// <returns>The last time when the device was fully charged.</returns>
163         /// <feature>http://tizen.org/feature/app_history</feature>
164         /// <feature>http://tizen.org/feature/battery</feature>
165         /// <exception cref="NotSupportedException">Thrown when the statistics is not supported.</exception>
166         public static DateTime GetLastFullyChargedTime()
167         {
168             Int64 timestamp;
169             int error = Interop.CtxHistory.GetLastFullyChargedTime(out timestamp);
170             if ((AppHistoryError)error != AppHistoryError.None)
171             {
172                 Log.Error(AppHistoryErrorFactory.LogTag, "Failed to request get last fully charged time");
173                 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request get last fully charged time");
174             }
175
176             return ConvertUnixTimestampToDateTime(timestamp);
177         }
178
179         internal override string ConvertSortOrderToString(int order)
180         {
181             switch ((SortOrderType)order)
182             {
183                 case SortOrderType.ConsumptionMost:
184                     return "stats/battery/history";
185                 default:
186                     return null;
187             }
188         }
189
190         private static BatteryStatisticsData ConvertOutputToStatsData(IntPtr cursor)
191         {
192             string appId;
193             double consumption;
194
195             Interop.CtxHistoryCursor.GetString(cursor, AppStatsAppId, out appId);
196             Interop.CtxHistoryCursor.GetDouble(cursor, AppStatsConsumption, out consumption);
197
198             return new BatteryStatisticsData(appId, consumption);
199         }
200
201         /// <summary>
202         /// Gets the criteria of battery statistics sorted by.
203         /// </summary>
204         /// <since_tizen> 4 </since_tizen>
205         /// <value>The criteria of battery statistics sorted by.</value>
206         public SortOrderType SortOrder { get; private set; }
207
208         /// <summary>
209         /// Sorts the order type of battery statistics.
210         /// </summary>
211         /// <since_tizen> 4 </since_tizen>
212         public enum SortOrderType
213         {
214             /// <summary>
215             /// Sorts the apps by consumption, the most battery consuming apps appear first (default).
216             /// </summary>
217             ConsumptionMost = 0
218         }
219     }
220 }