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 public class BatteryStatistics : AppStatistics
27 private const string AppStatsConsumption = "TotalAmount";
30 /// The default constructor of BatteryStatistics class.
32 /// <feature>http://tizen.org/feature/battery</feature>
33 /// <exception cref="InvalidOperationException">Thrown when method fail due to internal error.</exception>
34 /// <exception cref="NotSupportedException">Thrown when the statistics is not supported.</exception>
35 public BatteryStatistics()
37 SortOrder = SortOrderType.ConsumptionMost;
38 Uri = ConvertSortOrderToString((int)SortOrder);
40 bool isSupported = false;
41 int error = Interop.CtxHistory.IsSupported(Uri, out isSupported);
42 if ((AppHistoryError)error != AppHistoryError.None)
44 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri);
49 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.NotSupported, Uri);
54 /// The constructor of BatteryStatistics class.
56 /// <param name="order">The criteria of the battery statistics sorted by.</param>
57 /// <feature>http://tizen.org/feature/battery</feature>
58 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
59 /// <exception cref="InvalidOperationException">Thrown when method fail due to internal error.</exception>
60 /// <exception cref="NotSupportedException">Thrown when the statistics is not supported.</exception>
61 public BatteryStatistics(SortOrderType order)
64 Uri = ConvertSortOrderToString((int)SortOrder);
68 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.InvalidParameter, "Invalid SortOrderType");
71 bool isSupported = false;
72 int error = Interop.CtxHistory.IsSupported(Uri, out isSupported);
73 if ((AppHistoryError)error != AppHistoryError.None)
75 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri);
80 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.NotSupported, Uri);
85 /// Retrieves a given type of battery statistics.
87 /// <param name="startTime">The start time of the data to be aggregated.</param>
88 /// <param name="endTime">The end time of the data to be aggregated.</param>
89 /// <returns>Battery statistics data retrieved.</returns>
90 /// <privilege>http://tizen.org/privilege/apphistory.read</privilege>
91 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
92 /// <exception cref="InvalidOperationException">Thrown when invalid operation occurs.</exception>
93 /// <exception cref="UnauthorizedAccessException">Thrown when the app has no privilege to retrieve app history.</exception>
94 public IReadOnlyList<BatteryStatisticsData> Query(DateTime startTime, DateTime endTime)
96 CheckTimeSpan(startTime, endTime);
98 return Query(startTime, endTime, DefaultResultSize);
102 /// Retrieves a given type of battery statistics.
104 /// <param name="startTime">The start time of the data to be aggregated.</param>
105 /// <param name="endTime">The end time of the data to be aggregated.</param>
106 /// <param name="resultSize">The number of data records to be retrieved.</param>
107 /// <returns>Battery statistics data retrieved.</returns>
108 /// <privilege>http://tizen.org/privilege/apphistory.read</privilege>
109 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
110 /// <exception cref="InvalidOperationException">Thrown when invalid operation occurs.</exception>
111 /// <exception cref="UnauthorizedAccessException">Thrown when the app has no privilege to retrieve app history.</exception>
112 public IReadOnlyList<BatteryStatisticsData> Query(DateTime startTime, DateTime endTime, uint resultSize)
114 CheckTimeSpan(startTime, endTime);
115 CheckResultSize(resultSize);
117 List<BatteryStatisticsData> result = new List<BatteryStatisticsData>();
119 IntPtr cursor = IntPtr.Zero;
120 int error = Interop.CtxHistory.Query(Uri, (int)ConvertDateTimeToUnixTimestamp(startTime), (int)ConvertDateTimeToUnixTimestamp(endTime), resultSize, out cursor);
121 if ((AppHistoryError)error == AppHistoryError.NoData)
123 return result.AsReadOnly();
125 else if ((AppHistoryError)error != AppHistoryError.None)
127 Log.Error(AppHistoryErrorFactory.LogTag, "Failed to request query battery statistics");
128 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request query battery statistics");
132 error = Interop.CtxHistoryCursor.GetCount(cursor, out size);
133 Interop.CtxHistoryCursor.First(cursor);
135 for (int i = 0; i < size; i++)
137 result.Add(ConvertOutputToStatsData(cursor));
138 Interop.CtxHistoryCursor.Next(cursor);
140 Interop.CtxHistoryCursor.Destroy(cursor);
142 return result.AsReadOnly();
146 /// Gets the last time when the device was fully charged.
148 /// <returns>The last time when the device was fully charged.</returns>
149 /// <feature>http://tizen.org/feature/battery</feature>
150 /// <exception cref="NotSupportedException">Thrown when the statistics is not supported.</exception>
151 public static DateTime GetLastFullyChargedTime()
154 int error = Interop.CtxHistory.GetLastFullyChargedTime(out timestamp);
155 if ((AppHistoryError)error != AppHistoryError.None)
157 Log.Error(AppHistoryErrorFactory.LogTag, "Failed to request get last fully charged time");
158 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request get last fully charged time");
161 return ConvertUnixTimestampToDateTime(timestamp);
164 internal override string ConvertSortOrderToString(int order)
166 switch ((SortOrderType)order)
168 case SortOrderType.ConsumptionMost:
169 return "stats/battery/history";
175 private static BatteryStatisticsData ConvertOutputToStatsData(IntPtr cursor)
180 Interop.CtxHistoryCursor.GetString(cursor, AppStatsAppId, out appId);
181 Interop.CtxHistoryCursor.GetDouble(cursor, AppStatsConsumption, out consumption);
183 return new BatteryStatisticsData(appId, consumption);
187 /// Gets the criteria of battery statistics sorted by.
189 /// <value>The criteria of battery statistics sorted by.</value>
190 public SortOrderType SortOrder { get; private set; }
193 /// Sort order type of battery statistics.
195 public enum SortOrderType
198 /// Sorts apps by consumption, the most battery consuming apps appear first. (Default)