Persist ringtone change as it is made. Restore ringtone selected callback on rotate.
This commit is contained in:
parent
66cb180887
commit
ecf41785c3
@ -17,6 +17,13 @@ import android.support.v7.app.AlertDialog;
|
|||||||
* (1) this dialog matches the current theme,
|
* (1) this dialog matches the current theme,
|
||||||
* (2) the selected ringtone URI is delivered via the {@link OnRingtoneSelectedListener
|
* (2) the selected ringtone URI is delivered via the {@link OnRingtoneSelectedListener
|
||||||
* OnRingtoneSelectedListener} callback.
|
* OnRingtoneSelectedListener} callback.
|
||||||
|
* <p></p>
|
||||||
|
* TODO: If a ringtone was playing and the configuration changes, the ringtone is destroyed.
|
||||||
|
* Restore the playing ringtone (seamlessly, without the stutter that comes from restarting).
|
||||||
|
* Setting setRetainInstance(true) in onCreate() made our app crash (error said attempted to
|
||||||
|
* access closed Cursor).
|
||||||
|
* We might need to play the ringtone from a Service instead, so we won't have to worry about
|
||||||
|
* the ringtone being destroyed on rotation.
|
||||||
*/
|
*/
|
||||||
public class RingtonePickerDialog extends BaseAlertDialogFragment {
|
public class RingtonePickerDialog extends BaseAlertDialogFragment {
|
||||||
private static final String TAG = "RingtonePickerDialog";
|
private static final String TAG = "RingtonePickerDialog";
|
||||||
|
|||||||
@ -23,14 +23,12 @@ import com.philliphsu.clock2.util.AlarmController;
|
|||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
import butterknife.OnClick;
|
import butterknife.OnClick;
|
||||||
|
|
||||||
import static com.philliphsu.clock2.DaysOfWeek.SATURDAY;
|
|
||||||
import static com.philliphsu.clock2.DaysOfWeek.SUNDAY;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Phillip Hsu on 7/31/2016.
|
* Created by Phillip Hsu on 7/31/2016.
|
||||||
*/
|
*/
|
||||||
public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder {
|
public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder {
|
||||||
private static final String TAG = "ExpandedAlarmViewHolder";
|
private static final String TAG = "ExpandedAlarmViewHolder";
|
||||||
|
private static final String TAG_RINGTONE_PICKER = "ringtone_picker";
|
||||||
|
|
||||||
@Bind(R.id.ok) Button mOk;
|
@Bind(R.id.ok) Button mOk;
|
||||||
@Bind(R.id.delete) Button mDelete;
|
@Bind(R.id.delete) Button mDelete;
|
||||||
@ -87,6 +85,13 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder {
|
|||||||
/*item 2*/Utils.getTextColorFromThemeAttr(getContext(), android.R.attr.textColorHint)
|
/*item 2*/Utils.getTextColorFromThemeAttr(getContext(), android.R.attr.textColorHint)
|
||||||
};
|
};
|
||||||
mDayToggleColors = new ColorStateList(states, colors);
|
mDayToggleColors = new ColorStateList(states, colors);
|
||||||
|
|
||||||
|
RingtonePickerDialog picker = (RingtonePickerDialog)
|
||||||
|
mFragmentManager.findFragmentByTag(TAG_RINGTONE_PICKER);
|
||||||
|
if (picker != null) {
|
||||||
|
Log.i(TAG, "Restoring ringtone picker callback");
|
||||||
|
picker.setOnRingtoneSelectedListener(newOnRingtoneSelectedListener());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -126,13 +131,9 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder {
|
|||||||
// // TODO: This is VERY BAD. Use a Controller/Presenter instead.
|
// // TODO: This is VERY BAD. Use a Controller/Presenter instead.
|
||||||
// // The result will be delivered to MainActivity, and then delegated to AlarmsFragment.
|
// // The result will be delivered to MainActivity, and then delegated to AlarmsFragment.
|
||||||
// ((Activity) getContext()).startActivityForResult(intent, AlarmsFragment.REQUEST_PICK_RINGTONE);
|
// ((Activity) getContext()).startActivityForResult(intent, AlarmsFragment.REQUEST_PICK_RINGTONE);
|
||||||
RingtonePickerDialog dialog = RingtonePickerDialog.newInstance(new RingtonePickerDialog.OnRingtoneSelectedListener() {
|
RingtonePickerDialog dialog = RingtonePickerDialog.newInstance(
|
||||||
@Override
|
newOnRingtoneSelectedListener(), getSelectedRingtoneUri());
|
||||||
public void onRingtoneSelected(Uri ringtoneUri) {
|
dialog.show(mFragmentManager, TAG_RINGTONE_PICKER);
|
||||||
Log.d(TAG, "Selected ringtone: " + ringtoneUri);
|
|
||||||
}
|
|
||||||
}, getSelectedRingtoneUri());
|
|
||||||
dialog.show(mFragmentManager, "TAG");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnClick(R.id.vibrate)
|
@OnClick(R.id.vibrate)
|
||||||
@ -206,25 +207,18 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder {
|
|||||||
: Uri.parse(ringtone);
|
: Uri.parse(ringtone);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isRecurringDay(int weekDay) {
|
private RingtonePickerDialog.OnRingtoneSelectedListener newOnRingtoneSelectedListener() {
|
||||||
// What position in the week is this day located at?
|
return new RingtonePickerDialog.OnRingtoneSelectedListener() {
|
||||||
int pos = DaysOfWeek.getInstance(getContext()).positionOf(weekDay);
|
@Override
|
||||||
// Return the state of this day according to its button
|
public void onRingtoneSelected(Uri ringtoneUri) {
|
||||||
return mDays[pos].isChecked();
|
Log.d(TAG, "Selected ringtone: " + ringtoneUri.toString());
|
||||||
}
|
|
||||||
|
|
||||||
private void createNewAlarmAndWriteToDb() {
|
|
||||||
final Alarm oldAlarm = getAlarm();
|
final Alarm oldAlarm = getAlarm();
|
||||||
Alarm newAlarm = Alarm.builder()
|
Alarm newAlarm = oldAlarm.toBuilder()
|
||||||
.ringtone(""/*TODO*/)
|
.ringtone(ringtoneUri.toString())
|
||||||
.build();
|
.build();
|
||||||
oldAlarm.copyMutableFieldsTo(newAlarm);
|
oldAlarm.copyMutableFieldsTo(newAlarm);
|
||||||
// ----------------------------------------------
|
mAlarmController.save(newAlarm);
|
||||||
// TOneverDO: precede copyMutableFieldsTo()
|
|
||||||
newAlarm.setEnabled(mSwitch.isChecked());
|
|
||||||
for (int i = SUNDAY; i <= SATURDAY; i++) {
|
|
||||||
newAlarm.setRecurring(i, isRecurringDay(i));
|
|
||||||
}
|
}
|
||||||
// ----------------------------------------------
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user