Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "always" mode for shake to reset sleep #6165

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Expand Up @@ -7,11 +7,14 @@
import android.text.format.DateFormat;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
Expand All @@ -36,6 +39,7 @@
public class SleepTimerDialog extends DialogFragment {
private PlaybackController controller;
private EditText etxtTime;
private Spinner spShakeToResetMode;
private LinearLayout timeSetup;
private LinearLayout timeDisplay;
private TextView time;
Expand Down Expand Up @@ -76,6 +80,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
builder.setPositiveButton(R.string.close_label, null);

etxtTime = content.findViewById(R.id.etxtTime);
spShakeToResetMode = content.findViewById(R.id.spShakeToResetMode);
timeSetup = content.findViewById(R.id.timeSetup);
timeDisplay = content.findViewById(R.id.timeDisplay);
timeDisplay.setVisibility(View.GONE);
Expand Down Expand Up @@ -108,6 +113,24 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
imm.showSoftInput(etxtTime, InputMethodManager.SHOW_IMPLICIT);
}, 100);

String[] spShakeToResetModeContent = new String[] {
getString(R.string.sleep_timer_shake_to_reset_mode_end),
getString(R.string.sleep_timer_shake_to_reset_mode_always) };
ArrayAdapter<String> spShakeToResetModeAdapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_spinner_item, spShakeToResetModeContent);
spShakeToResetModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spShakeToResetMode.setAdapter(spShakeToResetModeAdapter);
spShakeToResetMode.setSelection(SleepTimerPreferences.shakeToResetMode());
spShakeToResetMode.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SleepTimerPreferences.setShakeToResetMode(position);
}

@Override
public void onNothingSelected(AdapterView<?> parent) { }
});

final CheckBox cbShakeToReset = content.findViewById(R.id.cbShakeToReset);
final CheckBox cbVibrate = content.findViewById(R.id.cbVibrate);
chAutoEnable = content.findViewById(R.id.chAutoEnable);
Expand Down
19 changes: 16 additions & 3 deletions app/src/main/res/layout/time_dialog.xml
Expand Up @@ -128,11 +128,24 @@
android:orientation="vertical"
android:layout_marginTop="8dp">

<CheckBox
android:id="@+id/cbShakeToReset"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/shake_to_reset_label" />
android:orientation="horizontal">

<CheckBox
android:id="@+id/cbShakeToReset"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/shake_to_reset_label" />

<Spinner
android:id="@+id/spShakeToResetMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

<CheckBox
android:id="@+id/cbVibrate"
Expand Down
Expand Up @@ -17,10 +17,12 @@ public class SleepTimerPreferences {

private static final String PREF_VIBRATE = "Vibrate";
private static final String PREF_SHAKE_TO_RESET = "ShakeToReset";
private static final String PREF_SHAKE_TO_RESET_MODE = "ShakeToResetMode";
private static final String PREF_AUTO_ENABLE = "AutoEnable";
private static final String PREF_AUTO_ENABLE_FROM = "AutoEnableFrom";
private static final String PREF_AUTO_ENABLE_TO = "AutoEnableTo";

private static final int DEFAULT_SHAKE_TO_RESET_MODE = 0;
private static final String DEFAULT_LAST_TIMER = "15";
private static final int DEFAULT_AUTO_ENABLE_FROM = 22;
private static final int DEFAULT_AUTO_ENABLE_TO = 6;
Expand Down Expand Up @@ -66,6 +68,14 @@ public static boolean shakeToReset() {
return prefs.getBoolean(PREF_SHAKE_TO_RESET, true);
}

public static void setShakeToResetMode(int value) {
prefs.edit().putInt(PREF_SHAKE_TO_RESET_MODE, value).apply();
}

public static int shakeToResetMode() {
return prefs.getInt(PREF_SHAKE_TO_RESET_MODE, DEFAULT_SHAKE_TO_RESET_MODE);
}

public static void setAutoEnable(boolean autoEnable) {
prefs.edit().putBoolean(PREF_AUTO_ENABLE, autoEnable).apply();
}
Expand Down
Expand Up @@ -290,6 +290,13 @@ public SleepTimer(long waitingTime) {
this.timeLeft = waitingTime;
}

private void stopShakeListener() {
if (shakeListener != null) {
shakeListener.pause();
shakeListener = null;
}
}

@Override
public void run() {
Log.d(TAG, "Starting");
Expand Down Expand Up @@ -318,16 +325,26 @@ public void run() {
hasVibrated = true;
}
}
if (shakeListener == null && SleepTimerPreferences.shakeToReset()) {
shakeListener = new ShakeListener(context, this);
}
}

//Start shake listener if enabled
//Mode 0: At end / fade out
//Mode 1: Always
if (shakeListener == null && SleepTimerPreferences.shakeToReset()
&& ((SleepTimerPreferences.shakeToResetMode() == 0 && timeLeft < NOTIFICATION_THRESHOLD)
|| (SleepTimerPreferences.shakeToResetMode() == 1))) {
shakeListener = new ShakeListener(context, this);
}
//Stop shake listener if not enabled anymore
if (shakeListener != null
&& (!SleepTimerPreferences.shakeToReset()
|| (SleepTimerPreferences.shakeToResetMode() == 0 && timeLeft > NOTIFICATION_THRESHOLD))) {
stopShakeListener();
}

if (timeLeft <= 0) {
Log.d(TAG, "Sleep timer expired");
if (shakeListener != null) {
shakeListener.pause();
shakeListener = null;
}
stopShakeListener();
hasVibrated = false;
}
}
Expand All @@ -340,17 +357,12 @@ public long getWaitingTime() {
public void restart() {
EventBus.getDefault().post(SleepTimerUpdatedEvent.cancelled());
setSleepTimer(waitingTime);
if (shakeListener != null) {
shakeListener.pause();
shakeListener = null;
}
stopShakeListener();
}

public void cancel() {
sleepTimerFuture.cancel(true);
if (shakeListener != null) {
shakeListener.pause();
}
stopShakeListener();
EventBus.getDefault().post(SleepTimerUpdatedEvent.cancelled());
}
}
Expand Down
2 changes: 2 additions & 0 deletions ui/i18n/src/main/res/values/strings.xml
Expand Up @@ -626,6 +626,8 @@
<string name="auto_enable_label_with_times">Automatically activate the sleep timer when pressing play between %1$s and %2$s</string>
<string name="auto_enable_change_times">Change time range</string>
<string name="sleep_timer_enabled_label">Sleep timer enabled</string>
<string name="sleep_timer_shake_to_reset_mode_end">At end</string>
<string name="sleep_timer_shake_to_reset_mode_always">Always</string>

<!-- Synchronisation -->
<string name="synchronization_choose_title">Choose synchronization provider</string>
Expand Down