1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
---
title: Power Management
slug: WebAPI/Power_Management
translation_of: Archive/B2G_OS/API/Power_Management_API
---
<p>{{ non-standard_header() }}</p>
<p>{{ B2GOnlyHeader2('certified') }}</p>
<h2 id="摘要">摘要</h2>
<p>Power Management API 可管理裝置的耗電情形。</p>
<h2 id="管理電力">管理電力</h2>
<p>電力管理與電力本身不盡相同,如避免大量耗電、限制回流 (Reflow) 等等,都屬於電源管理的一部分。而 Power Management API 則直接控管如 CPU 與螢幕等的耗電量。透過 <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.mozPower" title="/en-US/docs/Web/API/window.navigator.mozPower"><code>navigator.mozPower</code></a> 即可存取電力管理的主要介面。而 <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.mozPower" title="/en-US/docs/Web/API/window.navigator.mozPower"><code>navigator.mozPower</code></a> 則為 <a href="https://developer.mozilla.org/en-US/docs/Web/API/PowerManager" title="/en-US/docs/Web/API/PowerManager"><code>PowerManager</code></a> 介面的實例 (Instance)。</p>
<h3 id="基本電力操作">基本電力操作</h3>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/PowerManager" title="/en-US/docs/Web/API/PowerManager"><code>PowerManager</code></a> 介面可管理基本的電力操作情形。</p>
<h4 id="通用電力操作">通用電力操作</h4>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/PowerManager.powerOff" title="/en-US/docs/Web/API/PowerManager.powerOff"><code>powerOff()</code></a> 函式可關閉裝置;<a href="https://developer.mozilla.org/en-US/docs/Web/API/PowerManager.reboot" title="/en-US/docs/Web/API/PowerManager.reboot"><code>reboot()</code></a> 函式則可重新開機。</p>
<pre class="brush: js">navigator.mozPower.powerOff();</pre>
<h4 id="螢幕電力操作">螢幕電力操作</h4>
<p>透過可讀寫的 <a href="https://developer.mozilla.org/en-US/docs/Web/API/PowerManager.screenEnabled" title="/en-US/docs/Web/API/PowerManager.screenEnabled"><code>screenEnabled</code></a> 屬性即可開/關螢幕畫面;而存取/更改畫面亮度也能達到相同效果。如可讀寫的 <a href="https://developer.mozilla.org/en-US/docs/Web/API/PowerManager.screenBrightness" title="/en-US/docs/Web/API/PowerManager.screenBrightness"><code>screenBrightness</code></a> (用以定義螢幕背光的亮度) 就能變更畫面亮度,可調整為 0 (全暗) 到 1 (全亮)。</p>
<pre class="brush: js">// It doesn't make sense to change the brightness if the screen is off
if (navigator.mozPower.screenEnabled) {
navigator.mozPower.screenBrightness = 0.5;
}</pre>
<h4 id="CPU_電力操作">CPU 電力操作</h4>
<p>目前並無法直接關閉 CPU。但可進一步設定 <a href="https://developer.mozilla.org/en-US/docs/Web/API/PowerManager.cpuSleepAllowed" title="/en-US/docs/Web/API/PowerManager.cpuSleepAllowed"><code>cpuSleepAllowed</code></a>,以決定關閉畫面之時是否要關閉 CPU。在畫面關閉 (<code>true</code>) 或開啟 (<code>false</code>) 時,此函式可定義是否讓裝置的 CPU 休眠。若畫面處於開啟狀態 (<code>false</code>),則該函式將避免裝置進入靜止狀態。</p>
<h3 id="進階電力操作">進階電力操作</h3>
<p>若管理電力的 Apps 可確實接收到第三方 Apps 的需求通知,就能達到更好的電力管理效果。舉例來說,在使用者欣賞影片時,當然不希望系統在幾分鐘之後自動關閉畫面。</p>
<h4 id="請求_Wake_locks">請求 Wake locks</h4>
<p>任何 Apps 均可請求 Wake locks,避免裝置切斷 Apps 本身所需資源的電力。可透過 <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.requestWakeLock" title="/en-US/docs/Web/API/window.navigator.requestWakeLock"><code>navigator.requestWakeLock()</code></a> 函式請求 Wake locks。</p>
<p>特定資源所要求的 Wake locks,可能會因不同的理由而取消。舉例來說,行動裝置的電力管理功能,可能會在閒置一段時間之後決定關閉畫面,進而達到省電目的。在關閉相關資源之前,處理資源的 Apps 往往會檢查相關資源的鎖定狀態。再舉個例子,某個頁面可能持續 <code>screen </code>上的鎖定狀態,以避免畫面關閉或螢幕保護程式運作。</p>
<p>依預設值,Firefox OS 允許 <code>screen、cpu、wifi</code> 資源。但只要是處理資源的任何 App 均可定義資源名稱,並針對該鎖定功能設定規則。舉例來說,資源管理功能可無視 Apps 對 <code>screen</code> 所進行的 Wake locks,讓 Apps 無法插手鎖定狀態。</p>
<pre class="brush: js">var lock = navigator.requestWakeLock('screen');</pre>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.requestWakeLock" title="/en-US/docs/Web/API/window.navigator.requestWakeLock"><code>requestWakeLock</code></a> 函式所回傳的物件將包含 <code>topic</code> 屬性,代表目前要鎖定的資源。而 <code>unlock()</code> 函式可供手動解鎖。另請注意,若 App 進入關閉狀態 (真的關閉,而不是閒置),就會自動送出該 App 需要的所有鎖定請求。</p>
<h4 id="處理_Wake_locks">處理 Wake locks</h4>
<p>只要是可管理鎖定的 Certified Apps,均可得知鎖定狀態的變化。其實任何管理電力的 Apps 均應監聽 <code>screen</code> 與 <code>cpu 鎖定狀態的變化。透過</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/PowerManager.addWakeLockListener" title="/en-US/docs/Web/API/PowerManager.addWakeLockListener"><code>PowerManager.addWakeLockListener()</code></a> 函式即可達到此目的。另可透過 <a href="https://developer.mozilla.org/en-US/docs/Web/API/PowerManager.removeWakeLockListener" title="/en-US/docs/Web/API/PowerManager.removeWakeLockListener"><code>PowerManager.removeWakeLockListener()</code></a> 函式而停止監聽鎖定請求。</p>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/PowerManager.addWakeLockListener" title="/en-US/docs/Web/API/PowerManager.addWakeLockListener"><code>addWakeLockListener</code></a> 函式所接收的回呼 (Callback) 需具備 2 組參數。第一組字串代表應處理的資源 (本範例為 <code>screen</code> 或 <code>cpu</code>);第二組字串則代表鎖定的狀態。</p>
<p>鎖定可分為 3 種狀態:</p>
<dl>
<dt>
<code>unlocked</code></dt>
<dd>
沒有任何 App 針對既有資源持續 Wake lock。</dd>
<dt>
<code>locked-foreground</code></dt>
<dd>
至少 1 個 App 持續 Wake lock,且於前景顯示該 App。</dd>
<dt>
<code>locked-background</code></dt>
<dd>
至少 1 個 App 持續 Wake lock,但所有 Apps 均在背景而未顯示。</dd>
</dl>
<pre class="brush: js">// This is used to keep track of the last change on the lock state
var screenTimeout;
// A reference to the Power Manager
var power = window.navigator.mozPower;
// Here are the actions to handle based on the lock state
var powerAction = {
// If there is no lock at all, we will suspend the device:
// * Turn the screen off
// * Allow the cpu to shut down
unlocked: function suspendDevice() {
power.cpuSleepAllowed = true;
power.screenEnabled = false;
},
// If there is a lock but the applications requesting it
// are all in the background, we just turn the screen off
'locked-background': function shutOffOnlyScreen() {
power.cpuSleepAllowed = false;
power.screenEnabled = false;
},
// At last, if there is an active application that requests a lock,
// actually there is nothing to do. That's the whole point.
}
function screenLockListener(topic, state) {
// If the lock is not about the screen, there is nothing to do.
if ('screen' !== topic) return;
// Each time the lock changes state, we stop any pending power management operations
window.clearTimeout(screenTimeout);
// If there is an action defined for the given state
if (powerAction[state]) {
// We delay that action by 3s
screenTimeout = window.setTimeout(powerAction[state], 3000);
}
}
// We make sure our power management application is listening for any change on locks.
power.addWakeLockListener(screenLockListener);</pre>
<h2 id="Specification" name="Specification">規格</h2>
<p>尚無任何規格。</p>
<h2 id="另可參閱">另可參閱</h2>
<ul>
<li>{{ domxref("window.navigator.mozPower","navigator.mozPower") }}</li>
<li>{{ domxref("PowerManager") }}</li>
<li>{{ domxref("window.navigator.requestWakeLock()","navigator.requestWakeLock()") }}</li>
</ul>
|