直接上代码
1.从屏保界面 唤醒盒子
private void awakenFromDream(){ IDreamManager dreamManager = IDreamManager.Stub.asInterface( ServiceManager.getService(DreamService.DREAM_SERVICE)); try{ if (dreamManager != null && dreamManager.isDreaming()){ Log.d(TAG,"Wake up from dream."); dreamManager.awaken(); } } catch (Exception e){ Log.e(TAG,"Cannot awaken from dream,because " + e.getMessage()); } }
frameworks\base\core\java\android\service\dreams\IDreamManager.aidl
IDreamManager为aidl接口,具体实现在 BinderService里,BinderService是DreamManagerService的内部类
private final class BinderService extends IDreamManager.Stub {
frameworks\base\services\core\java\com\android\server\dreams\DreamManagerService.java
@Override // Binder call public void awaken() { checkPermission(android.Manifest.permission.WRITE_DREAM_STATE); final long ident = Binder.clearCallingIdentity(); try { requestAwakenInternal(); } finally { Binder.restoreCallingIdentity(ident); } }
2.进入 真待机
private void shutDown(){ Slog.d(TAG, "Device will shut down immediately."); final long ident = Binder.clearCallingIdentity(); try { shutdownOrRebootInternal(HALT_MODE_SHUTDOWN, false, "userrequested", false); } finally { Binder.restoreCallingIdentity(ident); } }
3.进入 假待机
private void sleep(){ Slog.d(TAG, "Device will go to sleep immediately."); synchronized (mLock) { if (goToSleepNoUpdateLocked(SystemClock.uptimeMillis(),PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID)) { updatePowerStateLocked(); } } }
4.进入屏保
private void startDream(){ IDreamManager dreamManager = IDreamManager.Stub.asInterface( ServiceManager.getService(DreamService.DREAM_SERVICE)); try{ if (dreamManager != null && dreamManager.isDreaming()){ Log.d(TAG,"Wake up from dream."); dreamManager.dream(); } } catch (Exception e){ Log.e(TAG,"Cannot awaken from dream,because " + e.getMessage()); } }
5.待机唤醒
frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java
private void wakeUpFromPowerKey(long eventTime) { if (wakeUp(eventTime, mAllowTheaterModeWakeFromPowerKey, PowerManager.WAKE_REASON_POWER_BUTTON, "android.policy:POWER")) { // Start HOME with "reason" extra if sleeping for more than mWakeUpToLastStateTimeout if (shouldWakeUpWithHomeIntent()) { startDockOrHome(DEFAULT_DISPLAY, /*fromHomeKey*/ false, /*wakenFromDreams*/ true, PowerManager.wakeReasonToString(PowerManager.WAKE_REASON_POWER_BUTTON)); } } } private void wakeUpFromWakeKey(KeyEvent event) { if (wakeUp(event.getEventTime(), mAllowTheaterModeWakeFromKey, PowerManager.WAKE_REASON_WAKE_KEY, "android.policy:KEY")) { // Start HOME with "reason" extra if sleeping for more than mWakeUpToLastStateTimeout if (shouldWakeUpWithHomeIntent() && event.getKeyCode() == KEYCODE_HOME) { startDockOrHome(DEFAULT_DISPLAY, /*fromHomeKey*/ true, /*wakenFromDreams*/ true, PowerManager.wakeReasonToString(PowerManager.WAKE_REASON_WAKE_KEY)); } } } private boolean wakeUp(long wakeTime, boolean wakeInTheaterMode, @WakeReason int reason, String details) { final boolean theaterModeEnabled = isTheaterModeEnabled(); if (!wakeInTheaterMode && theaterModeEnabled) { return false; } if (theaterModeEnabled) { Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.THEATER_MODE_ON, 0); } mPowerManager.wakeUp(wakeTime, reason, details); // Turn on the connected TV and switch HDMI input if we're a HDMI playback device. final HdmiControl hdmiControl = getHdmiControl(); if (hdmiControl != null) { hdmiControl.turnOnTv(); } return true; }
frameworks\base\services\core\java\com\android\server\power\PowerManagerService.java
@Override // Binder call public void wakeUp(long eventTime, @WakeReason int reason, String details, String opPackageName) { if (eventTime > mClock.uptimeMillis()) { throw new IllegalArgumentException("event time must not be in the future"); } mContext.enforceCallingOrSelfPermission( android.Manifest.permission.DEVICE_POWER, null); final int uid = Binder.getCallingUid(); final long ident = Binder.clearCallingIdentity(); try { wakeUpInternal(eventTime, reason, details, uid, opPackageName, uid); } finally { Binder.restoreCallingIdentity(ident); } }