-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHANGELOG: Added sleep on flat feature
Signed-off-by: DHD2280 <[email protected]>
- Loading branch information
Showing
16 changed files
with
962 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
app/src/main/java/it/dhd/oxygencustomizer/services/tiles/SleepOnSurfaceTileService.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,61 @@ | ||
package it.dhd.oxygencustomizer.services.tiles; | ||
|
||
import static android.service.quicksettings.Tile.STATE_ACTIVE; | ||
import static android.service.quicksettings.Tile.STATE_INACTIVE; | ||
|
||
import android.graphics.drawable.Icon; | ||
import android.service.quicksettings.Tile; | ||
import android.service.quicksettings.TileService; | ||
|
||
import it.dhd.oxygencustomizer.R; | ||
import it.dhd.oxygencustomizer.utils.OCPreferences; | ||
|
||
/** | ||
* A TileService that toggles the SleepOnFlatScreen preference | ||
* From Siavash79/PixelXpert | ||
*/ | ||
|
||
public class SleepOnSurfaceTileService extends TileService { | ||
private boolean mLastEnabled = false; | ||
|
||
@Override | ||
public void onStartListening() { | ||
super.onStartListening(); | ||
|
||
OCPreferences.getPrefs().registerOnSharedPreferenceChangeListener((sharedPreferences, key) -> | ||
setTile(OCPreferences.getBoolean("SleepOnFlatScreen", false))); | ||
|
||
setTile(OCPreferences.getBoolean("SleepOnFlatScreen", false)); | ||
} | ||
|
||
@Override | ||
public void onClick() { | ||
setTile(!mLastEnabled); //Isn't mandatory, but without it tile click will take time to reflect on UI | ||
|
||
new Thread(() -> OCPreferences.putBoolean("SleepOnFlatScreen", mLastEnabled)).start(); //otherwise click will be blocked until pref is saved | ||
} | ||
|
||
private void setTile(boolean enabled) { | ||
if(mLastEnabled == enabled) return; | ||
|
||
mLastEnabled = enabled; | ||
|
||
Tile thisTile = getQsTile(); | ||
|
||
thisTile.setIcon(Icon.createWithResource(getApplicationContext(), enabled | ||
? R.drawable.ic_sleep_surface_on | ||
: R.drawable.ic_sleep_surface_off)); | ||
|
||
thisTile.setState(enabled | ||
? STATE_ACTIVE | ||
: STATE_INACTIVE); | ||
|
||
thisTile.setSubtitle(getString(enabled | ||
? R.string.general_on | ||
: R.string.general_off)); | ||
|
||
thisTile.setLabel(getString(R.string.sleep_on_flat_screen_tile_title)); | ||
|
||
thisTile.updateTile(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/it/dhd/oxygencustomizer/ui/fragments/mods/misc/SleepOnFlatFragment.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,32 @@ | ||
package it.dhd.oxygencustomizer.ui.fragments.mods.misc; | ||
|
||
import it.dhd.oxygencustomizer.R; | ||
import it.dhd.oxygencustomizer.ui.base.ControlledPreferenceFragmentCompat; | ||
|
||
public class SleepOnFlatFragment extends ControlledPreferenceFragmentCompat { | ||
|
||
@Override | ||
public String getTitle() { | ||
return getString(R.string.sleep_on_flat_screen_tile_title); | ||
} | ||
|
||
@Override | ||
public boolean backButtonEnabled() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getLayoutResource() { | ||
return R.xml.sleep_on_flat; | ||
} | ||
|
||
@Override | ||
public boolean hasMenu() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String[] getScopes() { | ||
return new String[0]; | ||
} | ||
} |
207 changes: 207 additions & 0 deletions
207
app/src/main/java/it/dhd/oxygencustomizer/ui/preferences/OplusPrimarySwitchPreference.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,207 @@ | ||
package it.dhd.oxygencustomizer.ui.preferences; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.os.Parcel; | ||
import android.os.Parcelable; | ||
import android.util.AttributeSet; | ||
import android.view.MotionEvent; | ||
|
||
import androidx.annotation.Keep; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.preference.Preference; | ||
import androidx.preference.PreferenceViewHolder; | ||
|
||
import com.google.android.material.materialswitch.MaterialSwitch; | ||
|
||
import it.dhd.oxygencustomizer.R; | ||
|
||
/** | ||
* A custom preference that provides a switch toggle and a clickable preference. | ||
*/ | ||
public class OplusPrimarySwitchPreference extends OplusTwoTargetPreference { | ||
|
||
private MaterialSwitch mSwitch; | ||
private boolean mChecked; | ||
private boolean mCheckedSet; | ||
private boolean mEnableSwitch = true; | ||
|
||
public OplusPrimarySwitchPreference(Context context, AttributeSet attrs, | ||
int defStyleAttr, int defStyleRes) { | ||
super(context, attrs, defStyleAttr, defStyleRes); | ||
} | ||
|
||
public OplusPrimarySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
public OplusPrimarySwitchPreference(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public OplusPrimarySwitchPreference(Context context) { | ||
super(context); | ||
} | ||
|
||
@Override | ||
protected int getSecondTargetResId() { | ||
return R.layout.oplus_preference_widget_switch; | ||
} | ||
|
||
@SuppressLint("ClickableViewAccessibility") | ||
@Override | ||
public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { | ||
super.onBindViewHolder(holder); | ||
mSwitch = (MaterialSwitch) holder.findViewById(R.id.switchWidget); | ||
if (mSwitch != null) { | ||
mSwitch.setOnClickListener(v -> { | ||
if (mSwitch != null && !mSwitch.isEnabled()) { | ||
return; | ||
} | ||
final boolean newChecked = !mChecked; | ||
if (callChangeListener(newChecked)) { | ||
setChecked(newChecked); | ||
} | ||
}); | ||
|
||
// Consumes move events to ignore drag actions. | ||
mSwitch.setOnTouchListener((v, event) -> { | ||
return event.getActionMasked() == MotionEvent.ACTION_MOVE; | ||
}); | ||
|
||
mSwitch.setContentDescription(getTitle()); | ||
mSwitch.setChecked(mChecked); | ||
mSwitch.setEnabled(mEnableSwitch); | ||
} | ||
} | ||
|
||
public boolean isChecked() { | ||
return mChecked; | ||
} | ||
|
||
/** | ||
* Used to validate the state of mChecked and mCheckedSet when testing, without requiring | ||
* that a ViewHolder be bound to the object. | ||
*/ | ||
@Keep | ||
@Nullable | ||
public Boolean getCheckedState() { | ||
return mCheckedSet ? mChecked : null; | ||
} | ||
|
||
/** | ||
* Set the checked status to be {@code checked}. | ||
* | ||
* @param checked The new checked status | ||
*/ | ||
public void setChecked(boolean checked) { | ||
// Always persist/notify the first time; don't assume the field's default of false. | ||
final boolean changed = mChecked != checked; | ||
if (changed || !mCheckedSet) { | ||
mChecked = checked; | ||
mCheckedSet = true; | ||
persistBoolean(checked); | ||
if (changed) { | ||
notifyDependencyChange(shouldDisableDependents()); | ||
notifyChanged(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Set the Switch to be the status of {@code enabled}. | ||
* | ||
* @param enabled The new enabled status | ||
*/ | ||
public void setSwitchEnabled(boolean enabled) { | ||
mEnableSwitch = enabled; | ||
if (mSwitch != null) { | ||
mSwitch.setEnabled(enabled); | ||
} | ||
} | ||
|
||
public MaterialSwitch getSwitch() { | ||
return mSwitch; | ||
} | ||
|
||
@Override | ||
protected boolean shouldHideSecondTarget() { | ||
return getSecondTargetResId() == 0; | ||
} | ||
|
||
@Override | ||
protected void onSetInitialValue(Object defaultValue) { | ||
if (defaultValue == null) { | ||
defaultValue = false; | ||
} | ||
setChecked(getPersistedBoolean((Boolean) defaultValue)); | ||
} | ||
|
||
@Override | ||
protected Object onGetDefaultValue(TypedArray a, int index) { | ||
return a.getBoolean(index, false); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
protected Parcelable onSaveInstanceState() { | ||
final Parcelable superState = super.onSaveInstanceState(); | ||
if (isPersistent()) { | ||
// No need to save instance state since it's persistent | ||
return superState; | ||
} | ||
|
||
final SavedState myState = new SavedState(superState); | ||
myState.mChecked = isChecked(); | ||
return myState; | ||
} | ||
|
||
@Override | ||
protected void onRestoreInstanceState(@Nullable Parcelable state) { | ||
if (state == null || !state.getClass().equals(SavedState.class)) { | ||
// Didn't save state for us in onSaveInstanceState | ||
super.onRestoreInstanceState(state); | ||
return; | ||
} | ||
|
||
SavedState myState = (SavedState) state; | ||
super.onRestoreInstanceState(myState.getSuperState()); | ||
setChecked(myState.mChecked); | ||
} | ||
|
||
static class SavedState extends Preference.BaseSavedState { | ||
public static final Parcelable.Creator<SavedState> CREATOR = | ||
new Parcelable.Creator<>() { | ||
@Override | ||
public SavedState createFromParcel(Parcel in) { | ||
return new SavedState(in); | ||
} | ||
|
||
@Override | ||
public SavedState[] newArray(int size) { | ||
return new SavedState[size]; | ||
} | ||
}; | ||
|
||
boolean mChecked; | ||
|
||
SavedState(Parcel source) { | ||
super(source); | ||
mChecked = source.readInt() == 1; | ||
} | ||
|
||
SavedState(Parcelable superState) { | ||
super(superState); | ||
} | ||
|
||
@Override | ||
public void writeToParcel(Parcel dest, int flags) { | ||
super.writeToParcel(dest, flags); | ||
dest.writeInt(mChecked ? 1 : 0); | ||
} | ||
} | ||
|
||
} | ||
|
70 changes: 70 additions & 0 deletions
70
app/src/main/java/it/dhd/oxygencustomizer/ui/preferences/OplusTwoTargetPreference.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,70 @@ | ||
package it.dhd.oxygencustomizer.ui.preferences; | ||
|
||
|
||
import android.content.Context; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.preference.PreferenceViewHolder; | ||
|
||
import it.dhd.oneplusui.preference.OplusPreference; | ||
import it.dhd.oxygencustomizer.R; | ||
|
||
/** | ||
* The Base preference with two target areas divided by a vertical divider | ||
*/ | ||
public class OplusTwoTargetPreference extends OplusPreference { | ||
|
||
public OplusTwoTargetPreference(Context context, AttributeSet attrs, | ||
int defStyleAttr, int defStyleRes) { | ||
super(context, attrs, defStyleAttr, defStyleRes); | ||
init(context); | ||
} | ||
|
||
public OplusTwoTargetPreference(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
init(context); | ||
} | ||
|
||
public OplusTwoTargetPreference(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
init(context); | ||
} | ||
|
||
public OplusTwoTargetPreference(Context context) { | ||
super(context); | ||
init(context); | ||
} | ||
|
||
private void init(Context context) { | ||
setLayoutResource(R.layout.oplus_preference_two_target); | ||
final int secondTargetResId = getSecondTargetResId(); | ||
if (secondTargetResId != 0) { | ||
setWidgetLayoutResource(secondTargetResId); | ||
} | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { | ||
super.onBindViewHolder(holder); | ||
final View divider = holder.findViewById(R.id.two_target_divider); | ||
final View widgetFrame = holder.findViewById(android.R.id.widget_frame); | ||
final boolean shouldHideSecondTarget = shouldHideSecondTarget(); | ||
if (divider != null) { | ||
divider.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE); | ||
} | ||
if (widgetFrame != null) { | ||
widgetFrame.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE); | ||
} | ||
} | ||
|
||
protected boolean shouldHideSecondTarget() { | ||
return getSecondTargetResId() == 0; | ||
} | ||
|
||
protected int getSecondTargetResId() { | ||
return 0; | ||
} | ||
} | ||
|
Oops, something went wrong.