Skip to content

Commit

Permalink
add a set of base classes for creating Fragment-based preference UI
Browse files Browse the repository at this point in the history
  • Loading branch information
muhomorr authored and thestinger committed Apr 10, 2023
1 parent c7a68f4 commit c1d3b1e
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
128 changes: 128 additions & 0 deletions src/com/android/settings/ext/BoolSettingFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.android.settings.ext;

import android.content.Context;
import android.ext.settings.BoolSetting;
import android.os.Bundle;

import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;

import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.widget.FooterPreference;

public abstract class BoolSettingFragment extends DashboardFragment implements ExtSettingPrefController<BoolSetting> {

private static final String TAG = BoolSettingFragment.class.getSimpleName();

protected SwitchPreference mainSwitch;

private ExtSettingControllerHelper<BoolSetting> helper;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

helper = new ExtSettingControllerHelper<>(requireContext(), getSetting());

getActivity().setTitle(getTitle());

Context ctx = requireContext();

PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(ctx);

var mainSwitch = new SwitchPreference(ctx);
mainSwitch.setTitle(getMainSwitchTitle());

this.mainSwitch = mainSwitch;
refreshMainSwitch();

mainSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
boolean state = (boolean) newValue;

if (!getSetting().put(requireContext(), state)) {
return false;
}

onMainSwitchChanged(state);

return true;
});

screen.addPreference(mainSwitch);

addExtraPrefs(screen);

FooterPreference footer = makeFooterPref(new FooterPreference.Builder(ctx));

if (footer != null) {
screen.addPreference(footer);
}

setPreferenceScreen(screen);
}

protected abstract BoolSetting getSetting();

protected abstract CharSequence getTitle();

protected abstract CharSequence getMainSwitchTitle();

protected CharSequence getMainSwitchSummary() {
return null;
}

protected void addExtraPrefs(PreferenceScreen screen) {}

protected FooterPreference makeFooterPref(FooterPreference.Builder builder) {
return null;
}

protected void onMainSwitchChanged(boolean state) {}

private void refreshMainSwitch() {
mainSwitch.setChecked(getSetting().get(requireContext()));

CharSequence mainSwitchSummary = getMainSwitchSummary();
if (mainSwitchSummary != null) {
mainSwitch.setSummary(mainSwitchSummary);
}
}

@Override
public void onResume() {
super.onResume();

helper.onResume(this);
}

@Override
public void onPause() {
super.onPause();

helper.onPause(this);
}

@Override
public void accept(BoolSetting setting) {
refreshMainSwitch();
}

@Override
public int getMetricsCategory() {
return METRICS_CATEGORY_UNKNOWN;
}

@Override
protected int getPreferenceScreenResId() {
return 0;
}

@Override
protected String getLogTag() {
return TAG;
}

protected final CharSequence resText(int res) {
return requireContext().getText(res);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.android.settings.ext;

import android.content.Context;
import android.ext.settings.BoolSetting;

public abstract class BoolSettingFragmentPrefController extends ExtSettingFragmentPrefController<BoolSetting> {

protected BoolSettingFragmentPrefController(Context ctx, String key, BoolSetting setting) {
super(ctx, key, setting);
}

@Override
public CharSequence getSummary() {
return setting.get(mContext) ? getSummaryOn() : getSummaryOff();
}

protected abstract CharSequence getSummaryOn();
protected abstract CharSequence getSummaryOff();
}
42 changes: 42 additions & 0 deletions src/com/android/settings/ext/ExtSettingFragmentPrefController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.android.settings.ext;

import android.content.Context;
import android.ext.settings.Setting;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;

public abstract class ExtSettingFragmentPrefController<T extends Setting> extends FragmentPrefController
implements ExtSettingPrefController<T> {
protected final T setting;
protected final ExtSettingControllerHelper<T> helper;

protected ExtSettingFragmentPrefController(Context ctx, String key, T setting) {
super(ctx, key);
this.setting = setting;
helper = new ExtSettingControllerHelper<T>(ctx, setting);
}

@Override
public int getAvailabilityStatus() {
return helper.getAvailabilityStatus();
}

@Override
public void onResume(@NonNull LifecycleOwner owner) {
helper.onResume(this);
}

@Override
public void onPause(@NonNull LifecycleOwner owner) {
helper.onPause(this);
}

// called by the setting observer
@Override
public void accept(T setting) {
if (preference != null) {
updateState(preference);
}
}
}
36 changes: 36 additions & 0 deletions src/com/android/settings/ext/FragmentPrefController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.android.settings.ext;

import android.content.Context;

import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import com.android.settings.core.BasePreferenceController;

public abstract class FragmentPrefController<FragmentType extends PreferenceFragmentCompat>
extends BasePreferenceController {

protected FragmentPrefController(Context ctx, String key) {
super(ctx, key);
}

@Nullable
protected Preference preference;

@Override
public void updateState(Preference preference) {
super.updateState(preference);

if (preference != this.preference) {
preference.setSingleLineTitle(false);
preference.setPersistent(false);

this.preference = preference;
}
}

protected final CharSequence resText(int res) {
return mContext.getText(res);
}
}

0 comments on commit c1d3b1e

Please sign in to comment.