需求:
1.添加屏幕互保开关,默认关闭;
2.只保留时钟,可设定指针和数字、夜间模式;
3.启用时间改多长时间无操作进入屏保,可设定1、5、10、15、30分钟;
4.如果休眠时间设定小于屏保时间,则优先进入休眠,如果休眠时间设定大于屏保时间,则先进入屏保,达到休眠时间后再进入休眠
修改后的效果
分析与实现:
1.由于Android原生系统默认进入屏保的条件是充电或插入基座时,但到达休眠时才进入屏保。所以需要把进入屏保的条件修改为任何时候都能进入。
frameworks\base\core\res\res\values\config.xml
把"config_dreamsEnabledOnBattery"修改为true即实现任何时候都能进入屏保
<!-- Are we allowed to dream while not plugged in? -->
<bool name="config_dreamsEnabledOnBattery">true</bool>
2.在设置–>显示–>屏保中添加“屏幕互保”选项开关
2.1添加系统自定义全局变量,用来保存“屏幕互保”选项开关的状态值
frameworks/base/core/java/android/provider/Settings.java
+ /** @hide */
+ public static final String SCREENSAVER_SWITCH= "seewo.screensavers.switch";
public static final class Secure extends NameValueTable {
/** @hide */
public static final String SCREENSAVER_TIMEOUT= “seewo.screensavers.timeout”;
2.2 vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values-zh-rCN/strings.xml
<string name="screensaver_switch_title">屏幕互保</string>
2.3 vendor/mediatek/proprietary/packages/apps/MtkSettings/res/xml/dream_fragment_overview.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:settings="http://schemas.android.com/apk/res-auto"
android:key="dream_overview_screen"
<SwitchPreference
android:key="screensaver_switch"
android:title="@string/screensaver_switch_title"/>
2.4添加vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/dream/ScreenSaverSwitchPreferenceController.java文件
/*
* Copyright (C) 2017 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.settings.dream;
import static android.provider.Settings.Secure.SCREENSAVER_SWITCH;
import android.content.Context;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
public class ScreenSaverSwitchPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private static final String KEY_SCREENSAVER_SWITCH_NAME = "screensaver_switch";
public ScreenSaverSwitchPreferenceController(Context context) {
super(context);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY_SCREENSAVER_SWITCH_NAME;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean value = (Boolean) newValue;
Settings.Secure.putInt(mContext.getContentResolver(),
KEY_SCREENSAVER_SWITCH_NAME, value ? 1 : 0);
if (value) {
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.SCREENSAVER_SWITCH,
1);
} else {
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.SCREENSAV