Sleep timer dialog

This commit is contained in:
Martin Fietz 2015-11-08 17:52:12 +01:00
parent c16cd623e3
commit 48eac2f4bc
4 changed files with 96 additions and 188 deletions

View File

@ -19,6 +19,8 @@ import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView; import android.widget.TextView;
import com.afollestad.materialdialogs.MaterialDialog;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
@ -291,49 +293,40 @@ public abstract class MediaplayerActivity extends ActionBarActivity
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.disable_sleeptimer_item: case R.id.disable_sleeptimer_item:
if (controller.serviceAvailable()) { if (controller.serviceAvailable()) {
AlertDialog.Builder stDialog = new AlertDialog.Builder(this);
stDialog.setTitle(R.string.sleep_timer_label); MaterialDialog.Builder stDialog = new MaterialDialog.Builder(this);
stDialog.setMessage(getString(R.string.time_left_label) stDialog.title(R.string.sleep_timer_label);
stDialog.content(getString(R.string.time_left_label)
+ Converter.getDurationStringLong((int) controller + Converter.getDurationStringLong((int) controller
.getSleepTimerTimeLeft())); .getSleepTimerTimeLeft()));
stDialog.setPositiveButton( stDialog.positiveText(R.string.disable_sleeptimer_label);
R.string.disable_sleeptimer_label, stDialog.negativeText(R.string.cancel_label);
new DialogInterface.OnClickListener() { stDialog.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog dialog) {
dialog.dismiss();
controller.disableSleepTimer();
}
@Override @Override
public void onClick(DialogInterface dialog, public void onNegative(MaterialDialog dialog) {
int which) { dialog.dismiss();
dialog.dismiss(); }
controller.disableSleepTimer(); });
} stDialog.build().show();
}
);
stDialog.setNegativeButton(R.string.cancel_label,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
}
);
stDialog.create().show();
} }
break; break;
case R.id.set_sleeptimer_item: case R.id.set_sleeptimer_item:
if (controller.serviceAvailable()) { if (controller.serviceAvailable()) {
SleepTimerDialog td = new SleepTimerDialog(this, 0, 0) { SleepTimerDialog td = new SleepTimerDialog(this) {
@Override @Override
public void onTimerSet(long millis, boolean shakeToReset, boolean vibrate) { public void onTimerSet(long millis, boolean shakeToReset, boolean vibrate) {
controller.setSleepTimer(millis, shakeToReset, vibrate); controller.setSleepTimer(millis, shakeToReset, vibrate);
} }
}; };
td.show(); td.createNewDialog().show();
break;
} }
break;
case R.id.visit_website_item: case R.id.visit_website_item:
Uri uri = Uri.parse(media.getWebsiteLink()); Uri uri = Uri.parse(media.getWebsiteLink());
startActivity(new Intent(Intent.ACTION_VIEW, uri)); startActivity(new Intent(Intent.ACTION_VIEW, uri));

View File

