Use loader to load single alarm
This commit is contained in:
parent
d042891e51
commit
43e9c20468
@ -4,11 +4,10 @@ import android.app.IntentService;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
|
||||||
|
import com.philliphsu.clock2.model.AlarmDatabaseHelper.AlarmCursor;
|
||||||
import com.philliphsu.clock2.model.DatabaseManager;
|
import com.philliphsu.clock2.model.DatabaseManager;
|
||||||
import com.philliphsu.clock2.util.AlarmUtils;
|
import com.philliphsu.clock2.util.AlarmUtils;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An {@link IntentService} subclass for handling asynchronous task requests in
|
* An {@link IntentService} subclass for handling asynchronous task requests in
|
||||||
* a service on a separate handler thread.
|
* a service on a separate handler thread.
|
||||||
@ -60,12 +59,15 @@ public class OnBootUpAlarmScheduler extends IntentService {
|
|||||||
@Override
|
@Override
|
||||||
protected void onHandleIntent(Intent intent) {
|
protected void onHandleIntent(Intent intent) {
|
||||||
if (intent != null) {
|
if (intent != null) {
|
||||||
List<Alarm> alarms = DatabaseManager.getInstance(this).getAlarms();
|
// IntentService already works in a background thread, so we don't need to use a loader.
|
||||||
for (Alarm a : alarms) {
|
AlarmCursor cursor = DatabaseManager.getInstance(this).queryAlarms();
|
||||||
if (a.isEnabled()) {
|
while (cursor.moveToNext()) {
|
||||||
AlarmUtils.scheduleAlarm(this, a, false);
|
Alarm alarm = cursor.getAlarm();
|
||||||
|
if (alarm.isEnabled()) {
|
||||||
|
AlarmUtils.scheduleAlarm(this, alarm, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
cursor.close();
|
||||||
|
|
||||||
/* final String action = intent.getAction();
|
/* final String action = intent.getAction();
|
||||||
if (ACTION_FOO.equals(action)) {
|
if (ACTION_FOO.equals(action)) {
|
||||||
|
|||||||
@ -5,6 +5,8 @@ import android.media.RingtoneManager;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.StringRes;
|
import android.support.annotation.StringRes;
|
||||||
|
import android.support.v4.app.LoaderManager;
|
||||||
|
import android.support.v4.content.Loader;
|
||||||
import android.support.v7.app.ActionBar;
|
import android.support.v7.app.ActionBar;
|
||||||
import android.support.v7.widget.SwitchCompat;
|
import android.support.v7.widget.SwitchCompat;
|
||||||
import android.text.SpannableString;
|
import android.text.SpannableString;
|
||||||
@ -28,6 +30,7 @@ import com.philliphsu.clock2.BaseActivity;
|
|||||||
import com.philliphsu.clock2.DaysOfWeek;
|
import com.philliphsu.clock2.DaysOfWeek;
|
||||||
import com.philliphsu.clock2.R;
|
import com.philliphsu.clock2.R;
|
||||||
import com.philliphsu.clock2.SharedPreferencesHelper;
|
import com.philliphsu.clock2.SharedPreferencesHelper;
|
||||||
|
import com.philliphsu.clock2.model.AlarmLoader;
|
||||||
import com.philliphsu.clock2.model.DatabaseManager;
|
import com.philliphsu.clock2.model.DatabaseManager;
|
||||||
import com.philliphsu.clock2.ringtone.RingtoneActivity;
|
import com.philliphsu.clock2.ringtone.RingtoneActivity;
|
||||||
import com.philliphsu.clock2.util.AlarmUtils;
|
import com.philliphsu.clock2.util.AlarmUtils;
|
||||||
@ -51,7 +54,8 @@ import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
|
|||||||
public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyListener,
|
public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyListener,
|
||||||
EditAlarmContract.View, // TODO: Remove @Override from the methods
|
EditAlarmContract.View, // TODO: Remove @Override from the methods
|
||||||
AlarmUtilsHelper,
|
AlarmUtilsHelper,
|
||||||
SharedPreferencesHelper {
|
SharedPreferencesHelper,
|
||||||
|
LoaderManager.LoaderCallbacks<Alarm> {
|
||||||
private static final String TAG = "EditAlarmActivity";
|
private static final String TAG = "EditAlarmActivity";
|
||||||
public static final String EXTRA_ALARM_ID = "com.philliphsu.clock2.editalarm.extra.ALARM_ID";
|
public static final String EXTRA_ALARM_ID = "com.philliphsu.clock2.editalarm.extra.ALARM_ID";
|
||||||
private static final RelativeSizeSpan AMPM_SIZE_SPAN = new RelativeSizeSpan(0.5f);
|
private static final RelativeSizeSpan AMPM_SIZE_SPAN = new RelativeSizeSpan(0.5f);
|
||||||
@ -59,6 +63,7 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
private static final int REQUEST_PICK_RINGTONE = 0;
|
private static final int REQUEST_PICK_RINGTONE = 0;
|
||||||
private static final int ID_MENU_ITEM = 0;
|
private static final int ID_MENU_ITEM = 0;
|
||||||
|
|
||||||
|
private long mOldAlarmId;
|
||||||
private Uri mSelectedRingtoneUri;
|
private Uri mSelectedRingtoneUri;
|
||||||
private Alarm mOldAlarm;
|
private Alarm mOldAlarm;
|
||||||
private DatabaseManager mDatabaseManager;
|
private DatabaseManager mDatabaseManager;
|
||||||
@ -80,7 +85,17 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
setWeekDaysText();
|
setWeekDaysText();
|
||||||
mNumpad.setKeyListener(this);
|
mNumpad.setKeyListener(this);
|
||||||
mDatabaseManager = DatabaseManager.getInstance(this); // MUST be before loading alarm
|
mDatabaseManager = DatabaseManager.getInstance(this); // MUST be before loading alarm
|
||||||
loadAlarm(getIntent().getLongExtra(EXTRA_ALARM_ID, -1));
|
mOldAlarmId = getIntent().getLongExtra(EXTRA_ALARM_ID, -1);
|
||||||
|
if (mOldAlarmId != -1) {
|
||||||
|
// getLoaderManager() for support fragments by default returns the
|
||||||
|
// support version of LoaderManager. However, since this is an Activity,
|
||||||
|
// we have both the native getLoaderManager() and getSupportLoaderManager().
|
||||||
|
// Use the latter to remain consistent with the rest of our current code base.
|
||||||
|
getSupportLoaderManager().initLoader(0, null, this);
|
||||||
|
} else {
|
||||||
|
// Nothing to load, so show default values
|
||||||
|
showDetails();
|
||||||
|
}
|
||||||
setTimeTextHint(); // TODO: private access
|
setTimeTextHint(); // TODO: private access
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -503,11 +518,22 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
return AlarmUtils.readPreference(this, key, defaultValue);
|
return AlarmUtils.readPreference(this, key, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadAlarm(long alarmId) {
|
@Override
|
||||||
mOldAlarm = alarmId > -1 ? mDatabaseManager.getAlarm(alarmId) : null;
|
public Loader<Alarm> onCreateLoader(int id, Bundle args) {
|
||||||
|
return new AlarmLoader(this, mOldAlarmId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoadFinished(Loader<Alarm> loader, Alarm data) {
|
||||||
|
mOldAlarm = data;
|
||||||
showDetails();
|
showDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoaderReset(Loader<Alarm> loader) {
|
||||||
|
// nothing to reset
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Privatize access of each method called here.
|
// TODO: Privatize access of each method called here.
|
||||||
private void showDetails() {
|
private void showDetails() {
|
||||||
if (mOldAlarm != null) {
|
if (mOldAlarm != null) {
|
||||||
|
|||||||
@ -0,0 +1,23 @@
|
|||||||
|
package com.philliphsu.clock2.model;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.philliphsu.clock2.Alarm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Phillip Hsu on 6/30/2016.
|
||||||
|
*/
|
||||||
|
public class AlarmLoader extends DataLoader<Alarm> {
|
||||||
|
|
||||||
|
private long mAlarmId;
|
||||||
|
|
||||||
|
public AlarmLoader(Context context, long alarmId) {
|
||||||
|
super(context);
|
||||||
|
mAlarmId = alarmId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Alarm loadInBackground() {
|
||||||
|
return DatabaseManager.getInstance(getContext()).getAlarm(mAlarmId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package com.philliphsu.clock2.model;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.v4.content.AsyncTaskLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Phillip Hsu on 6/30/2016.
|
||||||
|
*/
|
||||||
|
public abstract class DataLoader<D> extends AsyncTaskLoader<D> {
|
||||||
|
|
||||||
|
private D mData;
|
||||||
|
|
||||||
|
public DataLoader(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStartLoading() {
|
||||||
|
if (mData != null) {
|
||||||
|
deliverResult(mData);
|
||||||
|
} else {
|
||||||
|
forceLoad();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deliverResult(D data) {
|
||||||
|
mData = data;
|
||||||
|
if (isStarted()) {
|
||||||
|
super.deliverResult(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -21,7 +21,6 @@ import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
|
|||||||
import static android.app.PendingIntent.FLAG_NO_CREATE;
|
import static android.app.PendingIntent.FLAG_NO_CREATE;
|
||||||
import static android.app.PendingIntent.getActivity;
|
import static android.app.PendingIntent.getActivity;
|
||||||
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 java.util.concurrent.TimeUnit.HOURS;
|
import static java.util.concurrent.TimeUnit.HOURS;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,12 +74,16 @@ public final class AlarmUtils {
|
|||||||
AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
|
AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
|
||||||
PendingIntent pi = alarmIntent(c, a, true);
|
PendingIntent pi = alarmIntent(c, a, true);
|
||||||
am.cancel(pi);
|
if (pi != null) {
|
||||||
pi.cancel();
|
am.cancel(pi);
|
||||||
|
pi.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
pi = notifyUpcomingAlarmIntent(c, a, true);
|
pi = notifyUpcomingAlarmIntent(c, a, true);
|
||||||
am.cancel(pi);
|
if (pi != null) {
|
||||||
pi.cancel();
|
am.cancel(pi);
|
||||||
|
pi.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
removeUpcomingAlarmNotification(c, a);
|
removeUpcomingAlarmNotification(c, a);
|
||||||
|
|
||||||
@ -159,9 +162,13 @@ public final class AlarmUtils {
|
|||||||
.putExtra(RingtoneActivity.EXTRA_ITEM_ID, alarm.id());
|
.putExtra(RingtoneActivity.EXTRA_ITEM_ID, alarm.id());
|
||||||
int flag = retrievePrevious ? FLAG_NO_CREATE : FLAG_CANCEL_CURRENT;
|
int flag = retrievePrevious ? FLAG_NO_CREATE : FLAG_CANCEL_CURRENT;
|
||||||
PendingIntent pi = getActivity(context, alarm.intId(), intent, flag);
|
PendingIntent pi = getActivity(context, alarm.intId(), intent, flag);
|
||||||
|
// Even when we try to retrieve a previous instance that actually did exist,
|
||||||
|
// null can be returned for some reason.
|
||||||
|
/*
|
||||||
if (retrievePrevious) {
|
if (retrievePrevious) {
|
||||||
checkNotNull(pi);
|
checkNotNull(pi);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
return pi;
|
return pi;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,9 +183,13 @@ public final class AlarmUtils {
|
|||||||
}
|
}
|
||||||
int flag = retrievePrevious ? FLAG_NO_CREATE : FLAG_CANCEL_CURRENT;
|
int flag = retrievePrevious ? FLAG_NO_CREATE : FLAG_CANCEL_CURRENT;
|
||||||
PendingIntent pi = PendingIntent.getBroadcast(context, alarm.intId(), intent, flag);
|
PendingIntent pi = PendingIntent.getBroadcast(context, alarm.intId(), intent, flag);
|
||||||
|
// Even when we try to retrieve a previous instance that actually did exist,
|
||||||
|
// null can be returned for some reason.
|
||||||
|
/*
|
||||||
if (retrievePrevious) {
|
if (retrievePrevious) {
|
||||||
checkNotNull(pi);
|
checkNotNull(pi);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
return pi;
|
return pi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user