上层对于kernel进入深睡的处理

来源:互联网 发布:阿里云 邮件超时 编辑:程序博客网 时间:2024/06/06 03:54
在PowerManagerService中加入内容:

private NusmartPM nusmartPM;

 PowerManagerService() {
     // Hack to get our uid...  should have a func for this.
     long token = Binder.clearCallingIdentity();
     MY_UID = Process.myUid();
     MY_PID = Process.myPid();
     Binder.restoreCallingIdentity(token);

     // XXX remove this when the kernel doesn't timeout wake locks
     Power.setLastUserActivityTimeout(7*24*3600*1000); // one week

     // assume nothing is on yet
     mUserState = mPowerState = 0;

     // Add ourself to the Watchdog monitors.
     Watchdog.getInstance().addMonitor(this);

     if (SystemProperties.getBoolean("ro.nusmart.pm", false)) {
         nusmartPM = new NusmartPM();
     }
 }


private int setScreenStateLocked(boolean on) {
        if (DEBUG_SCREEN_ON) {
            RuntimeException e = new RuntimeException("here");
            e.fillInStackTrace();
            Slog.i(TAG, "Set screen state: " + on, e);
        }
        if (on) {
            if ((mPowerState & SCREEN_ON_BIT) == 0 || mSkippedScreenOn) {
                // If we are turning the screen state on, but the screen
                // light is currently off, then make sure that we set the
                // light at this point to 0.  This is the case where we are
                // turning on the screen and waiting for the UI to be drawn
                // before showing it to the user.  We want the light off
                // until it is ready to be shown to the user, not it using
                // whatever the last value it had.
                if (DEBUG_SCREEN_ON) {
                    Slog.i(TAG, "Forcing brightness 0: mPowerState=0x"
                            + Integer.toHexString(mPowerState)
                            + " mSkippedScreenOn=" + mSkippedScreenOn);
                }
                // removed by donglongjiang@TEI for resolving flicker when starting
                //mScreenBrightness.forceValueLocked(Power.BRIGHTNESS_OFF);
            }
        }

        if (nusmartPM != null) {
            if (on) {
                nusmartPM.resume();
            } else {
                nusmartPM.suspend();
            }
        }

        int err = Power.setScreenState(on);
        if (err == 0) {
            mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0);
            if (mUseSoftwareAutoBrightness) {
                enableLightSensorLocked(on);
                if (!on) {
                    // make sure button and key backlights are off too
                    mButtonLight.turnOff();
                    mKeyboardLight.turnOff();
                    // clear current value so we will update based on the new conditions
                    // when the sensor is reenabled.
                    mLightSensorValue = -1;
                    // reset our highest light sensor value when the screen turns off
                    mHighestLightSensorValue = -1;
                }
            }
        }
        return err;
    }


 void systemReady() {
     mSensorManager = new SensorManager(mHandlerThread.getLooper());
     mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
     // don't bother with the light sensor if auto brightness is handled in hardware
     if (mUseSoftwareAutoBrightness) {
         mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
     }

     // wait until sensors are enabled before turning on screen.
     // some devices will not activate the light sensor properly on boot
     // unless we do this.
     if (mUseSoftwareAutoBrightness) {
         // turn the screen on
         setPowerState(SCREEN_BRIGHT);
     } else {
         // turn everything on
         setPowerState(ALL_BRIGHT);
     }

     synchronized (mLocks) {
         Slog.d(TAG, "system ready!");
         mDoneBooting = true;

         if (nusmartPM != null) {
             nusmartPM.ready();
         }

         enableLightSensorLocked(mUseSoftwareAutoBrightness && mAutoBrightessEnabled);

         long identity = Binder.clearCallingIdentity();
         try {
             mBatteryStats.noteScreenBrightness(getPreferredBrightness());
             mBatteryStats.noteScreenOn();
         } catch (RemoteException e) {
             // Nothing interesting to do.
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
     }
 }

 private final class NusmartPM {
        boolean isOnBeforeSuspendWifi = false;
        boolean isOnBeforeSuspendWifiAp = false;
        boolean isOnBeforeSuspendWifiP2p = false;
        boolean isOnBeforeSuspendBT = false;
        boolean isReady = false;
        boolean hasSuspendTask = false;
        private WifiManager wifiManager;
        private WifiP2pManager mwifiP2pManager;
        private WifiP2pManager.Channel mChannel;
        private BluetoothAdapter btAdapter;
        long preTraficPacketsWifi = 0;
        //boolean isOnBeforeSuspend3G = false;
        static final String NUSMART_PARTIAL_NAME = "NusmartPM";
        //LWC@nusmart modify,LWC fix bug 0003757
        static final int NUSMART_SUSPEND_DELAY = 1 * 1000;
        static final int NUSMART_RELEASE_WAKE_LOCK_DELAY = 10 * 1000;
        static final int NUSMART_WIFI_TRAFFIC_THRESHOLD = 10;

        NusmartPM() {

        }

        long getWifiTraffic() {
            String wifiInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
            long total = 0;
            long packetsTx = TrafficStats.getTxPackets(wifiInterfaceName);
            long packetsRx = TrafficStats.getRxPackets(wifiInterfaceName);
            total += packetsTx > 0 ? packetsTx : 0;
            total += packetsRx > 0 ? packetsRx : 0;
            return total;
        }

        synchronized void ready() {
            if (isReady) {
                return;
            }
            isReady = true;
            if (SystemProperties.getBoolean("ro.nusmart.pm.wifi", false)) {
                wifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
                mwifiP2pManager = (WifiP2pManager)mContext.getSystemService(Context.WIFI_P2P_SERVICE);
                if(mwifiP2pManager != null){
                    mChannel = mwifiP2pManager.initialize(mContext, mContext.getMainLooper(), null);
                }
            }
            if (SystemProperties.getBoolean("ro.nusmart.pm.bt", false)) {
                btAdapter = BluetoothAdapter.getDefaultAdapter();
            }
        }

        synchronized void suspend() {
            if (!isReady) {
                return;
            }
            hasSuspendTask = true;
            /*
            //add softap/wifi-direct s3 policy.
            //if (wifiManager !=null && wifiManager.isWifiEnabled()){
            if (wifiManager !=null && mwifiP2pManager != null && mChannel != null) {
                if (wifiManager.isWifiEnabled()){
                    preTraficPacketsWifi = getWifiTraffic();
                    hasSuspendTask = true;
                } else if (wifiManager.isWifiApEnabled()){
                    hasSuspendTask = true;
                } else if (SystemProperties.get("net.wifi.p2p.enable").equals("1")){
   //since the p2p donot support auto reconnect when disconnet, we disabled it.
   hasSuspendTask = true;
                } else {
                    hasSuspendTask = false;
                }
            }

            if (btAdapter != null && btAdapter.isEnabled()) {
                hasSuspendTask = true;
            }
            */
            if (hasSuspendTask) {
                Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK, NUSMART_PARTIAL_NAME);
                //LWC@nusmart modify,LWC fix bug 0003757
                SystemProperties.set("sys.s3.status","IN_S3");
                mHandler.postDelayed(suspendTask, NUSMART_SUSPEND_DELAY);
            }
        }

        synchronized void resume() {
            if (!isReady) {
                return;
            }
            // the system has not entered into S3
            if (hasSuspendTask) {
                hasSuspendTask = false;
                mHandler.removeCallbacks(suspendTask);
                Power.releaseWakeLock(NUSMART_PARTIAL_NAME);
                return;
            }

            //LWC@nusmart modify,LWC fix bug 0003757
            SystemProperties.set("sys.s3.status","OUT_S3");
            mHandler.removeCallbacks(releaseWakeLockTask);

            if (isOnBeforeSuspendWifi) {
                wifiManager.setWifiEnabled(true);
                isOnBeforeSuspendWifi = false;
                isOnBeforeSuspendWifiAp = false;
                isOnBeforeSuspendWifiP2p = false;
            }else if (isOnBeforeSuspendWifiAp) {
                wifiManager.setWifiApEnabled(null,true);
                isOnBeforeSuspendWifi = false;
                isOnBeforeSuspendWifiAp = false;
                isOnBeforeSuspendWifiP2p = false;
            }else if (isOnBeforeSuspendWifiP2p) {
                mwifiP2pManager.enableP2p(mChannel);
SystemProperties.set("net.wifi.p2p.enable","1");
                isOnBeforeSuspendWifi = false;
                isOnBeforeSuspendWifiAp = false;
                isOnBeforeSuspendWifiP2p = false;
            }

            if (isOnBeforeSuspendBT) {
                btAdapter.enable();
                isOnBeforeSuspendBT = false;
            }
        }

        private Runnable releaseWakeLockTask = new Runnable() {
            public void run() {
                Power.releaseWakeLock(NusmartPM.NUSMART_PARTIAL_NAME);
            }
        };

        private Runnable suspendTask = new Runnable() {
            private boolean hasPartialWakeLock() {
                final String filename = "/sys/power/wake_lock";
                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new FileReader(filename));
                    String line = reader.readLine();
                    String wakeLocks[] = line.split(" ");
                    for (String wl : wakeLocks) {
                        if (!NUSMART_PARTIAL_NAME.equals(wl)) {
                            return true;
                        }
                    }
                    return false;
                } catch (IOException ex) {
                    Log.w(TAG, "Couldn't read wakelock from " + filename + ": " + ex);
                    //any wrong when read the file, keep system not to suspend
                    return true;
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException ex) {
                        }
                    }
                }
            }

            public void run() {
                synchronized (NusmartPM.this) {
                    //Task is cancled by resume
                    if (!hasSuspendTask) {
                        return;
                    }
                    if (hasPartialWakeLock()) {
                        mHandler.postDelayed(suspendTask, NusmartPM.NUSMART_SUSPEND_DELAY);
                        return;
                    }
   //Modify by chen for add p2p/softap branch.
                    //This order is must wifi/softap-->wifi-direct, for auto switch from wifi-diretct to softap,the property is still 1.
                    //if (wifiManager != null) {
                    if (wifiManager != null && mwifiP2pManager != null && mChannel != null ) {
                        if (wifiManager.isWifiEnabled()) {
                            long traffic = getWifiTraffic();
                            /*if (traffic > preTraficPacketsWifi + NUSMART_WIFI_TRAFFIC_THRESHOLD) {
                                preTraficPacketsWifi = traffic;
                                mHandler.postDelayed(suspendTask, NusmartPM.NUSMART_SUSPEND_DELAY);
                                return;
                            }
                            */
                            wifiManager.setWifiEnabled(false);
                            //avoid to auto switch from wifi/softap/wifi-direct, disable all.
   wifiManager.setWifiApEnabled(null,false);
                            mwifiP2pManager.disableP2p(mChannel);
                            SystemProperties.set("net.wifi.p2p.enable","0");
                            isOnBeforeSuspendWifi = true;
                        } else if (wifiManager.isWifiApEnabled()) {
                            wifiManager.setWifiApEnabled(null,false);
                            //avoid to auto switch from wifi/softap/wifi-direct, disable all.
                            mwifiP2pManager.disableP2p(mChannel);
                            SystemProperties.set("net.wifi.p2p.enable","0");
                            wifiManager.setWifiEnabled(false);
                            isOnBeforeSuspendWifiAp = true;
                        } else if( SystemProperties.get("net.wifi.p2p.enable").equals("1")){ //this order is important, it must be after wifi/softap.
                            mwifiP2pManager.disableP2p(mChannel);
                            SystemProperties.set("net.wifi.p2p.enable","0");
                            //avoid to auto switch from wifi/softap/wifi-direct, disable all.
   wifiManager.setWifiApEnabled(null,false);
                            wifiManager.setWifiEnabled(false);
                            isOnBeforeSuspendWifiP2p = true;
                        } else {
                            //wifi is disabled by others while waiting for suspend
                            isOnBeforeSuspendWifi = false;
                            isOnBeforeSuspendWifiAp = false;
                            isOnBeforeSuspendWifiP2p = false;
                        }
                    }
                    
                    if (btAdapter != null) {
                        if (btAdapter.isEnabled()) {
                            btAdapter.disable();
                            isOnBeforeSuspendBT = true;
                        } else {
                            isOnBeforeSuspendBT = false;
                        }
                    }
                    hasSuspendTask = false;
                    mHandler.postDelayed(releaseWakeLockTask, NusmartPM.NUSMART_RELEASE_WAKE_LOCK_DELAY);
                }
            }
        };
    }// NusmartPM
阅读全文
0 0
原创粉丝点击