Creating a flashlight application

After creating a “hello world” app, a beginning programmer usually starts creating more and more complex applications. A flashlight app is usually one of the first projects beginners start. In this article we will demonstrate how to create such an app.

Where to start?

The first thing is to meet the necessary conditions, for the app to work. We will be making an app that has a button which, when clicked, toggles the LED light on or off. Furthermore, once the LED has been turned on, a notification will be displayed.

In order to use the LED and notifications, we need the appropriate privileges in the tizen-manifest.xml file.

    <privileges>
        <privilege>http://tizen.org/privilege/notification</privilege>
        <privilege>http://tizen.org/privilege/led</privilege>
    </privileges>

Having done that, we can move on to creating the application itself.

Building the app

Since this is a simple application, we won’t be using too many custom methods. In the header file we have only three declarations:

void change_state(bool on, appdata_s *ad);
void set_notification(appdata_s *ad);
void delete_notification(appdata_s *ad);

The header file also includes an appdata structure:

typedef struct appdata {
    Evas_Object *win;
    Evas_Object *button;
    notification_h notification;
} appdata_s;

It includes pointers to window and on/off button objects and a notification handler object.

As mentioned above, the app will feature a button that toggles the LED light on and off.

We create it in the create_base_gui method.

/* Create on/off button */
ad->button = elm_button_add(ad->win);
elm_object_text_set(ad->button, "<font_size=30>ON</font_size>");
evas_object_resize(ad->button, 0.6 * screen_width, button_height);
evas_object_move(ad->button, 0.2 * screen_width,
        (screen_height - button_height) / 2);
evas_object_show(ad->button);
elm_object_content_set(ad->win, ad->button);

We connect to the button via the _button_clicked callback function and pass a pointer to application data object.

evas_object_smart_callback_add(ad->button, "clicked", _button_clicked, ad);

We then setup what should happen when the button is clicked.

static void _button_clicked(void *data, Evas_Object *obj, void *event_info) {
    if (!turned_on) {
		change_state(true, data);
		set_notification(data);
	} else {
		change_state(false, data);
		delete_notification(data);
	}
}

If the light is off (turned_on value is false) then we turn it on using the change_state() function. We also add a notification to the notification bar that enables user to turn off the light even when the application is in background. If the light is already on we turn it off and delete the notification. Now, let's take a closer look at change_state() and delete_notification() methods.

void change_state(bool on, appdata_s *ad) {
    if (on) {
		int max_brightness;
		device_flash_get_max_brightness(&max_brightness);
		device_flash_set_brightness(max_brightness);
		elm_object_text_set(ad->button, "<font_size=30>OFF</font_size>");
		turned_on = true;
	} else {
		device_flash_set_brightness(0);
		elm_object_text_set(ad->button, "<font_size=30>ON</font_size>");
		turned_on = false;
	}
}

Change_state() method is the heart of the application. It takes two parameters. The first one is a logic value weather to turn the light on or off. The second one is a pointer to application data. We need it in order to access the text on the button and change it's value according to flashlight state changes. To actually change LED status we use device_flash_set_brightness(). It requires an integer brightness value (0-MAX). The maximal brightness can be obtained with device_flash_get_max_brightness() method.

We also have to remember to turn the light off in app_terminate() method:

static void app_terminate(void *data) {
    ...
    device_flash_set_brightness(0);
    ...
}

Working with notifications

Notification is added every time the light is turned on so that the users can turn the light off when the application goes to the bacground. It is done in the set_notification() method:

void set_notification(appdata_s *ad) {
        ad->notification = notification_create(NOTIFICATION_TYPE_NOTI);
	notification_set_text(ad->notification, NOTIFICATION_TEXT_TYPE_TITLE, "Flashlight",
	NULL, NOTIFICATION_VARIABLE_TYPE_NONE);

	notification_set_text(ad->notification, NOTIFICATION_TEXT_TYPE_CONTENT, "Tap to turn off",
	NULL, NOTIFICATION_VARIABLE_TYPE_NONE);

	notification_set_display_applist(ad->notification, NOTIFICATION_DISPLAY_APP_ALL);

	notification_set_size(ad->notification, 0.5);

	notification_set_layout(ad->notification, NOTIFICATION_LY_ONGOING_EVENT);

	app_control_h app_control = NULL;
	int noti_err = NOTIFICATION_ERROR_NONE;

	app_control_create(&app_control);
	app_control_set_app_id(app_control, "org.tizen.flashlight");

	noti_err = notification_set_launch_option(ad->notification, NOTIFICATION_LAUNCH_OPTION_APP_CONTROL,
			(void *) app_control);
	if (noti_err != NOTIFICATION_ERROR_NONE) {
		notification_free(ad->notification);
		return;
	}

	app_control_destroy(app_control);

	notification_post(ad->notification);
}

The notification_create() method is used to initialize notification. Then various notification parameters are set. After that we set launch callback for the action of clicking on the notification with notification_set_launch_option(). It requieres an app_control object that tells the system which application to launch. Now clicking on the notification will trigger app_control() method of Flashlight application:

static void app_control(app_control_h app_control, void *data) {
    change_state(false, data);
}

Finally, we have to implement a method cleaning the notifications. It is called when the light is turned off and in the app_terminate() method.

void delete_notification(appdata_s *ad) {
    notification_delete(ad->notification);
    notification_free(ad->notification);
}

For more information on notifications, please see the Notification tutorial in the Help section of Tizen SDK.

첨부 파일: 
List
SDK Version Since: 
2.3.1