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 application launch history.
25 public class UsageStatistics : AppStatistics
27 private const string AppStatsDuration = "TotalDuration";
28 private const string AppStatsLaunchCount = "TotalCount";
29 private const string AppStatsLastLaunchTime = "LastTime";
32 /// The default constructor of UsageStatistics class.
34 /// <exception cref="InvalidOperationException">Thrown when method fail due to internal error.</exception>
35 public UsageStatistics()
37 SortOrder = SortOrderType.LastLaunchTimeNewest;
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.OperationFailed, Uri);
54 /// The constructor of UsageStatistics class.
56 /// <param name="order">The criteria of the usage statistics sorted by.</param>
57 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
58 /// <exception cref="InvalidOperationException">Thrown when method fail due to internal error.</exception>
59 public UsageStatistics(SortOrderType order)
62 Uri = ConvertSortOrderToString((int)SortOrder);
66 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.InvalidParameter, "Invalid SortOrderType");
69 bool isSupported = false;
70 int error = Interop.CtxHistory.IsSupported(Uri, out isSupported);
71 if ((AppHistoryError)error != AppHistoryError.None)
73 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri);
78 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.OperationFailed, Uri);
83 /// Retrieves a given type of usage statistics.
85 /// <param name="startTime">The start time of the data to be aggregated.</param>
86 /// <param name="endTime">The end time of the data to be aggregated.</param>
87 /// <returns>Usage statistics data retrieved.</returns>
88 /// <privilege>http://tizen.org/privilege/apphistory.read</privilege>
89 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
90 /// <exception cref="InvalidOperationException">Thrown when invalid operation occurs.</exception>
91 /// <exception cref="UnauthorizedAccessException">Thrown when the app has no privilege to retrieve app history.</exception>
92 public IReadOnlyList<UsageStatisticsData> Query(DateTime startTime, DateTime endTime)
94 CheckTimeSpan(startTime, endTime);
96 return Query(startTime, endTime, DefaultResultSize);
100 /// Retrieves a given type of usage statistics.
102 /// <param name="startTime">The start time of the data to be aggregated.</param>
103 /// <param name="endTime">The end time of the data to be aggregated.</param>
104 /// <param name="resultSize">The number of data records to be retrieved.</param>
105 /// <returns>Usage statistics data retrieved.</returns>
106 /// <privilege>http://tizen.org/privilege/apphistory.read</privilege>
107 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
108 /// <exception cref="InvalidOperationException">Thrown when invalid operation occurs.</exception>
109 /// <exception cref="UnauthorizedAccessException">Thrown when the app has no privilege to retrieve app history.</exception>
110 public IReadOnlyList<UsageStatisticsData> Query(DateTime startTime, DateTime endTime, uint resultSize)
112 CheckTimeSpan(startTime, endTime);
113 CheckResultSize(resultSize);
115 List<UsageStatisticsData> result = new List<UsageStatisticsData>();
117 IntPtr cursor = IntPtr.Zero;
118 int error = Interop.CtxHistory.Query(Uri, ConvertDateTimeToUnixTimestamp(startTime), ConvertDateTimeToUnixTimestamp(endTime), resultSize, out cursor);
119 if ((AppHistoryError)error == AppHistoryError.NoData)
121 return result.AsReadOnly();
122 } else if ((AppHistoryError)error != AppHistoryError.None)
124 Log.Error(AppHistoryErrorFactory.LogTag, "Failed to request query usage statistics");
125 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request query usage statistics");
129 error = Interop.CtxHistoryCursor.GetCount(cursor, out size);
130 Interop.CtxHistoryCursor.First(cursor);
132 for (int i = 0; i < size; i++)
134 result.Add(ConvertOutputToStatsData(cursor));
135 Interop.CtxHistoryCursor.Next(cursor);
137 Interop.CtxHistoryCursor.Destroy(cursor);
139 return result.AsReadOnly();
142 internal override string ConvertSortOrderToString(int order)
144 switch ((SortOrderType)order)
146 case SortOrderType.LastLaunchTimeNewest:
147 return "stats/app/recently";
148 case SortOrderType.LaunchCountMost:
149 return "stats/app/often";
155 private static UsageStatisticsData ConvertOutputToStatsData(IntPtr cursor)
159 Int64 launchCount = 1;
160 Int64 lastLaunchTime;
162 Interop.CtxHistoryCursor.GetString(cursor, AppStatsAppId, out appId);
163 Interop.CtxHistoryCursor.GetInt64(cursor, AppStatsDuration, out duration);
164 Interop.CtxHistoryCursor.GetInt64(cursor, AppStatsLaunchCount, out launchCount);
165 Interop.CtxHistoryCursor.GetInt64(cursor, AppStatsLastLaunchTime, out lastLaunchTime);
167 return new UsageStatisticsData(appId, (int)duration, (int)launchCount, ConvertUnixTimestampToDateTime(lastLaunchTime));
171 /// Gets the criteria of usage statistics sorted by.
173 /// <value>The criteria of usage statistics sorted by.</value>
174 public SortOrderType SortOrder { get; private set; }
177 /// Sort order type of usage statistics
179 public enum SortOrderType
182 /// Sorts apps by the last launch time, the most recently launched apps appear first. (Default)
184 LastLaunchTimeNewest = 0,
186 /// Sorts apps by the launch count of being launched, the most frequently launched apps appear first.