Settings: Add system haptics settings [1/2]
Change-Id: I9072b299affaab1fa53853fea099e38fb8a20b57 Signed-off-by: minaripenguin <minaripenguin@users.noreply.github.com> Signed-off-by: Ghosuto <clash.raja10@gmail.com>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2023 the risingOS android 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.sound;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Settings for haptics
|
||||
*/
|
||||
public class HapticsPreferenceFragment extends DashboardFragment {
|
||||
|
||||
private static final String TAG = "HapticsPreferenceFragment";
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.VIEW_UNKNOWN;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.haptics_settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
controllers.add(new HapticsPreferenceFragmentController(context));
|
||||
return controllers;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (C) 2023 the risingOS android 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.sound;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Vibrator;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import android.preference.Preference.OnPreferenceChangeListener;
|
||||
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import org.evolution.settings.preferences.CustomSeekBarPreference;
|
||||
|
||||
import com.android.internal.util.evolution.VibrationUtils;
|
||||
|
||||
public class HapticsPreferenceFragmentController extends AbstractPreferenceController
|
||||
implements Preference.OnPreferenceChangeListener {
|
||||
|
||||
private static final String KEY = "haptics_settings";
|
||||
private static final String KEY_EDGE_SCROLLING_HAPTICS_INTENSITY = "edge_scrolling_haptics_intensity";
|
||||
private static final String KEY_VOLUME_SLIDER_HAPTICS_INTENSITY = "volume_slider_haptics_intensity";
|
||||
|
||||
private CustomSeekBarPreference mEdgeScrollingIntensity;
|
||||
private CustomSeekBarPreference mVolumeSliderIntensity;
|
||||
|
||||
private Context mContext;
|
||||
private Vibrator mVibrator;
|
||||
|
||||
public HapticsPreferenceFragmentController(Context context) {
|
||||
super(context);
|
||||
mContext = context;
|
||||
mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return mVibrator != null && mVibrator.hasVibrator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mEdgeScrollingIntensity = (CustomSeekBarPreference) screen.findPreference(KEY_EDGE_SCROLLING_HAPTICS_INTENSITY);
|
||||
mVolumeSliderIntensity = (CustomSeekBarPreference) screen.findPreference(KEY_VOLUME_SLIDER_HAPTICS_INTENSITY);
|
||||
updateSettings();
|
||||
}
|
||||
|
||||
private void updateSettings() {
|
||||
int edgeScrollingIntensity = Settings.System.getInt(mContext.getContentResolver(),
|
||||
Settings.System.EDGE_SCROLLING_HAPTICS_INTENSITY, 1);
|
||||
mEdgeScrollingIntensity.setValue(edgeScrollingIntensity);
|
||||
|
||||
int volumeSliderIntensity = Settings.System.getInt(mContext.getContentResolver(),
|
||||
KEY_VOLUME_SLIDER_HAPTICS_INTENSITY, 1);
|
||||
mVolumeSliderIntensity.setValue(volumeSliderIntensity);
|
||||
|
||||
mEdgeScrollingIntensity.setOnPreferenceChangeListener(this);
|
||||
mVolumeSliderIntensity.setOnPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
int intensity = (Integer) newValue;
|
||||
boolean isChanged = false;
|
||||
if (preference == mEdgeScrollingIntensity) {
|
||||
Settings.System.putInt(mContext.getContentResolver(),
|
||||
Settings.System.EDGE_SCROLLING_HAPTICS_INTENSITY, intensity);
|
||||
mEdgeScrollingIntensity.setValue(intensity);
|
||||
isChanged = true;
|
||||
} else if (preference == mVolumeSliderIntensity) {
|
||||
Settings.System.putInt(mContext.getContentResolver(),
|
||||
KEY_VOLUME_SLIDER_HAPTICS_INTENSITY, intensity);
|
||||
mVolumeSliderIntensity.setValue(intensity);
|
||||
isChanged = true;
|
||||
}
|
||||
if (isChanged) {
|
||||
VibrationUtils.triggerVibration(mContext, intensity);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user