Mobile Web Wearable Web

Tizen: Managing the Basic Tizen Features

This tutorial demonstrates how you can utilize generic Tizen Device API features, such as filters, sorting modes, and generic event handlers.

The Tizen API is mandatory for both Tizen mobile and wearable profiles, which means that it is supported in all mobile and wearable devices. All mandatory APIs are supported on the Tizen Emulators.

Warm-up

Become familiar with the Tizen API basics by learning about:

Creating Attribute Filters

Learning how to create attribute filters allows you effectively incorporate query methods in your application:

  1. The AttributeFilter filter (in mobile and wearable applications) is used to filter the search results based on an attribute value. In this example, the filter finds contacts, with the first name Chris, from the default address book.

    Create the filter with the AttributeFilter constructor. You can specify attribute options, such as the attribute name, match flag, and match value.

    /* Use the firstName attribute with the EXACTLY match flag and the Chris value */
    var firstNameFilter = new tizen.AttributeFilter('name.firstName', 'EXACTLY', 'Chris');
  2. Call the find() method of the AddressBook interface to find contacts. The filter (firstNameFilter) you created is included as a parameter.

    tizen.contact.getDefaultAddressBook().find(successCB, errorCB, firstNameFilter);

Creating Attribute Range Filters

Learning how to use attribute range filters allows you effectively incorporate query methods in your application:

  1. The AttributeRangeFilter filter (in mobile and wearable applications)is used to search for results based on a range of attribute values. In this example, the filter finds all events starting on a defined day from the calendar.

    Create the filter with the AttributeRangeFilter constructor. Specify the attribute, and the start and end points of the value range.

    /* Use the startDate attribute with a range that starts today and ends in 1 day */
    /* (meaning that you search for all events occurring today) */
    var now = tizen.time.getCurrentDateTime();
    var today_begin = new tizen.TZDate(now.getFullYear(), now.getMonth(), now.getDate());
    var today_end = today_begin.addDuration(new tizen.TimeDuration(1, "DAYS"));
    var dateRangeFilter = new tizen.AttributeRangeFilter("startDate", today_begin, today_end);
  2. Call the find() method of the Calendar interface to find events. The filter (dateRangeFilter) you created is included as a parameter.

    tizen.calendar.getDefaultCalendar("EVENT").find(successCB, errorCB, dateRangeFilter);

Creating Composite Filters

Learning how to use composite filters allows you effectively incorporate query methods in your application:

  1. The CompositeFilter filter (in mobile and wearable applications) is used to search for results based on a set of filters. In this example, the filter finds contacts, whose first name contains Chris and last name is Smith, from the default address book.

    Create the filter with the CompositeFilter constructor. You can specify multiple sub-filters for the filter set.

    /* Create an attribute filter based on the firstName attribute */
    var firstNameFilter = new tizen.AttributeFilter("name.firstName", "CONTAINS", "Chris");
    
    /* Create an attribute filter based on the lastName attribute */
    var lastNameFilter = new tizen.AttributeFilter("name.lastName", "EXACTLY", "Smith");
    
    /* Create a composite filter based on the intersection of these 2 filters */
    /* (intersection means that both filters must match for the contact to be included in the results) */
    var nameCompositeFilter = new tizen.CompositeFilter("INTERSECTION", [firstNameFilter, lastNameFilter]);
  2. Call the find() method of the AddressBook interface to find matching contacts. The filter (nameCompositeFilter) you created is included as a parameter.

    tizen.contact.getDefaultAddressBook().find(successCB, errorCB, nameCompositeFilter);

Using Sorting Modes

Learning how to use sorting modes allows you effectively incorporate query methods in your application:

  1. The SortMode interface (in mobile and wearable applications) is created to sort the search results. In this example, it is used to sort contacts in the device address book in ascending order, based on their first name.

    Create the sort order with the SortMode() method. Specify an attribute name to sort by and an order option.

    /* Use the firstName attribute with an ASC order */
    var sortMode = new tizen.SortMode("name.firstName", "ASC");
  2. Call the find() method of the AddressBook interface to find matching contacts. In this example, the filter parameter in the find() method is defined as null, which means that the method retrieves all contacts in the address book.

    tizen.contact.getDefaultAddressBook().find(successCB, errorCB, null, sortMode);

Using the Generic Event Handlers

Learning how to use generic, predefined event handlers allows you handle application operations and errors efficiently:

  1. The generic onSuccess() event handler of the SuccessCallBack interface (in mobile and wearable applications) can be used with methods that do not require a return value when successful.

    In this example, the event handler is used to stop a running application with the kill() method of the Application interface (in mobile and wearable applications).

    function onSuccess() 
    {
       console.log("Application terminated successfully");
    }
    
    tizen.application.kill(ctxIDToKill, onSuccess);
  2. The generic onError() event handler of the ErrorCallBack interface (in mobile and wearable applications) can be used with methods that only require an error as an input parameter in the error callback.

    In this example, the event handler is used to handle possible errors with the getCalendars() method of the CalendarManager interface.

    function errorCallback(error)
    {
       console.log("The following error occurred: " + error.name);
    }
    
    tizen.calendar.getCalendars("EVENT", calendarListCallback, errorCallback); 
    

Making Complex Queries Using Filters

Learning how to use filters allows you effectively incorporate complex query methods in your application. You can create queries using AND and OR conditions, like in SQL queries. The following example shows how to make the following query:

"where ((type='VIDEO' OR type='IMAGE') AND title like '%special%')"

Basically, you search in the content of the device for media items where the media type is video or image, and the title contains the word "special".

  1. Create attribute filters to include all content whose media type is either video or image:

    function makeQueryAndFire()
    {
       /* Filter for the video media type */
       var typeVideoFilter = new tizen.AttributeFilter("type", "EXACTLY", "VIDEO");
    
       /* Filter for the image media type */
       var typeImageFilter = new tizen.AttributeFilter("type", "EXACTLY", "IMAGE");
  2. Create a composite filter that finds all content that matches one of the media type filters:

       var typeFilter = new tizen.CompositeFilter("UNION", [typeVideoFilter, typeImageFilter]);
  3. Create another attribute filter that includes content containing the word "special" in its title:

       var titleFilter = new tizen.AttributeFilter("title", "CONTAINS", "special");
  4. Create the final composite filter that finds all content that matches both the composite media type filter and the title filter:

       var finalFilter = new tizen.CompositeFilter("INTERSECTION", [typeFilter, titleFilter]);
  5. Call the find() method of the Content API's ContentManager interface (in mobile and wearable applications) to retrieve the media items that match the final filter:

       tizen.content.find(findMediaContentsCallback, onError, null, finalFilter);
    }

    The findMediaContentsCallback() event handler returns the query result.

Go to top