@ -17,11 +17,14 @@ import android.widget.EditText;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.Toast; import android.widget.Toast;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
public abstract class SleepTimerDialog extends Dialog { public abstract class SleepTimerDialog {
private static final String TAG = SleepTimerDialog.class.getSimpleName(); private static final String TAG = SleepTimerDialog.class.getSimpleName();
@ -35,39 +38,54 @@ public abstract class SleepTimerDialog extends Dialog {
private String PREF_SHAKE_TO_RESET = "ShakeToReset"; private String PREF_SHAKE_TO_RESET = "ShakeToReset";
private SharedPreferences prefs; private SharedPreferences prefs;
private MaterialDialog dialog;
private EditText etxtTime; private EditText etxtTime;
private Spinner spTimeUnit; private Spinner spTimeUnit;
private CheckBox cbShakeToReset; private CheckBox cbShakeToReset;
private CheckBox cbVibrate; private CheckBox cbVibrate;
private Button butConfirm;
private Button butCancel;
private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS }; private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS };
public SleepTimerDialog(Context context, int titleTextId, int leftButtonTextId) { public SleepTimerDialog(Context context) {
super(context);
this.context = context; this.context = context;
prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
} }
@Override public MaterialDialog createNewDialog() {
protected void onCreate(Bundle savedInstanceState) { MaterialDialog.Builder builder = new MaterialDialog.Builder(context);
super.onCreate(savedInstanceState); builder.title(R.string.set_sleeptimer_label);
requestWindowFeature(Window.FEATURE_NO_TITLE); builder.customView(R.layout.time_dialog, false);
String[] spinnerContent = new String[] { builder.positiveText(R.string.set_sleeptimer_label);
context.getString(R.string.time_seconds), builder.negativeText(R.string.cancel_label);
context.getString(R.string.time_minutes), builder.callback(new MaterialDialog.ButtonCallback() {
context.getString(R.string.time_hours) }; @Override
public void onNegative(MaterialDialog dialog) {
dialog.dismiss();
}
setContentView(R.layout.time_dialog); @Override
etxtTime = (EditText) findViewById(R.id.etxtTime); public void onPositive(MaterialDialog dialog) {
spTimeUnit = (Spinner) findViewById(R.id.spTimeUnit); try {
cbShakeToReset = (CheckBox) findViewById(R.id.cbShakeToReset); savePreferences();
cbVibrate = (CheckBox) findViewById(R.id.cbVibrate); long input = readTimeMillis();
butConfirm = (Button) findViewById(R.id.butConfirm); onTimerSet(input, cbShakeToReset.isChecked(), cbVibrate.isChecked());
butCancel = (Button) findViewById(R.id.butCancel); dialog.dismiss();
} catch (NumberFormatException e) {
setTitle(R.string.set_sleeptimer_label); e.printStackTrace();
Toast toast = Toast.makeText(context, R.string.time_dialog_invalid_input,
Toast.LENGTH_LONG);
toast.show();
}
}
});
dialog = builder.build();
View view = dialog.getView();
etxtTime = (EditText) view.findViewById(R.id.etxtTime);
spTimeUnit = (Spinner) view.findViewById(R.id.spTimeUnit);
cbShakeToReset = (CheckBox) view.findViewById(R.id.cbShakeToReset);
cbVibrate = (CheckBox) view.findViewById(R.id.cbVibrate);
etxtTime.setText(prefs.getString(PREF_VALUE, "15")); etxtTime.setText(prefs.getString(PREF_VALUE, "15"));
etxtTime.addTextChangedListener(new TextWatcher() { etxtTime.addTextChangedListener(new TextWatcher() {
@ -84,15 +102,16 @@ public abstract class SleepTimerDialog extends Dialog {
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
} }
}); });
etxtTime.postDelayed(new Runnable() { etxtTime.postDelayed(() -> {
@Override InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
public void run() { imm.showSoftInput(etxtTime, InputMethodManager.SHOW_IMPLICIT);
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etxtTime, InputMethodManager.SHOW_IMPLICIT);
}
}, 100); }, 100);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this.getContext(), String[] spinnerContent = new String[] {
context.getString(R.string.time_seconds),
context.getString(R.string.time_minutes),
context.getString(R.string.time_hours) };
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(context,
android.R.layout.simple_spinner_item, spinnerContent); android.R.layout.simple_spinner_item, spinnerContent);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spTimeUnit.setAdapter(spinnerAdapter); spTimeUnit.setAdapter(spinnerAdapter);
@ -102,40 +121,16 @@ public abstract class SleepTimerDialog extends Dialog {
cbShakeToReset.setChecked(prefs.getBoolean(PREF_SHAKE_TO_RESET, true)); cbShakeToReset.setChecked(prefs.getBoolean(PREF_SHAKE_TO_RESET, true));
cbVibrate.setChecked(prefs.getBoolean(PREF_VIBRATE, true)); cbVibrate.setChecked(prefs.getBoolean(PREF_VIBRATE, true));
butConfirm.setText(R.string.set_sleeptimer_label); return dialog;
butConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
savePreferences();
long input = readTimeMillis();
onTimerSet(input, cbShakeToReset.isChecked(), cbVibrate.isChecked());
dismiss();
} catch (NumberFormatException e) {
e.printStackTrace();
Toast toast = Toast.makeText(context, R.string.time_dialog_invalid_input,
Toast.LENGTH_LONG);
toast.show();
}
}
});
butCancel.setText(R.string.cancel_label);
butCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
} }
private void checkInputLength(int length) { private void checkInputLength(int length) {
if (length > 0) { if (length > 0) {
Log.d(TAG, "Length is larger than 0, enabling confirm button"); Log.d(TAG, "Length is larger than 0, enabling confirm button");
butConfirm.setEnabled(true); dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
} else { } else {
Log.d(TAG, "Length is smaller than 0, disabling confirm button"); Log.d(TAG, "Length is smaller than 0, disabling confirm button");
butConfirm.setEnabled(false); dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
} }
} }

View File

@ -1,21 +1,22 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical"
android:gravity="center">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" > android:orientation="horizontal" >
<EditText <EditText
android:id="@+id/etxtTime" android:id="@+id/etxtTime"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:layout_margin="8dp" android:layout_margin="8dp"
android:ems="7" android:ems="2"
android:hint="@string/enter_time_here_label" android:hint="@string/enter_time_here_label"
android:inputType="number" android:inputType="number"
android:maxLength="2" > android:maxLength="2" >
@ -25,18 +26,13 @@
<Spinner <Spinner
android:id="@+id/spTimeUnit" android:id="@+id/spTimeUnit"
android:layout_width="180dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
android:layout_marginTop="8dp" /> android:layout_marginTop="8dp" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
@ -58,52 +54,6 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/timer_vibration_label"/> android:text="@string/timer_vibration_label"/>
</LinearLayout>
</LinearLayout> </LinearLayout>
<RelativeLayout
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="48dp" >
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_alignParentTop="true"
android:background="?android:attr/dividerVertical" />
<View
android:id="@+id/horizontal_divider"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:background="?android:attr/dividerVertical" />
<Button
android:id="@+id/butCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@id/horizontal_divider"
android:background="?android:attr/selectableItemBackground"
android:text="@string/cancel_label" />
<Button
android:id="@+id/butConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/horizontal_divider"
android:background="?android:attr/selectableItemBackground"
android:text="@string/confirm_label" />
</RelativeLayout>
</LinearLayout> </LinearLayout>

View File

@ -1,46 +1,40 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="match_parent" android:orientation="vertical"
android:orientation="vertical" > android:gravity="center">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" > android:orientation="horizontal">
<EditText <EditText
android:id="@+id/etxtTime" android:id="@+id/etxtTime"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:layout_margin="8dp" android:layout_margin="8dp"
android:ems="7" android:ems="2"
android:hint="@string/enter_time_here_label" android:hint="@string/enter_time_here_label"
android:inputType="number" android:inputType="number"
android:maxLength="2" > android:maxLength="2" />
</EditText>
<Spinner <Spinner
android:id="@+id/spTimeUnit" android:id="@+id/spTimeUnit"
android:layout_width="180dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
android:layout_marginTop="8dp" /> android:layout_marginTop="8dp" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center"> android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -59,30 +53,6 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/timer_vibration_label"/> android:text="@string/timer_vibration_label"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
style="@android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/butConfirm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
tools:text="Confirm" />
<Button
android:id="@+id/butCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:text="Cancel" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>