Power Manager sample application documentation
authorAgnieszka Janowicz <a.janowicz@samsung.com>
Mon, 30 Nov 2015 14:30:11 +0000 (15:30 +0100)
committerAgnieszka Janowicz <a.janowicz@samsung.com>
Fri, 4 Dec 2015 10:22:40 +0000 (11:22 +0100)
Change-Id: Icd1f578bfec03e525234c2d7a0ca74b02dc1aa14
Signed-off-by: Agnieszka Janowicz <a.janowicz@samsung.com>
org.tizen.sampledescriptions/html/images/power_manager_battery_lower.png [new file with mode: 0644]
org.tizen.sampledescriptions/html/images/power_manager_device_charging.png [new file with mode: 0644]
org.tizen.sampledescriptions/html/images/power_manager_location_settings.png [new file with mode: 0644]
org.tizen.sampledescriptions/html/images/power_manager_main_view.png [new file with mode: 0644]
org.tizen.sampledescriptions/html/images/power_manager_power_mode_on.png [new file with mode: 0644]
org.tizen.sampledescriptions/html/images/power_manager_turn_off_settings.png [new file with mode: 0644]
org.tizen.sampledescriptions/html/images/power_manager_turn_on_settings.png [new file with mode: 0644]
org.tizen.sampledescriptions/html/mobile_n/power_manager_sd_mn.htm [new file with mode: 0644]
org.tizen.sampledescriptions/html/mobile_n/sd_mn.htm

