-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a set of base classes for creating Fragment-based preference UI
- Loading branch information
1 parent
e37118d
commit 4ad6aa2
Showing
4 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/com/android/settings/ext/BoolSettingFragmentPrefController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/com/android/settings/ext/ExtSettingFragmentPrefController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |