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

View File

@ -17,11 +17,14 @@ import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.R;
public abstract class SleepTimerDialog extends Dialog {
public abstract class SleepTimerDialog {
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 SharedPreferences prefs;
private MaterialDialog dialog;
private EditText etxtTime;
private Spinner spTimeUnit;
private CheckBox cbShakeToReset;
private CheckBox cbVibrate;
private Button butConfirm;
private Button butCancel;
private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS };
public SleepTimerDialog(Context context, int titleTextId, int leftButtonTextId) {
super(context);
public SleepTimerDialog(Context context) {
this.context = context;
prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
String[] spinnerContent = new String[] {
context.getString(R.string.time_seconds),
context.getString(R.string.time_minutes),
context.getString(R.string.time_hours) };
public MaterialDialog createNewDialog() {
MaterialDialog.Builder builder = new MaterialDialog.Builder(context);
builder.title(R.string.set_sleeptimer_label);
builder.customView(R.layout.time_dialog, false);
builder.positiveText(R.string.set_sleeptimer_label);
builder.negativeText(R.string.cancel_label);
builder.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onNegative(MaterialDialog dialog) {
dialog.dismiss();
}
setContentView(R.layout.time_dialog);
etxtTime = (EditText) findViewById(R.id.etxtTime);
spTimeUnit = (Spinner) findViewById(R.id.spTimeUnit);
cbShakeToReset = (CheckBox) findViewById(R.id.cbShakeToReset);
cbVibrate = (CheckBox) findViewById(R.id.cbVibrate);
butConfirm = (Button) findViewById(R.id.butConfirm);
butCancel = (Button) findViewById(R.id.butCancel);
setTitle(R.string.set_sleeptimer_label);
@Override
public void onPositive(MaterialDialog dialog) {
try {
savePreferences();
long input = readTimeMillis();
onTimerSet(input, cbShakeToReset.isChecked(), cbVibrate.isChecked());
dialog.dismiss();
} catch (NumberFormatException e) {
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.addTextChangedListener(new TextWatcher() {
@ -84,15 +102,16 @@ public abstract class SleepTimerDialog extends Dialog {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
etxtTime.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etxtTime, InputMethodManager.SHOW_IMPLICIT);
}
etxtTime.postDelayed(() -> {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etxtTime, InputMethodManager.SHOW_IMPLICIT);
}, 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);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spTimeUnit.setAdapter(spinnerAdapter);
@ -102,40 +121,16 @@ public abstract class SleepTimerDialog extends Dialog {
cbShakeToReset.setChecked(prefs.getBoolean(PREF_SHAKE_TO_RESET, true));
cbVibrate.setChecked(prefs.getBoolean(PREF_VIBRATE, true));
butConfirm.setText(R.string.set_sleeptimer_label);
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();
}
});
return dialog;
}
private void checkInputLength(int length) {
if (length > 0) {
Log.d(TAG, "Length is larger than 0, enabling confirm button");
butConfirm.setEnabled(true);
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
} else {
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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/etxtTime"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:ems="7"
android:ems="2"
android:hint="@string/enter_time_here_label"
android:inputType="number"
android:maxLength="2" >
@ -25,18 +26,13 @@
<Spinner
android:id="@+id/spTimeUnit"
android:layout_width="180dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
@ -58,52 +54,6 @@
android:layout_height="wrap_content"
android:text="@string/timer_vibration_label"/>
</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>

View File

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