The major goal of this rewrite is to make it easier to implement power management policies correctly. According, the new implementation primarily uses state-based rather than event-based triggers for applying changes to the current power state. For example, when an application requests that the proximity sensor be used to manage the screen state (by way of a wake lock), the power manager makes note of the fact that the set of wake locks changed. Then it executes a common update function that recalculates the entire state, first looking at wake locks, then considering user activity, and eventually determining whether the screen should be turned on or off. At this point it may make a request to a component called the DisplayPowerController to asynchronously update the display's powe state. Likewise, DisplayPowerController makes note of the updated power request and schedules its own update function to figure out what needs to be changed. The big benefit of this approach is that it's easy to mutate multiple properties of the power state simultaneously then apply their joint effects together all at once. Transitions between states are detected and resolved by the update in a consistent manner. The new power manager service has is implemented as a set of loosely coupled components. For the most part, information only flows one way through these components (by issuing a request to that component) although some components support sending a message back to indicate when the work has been completed. For example, the DisplayPowerController posts a callback runnable asynchronously to tell the PowerManagerService when the display is ready. An important feature of this approach is that each component neatly encapsulates its state and maintains its own invariants. Moreover, we do not need to worry about deadlocks or awkward mutual exclusion semantics because most of the requests are asynchronous. The benefits of this design are especially apparent in the implementation of the screen on / off and brightness control animations which are able to take advantage of framework features like properties, ObjectAnimator and Choreographer. The screen on / off animation is now the responsibility of the power manager (instead of surface flinger). This change makes it much easier to ensure that the animation is properly coordinated with other power state changes and eliminates the cause of race conditions in the older implementation. The because of the userActivity() function has been changed so that it never wakes the device from sleep. This change removes ambiguity around forcing or disabling user activity for various purposes. To wake the device, use wakeUp(). To put it to sleep, use goToSleep(). Simple. The power manager service interface and API has been significantly simplified and consolidated. Also fixed some inconsistencies related to how the minimum and maximum screen brightness setting was presented in brightness control widgets and enforced behind the scenes. At present the following features are implemented: - Wake locks. - User activity. - Wake up / go to sleep. - Power state broadcasts. - Battery stats and event log notifications. - Dreams. - Proximity screen off. - Animated screen on / off transitions. - Auto-dimming. - Auto-brightness control for the screen backlight with different timeouts for ramping up versus ramping down. - Auto-on when plugged or unplugged. - Stay on when plugged. - Device administration maximum user activity timeout. - Application controlled brightness via window manager. The following features are not yet implemented: - Reduced user activity timeout for the key guard. - Reduced user activity timeout for the phone application. - Coordinating screen on barriers with the window manager. - Preventing auto-rotation during power state changes. - Auto-brightness adjustment setting (feature was disabled in previous version of the power manager service pending an improved UI design so leaving it out for now). - Interpolated brightness control (a proposed new scheme for more compactly specifying auto-brightness levels in config.xml). - Button / keyboard backlight control. - Change window manager to associated WorkSource with KEEP_SCREEN_ON_FLAG wake lock instead of talking directly to the battery stats service. - Optionally support animating screen brightness when turning on/off instead of playing electron beam animation (config_animateScreenLights). Change-Id: I1d7a52e98f0449f76d70bf421f6a7f245957d1d7
175 lines
6.2 KiB
Java
175 lines
6.2 KiB
Java
/*
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.android.statusbartest;
|
|
|
|
import android.os.Binder;
|
|
import android.os.IBinder;
|
|
import android.os.IPowerManager;
|
|
import android.content.ComponentName;
|
|
import android.content.pm.PackageManager;
|
|
import android.os.RemoteException;
|
|
import android.os.Handler;
|
|
import android.os.LocalPowerManager;
|
|
import android.os.ServiceManager;
|
|
import android.os.PowerManager;
|
|
|
|
public class PowerTest extends TestActivity
|
|
{
|
|
private final static String TAG = "PowerTest";
|
|
IPowerManager mPowerManager;
|
|
int mPokeState = 0;
|
|
IBinder mPokeToken = new Binder();
|
|
Handler mHandler = new Handler();
|
|
PowerManager mPm;
|
|
PowerManager.WakeLock mProx;
|
|
|
|
@Override
|
|
protected String tag() {
|
|
return TAG;
|
|
}
|
|
|
|
@Override
|
|
protected Test[] tests() {
|
|
mPowerManager = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
|
|
mPm = (PowerManager)getSystemService("power");
|
|
mProx = mPm.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "PowerTest-prox");
|
|
|
|
return mTests;
|
|
}
|
|
private Test[] mTests = new Test[] {
|
|
new Test("Enable settings widget") {
|
|
public void run() {
|
|
PackageManager pm = getPackageManager();
|
|
pm.setComponentEnabledSetting(new ComponentName("com.android.settings",
|
|
"com.android.settings.widget.SettingsAppWidgetProvider"),
|
|
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
|
|
|
|
}
|
|
},
|
|
new Test("Disable settings widget") {
|
|
public void run() {
|
|
PackageManager pm = getPackageManager();
|
|
pm.setComponentEnabledSetting(new ComponentName("com.android.settings",
|
|
"com.android.settings.widget.SettingsAppWidgetProvider"),
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
|
|
|
|
}
|
|
},
|
|
new Test("Enable proximity") {
|
|
public void run() {
|
|
mProx.acquire();
|
|
}
|
|
},
|
|
new Test("Disable proximity") {
|
|
public void run() {
|
|
mProx.release();
|
|
}
|
|
},
|
|
new Test("Disable proximity (WAIT_FOR_PROXIMITY_NEGATIVE)") {
|
|
public void run() {
|
|
mProx.release(PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE);
|
|
}
|
|
},
|
|
new Test("Enable proximity, wait 5 seconds then disable") {
|
|
public void run() {
|
|
mProx.acquire();
|
|
mHandler.postDelayed(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
mProx.release();
|
|
}
|
|
}, 5000);
|
|
}
|
|
},
|
|
new Test("Enable proximity, wait 5 seconds then disable (WAIT_FOR_PROXIMITY_NEGATIVE)") {
|
|
public void run() {
|
|
mProx.acquire();
|
|
mHandler.postDelayed(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
mProx.release(PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE);
|
|
}
|
|
}, 5000);
|
|
}
|
|
},
|
|
new Test("Touch events don't poke") {
|
|
public void run() {
|
|
mPokeState |= LocalPowerManager.POKE_LOCK_IGNORE_TOUCH_EVENTS;
|
|
try {
|
|
mPowerManager.setPokeLock(mPokeState, mPokeToken, TAG);
|
|
} catch (RemoteException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
},
|
|
|
|
new Test("Touch events poke") {
|
|
public void run() {
|
|
mPokeState &= ~LocalPowerManager.POKE_LOCK_IGNORE_TOUCH_EVENTS;
|
|
try {
|
|
mPowerManager.setPokeLock(mPokeState, mPokeToken, TAG);
|
|
} catch (RemoteException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
},
|
|
new Test("Short timeout") {
|
|
public void run() {
|
|
mPokeState &= ~LocalPowerManager.POKE_LOCK_TIMEOUT_MASK;
|
|
mPokeState |= LocalPowerManager.POKE_LOCK_SHORT_TIMEOUT;
|
|
try {
|
|
mPowerManager.setPokeLock(mPokeState, mPokeToken, TAG);
|
|
} catch (RemoteException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
},
|
|
new Test("Medium timeout") {
|
|
public void run() {
|
|
mPokeState &= ~LocalPowerManager.POKE_LOCK_TIMEOUT_MASK;
|
|
mPokeState |= LocalPowerManager.POKE_LOCK_MEDIUM_TIMEOUT;
|
|
try {
|
|
mPowerManager.setPokeLock(mPokeState, mPokeToken, TAG);
|
|
} catch (RemoteException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
},
|
|
new Test("Normal timeout") {
|
|
public void run() {
|
|
mPokeState &= ~LocalPowerManager.POKE_LOCK_TIMEOUT_MASK;
|
|
try {
|
|
mPowerManager.setPokeLock(mPokeState, mPokeToken, TAG);
|
|
} catch (RemoteException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
},
|
|
new Test("Illegal timeout") {
|
|
public void run() {
|
|
mPokeState |= LocalPowerManager.POKE_LOCK_SHORT_TIMEOUT
|
|
| LocalPowerManager.POKE_LOCK_MEDIUM_TIMEOUT;
|
|
try {
|
|
mPowerManager.setPokeLock(mPokeState, mPokeToken, TAG);
|
|
} catch (RemoteException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
},
|
|
};
|
|
}
|