Bug fixed

This commit is contained in:
Phillip Hsu 2016-06-05 21:21:03 -07:00
parent 8617137b73
commit edacd53ac2
2 changed files with 31 additions and 23 deletions

View File

@ -33,6 +33,7 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
public static final String EXTRA_ITEM_ID = "com.philliphsu.clock2.ringtone.extra.ITEM_ID"; public static final String EXTRA_ITEM_ID = "com.philliphsu.clock2.ringtone.extra.ITEM_ID";
private Alarm mAlarm; private Alarm mAlarm;
private boolean mBound = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -55,10 +56,7 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
// This could be the case if we're starting a new instance of this activity after leaving the first launch. // This could be the case if we're starting a new instance of this activity after leaving the first launch.
AlarmUtils.removeUpcomingAlarmNotification(this, mAlarm); AlarmUtils.removeUpcomingAlarmNotification(this, mAlarm);
// Play the ringtone Intent intent = new Intent(this, RingtoneService.class);
Intent intent = new Intent(this, RingtoneService.class)
.putExtra(EXTRA_ITEM_ID, mAlarm.id());
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE); bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Button snooze = (Button) findViewById(R.id.btn_snooze); Button snooze = (Button) findViewById(R.id.btn_snooze);
@ -77,19 +75,19 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
}); });
} }
@Override
protected void onResume() {
super.onResume();
}
@Override @Override
protected void onNewIntent(Intent intent) { protected void onNewIntent(Intent intent) {
//super.onNewIntent(intent); // Not needed since no fragments hosted? //super.onNewIntent(intent); // Not needed since no fragments hosted?
if (mBound) {
mBoundService.interrupt(); // prepare to notify the alarm was missed
// Cannot rely on finish() to call onDestroy() on time before the activity is restarted,
// so unbind from the service manually.
unbindService();
// Calling recreate() would recreate this with its current intent, not the new intent passed in here. // Calling recreate() would recreate this with its current intent, not the new intent passed in here.
mBoundService.onNewActivity(); // notify alarm missed finish();
finish(); // destroy this, unbind from service, and stop service
startActivity(intent); startActivity(intent);
} }
}
@Override @Override
public void onWindowFocusChanged(boolean hasFocus) { public void onWindowFocusChanged(boolean hasFocus) {
@ -124,11 +122,7 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
// According to the lifecycle diagram, you unbind from the service first and then stop the service. unbindService();
unbindService(mConnection);
// TODO: Use appropriate subclass
// If service is not running, nothing happens.
stopService(new Intent(this, RingtoneService.class));
} }
@Override @Override
@ -148,18 +142,28 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
finish(); finish();
} }
private void unbindService() {
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
private RingtoneService mBoundService; private RingtoneService mBoundService;
private ServiceConnection mConnection = new ServiceConnection() { private ServiceConnection mConnection = new ServiceConnection() {
@Override @Override
public void onServiceConnected(ComponentName name, IBinder service) { public void onServiceConnected(ComponentName name, IBinder service) {
mBoundService = ((RingtoneService.RingtoneBinder) service).getService(); mBoundService = ((RingtoneService.RingtoneBinder) service).getService();
mBoundService.playRingtone(mAlarm);
mBoundService.setRingtoneCallback(RingtoneActivity.this); mBoundService.setRingtoneCallback(RingtoneActivity.this);
mBound = true;
} }
@Override @Override
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
mBoundService = null; mBoundService = null;
mBound = false;
} }
}; };
} }

View File

@ -12,12 +12,12 @@ import android.net.Uri;
import android.os.Binder; import android.os.Binder;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log; import android.util.Log;
import com.philliphsu.clock2.Alarm; import com.philliphsu.clock2.Alarm;
import com.philliphsu.clock2.R; import com.philliphsu.clock2.R;
import com.philliphsu.clock2.model.AlarmsRepository;
import static com.philliphsu.clock2.util.DateFormatUtils.formatTime; import static com.philliphsu.clock2.util.DateFormatUtils.formatTime;
import static com.philliphsu.clock2.util.Preconditions.checkNotNull; import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
@ -102,9 +102,9 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc
return mBinder; return mBinder;
} }
public void playRingtone(long itemId) { public void playRingtone(@NonNull Alarm alarm) {
if (mAudioManager == null && mRingtone == null) { if (mAudioManager == null && mRingtone == null) {
mAlarm = checkNotNull(AlarmsRepository.getInstance(this).getItem(itemId)); mAlarm = checkNotNull(alarm);
// TODO: The below call requires a notification, and there is no way to provide one suitable // TODO: The below call requires a notification, and there is no way to provide one suitable
// for both Alarms and Timers. Consider making this class abstract, and have subclasses // for both Alarms and Timers. Consider making this class abstract, and have subclasses
// implement an abstract method that calls startForeground(). You would then call that // implement an abstract method that calls startForeground(). You would then call that
@ -144,9 +144,13 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc
mRingtoneCallback = callback; mRingtoneCallback = callback;
} }
public void onNewActivity() { /**
* A way for clients to interrupt the currently running instance of this service. Interrupting
* the service is akin to prematurely auto silencing the ringtone right now. <b>Clients MUST
* unbind from this service immediately after interrupting.</b>
*/
public void interrupt() {
mAutoSilenced = true; mAutoSilenced = true;
//stopSelf();
} }
// Needed so clients can get the Service instance and e.g. call setRingtoneCallback(). // Needed so clients can get the Service instance and e.g. call setRingtoneCallback().