android4.2 增添永不休眠选项

android4.2 增添永不休眠选项

感谢原创:http://www.csdn123.com/html/exception/626/626185_626187_626186.htm
设置 > 显示 > 休眠

a. 
packages/apps/Settings/res/values/arrays.xml
    

        15 seconds

        30 seconds

        1 minute

        2 minutes

        5 minutes

        10 minutes

        30 minutes

        Never 

    



    

    

        

        15000

        

        30000

        

        60000

        

        120000

        

        300000

        

        600000

        

        1800000

        -1

    




b. 
在其它有定义
的地方添加一行,用于显示不同语言文字的内容,这是为了解决在不同语言时,如果没有定义,会出现异常退出的情况。大概有50个arrays.xml文件需要修改。
如:packages/apps/Settings/res/values-zh-rCN/arrays.xml中(此路径中绿色表示国家语言)
  

    "15 秒"

    "30 秒"

    "1 分钟"

    "2 分钟"

    "5 分钟"

    "10 分钟"

    "30 分钟"

    "从不"

  


c.
在作出以上修改后,主界面的显示会不正常,需要在代码中加以下处理:
找到如下方法:packages/apps/Settings/src/com/android/settings/DisplaySettings.java

 private void updateTimeoutPreferenceDescription(long currentTimeout) {

        ListPreference preference = mScreenTimeoutPreference;

        String summary;

        if (currentTimeout < -1) {

        //if (currentTimeout < 0) { AnsonCode2012.7.4

            // Unsupported value

            summary = "";

        } else {

            final CharSequence[] entries = preference.getEntries();

            final CharSequence[] values = preference.getEntryValues();

            int best = 0;

            for (int i = 0; i < values.length; i++) {

                long timeout = Long.parseLong(values[i].toString());

                if (currentTimeout == timeout) {

                    best = i;

                    break;AnsonCode2012.7.4

                }

            }

            summary = preference.getContext().getString(R.string.screen_timeout_summary,

                    entries[best]);

            if(currentTimeout < 0)summary = entries[best].toString();//AnsonCode2012.7.4

        }

        preference.setSummary(summary);

    }


//注意与原来代码的区别:
//1) 修改 0 改为-1, 为保证,在界面中,显示的文字不为空。
//2)将 >=改为 ==是作一个准确判断,找到所设定的值便返回,退出循环;
//3)不加此句内容,在界面上会显示: 无操作 从不 后, 加了以后则显示: 从不

//在4.2上稍微有些不同, 主要是修改了对时间值的判断。
    private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15 * 1000;
    private static final int MINIMUM_SCREEN_OFF_TIMEOUT = 10 * 1000;
    private int getScreenOffTimeoutLocked() {
        int timeout = mScreenOffTimeoutSetting;
        if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
            timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin);
        }
        if (mUserActivityTimeoutOverrideFromWindowManager >= 0) {
            timeout = (int)Math.min(timeout, mUserActivityTimeoutOverrideFromWindowManager);
        }
        return Math.max(timeout, MINIMUM_SCREEN_OFF_TIMEOUT);
    }


 /**
     * Updates the value of mUserActivitySummary to summarize the user requested
     * state of the system such as whether the screen should be bright or dim.
     * Note that user activity is ignored when the system is asleep.
     *
     * This function must have no other side-effects.
     */
    private void updateUserActivitySummaryLocked(long now, int dirty) {
        // Update the status of the user activity timeout timer.
        if ((dirty & (DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS | DIRTY_SETTINGS)) != 0) {
            mHandler.removeMessages(MSG_USER_ACTIVITY_TIMEOUT);

            long nextTimeout = 0;
            if (mWakefulness != WAKEFULNESS_ASLEEP) {
                final int screenOffTimeout = getScreenOffTimeoutLocked();
                final int screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);

                mUserActivitySummary = 0;
                if (mLastUserActivityTime >= mLastWakeTime) {
                    nextTimeout = mLastUserActivityTime
                            + screenOffTimeout - screenDimDuration;
                    if (now < nextTimeout) {
                        mUserActivitySummary |= USER_ACTIVITY_SCREEN_BRIGHT;
                    } else {
                        nextTimeout = mLastUserActivityTime + screenOffTimeout;
                        if (now < nextTimeout) {
                            mUserActivitySummary |= USER_ACTIVITY_SCREEN_DIM;
                        }
                    }
                }
                if (mUserActivitySummary == 0
                        && mLastUserActivityTimeNoChangeLights >= mLastWakeTime) {
                    nextTimeout = mLastUserActivityTimeNoChangeLights + screenOffTimeout;
                    if (now < nextTimeout
                            && mDisplayPowerRequest.screenState
                                    != DisplayPowerRequest.SCREEN_STATE_OFF) {
                        mUserActivitySummary = mDisplayPowerRequest.screenState
                                == DisplayPowerRequest.SCREEN_STATE_BRIGHT ?
                                USER_ACTIVITY_SCREEN_BRIGHT : USER_ACTIVITY_SCREEN_DIM;
                    }
                }
                if (mUserActivitySummary != 0) {
                    Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT);
                    msg.setAsynchronous(true);
                    ///mHandler.sendMessageAtTime(msg, nextTimeout);
					if(mScreenOffTimeoutSetting > 0)mHandler.sendMessageAtTime(msg, nextTimeout);//添加了一行判断, 如果值为-1则不发送广播
                }
            } else {
                mUserActivitySummary = 0;
            }

            if (DEBUG_SPEW) {
                Slog.d(TAG, "updateUserActivitySummaryLocked: mWakefulness="
                        + wakefulnessToString(mWakefulness)
                        + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)
                        + ", nextTimeout=" + TimeUtils.formatUptime(nextTimeout));
            }
        }
    }
还需要改个地方:
frameworks/base/services/java/com/android/server/power/PowerManagerService.java

private void updateUserActivitySummaryLocked(long now, int dirty) {

..... 

//change code here ==============

                      Slog.d(TAG,"mScreenOffTimeoutSetting ="+mScreenOffTimeoutSetting);

                      //if (mUserActivitySummary != 0 ) {

                if (mUserActivitySummary != 0 && mScreenOffTimeoutSetting > 0) {

//change code here  end ==============

                    Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT);

                    msg.setAsynchronous(true);

                    mHandler.sendMessageAtTime(msg, nextTimeout);

                }

 最后,设置默认休眠时间值的路径:frameworks\base\packages\SettingsProvider\res\values\defaults.xml中设置def_screen_off_timeout的值就可以!
 
 

你可能感兴趣的:(android,安卓,setting,休眠)