Cherish:Add some button settings

Hardware key: Customization support[2/2]

Volume Rocker Wake [2/2]

Volume key cursor control (2/2)

Co-authored-by: jhenrique09 <jhsv09@gmail.com>
Co-authored-by: Max <xyyx@mail.ru>
Co-authored-by: bigrushdog <randall.rushing@gmail.com>
Signed-off-by: Hưng Phan <phandinhhungvp2001@gmail.com>
This commit is contained in:
Hưng Phan
2022-09-05 23:10:11 +07:00
parent b6c4c176e5
commit 135aefde28
13 changed files with 1671 additions and 83 deletions

View File

@@ -18,44 +18,76 @@
package com.cherish.settings.fragments;
import android.content.ContentResolver;
import android.content.res.Resources;
import android.content.Context;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.Vibrator;
import androidx.preference.PreferenceCategory;
import android.provider.Settings;
import android.view.View;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.SwitchPreference;
import android.provider.Settings;
import com.android.internal.lineage.hardware.LineageHardwareManager;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.util.cherish.CherishUtils;
import com.android.internal.util.hwkeys.ActionConstants;
import com.android.internal.util.hwkeys.ActionUtils;
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.cherish.settings.preferences.CustomSeekBarPreference;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
import android.provider.SearchIndexableResource;
import com.cherish.settings.preferences.ActionFragment;
import com.cherish.settings.preferences.SecureSettingSwitchPreference;
import com.cherish.settings.preferences.SystemSettingSwitchPreference;
import java.util.ArrayList;
import java.util.List;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class ButtonSettings extends SettingsPreferenceFragment implements
Preference.OnPreferenceChangeListener {
private static final String KEY_NAVBAR_INVERSE = "navigation_bar_inverse";
private static final String KEY_NAVIGATION_COMPACT_LAYOUT = "navigation_bar_compact_layout";
private SystemSettingSwitchPreference mNavbarInverse;
private SystemSettingSwitchPreference mNavigationCompactLayout;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class ButtonSettings extends ActionFragment implements
Preference.OnPreferenceChangeListener {
private static final String HWKEY_DISABLE = "hardware_keys_disable";
private static final String KEY_NAVBAR_INVERSE = "navigation_bar_inverse";
private static final String KEY_NAVIGATION_COMPACT_LAYOUT = "navigation_bar_compact_layout";
private static final String KEY_SWAP_CAPACITIVE_KEYS = "swap_capacitive_keys";
// 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";
// 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 PreferenceCategory mButtonBackLightCategory;
private PreferenceCategory mHwKeyCategory;
private SecureSettingSwitchPreference mSwapCapacitiveKeys;
private SwitchPreference mHwKeyDisable;
private SystemSettingSwitchPreference mNavbarInverse;
private SystemSettingSwitchPreference mNavigationCompactLayout;
@Override
public void onCreate(Bundle icicle) {
@@ -65,47 +97,133 @@ public class ButtonSettings extends SettingsPreferenceFragment implements
final Resources res = getResources();
final ContentResolver resolver = getActivity().getContentResolver();
final PreferenceScreen prefScreen = getPreferenceScreen();
final boolean isThreeButtonNavbarEnabled = EvolutionUtils.isThemeEnabled("com.android.internal.systemui.navbar.threebutton");
mSwapCapacitiveKeys = findPreference(KEY_SWAP_CAPACITIVE_KEYS);
if (mSwapCapacitiveKeys != null && !isKeySwapperSupported(getActivity())) {
prefScreen.removePreference(mSwapCapacitiveKeys);
mSwapCapacitiveKeys = null;
}
final boolean needsNavbar = ActionUtils.hasNavbarByDefault(getActivity());
mHwKeyCategory = (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(mHwKeyCategory);
}
// 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);
final boolean isThreeButtonNavbarEnabled = CherishUtils.isThemeEnabled("com.android.internal.systemui.navbar.threebutton");
mNavbarInverse = (SystemSettingSwitchPreference) findPreference(KEY_NAVBAR_INVERSE);
mNavbarInverse.setEnabled(isThreeButtonNavbarEnabled);
mNavigationCompactLayout = (SystemSettingSwitchPreference) findPreference(KEY_NAVIGATION_COMPACT_LAYOUT);
mNavigationCompactLayout.setEnabled(isThreeButtonNavbarEnabled);
}
private static boolean isKeyDisablerSupported(Context context) {
final LineageHardwareManager hardware = LineageHardwareManager.getInstance(context);
return hardware.isSupported(LineageHardwareManager.FEATURE_KEY_DISABLE);
}
private static boolean isKeySwapperSupported(Context context) {
final LineageHardwareManager hardware = LineageHardwareManager.getInstance(context);
return hardware.isSupported(LineageHardwareManager.FEATURE_KEY_SWAP);
}
@Override
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);
return true;
}
return false;
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.CHERISH_SETTINGS;
return MetricsEvent.CHERISH_SETTINGS;
}
/**
/**
* 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;
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.cherish_settings_button) {
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
LineageHardwareManager mLineageHardware = LineageHardwareManager.getInstance(context);
if (!isKeyDisablerSupported(context)) {
keys.add(HWKEY_DISABLE);
keys.add(CATEGORY_HWKEY);
keys.add(CATEGORY_BACK);
keys.add(CATEGORY_HOME);
keys.add(CATEGORY_MENU);
keys.add(CATEGORY_ASSIST);
keys.add(CATEGORY_APPSWITCH);
}
return keys;
}
};
}
};
}

View File

@@ -1,67 +1,194 @@
package com.cherish.settings.fragments;
import com.android.internal.logging.nano.MetricsProto;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import android.preference.SwitchPreference;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceFragment;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settingslib.search.SearchIndexable;
import android.provider.SearchIndexableResource;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class VolumeRockerSettings extends SettingsPreferenceFragment implements
Preference.OnPreferenceChangeListener {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
private static final String TAG = "VolumeSteps";
private static final String VOLUME_STEP_DEFAULTS = "volume_step_defaults";
private static final String FIRST_RUN_KEY = "first_run";
// base map of all preference keys and the associated stream
private static final Map<String, Integer> volume_map = new HashMap<String, Integer>();
static {
volume_map.put("volume_steps_alarm", new Integer(AudioManager.STREAM_ALARM));
volume_map.put("volume_steps_dtmf", new Integer(AudioManager.STREAM_DTMF));
volume_map.put("volume_steps_music", new Integer(AudioManager.STREAM_MUSIC));
volume_map.put("volume_steps_notification", new Integer(AudioManager.STREAM_NOTIFICATION));
volume_map.put("volume_steps_ring", new Integer(AudioManager.STREAM_RING));
volume_map.put("volume_steps_system", new Integer(AudioManager.STREAM_SYSTEM));
volume_map.put("volume_steps_voice_call", new Integer(AudioManager.STREAM_VOICE_CALL));
}
// entries to remove on non-telephony devices
private static final Set<String> telephony_set = new HashSet<String>();
static {
telephony_set.add("volume_steps_dtmf");
telephony_set.add("volume_steps_ring");
telephony_set.add("volume_steps_voice_call");
}
// set of available pref keys after device configuration filter
private Set<String> mAvailableKeys = new HashSet<String>();
private AudioManager mAudioManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.cherish_settings_volume);
mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
final PreferenceScreen prefScreen = getPreferenceScreen();
mAvailableKeys = volume_map.keySet();
// remove invalid audio stream prefs
boolean isPhone = TelephonyManager.getDefault().getCurrentPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
if (!isPhone) {
// remove telephony keys from available set
mAvailableKeys.removeAll(telephony_set);
for (String key : telephony_set) {
Preference toRemove = prefScreen.findPreference(key);
if (toRemove != null) {
prefScreen.removePreference(toRemove);
}
}
}
// check prefs for initial run of this fragment
final boolean firstRun = checkForFirstRun();
// entries array isn't translatable ugh
final String defEntry = getString(R.string.volume_steps_reset);
// initialize prefs: set defaults if first run, set listeners and update values
for (String key : mAvailableKeys) {
Preference pref = prefScreen.findPreference(key);
if (pref == null || !(pref instanceof ListPreference)) {
continue;
}
final ListPreference listPref = (ListPreference) pref;
int steps = mAudioManager.getStreamMaxVolume(volume_map.get(key));
if (firstRun) {
saveDefaultSteps(listPref, steps);
}
final int defSteps = getDefaultSteps(listPref);
CharSequence[] entries = new CharSequence[listPref.getEntries().length + 1];
CharSequence[] values = new CharSequence[listPref.getEntryValues().length + 1];
for (int i = 0; i < entries.length; i++) {
if (i == 0) {
entries[i] = defEntry;
values[i] = String.valueOf(defSteps);
continue;
}
entries[i] = listPref.getEntries()[i - 1];
values[i] = listPref.getEntryValues()[i - 1];
}
listPref.setEntries(entries);
listPref.setEntryValues(values);
updateVolumeStepPrefs(listPref, steps);
listPref.setOnPreferenceChangeListener(this);
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
public boolean onPreferenceChange(Preference preference, Object objValue) {
if (preference.hasKey() && mAvailableKeys.contains(preference.getKey())) {
commitVolumeSteps(preference, Integer.parseInt(objValue.toString()));
}
return true;
}
private SharedPreferences getDefaultStepsPrefs() {
return getActivity().getSharedPreferences(VOLUME_STEP_DEFAULTS,
Context.MODE_PRIVATE);
}
// test for initial run of this fragment
private boolean checkForFirstRun() {
String isFirstRun = getDefaultStepsPrefs().getString(FIRST_RUN_KEY, null);
if (isFirstRun == null) {
getDefaultStepsPrefs().edit().putString(FIRST_RUN_KEY, "first_run_initialized").commit();
return true;
}
return false;
}
private int getDefaultSteps(Preference pref) {
if (pref == null || !(pref instanceof ListPreference)) {
// unlikely
return -1;
}
String key = pref.getKey();
String value = getDefaultStepsPrefs().getString(key, null);
if (value == null) {
// unlikely
return -1;
}
return Integer.parseInt(value);
}
// on the initial run, let's store true device defaults in sharedPrefs
private void saveDefaultSteps(Preference volPref, int defaultSteps) {
String key = volPref.getKey();
getDefaultStepsPrefs().edit().putString(key, String.valueOf(defaultSteps)).commit();
}
private void updateVolumeStepPrefs(Preference pref, int steps) {
if (pref == null || !(pref instanceof ListPreference)) {
return;
}
final ListPreference listPref = (ListPreference) pref;
listPref.setSummary(String.valueOf(steps));
listPref.setValue(String.valueOf(steps));
}
private void commitVolumeSteps(Preference pref, int steps) {
Settings.System.putInt(getActivity().getContentResolver(), pref.getKey(), steps);
mAudioManager.setStreamMaxVolume(volume_map.get(pref.getKey()), steps);
updateVolumeStepPrefs(pref, steps);
Log.i(TAG, "Volume steps:" + pref.getKey() + "" + String.valueOf(steps));
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.CHERISH_SETTINGS;
return MetricsEvent.CHERISH_SETTINGS;
}
/**
/**
* 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_volume;
result.add(sir);
return result;
}
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
return keys;
}
};
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.cherish_settings_volume);
}