support: MasterSwitchPreference: Fix settings not applying
Signed-off-by: spezi77 <spezi7713@gmx.net>
This commit is contained in:
@@ -19,6 +19,7 @@ package com.cherish.settings.preferences;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.TypedArray;
|
import android.content.res.TypedArray;
|
||||||
import androidx.preference.PreferenceViewHolder;
|
import androidx.preference.PreferenceViewHolder;
|
||||||
|
import android.provider.Settings;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Switch;
|
import android.widget.Switch;
|
||||||
@@ -114,15 +115,32 @@ public class MasterSwitchPreference extends TwoTargetPreference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
|
protected boolean persistBoolean(boolean value) {
|
||||||
setChecked(restoreValue ? getPersistedBoolean((Boolean) defaultValue)
|
if (shouldPersist()) {
|
||||||
: (Boolean) defaultValue);
|
if (value == getPersistedBoolean(!value)) {
|
||||||
|
// It's already there, so the same as persisting
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Settings.System.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Call from outside when value might have changed.
|
protected boolean getPersistedBoolean(boolean defaultReturnValue) {
|
||||||
*/
|
if (!shouldPersist()) {
|
||||||
public void reloadValue() {
|
return defaultReturnValue;
|
||||||
setChecked(getPersistedBoolean(mChecked));
|
}
|
||||||
|
|
||||||
|
return Settings.System.getInt(getContext().getContentResolver(),
|
||||||
|
getKey(), defaultReturnValue ? 1 : 0) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isPersisted() {
|
||||||
|
// Using getString instead of getInt so we can simply check for null
|
||||||
|
// instead of catching an exception. (All values are stored as strings.)
|
||||||
|
return Settings.System.getString(getContext().getContentResolver(), getKey()) != null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user