Page Example

SampleAccount Sample Overview

Mobile native

The Sample Account application demonstrates how to implement an account provider which can add and configure an account. This application uses the Account Manager API to control the account.

The following figure illustrates the main views of the Sample Account.

Figure: Sample Account views

Sample Account views Sample Account views

The application main window consists of 2 screens:

  • Sign in: Displays the account sign in view and handles adding a new account.
  • Configure: Displays the account information and setting for the selected account.

Prerequisites

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

  • http://tizen.org/privilege/account.read
  • http://tizen.org/privilege/account.write

Implementation

To run this application, go to Settings > Accounts. You can see SampleAccount on the Add account list.

Adding an Account in the Sign In View

To add an account in the sign in view:

  1. When the user clicks SIGN IN in the sign in view, the application adds an account with input text.

    The account_create() function is used to initialize the account manager handle, which is later used to set account data.

    int ret = 0;
    account_h account = NULL;
    
    // Create account handle
    ret = account_create(&account);
    
    if (ret != ACCOUNT_ERROR_NONE)
    {
       // Error handling
       return;
    }
    
  2. Set the user ID and password with the account_set_user_name() and account_set_access_token() functions.

    You can use various other account_set_XXX() account data functions too. For more information, see the Account Manager API.

    const char *user_name = NULL, *password = NULL;
    
    // Set account data
    ret = account_set_user_name(account, user_name);
    
    if (ret != ACCOUNT_ERROR_NONE)
    {
       // Error handling
       return;
    }
    
    ret = account_set_access_token(account, password);
    
    if (ret != ACCOUNT_ERROR_NONE)
    {
       // Error handling
       return;
    }
    
  3. Once the account data has been set, insert data to the account database. The account_insert_to_db() function uses the account ID as the second parameter.

    int account_id = 0;
    
    // Insert data to the database
    ret = account_insert_to_db(account, &account_id);
    
    if (ret != ACCOUNT_ERROR_NONE)
    {
       // Error handling
       return;
    }
    
  4. When no longer needed, release the account handle with the account_destroy() function:

    // Release account handle
    ret = account_destroy(account);
    
    if (ret != ACCOUNT_ERROR_NONE)
    {
       // Error handling
       return;
    }
    

Getting an Account in the Configure View

To get an account in the configure view:

  1. When the user selects the account that has been added to the account list, the account ID is handed to the application.

    Use the account ID to get the account data with the account_query_account_by_account_id() function.

    int account_id = handed_value;
    account_h account = NULL;
    int ret = 0;
    
    // Create the account with account_create()
    
    // Get account data with account_id
    ret = account_query_account_by_account_id(account_id, &account);
    
    if (ret != ACCOUNT_ERROR_NONE)
    {
       // Error handling
    }
  2. Get the user ID with the account_get_user_name() function.

    If you added more account data during the account creation, you can access that with various account_get_XXX() functions.

    // Get user ID from account data
    ret = account_get_user_name(account, &account_user_id);
    
    if (ret != ACCOUNT_ERROR_NONE) 
    {
       // Error handling
    }
    
    // Destroy the account with account_destroy()
    

Deleting an Account in the Configure View

When the user clicks Sign out in the configure view, the selected account is removed from the account database.

To delete an account, use the account_delete_from_db_by_id() function with the account ID.

There are several account_delete_XXX() functions provided for deleting an account. For more information, see the Account Manager API.

int account_id = handed_value;
int ret = 0;

// Remove the current account
ret = account_delete_from_db_by_id(account_id);

if (ret != ACCOUNT_ERROR_NONE) 
{
   // Error handling
}