示例#1
0
        /// <summary>
        /// Returns the <see cref="Android.Support.V7.Widget.RecyclerView"/> including the time span
        /// specified by the intervalType argument.
        /// </summary>
        /// <returns>A list of <see cref="Android.App.Usage.UsageStats"/>.</returns>
        /// <param name="intervalType">The time interval by which the stats are aggregated.</param>
        public IList <UsageStats> GetUsageStatistics(UsageStatsInterval intervalType)
        {
            var currentDate = System.DateTime.Now;
            var beginDate   = System.DateTime.Now;

            switch (intervalType)
            {
            case UsageStatsInterval.Yearly:
                beginDate = new DateTime(beginDate.Year, 1, 1);
                break;

            case UsageStatsInterval.Monthly:
                beginDate = new DateTime(beginDate.Year, beginDate.Month, 1);
                break;

            case UsageStatsInterval.Weekly:
                beginDate = beginDate.StartOfWeek(DayOfWeek.Monday);
                break;

            case UsageStatsInterval.Daily:
                beginDate = beginDate.Date;
                break;

            default:
                break;
            }

            var beginTime       = (long)(beginDate - new DateTime(1970, 1, 1)).TotalMilliseconds;
            var currentTime     = (long)(currentDate - new DateTime(1970, 1, 1)).TotalMilliseconds;
            var queryUsageStats = mUsageStatsManager
                                  .QueryAndAggregateUsageStats(beginTime,
                                                               currentTime);

            if (queryUsageStats.Count == 0)
            {
                Log.Info(TAG, "The user may not allow the access to apps usage. ");
                Toast.MakeText(Activity,
                               GetString(Resource.String.explanation_access_to_appusage_is_not_enabled),
                               ToastLength.Long).Show();
                mOpenUsageSettingButton.Visibility = ViewStates.Visible;
                mOpenUsageSettingButton.Click     += (sender, e) =>
                                                     StartActivity(new Intent(Settings.ActionUsageAccessSettings));
            }

            var result = queryUsageStats.Values.ToList();

            return(result);
        }
示例#2
0
        /// <summary>
        /// Returns the <see cref="Android.Support.V7.Widget.RecyclerView"/> including the time span
        /// specified by the intervalType argument.
        /// </summary>
        /// <returns>A list of <see cref="Android.App.Usage.UsageStats"/>.</returns>
        /// <param name="intervalType">The time interval by which the stats are aggregated.</param>
        public IList <UsageStats> GetUsageStatistics(UsageStatsInterval intervalType)
        {
            var cal = Calendar.GetInstance(Java.Util.Locale.Default);

            cal.Add(CalendarField.Year, -1);

            // Query stats beginning one year ago to the current date.
            var queryUsageStats = mUsageStatsManager
                                  .QueryUsageStats(intervalType, cal.TimeInMillis,
                                                   Java.Lang.JavaSystem.CurrentTimeMillis());

            if (queryUsageStats.Count == 0)
            {
                Log.Info(TAG, "The user may not allow the access to apps usage. ");
                Toast.MakeText(Activity,
                               GetString(Resource.String.explanation_access_to_appusage_is_not_enabled),
                               ToastLength.Long).Show();
                mOpenUsageSettingButton.Visibility = ViewStates.Visible;
                mOpenUsageSettingButton.Click     += (sender, e) =>
                                                     StartActivity(new Intent(Settings.ActionUsageAccessSettings));
            }
            return(queryUsageStats);
        }
		/// <summary>
		/// Returns the <see cref="Android.Support.V7.Widget.RecyclerView"/> including the time span 
		/// specified by the intervalType argument.
		/// </summary>
		/// <returns>A list of <see cref="Android.App.Usage.UsageStats"/>.</returns>
		/// <param name="intervalType">The time interval by which the stats are aggregated.</param>
		public IList<UsageStats> GetUsageStatistics (UsageStatsInterval intervalType)
		{
			var cal = Calendar.GetInstance (Java.Util.Locale.Default);
			cal.Add (CalendarField.Year, -1);	

			// Query stats beginning one year ago to the current date.
			var queryUsageStats = mUsageStatsManager
				.QueryUsageStats (intervalType, cal.TimeInMillis,
				                      Java.Lang.JavaSystem.CurrentTimeMillis ());

			if (queryUsageStats.Count == 0) {
				Log.Info (TAG, "The user may not allow the access to apps usage. ");
				Toast.MakeText (Activity,
					GetString (Resource.String.explanation_access_to_appusage_is_not_enabled),
					ToastLength.Long).Show ();
				mOpenUsageSettingButton.Visibility = ViewStates.Visible;
				mOpenUsageSettingButton.Click += ( sender, e) => 
					StartActivity (new Intent (Settings.ActionUsageAccessSettings));
			}
			return queryUsageStats;
		}