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/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()
38 SortOrder = SortOrderType.ConsumptionMost;
39 Uri = ConvertSortOrderToString((int)SortOrder);
41 bool isSupported = false;
42 int error = Interop.CtxHistory.IsSupported(Uri, out isSupported);
43 if ((AppHistoryError)error != AppHistoryError.None)
45 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri);
50 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.NotSupported, Uri);
55 /// The constructor of BatteryStatistics class.
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)
66 Uri = ConvertSortOrderToString((int)SortOrder);
70 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.InvalidParameter, "Invalid SortOrderType");
73 bool isSupported = false;
74 int error = Interop.CtxHistory.IsSupported(Uri, out isSupported);
75 if ((AppHistoryError)error != AppHistoryError.None)
77 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri);
82 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.NotSupported, Uri);
87 /// Retrieves a given type of battery statistics.
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)
101 CheckTimeSpan(startTime, endTime);
103 return Query(startTime, endTime, DefaultResultSize);
107 /// Retrieves a given type of battery statistics.
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)
122 CheckTimeSpan(startTime, endTime);
123 CheckResultSize(resultSize);
125 List<BatteryStatisticsData> result = new List<BatteryStatisticsData>();
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)
131 return result.AsReadOnly();
133 else if ((AppHistoryError)error != AppHistoryError.None)
135 Log.Error(AppHistoryErrorFactory.LogTag, "Failed to request query battery statistics");
136 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request query battery statistics");
140 error = Interop.CtxHistoryCursor.GetCount(cursor, out size);
141 Interop.CtxHistoryCursor.First(cursor);
143 for (int i = 0; i < size; i++)
145 result.Add(ConvertOutputToStatsData(cursor));
146 Interop.CtxHistoryCursor.Next(cursor);
148 Interop.CtxHistoryCursor.Destroy(cursor);
150 return result.AsReadOnly();
154 /// Gets the last time when the device was fully charged.
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()
163 int error = Interop.CtxHistory.GetLastFullyChargedTime(out timestamp);
164 if ((AppHistoryError)error != AppHistoryError.None)
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");
170 return ConvertUnixTimestampToDateTime(timestamp);
173 internal override string ConvertSortOrderToString(int order)
175 switch ((SortOrderType)order)
177 case SortOrderType.ConsumptionMost:
178 return "stats/battery/history";
184 private static BatteryStatisticsData ConvertOutputToStatsData(IntPtr cursor)
189 Interop.CtxHistoryCursor.GetString(cursor, AppStatsAppId, out appId);
190 Interop.CtxHistoryCursor.GetDouble(cursor, AppStatsConsumption, out consumption);
192 return new BatteryStatisticsData(appId, consumption);
196 /// Gets the criteria of battery statistics sorted by.
198 /// <value>The criteria of battery statistics sorted by.</value>
199 public SortOrderType SortOrder { get; private set; }
202 /// Sort order type of battery statistics.
204 public enum SortOrderType
207 /// Sorts apps by consumption, the most battery consuming apps appear first. (Default)