android代码实现深色模式切换(在设置-显示中增加)
效果图:
1.vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values/strings.xml
<string name="read_model_title">Read model</string>
2.vendor/mediatek/proprietary/packages/apps/MtkSettings/res/xml/display_settings.xml
<SwitchPreference
android:key="read_model"
android:title="@string/read_model_title"/>
3.vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/DisplaySettings.java
import com.android.settings.display.ShowOperatorNamePreferenceController;
import com.android.settings.display.ReadModelPreferenceController;
import com.android.settings.display.TapToWakePreferenceController;
@@ -98,6 99,7 @@ public class DisplaySettings extends DashboardFragment {
controllers.add(new ShowOperatorNamePreferenceController(context));
controllers.add(new ReadModelPreferenceController(context));
4.添加vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/display/ReadModelPreferenceController.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.display;
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 ReadModelPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private static final String KEY_READ_MODEL_NAME = "read_model";
public ReadModelPreferenceController(Context context) {
super(context);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY_READ_MODEL_NAME;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean value = (Boolean) newValue;
Settings.Secure.putInt(mContext.getContentResolver(),
KEY_READ_MODEL_NAME, value ? 0 : 1);
if (!value) {
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
0);
} else {
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
1);
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, 0);
}
return true;
}
@Override
public void updateState(Preference preference) {
int value = Settings.Secure.getInt(mContext.getContentResolver(),
KEY_READ_MODEL_NAME, 1);
((SwitchPreference) preference).setChecked(value != 1);
}
}
参考源码
vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/development/SimulateColorSpacePreferenceController.java
参考google code:
打开开关调用writeSimulateColorSpace(0);
关闭开关调用writeSimulateColorSpace(-1);
96 private void writeSimulateColorSpace(Object value) {
97 final ContentResolver cr = mContext.getContentResolver();
98 final int newMode = Integer.parseInt(value.toString());
99 if (newMode < 0) {
100 Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
101 SETTING_VALUE_OFF);
102 } else {
103 Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
104 SETTING_VALUE_ON);
105 Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, newMode);
106 }
107 }
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com