diff --git a/org.tizen.sampledescriptions/html/images/power_manager_battery_lower.png b/org.tizen.sampledescriptions/html/images/power_manager_battery_lower.png
new file mode 100644 (file)
index 0000000..2037c90
Binary files /dev/null and b/org.tizen.sampledescriptions/html/images/power_manager_battery_lower.png differ
diff --git a/org.tizen.sampledescriptions/html/images/power_manager_device_charging.png b/org.tizen.sampledescriptions/html/images/power_manager_device_charging.png
new file mode 100644 (file)
index 0000000..83c0605
Binary files /dev/null and b/org.tizen.sampledescriptions/html/images/power_manager_device_charging.png differ
diff --git a/org.tizen.sampledescriptions/html/images/power_manager_location_settings.png b/org.tizen.sampledescriptions/html/images/power_manager_location_settings.png
new file mode 100644 (file)
index 0000000..b74c23f
Binary files /dev/null and b/org.tizen.sampledescriptions/html/images/power_manager_location_settings.png differ
diff --git a/org.tizen.sampledescriptions/html/images/power_manager_main_view.png b/org.tizen.sampledescriptions/html/images/power_manager_main_view.png
new file mode 100644 (file)
index 0000000..3bd1ac1
Binary files /dev/null and b/org.tizen.sampledescriptions/html/images/power_manager_main_view.png differ
diff --git a/org.tizen.sampledescriptions/html/images/power_manager_power_mode_on.png b/org.tizen.sampledescriptions/html/images/power_manager_power_mode_on.png
new file mode 100644 (file)
index 0000000..3e3d60c
Binary files /dev/null and b/org.tizen.sampledescriptions/html/images/power_manager_power_mode_on.png differ
diff --git a/org.tizen.sampledescriptions/html/images/power_manager_turn_off_settings.png b/org.tizen.sampledescriptions/html/images/power_manager_turn_off_settings.png
new file mode 100644 (file)
index 0000000..fab24d5
Binary files /dev/null and b/org.tizen.sampledescriptions/html/images/power_manager_turn_off_settings.png differ
diff --git a/org.tizen.sampledescriptions/html/images/power_manager_turn_on_settings.png b/org.tizen.sampledescriptions/html/images/power_manager_turn_on_settings.png
new file mode 100644 (file)
index 0000000..3f14afe
Binary files /dev/null and b/org.tizen.sampledescriptions/html/images/power_manager_turn_on_settings.png differ
diff --git a/org.tizen.sampledescriptions/html/mobile_n/power_manager_sd_mn.htm b/org.tizen.sampledescriptions/html/mobile_n/power_manager_sd_mn.htm
new file mode 100644 (file)
index 0000000..7b7338e
--- /dev/null
@@ -0,0 +1,463 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+       <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+       <meta http-equiv="X-UA-Compatible" content="IE=9" />
+       <link rel="stylesheet" type="text/css" href="../css/styles.css" />
+       <link rel="stylesheet" type="text/css" href="../css/snippet.css" />
+       <script type="text/javascript" src="../scripts/snippet.js"></script>
+       <script type="text/javascript" src="../scripts/jquery.util.js" charset="utf-8"></script>
+       <script type="text/javascript" src="../scripts/common.js" charset="utf-8"></script>
+       <script type="text/javascript" src="../scripts/core.js" charset="utf-8"></script>
+       <script type="text/javascript" src="../scripts/search.js" charset="utf-8"></script>
+
+       <title>Power Manager Sample Overview</title>
+</head>
+
+<body class="no-toc" onload="prettyPrint()" style="overflow: auto;">
+
+<div id="toc-navigation">
+</div>
+
+<div id="container"><div id="contents"><div class="content">
+       <div id="profile">
+               <p><img alt="Mobile native" src="../images/mobile_s_n.png"/></p>
+       </div>
+
+<h1>Power Manager Sample Overview</h1>
+
+<p>The sample demonstrates how to use System and App Control API to monitor and change device's settings (battery level, screen brightness, Wi-Fi, GPS, Bluetooth).</p>
+
+<p>The Power Manager sample application implements three following features:</p>
+<ul>
+       <li>
+               <p><u>Displaying device information</u></p>
+               <p>The panel at the top of the screen displays battery percentage and level (empty/critical/low/high), charging status (on/off) as well as the status of
+               some power consuming device's settings (Wi-Fi, Bluetooth, GPS).</p>
+       </li>
+       <li>
+               <p><u>Warning the user when the battery level drops too low</u></p>
+               <p>When the battery level drops below the defined warnings level, a popup is displayed to warn the user about it (Fig. 2). The user can manually set the warnings level.</p>
+       </li>
+       <li>
+               <p><u>Enabling/disabling custom Power Saving mode</u></p>
+               <p>The application provides a convenient way to change the following device's settings to reduce power consumption:</p>
+               <ul>
+                       <li>display brightness - when in Power Saving mode, the display brightness is changed to low (20%);</li>
+                       <li>Wi-Fi state - when in Power Saving mode, the Wi-Fi is automatically turned off;</li>
+                       <li>Bluetooth state - when in Power Saving mode, the user will be advised to turn off Bluetooth and, upon request, Settings application will be launched;</li>
+                       <li>GPS state - approach same as in the Bluetooth state case.</li>
+               </ul>
+               <p>When the Power Saving mode is disabled, display brightness is changed to full (100%) and the power consuming settings can be turned on the same way they were turned off
+               (whether automatically or upon request).</p>
+               <p>There are 3 types of alerts the user can encounter when trying to turn Power Saving mode on/off:</p>
+               <ul>
+                       <li>turn power consuming settings off - if the application detects that any of the three power consuming settings (Wi-Fi, Bluetooth or GPS) is on when enabling Power Saving mode,
+                       a popup is displayed to alert the user about it (Fig. 4) and offers to launch Settings application to manage it;</li>
+                       <li>turn power consuming settings on - similarly, if the application detects that any of the three settings is off when disabling Power Saving mode,
+                       a popup is displayed (Fig. 5);</li>
+                       <li>device is charging - Power Saving mode will not be enabled if the device is charging. If the user tries to turn the mode on in such situation, a popup
+                       is displayed to inform the user about it (Fig. 6).</li>
+               </ul>
+       </li>
+</ul>
+
+<table>
+<col/>
+<tr>
+  <td>
+  <p class="figure">Figure 1: Power Manager sample app main view.</p>
+  <p align="center"><img alt="Power Manager sample app main view" src="../images/power_manager_main_view.png" border="1"/></p>
+  </td>
+  <td>
+  <p class="figure">Figure 2: Power Manager sample app view: battery level dropped.</p>
+  <p align="center"><img alt="Power Manager sample app view: battery level dropped" src="../images/power_manager_battery_lower.png" border="1"/></p>
+  </td>
+  <td>
+  <p class="figure">Figure 3: Power Manager sample app view: Power Saving mode on.</p>
+  <p align="center"><img alt="Power Manager sample app view: Power Saving mode on" src="../images/power_manager_power_mode_on.png" border="1"/></p>
+  </td>
+</tr>
+<tr>
+  <td>
+  <p class="figure">Figure 4: Power Manager sample app main: turn off settings.</p>
+  <p align="center"><img alt="Power Manager sample app main: turn off settings" src="../images/power_manager_turn_off_settings.png" border="1"/></p>
+  </td>
+  <td>
+  <p class="figure">Figure 5: Power Manager sample app view: turn on settings.</p>
+  <p align="center"><img alt="Power Manager sample app view: turn on settings" src="../images/power_manager_turn_on_settings.png" border="1"/></p>
+  </td>
+  <td>
+  <p class="figure">Figure 6: Power Manager sample app view: device is charging.</p>
+  <p align="center"><img alt="Power Manager sample app view: device is charging" src="../images/power_manager_device_charging.png" border="1"/></p>
+  </td>
+</tr>
+</table>
+
+<p>The main view of the application (as seen in Fig. 1) consists of the following areas (top to bottom):</p>
+       <ul>
+               <li><p><u>Device status area</u></p>
+                       <p>Information about device's settings and state is displayed here. There are 5 subareas that convey the following data:</p>
+                       <ul>
+                               <li>charging status (on/off),</li>
+                               <li>Wi-Fi status (on/off),</li>
+                               <li>Bluetooth status (on/off),</li>
+                               <li>GPS status (on/off),</li>
+                               <li>Battery state (percentage and level).</li>
+                       </ul>
+               </li>
+               <li><p><u>Battery level warnings area</u></p>
+               <p>This is the control panel for warnings about battery level going too low. The user can set the battery level at which he or she wants to receive warnings using a slider.</p>
+               </li>
+               <li><p><u>Power Saving mode area</u></p>
+               <p>The area consists only of a button the user can use to turn the Power Saving mode on/off.</p>
+               </li>
+       </ul>
+
+
+<h2>Prerequisites</h2>
+<p>To ensure proper application execution, the following privileges must be set:</p>
+ <ul>
+  <li><span style="font-family: Courier New,Courier,monospace">http://tizen.org/privilege/network.get</span> - to read Wi-Fi status;</li>
+  <li><span style="font-family: Courier New,Courier,monospace">http://tizen.org/privilege/network.set</span> - to set Wi-Fi status;</li>
+  <li><span style="font-family: Courier New,Courier,monospace">http://tizen.org/privilege/appmanager.launch</span> - to launch Settings application;</li>
+  <li><span style="font-family: Courier New,Courier,monospace">http://tizen.org/privilege/display</span> - to change display brightness.</li>
+ </ul>
+
+
+<h2>Implementation</h2>
+
+<h3>Acquiring device's information</h3>
+
+<h5>Runtime information + Wi-Fi status</h5>
+
+<p>The Runtime Information API is used in this sample application to obtain the following runtime information:</p>
+<ul>
+       <li>battery charging status (<span style="font-family: Courier New,Courier,monospace">RUNTIME_INFO_KEY_BATTERY_IS_CHARGING</span>),</li>
+       <li>Bluetooth status (<span style="font-family: Courier New,Courier,monospace">RUNTIME_INFO_KEY_BLUETOOTH_ENABLED</span>),</li>
+       <li>GPS status (<span style="font-family: Courier New,Courier,monospace">RUNTIME_INFO_KEY_LOCATION_SERVICE_ENABLED</span>).</li>
+</ul>
+
+<p>In addition to this information, Wi-Fi status is acquired using the Wi-Fi API. In order to do that,
+the Wi-Fi module needs to be initialized first. It is done using the <span style="font-family: Courier New,Courier,monospace">wifi_initialize()</span>
+function. If the initialization is successful, Wi-Fi status is acquired using <span style="font-family: Courier New,Courier,monospace">wifi_is_activated()</span> function.</p>
+
+<pre class="prettyprint">
+bool dev_manager_get_runtime_info(bool *charging, bool *bluetooth_on, bool *wifi_on, bool *gps_on)
+{
+&nbsp;&nbsp;&nbsp;if (charging) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*charging = false;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, charging) != RUNTIME_INFO_ERROR_NONE) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get runtime info: battery is charging");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
+&nbsp;&nbsp;&nbsp;}
+&nbsp;&nbsp;&nbsp;settings_on[SETTINGS_CHARGING] = *charging;
+
+&nbsp;&nbsp;&nbsp;if (bluetooth_on) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*bluetooth_on = false;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (runtime_info_get_value_bool(RUNTIME_INFO_KEY_BLUETOOTH_ENABLED, bluetooth_on) != RUNTIME_INFO_ERROR_NONE) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get runtime info: bluetooth enabled");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
+&nbsp;&nbsp;&nbsp;}
+&nbsp;&nbsp;&nbsp;settings_on[SETTINGS_BLUETOOTH] = *bluetooth_on;
+
+&nbsp;&nbsp;&nbsp;if (wifi_on) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*wifi_on = false;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (wifi_initialize() != WIFI_ERROR_NONE || wifi_is_activated(wifi_on) != WIFI_ERROR_NONE) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get runtime info: wifi enabled");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
+&nbsp;&nbsp;&nbsp;}
+&nbsp;&nbsp;&nbsp;settings_on[SETTINGS_WIFI] = *wifi_on;
+
+&nbsp;&nbsp;&nbsp;if (gps_on) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*gps_on = false;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (runtime_info_get_value_bool(RUNTIME_INFO_KEY_LOCATION_SERVICE_ENABLED, gps_on) != RUNTIME_INFO_ERROR_NONE) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get runtime info: gps enabled");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
+&nbsp;&nbsp;&nbsp;}
+&nbsp;&nbsp;&nbsp;settings_on[SETTINGS_GPS] = *gps_on;
+
+&nbsp;&nbsp;&nbsp;return true;
+}
+</pre>
+
+<h5>Battery information</h5>
+
+<p>Battery information is obtained using Device/Battery API. The <span style="font-family: Courier New,Courier,monospace">device_battery_get_percent()</span>
+function is used to get the battery level in a form of a percentage whereas the <span style="font-family: Courier New,Courier,monospace">device_battery_get_level_status()</span>
+is used to get the battery level status. The API defines five battery levels:</p>
+<ul>
+       <li>DEVICE_BATTERY_LEVEL_EMPTY (<2%),</li>
+       <li>DEVICE_BATTERY_LEVEL_CRITICAL (2-5%),</li>
+       <li>DEVICE_BATTERY_LEVEL_LOW (6-15%),</li>
+       <li>DEVICE_BATTERY_LEVEL_HIGH (16-95%),</li>
+       <li>DEVICE_BATTERY_LEVEL_FULL (>95%).</li>
+</ul>
+
+<pre class="prettyprint">
+int dev_manager_get_battery_percent(void)
+{
+&nbsp;&nbsp;&nbsp;int percent = -1;
+
+&nbsp;&nbsp;&nbsp;if (device_battery_get_percent(&percent) != DEVICE_ERROR_NONE)
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get battery percentage");
+
+&nbsp;&nbsp;&nbsp;return percent;
+}
+
+char *dev_manager_get_battery_level(void)
+{
+&nbsp;&nbsp;&nbsp;device_battery_level_e status = DEVICE_BATTERY_LEVEL_EMPTY;
+
+&nbsp;&nbsp;&nbsp;if (device_battery_get_level_status(&status) != DEVICE_ERROR_NONE)
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get battery level");
+
+&nbsp;&nbsp;&nbsp;return __format_battery_level_string(status);
+}
+</pre>
+
+<h3>Setting relevant callbacks</h3>
+
+<p>To monitor changes in relevant device's settings and battery state, a set of callbacks is used.</p>
+
+<h5>Runtime information callbacks</h5>
+
+<p>To monitor relevant runtime information, the <span style="font-family: Courier New,Courier,monospace">runtime_info_set_changed()</span> function is used.
+It registers a change event callback for given runtime information key. In case of this application, callbacks are set for
+<span style="font-family: Courier New,Courier,monospace">RUNTIME_INFO_KEY_BLUETOOTH_ENABLED</span>,
+<span style="font-family: Courier New,Courier,monospace">RUNTIME_INFO_KEY_LOCATION_SERVICE_ENABLED</span> and
+<span style="font-family: Courier New,Courier,monospace">RUNTIME_INFO_KEY_BATTERY_IS_CHARGING</span> keys.</p>
+
+<pre class="prettyprint">
+bool dev_manager_set_runtime_info_callbacks(changed_setting_cb changed_cb)
+{
+&nbsp;&nbsp;&nbsp;if (runtime_info_set_changed_cb(RUNTIME_INFO_KEY_BLUETOOTH_ENABLED, __runtime_cb, changed_cb) != RUNTIME_INFO_ERROR_NONE ||
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runtime_info_set_changed_cb(RUNTIME_INFO_KEY_LOCATION_SERVICE_ENABLED, __runtime_cb, changed_cb) != RUNTIME_INFO_ERROR_NONE ||
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runtime_info_set_changed_cb(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, __runtime_cb, changed_cb) != RUNTIME_INFO_ERROR_NONE) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to set runtime callbacks");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+&nbsp;&nbsp;&nbsp;}
+
+&nbsp;&nbsp;&nbsp;return true;
+}
+</pre>
+
+<p>When a change event is detected and the <span style="font-family: Courier New,Courier,monospace">__runtime_cb</span> function is invoked which gets
+the runtime information key type as an argument. The new runtime information is used to update the application's UI using
+<span style="font-family: Courier New,Courier,monospace">changed_cb</span> function and the relevant setting's value is stored internally.</p>
+
+<pre class="pretty">
+static void __runtime_cb(runtime_info_key_e key, void *user_data)
+{
+&nbsp;&nbsp;&nbsp;bool val = false;
+&nbsp;&nbsp;&nbsp;changed_setting_cb changed_cb;
+
+&nbsp;&nbsp;&nbsp;if (!user_data) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get cb func");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;
+&nbsp;&nbsp;&nbsp;}
+
+&nbsp;&nbsp;&nbsp;changed_cb = (changed_setting_cb)user_data;
+
+&nbsp;&nbsp;&nbsp;switch (key) {
+&nbsp;&nbsp;&nbsp;case RUNTIME_INFO_KEY_BLUETOOTH_ENABLED:
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runtime_info_get_value_bool(RUNTIME_INFO_KEY_BLUETOOTH_ENABLED, &val);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;settings_on[SETTINGS_BLUETOOTH] = val;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed_cb(SETTINGS_BLUETOOTH, settings_on[SETTINGS_BLUETOOTH]);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
+&nbsp;&nbsp;&nbsp;case RUNTIME_INFO_KEY_LOCATION_SERVICE_ENABLED:
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runtime_info_get_value_bool(RUNTIME_INFO_KEY_LOCATION_SERVICE_ENABLED, &val);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;settings_on[SETTINGS_GPS] = val;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed_cb(SETTINGS_GPS, settings_on[SETTINGS_GPS]);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
+&nbsp;&nbsp;&nbsp;case RUNTIME_INFO_KEY_BATTERY_IS_CHARGING:
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &val);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;settings_on[SETTINGS_CHARGING] = val;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed_cb(SETTINGS_CHARGING, settings_on[SETTINGS_CHARGING]);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
+&nbsp;&nbsp;&nbsp;default:
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
+&nbsp;&nbsp;&nbsp;}
+}
+</pre>
+
+<h5>Device information callbacks</h5>
+
+<p>In order to be notified about battery state changes, the <span style="font-family: Courier New,Courier,monospace">device_add_callback()</span>
+is invoked for <span style="font-family: Courier New,Courier,monospace">DEVICE_CALLBACK_BATTERY_CAPACITY</span> (battery level percentage) and
+<span style="font-family: Courier New,Courier,monospace">DEVICE_CALLBACK_BATTERY_LEVEL</span> (battery level status) callback types.</p>
+
+<pre class="prettyprint">
+bool dev_manager_set_device_callbacks(changed_bat_percentage_cb percentage_cb, changed_bat_level_cb level_cb)
+{
+&nbsp;&nbsp;&nbsp;if (device_add_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, __bat_changed_cb, percentage_cb) != DEVICE_ERROR_NONE ||
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device_add_callback(DEVICE_CALLBACK_BATTERY_LEVEL, __bat_changed_cb, level_cb) != DEVICE_ERROR_NONE) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to set device callbacks");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+&nbsp;&nbsp;&nbsp;}
+
+&nbsp;&nbsp;&nbsp;return true;
+}
+</pre>
+
+<p>The <span style="font-family: Courier New,Courier,monospace">__bat_changed_cb()</span> callback function gets the callback type
+(<span style="font-family: Courier New,Courier,monospace">device_callback_e</span>) as an argument which is then used to update the application's UI accordingly.</p>
+
+<pre class="prettyprint">
+static void __bat_changed_cb(device_callback_e type, void *value, void *user_data)
+{
+&nbsp;&nbsp;&nbsp;changed_bat_percentage_cb percentage_cb;
+&nbsp;&nbsp;&nbsp;changed_bat_level_cb level_cb;
+&nbsp;&nbsp;&nbsp;int val = (int)value;
+
+&nbsp;&nbsp;&nbsp;if (!user_data) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get cb func");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;
+&nbsp;&nbsp;&nbsp;}
+
+&nbsp;&nbsp;&nbsp;switch (type) {
+&nbsp;&nbsp;&nbsp;case DEVICE_CALLBACK_BATTERY_CAPACITY:
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;percentage_cb = (changed_bat_percentage_cb)user_data;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;percentage_cb(val);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
+&nbsp;&nbsp;&nbsp;case DEVICE_CALLBACK_BATTERY_LEVEL:
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;level_cb = (changed_bat_level_cb)user_data;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;level_cb(__format_battery_level_string((device_battery_level_e)val));
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
+&nbsp;&nbsp;&nbsp;default:
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
+&nbsp;&nbsp;&nbsp;}
+}
+</pre>
+
+<h3>Setting display brightness</h3>
+
+<p>When the Power Saving mode is enabled, the application changes the device's display brightness. The Device/Display API is used to do that.
+The code snippet below shows the implementation of this feature.</p>
+
+<pre class="prettyprint">
+bool dev_manager_power_mode_set_brightness(Eina_Bool to_low)
+{
+&nbsp;&nbsp;&nbsp;int max = 0;
+&nbsp;&nbsp;&nbsp;int percent = to_low ? BRIGHTNESS_LEVEL_LOW : BRIGHTNESS_LEVEL_FULL;
+
+&nbsp;&nbsp;&nbsp;if (device_display_get_max_brightness(0, &max) != DEVICE_ERROR_NONE) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get max brightness");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+&nbsp;&nbsp;&nbsp;}
+
+&nbsp;&nbsp;&nbsp;if (device_display_set_brightness(0, (int)percent*max/100) != DEVICE_ERROR_NONE) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to set brightness");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+&nbsp;&nbsp;&nbsp;}
+
+&nbsp;&nbsp;&nbsp;return true;
+}
+</pre>
+
+<h3>Turning Settings on/off</h3>
+
+<p>To reduce power consumption when in Power Saving mode, the sample application allows the user to turn more power consuming
+settings off. Wi-Fi deactivation is done automatically using <span style="font-family: Courier New,Courier,monospace">wifi_deactivate()</span> function.</p>
+
+<pre class="prettyprint">
+bool dev_manager_power_mode_settings_off(Eina_Bool turn_off)
+{
+&nbsp;&nbsp;&nbsp;if (turn_off) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (settings_on[SETTINGS_WIFI] && wifi_deactivate(NULL, NULL) != WIFI_ERROR_NONE)
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to deactivate Wi-Fi");
+
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (settings_on[SETTINGS_GPS])
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__launch_setting_location_app();
+
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (settings_on[SETTINGS_BLUETOOTH])
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__launch_bluetooth_app();
+&nbsp;&nbsp;&nbsp;} else {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (settings_on[SETTINGS_WIFI] && wifi_activate(NULL, NULL) != WIFI_ERROR_NONE)
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_INFO, LOG_TAG, "Failed to activate Wi-Fi");
+
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!settings_on[SETTINGS_GPS])
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__launch_setting_location_app();
+
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!settings_on[SETTINGS_BLUETOOTH])
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__launch_bluetooth_app();
+&nbsp;&nbsp;&nbsp;}
+
+&nbsp;&nbsp;&nbsp;return true;
+}
+</pre>
+
+<h5>Launching Settings application</h5>
+
+<p>Changing the state of GPS or Bluetooth is not allowed for regular applications. To allow the user to conveniently change these settings, the Power Manager sample applications
+launches relevant module of Settings application upon user's request. App Control API is used to do that.
+In case of Bluetooth, <span style="font-family: Courier New,Courier,monospace">application/x-bluetooth-on-off</span> is set explicitly as the MIME type of the data to be edited, whereas
+in case of GPS settings, <span style="font-family: Courier New,Courier,monospace">org.tizen.setting-location</span> app ID is set, also with 'edit' operation type.
+See the code snippets below for the Settings application launching implementation.</p>
+
+<pre class="prettyprint">
+static void __launch_bluetooth_app(void)
+{
+&nbsp;&nbsp;&nbsp;app_control_h service = NULL;
+
+&nbsp;&nbsp;&nbsp;app_control_create(&service);
+&nbsp;&nbsp;&nbsp;if (!service) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to create app control handle");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;
+&nbsp;&nbsp;&nbsp;}
+
+&nbsp;&nbsp;&nbsp;app_control_set_operation(service, "http://tizen.org/appcontrol/operation/edit");
+&nbsp;&nbsp;&nbsp;app_control_set_mime(service, "application/x-bluetooth-on-off");
+
+&nbsp;&nbsp;&nbsp;if (app_control_send_launch_request(service, NULL, NULL) != APP_CONTROL_ERROR_NONE)
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to launch Bluetooth On/Off app");
+
+&nbsp;&nbsp;&nbsp;app_control_destroy(service);
+}
+</pre>
+
+<pre class="prettyprint">
+static void __launch_setting_location_app(void)
+{
+&nbsp;&nbsp;&nbsp;app_control_h service = NULL;
+
+&nbsp;&nbsp;&nbsp;app_control_create(&service);
+&nbsp;&nbsp;&nbsp;if (!service) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to create app control handle");
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;
+&nbsp;&nbsp;&nbsp;}
+
+&nbsp;&nbsp;&nbsp;app_control_set_operation(service, "http://tizen.org/appcontrol/operation/edit");
+&nbsp;&nbsp;&nbsp;app_control_set_app_id(service, "org.tizen.setting-location");
+
+&nbsp;&nbsp;&nbsp;if (app_control_send_launch_request(service, NULL, NULL) != APP_CONTROL_ERROR_NONE)
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dlog_print(DLOG_ERROR, LOG_TAG, "Failed to launch Location Settings app");
+
+&nbsp;&nbsp;&nbsp;app_control_destroy(service);
+}
+</pre>
+
+
+<script type="text/javascript" src="../scripts/jquery.zclip.min.js"></script>
+<script type="text/javascript" src="../scripts/showhide.js"></script>
+</div></div></div>
+
+<a class="top sms" href="#"><img src="../images/btn_top.gif" alt="Go to top" /></a>
+
+<div id="footer">
+<p class="footer">Except as noted, this content - excluding the Code Examples - is licensed under <a href="http://creativecommons.org/licenses/by/3.0/legalcode" target="_blank">Creative Commons Attribution 3.0</a> and all of the Code Examples contained herein are licensed under <a href="https://www.tizen.org/bsd-3-clause-license" target="_blank">BSD-3-Clause</a>.<br/>For details, see the <a href="https://www.tizen.org/content-license" target="_blank">Content License</a>.</p>
+</div>
+
+<script type="text/javascript">
+var _gaq = _gaq || [];
+_gaq.push(['_setAccount', 'UA-25976949-1']);
+_gaq.push(['_trackPageview']);
+(function() {
+var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+})();
+</script>
+
+</body>
+</html>
\ No newline at end of file
index c0d105c..e1b6989 100644 (file)
      <td><a href="piano_sd_mn.htm">Piano</a></td> 
      <td>Demonstrates how you can implement a complex view using EFL UI components with different EDC styles.</td>
     </tr>
+    <tr>
+     <td><a href="power_manager_sd_mn.htm">Power Manager</a></td>
+     <td>Demonstrates how you can use System and App Control API to monitor and change device's settings (battery level, screen brightness, Wi-Fi, GPS, Bluetooth).</td>
+    </tr>
        <tr>
      <td><a href="preference_sd_mn.htm">Preference</a></td>
      <td>Demonstrates how you can work with custom preferences.</td>