Refactor ToggleAutoclickPreferenceFragment to improve maintainability

Root cause: There is a bunch of different logic of preferences in ToggleAutoclickPreferenceFragment. It’s hard to implement new features and hard to maintain and hard to be testable.
Solution: Move out logic of ToggleAutoclickPreferenceFragment into controllers to reduce the complexity of the relationship between preference and fragment.

Bug: 197695932
Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.accessibility
Change-Id: I5db18d5a0c577ad67d15c2d0169a36a67e9f13db
This commit is contained in:
menghanli
2022-07-21 11:22:30 +08:00
parent 6544dee983
commit cb008ccf17
11 changed files with 486 additions and 423 deletions

View File

@@ -16,6 +16,9 @@
package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import android.content.Context;
import android.provider.Settings;
import android.view.accessibility.AccessibilityManager;
@@ -23,8 +26,19 @@ import android.view.accessibility.AccessibilityManager;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
/** Preference controller for autoclick (dwell timing). */
public class AutoclickPreferenceController extends BasePreferenceController {
/**
* Resource ids from which autoclick preference summaries should be derived. The strings have
* placeholder for integer delay value.
*/
private static final int[] AUTOCLICK_PREFERENCE_SUMMARIES = {
R.plurals.accessibilty_autoclick_preference_subtitle_short_delay,
R.plurals.accessibilty_autoclick_preference_subtitle_medium_delay,
R.plurals.accessibilty_autoclick_preference_subtitle_long_delay
};
public AutoclickPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@@ -37,14 +51,29 @@ public class AutoclickPreferenceController extends BasePreferenceController {
@Override
public CharSequence getSummary() {
final boolean enabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, 0) == 1;
Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, OFF) == ON;
if (!enabled) {
return mContext.getResources().getText(R.string.accessibility_feature_state_off);
}
final int delay = Settings.Secure.getInt(mContext.getContentResolver(),
final int delayMillis = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY,
AccessibilityManager.AUTOCLICK_DELAY_DEFAULT);
return ToggleAutoclickPreferenceFragment.getAutoclickPreferenceSummary(
mContext.getResources(), delay);
final int summaryIndex = getAutoclickPreferenceSummaryIndex(delayMillis);
return AutoclickUtils.getAutoclickDelaySummary(mContext.getResources(),
AUTOCLICK_PREFERENCE_SUMMARIES[summaryIndex], delayMillis);
}
}
/** Finds index of the summary that should be used for the provided autoclick delay. */
private int getAutoclickPreferenceSummaryIndex(int delay) {
if (delay <= AutoclickUtils.MIN_AUTOCLICK_DELAY_MS) {
return 0;
}
if (delay >= AutoclickUtils.MAX_AUTOCLICK_DELAY_MS) {
return AUTOCLICK_PREFERENCE_SUMMARIES.length - 1;
}
int delayRange =
AutoclickUtils.MAX_AUTOCLICK_DELAY_MS - AutoclickUtils.MIN_AUTOCLICK_DELAY_MS;
int rangeSize = (delayRange) / (AUTOCLICK_PREFERENCE_SUMMARIES.length - 1);
return (delay - AutoclickUtils.MIN_AUTOCLICK_DELAY_MS) / rangeSize;
}
}