/*
* 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 application launch history.
///
public class UsageStatistics : AppStatistics
{
private const string AppStatsDuration = "TotalDuration";
private const string AppStatsLaunchCount = "TotalCount";
private const string AppStatsLastLaunchTime = "LastTime";
///
/// The default constructor of UsageStatistics class.
///
/// http://tizen.org/feature/app_history
/// Thrown when method fail due to internal error.
/// Thrown when the feature is not supported.
public UsageStatistics()
{
SortOrder = SortOrderType.LastLaunchTimeNewest;
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.OperationFailed, Uri);
}
}
///
/// The constructor of UsageStatistics class.
///
/// The criteria of the usage statistics sorted by.
/// http://tizen.org/feature/app_history
/// Thrown when an invalid argument is used.
/// Thrown when method fail due to internal error.
/// Thrown when the feature is not supported.
public UsageStatistics(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.OperationFailed, Uri);
}
}
///
/// Retrieves a given type of usage statistics.
///
/// The start time of the data to be aggregated.
/// The end time of the data to be aggregated.
/// Usage statistics data retrieved.
/// http://tizen.org/privilege/apphistory.read
/// http://tizen.org/feature/app_history
/// Thrown when an invalid argument is used.
/// Thrown when invalid operation occurs.
/// Thrown when the feature is not supported.
/// Thrown when the app has no privilege to retrieve app history.
public IReadOnlyList Query(DateTime startTime, DateTime endTime)
{
CheckTimeSpan(startTime, endTime);
return Query(startTime, endTime, DefaultResultSize);
}
///
/// Retrieves a given type of usage statistics.
///
/// 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.
/// Usage statistics data retrieved.
/// http://tizen.org/privilege/apphistory.read
/// http://tizen.org/feature/app_history
/// Thrown when an invalid argument is used.
/// Thrown when invalid operation occurs.
/// Thrown when the feature is not supported.
/// Thrown when the app has no privilege to retrieve app 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, ConvertDateTimeToUnixTimestamp(startTime), 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 usage statistics");
throw AppHistoryErrorFactory.CheckAndThrowException((AppHistoryError)error, "Failed to request query usage 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();
}
internal override string ConvertSortOrderToString(int order)
{
switch ((SortOrderType)order)
{
case SortOrderType.LastLaunchTimeNewest:
return "stats/app/recently";
case SortOrderType.LaunchCountMost:
return "stats/app/often";
default:
return null;
}
}
private static UsageStatisticsData ConvertOutputToStatsData(IntPtr cursor)
{
string appId;
Int64 duration = 1;
Int64 launchCount = 1;
Int64 lastLaunchTime;
Interop.CtxHistoryCursor.GetString(cursor, AppStatsAppId, out appId);
Interop.CtxHistoryCursor.GetInt64(cursor, AppStatsDuration, out duration);
Interop.CtxHistoryCursor.GetInt64(cursor, AppStatsLaunchCount, out launchCount);
Interop.CtxHistoryCursor.GetInt64(cursor, AppStatsLastLaunchTime, out lastLaunchTime);
return new UsageStatisticsData(appId, (int)duration, (int)launchCount, ConvertUnixTimestampToDateTime(lastLaunchTime));
}
///
/// Gets the criteria of usage statistics sorted by.
///
/// The criteria of usage statistics sorted by.
public SortOrderType SortOrder { get; private set; }
///
/// Sort order type of usage statistics
///
public enum SortOrderType
{
///
/// Sorts apps by the last launch time, the most recently launched apps appear first. (Default)
///
LastLaunchTimeNewest = 0,
///
/// Sorts apps by the launch count of being launched, the most frequently launched apps appear first.
///
LaunchCountMost
}
}
}