/* * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; namespace Tizen.Context.AppHistory { /// /// This class provides APIs to query the battery consumption per application. /// /// 4 public class BatteryStatistics : AppStatistics { private const string AppStatsConsumption = "TotalAmount"; /// /// The default constructor of BatteryStatistics class. /// /// 4 /// http://tizen.org/feature/app_history /// http://tizen.org/feature/battery /// Thrown when the method failed due to an internal error. /// Thrown when the features are not supported. public BatteryStatistics() { SortOrder = SortOrderType.ConsumptionMost; Uri = ConvertSortOrderToString((int)SortOrder); bool isSupported = false; int error = Interop.CtxHistory.IsSupported(Uri, out isSupported); if ((AppHistoryError)error != AppHistoryError.None) { throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri); } if (!isSupported) { throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.NotSupported, Uri); } } /// /// The constructor of BatteryStatistics class. /// /// 4 /// The criteria of the battery statistics sorted by. /// http://tizen.org/feature/app_history /// http://tizen.org/feature/battery /// Thrown when an invalid argument is used. /// Thrown when the method failed due to an internal error. /// Thrown when the features are not supported. public BatteryStatistics(SortOrderType order) { SortOrder = order; Uri = ConvertSortOrderToString((int)SortOrder); if (Uri == null) { throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.InvalidParameter, "Invalid SortOrderType"); } bool isSupported = false; int error = Interop.CtxHistory.IsSupported(Uri, out isSupported); if ((AppHistoryError)error != AppHistoryError.None) { throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri); } if (!isSupported) { throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.NotSupported, Uri); } } /// /// Retrieves a given type of battery statistics. /// /// 4 /// The start time of the data to be aggregated. /// The end time of the data to be aggregated. /// Battery statistics data retrieved. /// http://tizen.org/privilege/apphistory.read /// http://tizen.org/feature/app_history /// http://tizen.org/feature/battery /// Thrown when an invalid argument is used. /// Thrown when invalid operation occurs. /// Thrown when the features are not supported. /// Thrown when the application has no privilege to retrieve the application history. public IReadOnlyList Query(DateTime startTime, DateTime endTime) { CheckTimeSpan(startTime, endTime); return Query(startTime, endTime, DefaultResultSize); } /// /// Retrieves a given type of battery statistics. /// /// 4 /// The start time of the data to be aggregated. /// The end time of the data to be aggregated. /// The number of data records to be retrieved. /// Battery statistics data retrieved. /// http://tizen.org/privilege/apphistory.read /// http://tizen.org/feature/app_history /// http://tizen.org/feature/battery /// Thrown when an invalid argument is used. /// Thrown when an invalid operation occurs. /// Thrown when the features are not supported. /// Thrown when the application has no privilege to retrieve the application history. public IReadOnlyList Query(DateTime startTime, DateTime endTime, uint resultSize) { CheckTimeSpan(startTime, endTime); CheckResultSize(resultSize); List result = new List(); IntPtr cursor = IntPtr.Zero; int error = Interop.CtxHistory.Query(Uri, (int)ConvertDateTimeToUnixTimestamp(startTime), (int)ConvertDateTimeToUnixTimestamp(endTime), resultSize, out cursor); if ((AppHistoryError)error == AppHistoryError.NoData) { return result.AsReadOnly(); } else if ((AppHistoryError)error != AppHistoryError.None) { Log.Error(AppHistoryErrorFactory.LogTag, "Failed to request query battery statistics"); throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request query battery statistics"); } int size; error = Interop.CtxHistoryCursor.GetCount(cursor, out size); Interop.CtxHistoryCursor.First(cursor); for (int i = 0; i < size; i++) { result.Add(ConvertOutputToStatsData(cursor)); Interop.CtxHistoryCursor.Next(cursor); } Interop.CtxHistoryCursor.Destroy(cursor); return result.AsReadOnly(); } /// /// Gets the last time when the device was fully charged. /// /// 4 /// The last time when the device was fully charged. /// http://tizen.org/feature/app_history /// http://tizen.org/feature/battery /// Thrown when the statistics is not supported. public static DateTime GetLastFullyChargedTime() { Int64 timestamp; int error = Interop.CtxHistory.GetLastFullyChargedTime(out timestamp); if ((AppHistoryError)error != AppHistoryError.None) { Log.Error(AppHistoryErrorFactory.LogTag, "Failed to request get last fully charged time"); throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request get last fully charged time"); } return ConvertUnixTimestampToDateTime(timestamp); } internal override string ConvertSortOrderToString(int order) { switch ((SortOrderType)order) { case SortOrderType.ConsumptionMost: return "stats/battery/history"; default: return null; } } private static BatteryStatisticsData ConvertOutputToStatsData(IntPtr cursor) { string appId; double consumption; Interop.CtxHistoryCursor.GetString(cursor, AppStatsAppId, out appId); Interop.CtxHistoryCursor.GetDouble(cursor, AppStatsConsumption, out consumption); return new BatteryStatisticsData(appId, consumption); } /// /// Gets the criteria of battery statistics sorted by. /// /// 4 /// The criteria of battery statistics sorted by. public SortOrderType SortOrder { get; private set; } /// /// Sorts the order type of battery statistics. /// /// 4 public enum SortOrderType { /// /// Sorts the apps by consumption, the most battery consuming apps appear first (default). /// /// 4 ConsumptionMost = 0 } } }