This repository has been archived on 2025-09-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
android_packages_apps_Cherish/src/com/cherish/settings/fragments/ButtonSettings.java
2021-12-26 02:00:29 +07:00

248 lines
11 KiB
Java

/*
* Copyright (C) 2016 The Dirty Unicorns Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cherish.settings.fragments;
import android.content.ContentResolver;
import android.content.res.Resources;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.UserHandle;
import android.os.Vibrator;
import android.os.RemoteException;
import android.os.PowerManager;
import android.os.ServiceManager;
import androidx.preference.PreferenceCategory;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.SwitchPreference;
import android.provider.Settings;
import android.widget.Toast;
import com.android.settings.R;
import com.cherish.settings.preferences.SystemSettingSwitchPreference;
import com.cherish.settings.preferences.SecureSettingSwitchPreference;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.SettingsPreferenceFragment;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.Utils;
import com.cherish.settings.preferences.CustomSeekBarPreference;
import com.android.internal.util.hwkeys.ActionConstants;
import com.android.internal.util.hwkeys.ActionUtils;
import com.cherish.settings.preferences.CustomSeekBarPreference;
import com.cherish.settings.preferences.ActionFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
import android.provider.SearchIndexableResource;
import java.util.ArrayList;
import java.util.List;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class ButtonSettings extends ActionFragment implements OnPreferenceChangeListener {
private static final String HWKEY_DISABLE = "hardware_keys_disable";
// category keys
private static final String CATEGORY_HWKEY = "hardware_keys";
private static final String CATEGORY_BACK = "back_key";
private static final String CATEGORY_HOME = "home_key";
private static final String CATEGORY_MENU = "menu_key";
private static final String CATEGORY_ASSIST = "assist_key";
private static final String CATEGORY_APPSWITCH = "app_switch_key";
private static final String CATEGORY_VOLUME = "volume_keys";
private static final String CATEGORY_POWER = "power_key";
private static final String KEY_BUTTON_MANUAL_BRIGHTNESS_NEW = "button_manual_brightness_new";
private static final String KEY_BUTTON_TIMEOUT = "button_timeout";
private static final String KEY_BUTON_BACKLIGHT_OPTIONS = "button_backlight_options_category";
// Masks for checking presence of hardware keys.
// Must match values in frameworks/base/core/res/res/values/config.xml
public static final int KEY_MASK_HOME = 0x01;
public static final int KEY_MASK_BACK = 0x02;
public static final int KEY_MASK_MENU = 0x04;
public static final int KEY_MASK_ASSIST = 0x08;
public static final int KEY_MASK_APP_SWITCH = 0x10;
public static final int KEY_MASK_CAMERA = 0x20;
public static final int KEY_MASK_VOLUME = 0x40;
private SwitchPreference mHwKeyDisable;
private CustomSeekBarPreference mButtonTimoutBar;
private CustomSeekBarPreference mManualButtonBrightness;
private PreferenceCategory mButtonBackLightCategory;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
addPreferencesFromResource(R.xml.cherish_settings_button);
final Resources res = getResources();
final ContentResolver resolver = getActivity().getContentResolver();
final PreferenceScreen prefScreen = getPreferenceScreen();
final boolean needsNavbar = ActionUtils.hasNavbarByDefault(getActivity());
final PreferenceCategory hwkeyCat = (PreferenceCategory) prefScreen
.findPreference(CATEGORY_HWKEY);
int keysDisabled = 0;
if (!needsNavbar) {
mHwKeyDisable = (SwitchPreference) findPreference(HWKEY_DISABLE);
keysDisabled = Settings.System.getIntForUser(getContentResolver(),
Settings.System.HARDWARE_KEYS_DISABLE, 0,
UserHandle.USER_CURRENT);
mHwKeyDisable.setChecked(keysDisabled != 0);
mHwKeyDisable.setOnPreferenceChangeListener(this);
} else {
prefScreen.removePreference(hwkeyCat);
}
// bits for hardware keys present on device
final int deviceKeys = getResources().getInteger(
com.android.internal.R.integer.config_deviceHardwareKeys);
// read bits for present hardware keys
final boolean hasHomeKey = (deviceKeys & KEY_MASK_HOME) != 0;
final boolean hasBackKey = (deviceKeys & KEY_MASK_BACK) != 0;
final boolean hasMenuKey = (deviceKeys & KEY_MASK_MENU) != 0;
final boolean hasAssistKey = (deviceKeys & KEY_MASK_ASSIST) != 0;
final boolean hasAppSwitchKey = (deviceKeys & KEY_MASK_APP_SWITCH) != 0;
// load categories and init/remove preferences based on device
// configuration
final PreferenceCategory backCategory = (PreferenceCategory) prefScreen
.findPreference(CATEGORY_BACK);
final PreferenceCategory homeCategory = (PreferenceCategory) prefScreen
.findPreference(CATEGORY_HOME);
final PreferenceCategory menuCategory = (PreferenceCategory) prefScreen
.findPreference(CATEGORY_MENU);
final PreferenceCategory assistCategory = (PreferenceCategory) prefScreen
.findPreference(CATEGORY_ASSIST);
final PreferenceCategory appSwitchCategory = (PreferenceCategory) prefScreen
.findPreference(CATEGORY_APPSWITCH);
// back key
if (!hasBackKey) {
prefScreen.removePreference(backCategory);
}
// home key
if (!hasHomeKey) {
prefScreen.removePreference(homeCategory);
}
// App switch key (recents)
if (!hasAppSwitchKey) {
prefScreen.removePreference(appSwitchCategory);
}
// menu key
if (!hasMenuKey) {
prefScreen.removePreference(menuCategory);
}
// search/assist key
if (!hasAssistKey) {
prefScreen.removePreference(assistCategory);
}
// let super know we can load ActionPreferences
onPreferenceScreenLoaded(ActionConstants.getDefaults(ActionConstants.HWKEYS));
// load preferences first
setActionPreferencesEnabled(keysDisabled == 0);
mManualButtonBrightness = (CustomSeekBarPreference) findPreference(
KEY_BUTTON_MANUAL_BRIGHTNESS_NEW);
final int customButtonBrightness = getResources().getInteger(
com.android.internal.R.integer.config_button_brightness_default);
final int currentBrightness = Settings.System.getInt(resolver,
Settings.System.CUSTOM_BUTTON_BRIGHTNESS, customButtonBrightness);
PowerManager pm = (PowerManager)getActivity().getSystemService(Context.POWER_SERVICE);
mManualButtonBrightness.setMax(pm.getMaximumScreenBrightnessSetting());
mManualButtonBrightness.setValue(currentBrightness);
mManualButtonBrightness.setOnPreferenceChangeListener(this);
mButtonTimoutBar = (CustomSeekBarPreference) findPreference(KEY_BUTTON_TIMEOUT);
int currentTimeout = Settings.System.getInt(resolver,
Settings.System.BUTTON_BACKLIGHT_TIMEOUT, 0);
mButtonTimoutBar.setValue(currentTimeout);
mButtonTimoutBar.setOnPreferenceChangeListener(this);
final boolean enableBacklightOptions = getResources().getBoolean(
com.android.internal.R.bool.config_button_brightness_support);
mButtonBackLightCategory = (PreferenceCategory) findPreference(KEY_BUTON_BACKLIGHT_OPTIONS);
if (!enableBacklightOptions) {
prefScreen.removePreference(mButtonBackLightCategory);
}
}
public boolean onPreferenceChange(Preference preference, Object newValue) {
ContentResolver resolver = getActivity().getContentResolver();
if (preference == mHwKeyDisable) {
boolean value = (Boolean) newValue;
Settings.System.putInt(getContentResolver(), Settings.System.HARDWARE_KEYS_DISABLE,
value ? 1 : 0);
setActionPreferencesEnabled(!value);
} else if (preference == mButtonTimoutBar) {
int buttonTimeout = (Integer) newValue;
Settings.System.putInt(getContentResolver(),
Settings.System.BUTTON_BACKLIGHT_TIMEOUT, buttonTimeout);
} else if (preference == mManualButtonBrightness) {
int buttonBrightness = (Integer) newValue;
Settings.System.putInt(getContentResolver(),
Settings.System.CUSTOM_BUTTON_BRIGHTNESS, buttonBrightness);
} else {
return false;
}
return true;
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.CHERISH_SETTINGS;
}
@Override
protected boolean usesExtendedActionsList() {
return true;
}
/**
* For Search.
*/
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
boolean enabled) {
ArrayList<SearchIndexableResource> result =
new ArrayList<SearchIndexableResource>();
SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.cherish_settings_button;
result.add(sir);
return result;
}
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
return keys;
}
};
}