Page Example

Context History Sample Overview

Mobile native

The Context History sample application demonstrates how you can retrieve device usage statistics using the Contextual History API.

The following figure shows the main screens of the Context History sample application.

Figure: Context History screens

Context History screens Context History screens

The application opens with the main screen that shows the presets of usage statistics. The corresponding history item and the filter values are set differently on each preset.

To retrieve the corresponding usage statistics, if available, select a preset. For example, if you select the second preset from the top, the screen lists the top-7 most frequently used applications during the last 30 days, only considering the applications used while a headphone is connected.

Note
Some presets do not show any results if there are no usage logs available yet.

Prerequisites

To ensure proper application execution, the following privileges must be set:

  • To access call history:
    • http://tizen.org/privilege/callhistory.read
  • To access application history:
    • http://tizen.org/privilege/apphistory.read
  • To access media history:
    • http://tizen.org/privilege/mediahistory.read
  • To get the current Wi-Fi BSSID that is used as a filter value of one preset:
    • http://tizen.org/privilege/network.get

Implementation

The application is initialized within the app_create() callback function. The following example shows how to use the Contextual History API to retrieve usage statistics in the history module: the get_frequently_used_apps() function retrieves the CONTEXT_HISTORY_FREQUENTLY_USED_APP statistics with 3 filtering options (timespan of the logs to be processed, result size, and audio jack status).

void
get_frequently_used_apps(void *data EINA_UNUSED, Evas_Object * obj EINA_UNUSED, void *event_info)
{
   context_history_h history_h = NULL;
   context_history_list_h list_h = NULL;
   context_history_record_h record_h = NULL;
   context_history_filter_h filter_h = NULL;

   int history_count = -1;
   char* app_id = NULL;
   int total_count = -1;
   int total_duration = -1;
   int last_time = -1;

   // Create history handle
   context_history_create(&history_h);

   // Create filter handle
   context_history_filter_create(&filter_h);

   // Set a filtering option: audio jack status
   context_history_filter_set_int(filter_h, CONTEXT_HISTORY_FILTER_AUDIO_JACK, CONTEXT_HISTORY_FILTER_AUDIO_JACK_CONNECTED);

   // Set a filtering option: timespan
   context_history_filter_set_int(filter_h, CONTEXT_HISTORY_FILTER_TIME_SPAN, 30);

   // Set a filtering option: result size
   context_history_filter_set_int(filter_h, CONTEXT_HISTORY_FILTER_RESULT_SIZE, 7);

   // Get the statistics
   context_history_get_list(history_h, CONTEXT_HISTORY_FREQUENTLY_USED_APP, filter_h, &list_h);

   // Get the number of records in the retrieved list (may not be equal to the given filter)
   context_history_list_get_count(list_h, &history_count);

   // Extract records from list
   for (int i = 0; i < history_count; i++) 
   {
      // Get the current record from the list
      context_history_list_get_current(list_h, &record_h);

      // Get an attribute: App ID
      context_history_record_get_string(record_h, CONTEXT_HISTORY_APP_ID, &app_id);

      // Get an attribute: Total count of use
      context_history_record_get_int(record_h, CONTEXT_HISTORY_TOTAL_COUNT, &total_count);

      // Get an attribute: Total duration of use
      context_history_record_get_int(record_h, CONTEXT_HISTORY_TOTAL_DURATION, &total_duration);

      // Get an attribute: Last time of use
      context_history_record_get_int(record_h, CONTEXT_HISTORY_LAST_TIME, &last_time);

      // Destroy the record handle
      context_history_record_destroy(record_h);
      record_h = NULL;

      // Use the retrieved attributes

      // Move to the next record
      context_history_list_move_next(list_h);

      // Free the string attributes
      free(app_id);
      app_id = NULL;
      total_count = -1;
      total_duration = -1;
      last_time = -1;
   }

   // Destroy the list handle
   context_history_list_destroy(list_h);

   // Destroy the filter handle
   context_history_filter_destroy(filter_h);

   // Destroy the history handle
   context_history_destroy(history_h);
}