1 /* 2 * Copyright 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.inputmethod; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.database.ContentObserver; 23 import android.hardware.input.InputSettings; 24 import android.os.Handler; 25 import android.os.Looper; 26 import android.provider.Settings; 27 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.core.SliderPreferenceController; 34 import com.android.settings.overlay.FeatureFactory; 35 import com.android.settings.widget.SeekBarPreference; 36 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 37 import com.android.settingslib.core.lifecycle.LifecycleObserver; 38 import com.android.settingslib.core.lifecycle.events.OnStart; 39 import com.android.settingslib.core.lifecycle.events.OnStop; 40 41 42 public class MouseScrollingSpeedPreferenceController extends SliderPreferenceController implements 43 Preference.OnPreferenceChangeListener, LifecycleObserver, OnStop, OnStart { 44 45 private final MetricsFeatureProvider mMetricsFeatureProvider; 46 47 private final ContentResolver mContentResolver; 48 private final ContentObserver mContentObserver; 49 50 @Nullable 51 private SeekBarPreference mPreference; 52 MouseScrollingSpeedPreferenceController(@onNull Context context, @NonNull String key)53 public MouseScrollingSpeedPreferenceController(@NonNull Context context, @NonNull String key) { 54 super(context, key); 55 56 mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); 57 mContentResolver = context.getContentResolver(); 58 mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { 59 @Override 60 public void onChange(boolean selfChange) { 61 updateAvailabilityStatus(); 62 } 63 }; 64 } 65 66 @Override displayPreference(@onNull PreferenceScreen screen)67 public void displayPreference(@NonNull PreferenceScreen screen) { 68 super.displayPreference(screen); 69 mPreference = screen.findPreference(getPreferenceKey()); 70 mPreference.setMax(getMax()); 71 mPreference.setMin(getMin()); 72 mPreference.setProgress(getSliderPosition()); 73 updateState(mPreference); 74 } 75 76 @Override getAvailabilityStatus()77 public int getAvailabilityStatus() { 78 if (!InputSettings.isMouseScrollingAccelerationFeatureFlagEnabled()) { 79 return UNSUPPORTED_ON_DEVICE; 80 } 81 return shouldEnableSlideBar() ? AVAILABLE : DISABLED_DEPENDENT_SETTING; 82 } 83 84 @Override setSliderPosition(int position)85 public boolean setSliderPosition(int position) { 86 if (position < getMin() || position > getMax()) { 87 return false; 88 } 89 InputSettings.setMouseScrollingSpeed(mContext, position); 90 mMetricsFeatureProvider.action( 91 mContext, SettingsEnums.ACTION_MOUSE_SCROLLING_SPEED_CHANGED, position); 92 93 return true; 94 } 95 96 @Override getSliderPosition()97 public int getSliderPosition() { 98 return InputSettings.getMouseScrollingSpeed(mContext); 99 } 100 101 @Override getMin()102 public int getMin() { 103 return InputSettings.MIN_MOUSE_SCROLLING_SPEED; 104 } 105 106 @Override getMax()107 public int getMax() { 108 return InputSettings.MAX_MOUSE_SCROLLING_SPEED; 109 } 110 111 /** 112 * Returns whether the mouse scrolling speed slide bar should allow users to customize or not. 113 */ shouldEnableSlideBar()114 public boolean shouldEnableSlideBar() { 115 return !InputSettings.isMouseScrollingAccelerationEnabled(mContext); 116 } 117 118 @Override onStart()119 public void onStart() { 120 mContentResolver.registerContentObserver( 121 Settings.System.getUriFor( 122 Settings.System.MOUSE_SCROLLING_ACCELERATION), 123 /* notifyForDescendants= */ false, mContentObserver); 124 } 125 126 @Override onStop()127 public void onStop() { 128 mContentResolver.unregisterContentObserver(mContentObserver); 129 } 130 updateAvailabilityStatus()131 private void updateAvailabilityStatus() { 132 if (mPreference != null) { 133 mPreference.setEnabled(shouldEnableSlideBar()); 134 } 135 } 136 } 137