2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
20 namespace Tizen.Context.AppHistory
23 /// This class provides APIs to query battery consumption per application.
25 /// <since_tizen> 4 </since_tizen>
26 public class BatteryStatistics : AppStatistics
28 private const string AppStatsConsumption = "TotalAmount";
31 /// The default constructor of BatteryStatistics class.
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 method fail due to internal error.</exception>
37 /// <exception cref="NotSupportedException">Thrown when the features are not supported.</exception>
38 public BatteryStatistics()
40 SortOrder = SortOrderType.ConsumptionMost;
41 Uri = ConvertSortOrderToString((int)SortOrder);
43 bool isSupported = false;
44 int error = Interop.CtxHistory.IsSupported(Uri, out isSupported);
45 if ((AppHistoryError)error != AppHistoryError.None)
47 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri);
52 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.NotSupported, Uri);
57 /// The constructor of BatteryStatistics class.
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 method fail due to internal error.</exception>
65 /// <exception cref="NotSupportedException">Thrown when the features are not supported.</exception>
66 public BatteryStatistics(SortOrderType order)
69 Uri = ConvertSortOrderToString((int)SortOrder);
73 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.InvalidParameter, "Invalid SortOrderType");
76 bool isSupported = false;
77 int error = Interop.CtxHistory.IsSupported(Uri, out isSupported);
78 if ((AppHistoryError)error != AppHistoryError.None)
80 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri);
85 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.NotSupported, Uri);
90 /// Retrieves a given type of battery statistics.
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 app has no privilege to retrieve app history.</exception>
103 public IReadOnlyList<BatteryStatisticsData> Query(DateTime startTime, DateTime endTime)
105 CheckTimeSpan(startTime, endTime);
107 return Query(startTime, endTime, DefaultResultSize);
111 /// Retrieves a given type of battery statistics.
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 invalid operation occurs.</exception>
123 /// <exception cref="NotSupportedException">Thrown when the features are not supported.</exception>
124 /// <exception cref="UnauthorizedAccessException">Thrown when the app has no privilege to retrieve app history.</exception>
125 public IReadOnlyList<BatteryStatisticsData> Query(DateTime startTime, DateTime endTime, uint resultSize)
127 CheckTimeSpan(startTime, endTime);
128 CheckResultSize(resultSize);
130 List<BatteryStatisticsData> result = new List<BatteryStatisticsData>();
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)
136 return result.AsReadOnly();
138 else if ((AppHistoryError)error != AppHistoryError.None)
140 Log.Error(AppHistoryErrorFactory.LogTag, "Failed to request query battery statistics");
141 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request query battery statistics");
145 error = Interop.CtxHistoryCursor.GetCount(cursor, out size);
146 Interop.CtxHistoryCursor.First(cursor);
148 for (int i = 0; i < size; i++)
150 result.Add(ConvertOutputToStatsData(cursor));
151 Interop.CtxHistoryCursor.Next(cursor);
153 Interop.CtxHistoryCursor.Destroy(cursor);
155 return result.AsReadOnly();
159 /// Gets the last time when the device was fully charged.
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()
169 int error = Interop.CtxHistory.GetLastFullyChargedTime(out timestamp);
170 if ((AppHistoryError)error != AppHistoryError.None)
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");
176 return ConvertUnixTimestampToDateTime(timestamp);
179 internal override string ConvertSortOrderToString(int order)
181 switch ((SortOrderType)order)
183 case SortOrderType.ConsumptionMost:
184 return "stats/battery/history";
190 private static BatteryStatisticsData ConvertOutputToStatsData(IntPtr cursor)
195 Interop.CtxHistoryCursor.GetString(cursor, AppStatsAppId, out appId);
196 Interop.CtxHistoryCursor.GetDouble(cursor, AppStatsConsumption, out consumption);
198 return new BatteryStatisticsData(appId, consumption);
202 /// Gets the criteria of battery statistics sorted by.
204 /// <since_tizen> 4 </since_tizen>
205 /// <value>The criteria of battery statistics sorted by.</value>
206 public SortOrderType SortOrder { get; private set; }
209 /// Sort order type of battery statistics.
211 /// <since_tizen> 4 </since_tizen>
212 public enum SortOrderType
215 /// Sorts apps by consumption, the most battery consuming apps appear first. (Default)