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 /// <since_tizen> 4 </since_tizen>
26 public class UsageStatistics : AppStatistics
28 private const string AppStatsDuration = "TotalDuration";
29 private const string AppStatsLaunchCount = "TotalCount";
30 private const string AppStatsLastLaunchTime = "LastTime";
33 /// The default constructor of UsageStatistics class.
35 /// <since_tizen> 4 </since_tizen>
36 /// <feature>http://tizen.org/feature/app_history</feature>
37 /// <exception cref="InvalidOperationException">Thrown when method fail due to internal error.</exception>
38 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
39 public UsageStatistics()
41 SortOrder = SortOrderType.LastLaunchTimeNewest;
42 Uri = ConvertSortOrderToString((int)SortOrder);
44 bool isSupported = false;
45 int error = Interop.CtxHistory.IsSupported(Uri, out isSupported);
46 if ((AppHistoryError)error != AppHistoryError.None)
48 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, Uri);
53 throw AppHistoryErrorFactory.CheckAndThrowException(AppHistoryError.OperationFailed, Uri);
58 /// The constructor of UsageStatistics class.
60 /// <since_tizen> 4 </since_tizen>
61 /// <param name="order">The criteria of the usage statistics sorted by.</param>
62 /// <feature>http://tizen.org/feature/app_history</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 feature is not supported.</exception>
66 public UsageStatistics(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.OperationFailed, Uri);
90 /// Retrieves a given type of usage 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>Usage statistics data retrieved.</returns>
96 /// <privilege>http://tizen.org/privilege/apphistory.read</privilege>
97 /// <feature>http://tizen.org/feature/app_history</feature>
98 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
99 /// <exception cref="InvalidOperationException">Thrown when invalid operation occurs.</exception>
100 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
101 /// <exception cref="UnauthorizedAccessException">Thrown when the app has no privilege to retrieve app history.</exception>
102 public IReadOnlyList<UsageStatisticsData> Query(DateTime startTime, DateTime endTime)
104 CheckTimeSpan(startTime, endTime);
106 return Query(startTime, endTime, DefaultResultSize);
110 /// Retrieves a given type of usage statistics.
112 /// <since_tizen> 4 </since_tizen>
113 /// <param name="startTime">The start time of the data to be aggregated.</param>
114 /// <param name="endTime">The end time of the data to be aggregated.</param>
115 /// <param name="resultSize">The number of data records to be retrieved.</param>
116 /// <returns>Usage statistics data retrieved.</returns>
117 /// <privilege>http://tizen.org/privilege/apphistory.read</privilege>
118 /// <feature>http://tizen.org/feature/app_history</feature>
119 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
120 /// <exception cref="InvalidOperationException">Thrown when invalid operation occurs.</exception>
121 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
122 /// <exception cref="UnauthorizedAccessException">Thrown when the app has no privilege to retrieve app history.</exception>
123 public IReadOnlyList<UsageStatisticsData> Query(DateTime startTime, DateTime endTime, uint resultSize)
125 CheckTimeSpan(startTime, endTime);
126 CheckResultSize(resultSize);
128 List<UsageStatisticsData> result = new List<UsageStatisticsData>();
130 IntPtr cursor = IntPtr.Zero;
131 int error = Interop.CtxHistory.Query(Uri, ConvertDateTimeToUnixTimestamp(startTime), ConvertDateTimeToUnixTimestamp(endTime), resultSize, out cursor);
132 if ((AppHistoryError)error == AppHistoryError.NoData)
134 return result.AsReadOnly();
135 } else if ((AppHistoryError)error != AppHistoryError.None)
137 Log.Error(AppHistoryErrorFactory.LogTag, "Failed to request query usage statistics");
138 throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request query usage statistics");
142 error = Interop.CtxHistoryCursor.GetCount(cursor, out size);
143 Interop.CtxHistoryCursor.First(cursor);
145 for (int i = 0; i < size; i++)
147 result.Add(ConvertOutputToStatsData(cursor));
148 Interop.CtxHistoryCursor.Next(cursor);
150 Interop.CtxHistoryCursor.Destroy(cursor);
152 return result.AsReadOnly();
155 internal override string ConvertSortOrderToString(int order)
157 switch ((SortOrderType)order)
159 case SortOrderType.LastLaunchTimeNewest:
160 return "stats/app/recently";
161 case SortOrderType.LaunchCountMost:
162 return "stats/app/often";
168 private static UsageStatisticsData ConvertOutputToStatsData(IntPtr cursor)
172 Int64 launchCount = 1;
173 Int64 lastLaunchTime;
175 Interop.CtxHistoryCursor.GetString(cursor, AppStatsAppId, out appId);
176 Interop.CtxHistoryCursor.GetInt64(cursor, AppStatsDuration, out duration);
177 Interop.CtxHistoryCursor.GetInt64(cursor, AppStatsLaunchCount, out launchCount);
178 Interop.CtxHistoryCursor.GetInt64(cursor, AppStatsLastLaunchTime, out lastLaunchTime);
180 return new UsageStatisticsData(appId, (int)duration, (int)launchCount, ConvertUnixTimestampToDateTime(lastLaunchTime));
184 /// Gets the criteria of usage statistics sorted by.
186 /// <since_tizen> 4 </since_tizen>
187 /// <value>The criteria of usage statistics sorted by.</value>
188 public SortOrderType SortOrder { get; private set; }
191 /// Sort order type of usage statistics
193 /// <since_tizen> 4 </since_tizen>
194 public enum SortOrderType
197 /// Sorts apps by the last launch time, the most recently launched apps appear first. (Default)
199 LastLaunchTimeNewest = 0,
201 /// Sorts apps by the launch count of being launched, the most frequently launched apps appear first.