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