Marshall and unmarshall Alarms passed to and from AlarmActivity and AlarmRingtoneService.

This commit is contained in:
Phillip Hsu 2017-02-28 16:15:26 -08:00
parent dd4c6d417b
commit 92fac3eb34
5 changed files with 41 additions and 4 deletions

View File

@ -230,7 +230,7 @@ public final class AlarmController {
private PendingIntent alarmIntent(Alarm alarm, boolean retrievePrevious) {
Intent intent = new Intent(mAppContext, AlarmActivity.class)
.putExtra(AlarmActivity.EXTRA_RINGING_OBJECT, alarm);
.putExtra(AlarmActivity.EXTRA_RINGING_OBJECT, ParcelableUtil.marshall(alarm));
int flag = retrievePrevious ? FLAG_NO_CREATE : FLAG_CANCEL_CURRENT;
// Even when we try to retrieve a previous instance that actually did exist,
// null can be returned for some reason. Thus, we don't checkNotNull().

View File

@ -20,6 +20,7 @@ import android.app.Notification;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.NotificationCompat;
import android.view.ViewGroup;
@ -124,6 +125,11 @@ public class AlarmActivity extends RingtoneActivity<Alarm> {
stopAndFinish();
}
@Override
protected Parcelable.Creator<Alarm> getParcelableCreator() {
return Alarm.CREATOR;
}
// TODO: Consider changing the return type to Notification, and move the actual
// task of notifying to the base class.
@Override

View File

@ -34,6 +34,7 @@ import com.philliphsu.clock2.BaseActivity;
import com.philliphsu.clock2.R;
import com.philliphsu.clock2.ringtone.playback.RingtoneService;
import com.philliphsu.clock2.util.LocalBroadcastHelper;
import com.philliphsu.clock2.util.ParcelableUtil;
import butterknife.Bind;
import butterknife.ButterKnife;
@ -98,13 +99,25 @@ public abstract class RingtoneActivity<T extends Parcelable> extends BaseActivit
@OnClick(R.id.btn_right)
protected abstract void onRightButtonClick();
/**
* @return An implementation of {@link android.os.Parcelable.Creator} that can create
* an instance of the {@link #mRingingObject ringing object}.
*/
// TODO: Make abstract when we override this in all RingtoneActivities.
protected Parcelable.Creator<T> getParcelableCreator() {
return null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButterKnife.bind(this);
if ((mRingingObject = getIntent().getParcelableExtra(EXTRA_RINGING_OBJECT)) == null)
final byte[] bytes = getIntent().getByteArrayExtra(EXTRA_RINGING_OBJECT);
if (bytes == null) {
throw new IllegalStateException("Cannot start RingtoneActivity without a ringing object");
}
mRingingObject = ParcelableUtil.unmarshall(bytes, getParcelableCreator());
sIsAlive = true;
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
@ -122,7 +135,7 @@ public abstract class RingtoneActivity<T extends Parcelable> extends BaseActivit
mRightButton.setCompoundDrawablesWithIntrinsicBounds(0, getRightButtonDrawable(), 0, 0);
Intent intent = new Intent(this, getRingtoneServiceClass())
.putExtra(EXTRA_RINGING_OBJECT, mRingingObject);
.putExtra(EXTRA_RINGING_OBJECT, ParcelableUtil.marshall(mRingingObject));
startService(intent);
}

View File

@ -19,6 +19,7 @@ package com.philliphsu.clock2.ringtone.playback;
import android.app.Notification;
import android.content.Intent;
import android.net.Uri;
import android.os.Parcelable;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
@ -106,4 +107,9 @@ public class AlarmRingtoneService extends RingtoneService<Alarm> {
protected int minutesToAutoSilence() {
return AlarmPreferences.minutesToSilenceAfter(this);
}
@Override
protected Parcelable.Creator<Alarm> getParcelableCreator() {
return Alarm.CREATOR;
}
}

View File

@ -34,6 +34,7 @@ import android.util.Log;
import com.philliphsu.clock2.R;
import com.philliphsu.clock2.ringtone.RingtoneActivity;
import com.philliphsu.clock2.util.LocalBroadcastHelper;
import com.philliphsu.clock2.util.ParcelableUtil;
import java.util.concurrent.TimeUnit;
@ -104,12 +105,23 @@ public abstract class RingtoneService<T extends Parcelable> extends Service {
*/
protected abstract int minutesToAutoSilence();
/**
* @return An implementation of {@link android.os.Parcelable.Creator} that can create
* an instance of the {@link #mRingingObject ringing object}.
*/
// TODO: Make abstract when we override this in all RingtoneServices.
protected Parcelable.Creator<T> getParcelableCreator() {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mRingingObject == null) {
if ((mRingingObject = intent.getParcelableExtra(EXTRA_RINGING_OBJECT)) == null) {
final byte[] bytes = intent.getByteArrayExtra(EXTRA_RINGING_OBJECT);
if (bytes == null) {
throw new IllegalStateException("Cannot start RingtoneService without a ringing object");
}
mRingingObject = ParcelableUtil.unmarshall(bytes, getParcelableCreator());
}
// Play ringtone, if not already playing
if (mAudioManager == null && mRingtone == null) {