Refactor sync service task types to use enums.

This commit is contained in:
ojiikun 2013-07-19 10:14:59 +00:00
parent b9d9376212
commit 77cf965fa9
14 changed files with 35 additions and 33 deletions

View file

@ -66,7 +66,7 @@ public class AllSharedStoriesItemsList extends ItemsList {
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_MULTISOCIALFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.MULTISOCIALFEED_UPDATE);
String[] feeds = new String[feedIds.size()];
feedIds.toArray(feeds);

View file

@ -79,7 +79,7 @@ public class AllSharedStoriesReading extends Reading {
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_MULTISOCIALFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.MULTISOCIALFEED_UPDATE);
String[] feeds = new String[feedIds.size()];
feedIds.toArray(feeds);

View file

@ -69,7 +69,7 @@ public class AllStoriesItemsList extends ItemsList implements MarkAllReadDialogL
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_MULTIFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.MULTIFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_MULTIFEED_IDS, new String[0]); // the API will return all feeds if no IDs are passed
intent.putExtra(SyncService.EXTRA_TASK_PAGE_NUMBER, Integer.toString(page));
intent.putExtra(SyncService.EXTRA_TASK_ORDER, getStoryOrder());

View file

@ -76,7 +76,7 @@ public class AllStoriesReading extends Reading {
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_MULTIFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.MULTIFEED_UPDATE);
String[] feeds = new String[feedIds.size()];
feedIds.toArray(feeds);

View file

@ -129,7 +129,7 @@ public class FeedItemsList extends ItemsList {
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_FEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.FEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_PAGE_NUMBER, Integer.toString(page));
intent.putExtra(SyncService.EXTRA_TASK_FEED_ID, feedId);
intent.putExtra(SyncService.EXTRA_TASK_ORDER, getStoryOrder());

View file

@ -96,7 +96,7 @@ public class FeedReading extends Reading {
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_FEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.FEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_FEED_ID, feedId);
if (page > 1) {
intent.putExtra(SyncService.EXTRA_TASK_PAGE_NUMBER, Integer.toString(page));

View file

@ -87,7 +87,7 @@ public class FolderItemsList extends ItemsList implements MarkAllReadDialogListe
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_MULTIFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.MULTIFEED_UPDATE);
String[] feeds = new String[feedIds.size()];
feedIds.toArray(feeds);
intent.putExtra(SyncService.EXTRA_TASK_MULTIFEED_IDS, feeds);

View file

@ -54,7 +54,7 @@ public class FolderReading extends Reading {
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_MULTIFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.MULTIFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_MULTIFEED_IDS, feedIds);
if (page > 1) {

View file

@ -77,7 +77,7 @@ public class Main extends NbFragmentActivity implements StateChangedListener, Sy
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_FOLDER_UPDATE_TWO_STEP);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.FOLDER_UPDATE_TWO_STEP);
startService(intent);
}
@ -93,7 +93,7 @@ public class Main extends NbFragmentActivity implements StateChangedListener, Sy
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_FOLDER_UPDATE_WITH_COUNT);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.FOLDER_UPDATE_WITH_COUNT);
startService(intent);
}

View file

@ -69,7 +69,7 @@ public class SavedStoriesItemsList extends ItemsList {
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_STARRED_STORIES_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.STARRED_STORIES_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_PAGE_NUMBER, Integer.toString(page));
startService(intent);
}

View file

@ -56,7 +56,7 @@ public class SavedStoriesReading extends Reading {
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_STARRED_STORIES_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.STARRED_STORIES_UPDATE);
if (page > 1) {
intent.putExtra(SyncService.EXTRA_TASK_PAGE_NUMBER, Integer.toString(page));
}

View file

@ -72,7 +72,7 @@ public class SocialFeedItemsList extends ItemsList {
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_SOCIALFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.SOCIALFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_SOCIALFEED_ID, userId);
intent.putExtra(SyncService.EXTRA_TASK_PAGE_NUMBER, Integer.toString(page));
intent.putExtra(SyncService.EXTRA_TASK_SOCIALFEED_USERNAME, username);

View file

@ -64,7 +64,7 @@ public class SocialFeedReading extends Reading {
setSupportProgressBarIndeterminateVisibility(true);
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, SyncService.class);
intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, syncFragment.receiver);
intent.putExtra(SyncService.SYNCSERVICE_TASK, SyncService.EXTRA_TASK_SOCIALFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_TYPE, SyncService.TaskType.SOCIALFEED_UPDATE);
intent.putExtra(SyncService.EXTRA_TASK_SOCIALFEED_ID, userId);
if (page > 1) {
intent.putExtra(SyncService.EXTRA_TASK_PAGE_NUMBER, Integer.toString(page));

View file

@ -32,6 +32,7 @@ import com.newsblur.util.StoryOrder;
*/
public class SyncService extends IntentService {
public static final String EXTRA_TASK_TYPE = "syncServiceTaskType";
public static final String EXTRA_STATUS_RECEIVER = "resultReceiverExtra";
public static final String EXTRA_TASK_FEED_ID = "taskFeedId";
public static final String EXTRA_TASK_FOLDER_NAME = "taskFoldername";
@ -54,18 +55,18 @@ public class SyncService extends IntentService {
STATUS_PARTIAL_PROGRESS,
};
public static final int EXTRA_TASK_FOLDER_UPDATE_TWO_STEP = 30;
public static final int EXTRA_TASK_FOLDER_UPDATE_WITH_COUNT = 41;
public static final int EXTRA_TASK_FEED_UPDATE = 31;
public static final int EXTRA_TASK_SOCIALFEED_UPDATE = 34;
public static final int EXTRA_TASK_MULTIFEED_UPDATE = 36;
public static final int EXTRA_TASK_MULTISOCIALFEED_UPDATE = 40;
public static final int EXTRA_TASK_STARRED_STORIES_UPDATE = 42;
public enum TaskType {
FOLDER_UPDATE_TWO_STEP,
FOLDER_UPDATE_WITH_COUNT,
FEED_UPDATE,
SOCIALFEED_UPDATE,
MULTIFEED_UPDATE,
MULTISOCIALFEED_UPDATE,
STARRED_STORIES_UPDATE
};
private APIManager apiManager;
private ContentResolver contentResolver;
public static final String SYNCSERVICE_TASK = "syncservice_task";
public SyncService() {
super(SyncService.class.getName());
@ -82,18 +83,19 @@ public class SyncService extends IntentService {
protected void onHandleIntent(Intent intent) {
final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER);
try {
TaskType taskType = (TaskType) intent.getSerializableExtra(EXTRA_TASK_TYPE);
Log.d( this.getClass().getName(), "Sync Intent: " + taskType );
if (receiver != null) {
receiver.send(SyncStatus.STATUS_RUNNING.ordinal(), Bundle.EMPTY);
}
Log.d( this.getClass().getName(), "Sync Intent: " + intent.getIntExtra(SYNCSERVICE_TASK , -1) );
// an extra result code to callback before the final STATUS_FINISHED that is always sent
SyncStatus resultStatus = null;
switch (intent.getIntExtra(SYNCSERVICE_TASK , -1)) {
switch (taskType) {
case EXTRA_TASK_FOLDER_UPDATE_TWO_STEP:
case FOLDER_UPDATE_TWO_STEP:
// do a quick fetch of folders/feeds
apiManager.getFolderFeedMapping(false);
// notify UI of progress
@ -105,11 +107,11 @@ public class SyncService extends IntentService {
// UI will be notified again by default
break;
case EXTRA_TASK_FOLDER_UPDATE_WITH_COUNT:
case FOLDER_UPDATE_WITH_COUNT:
apiManager.getFolderFeedMapping(true);
break;
case EXTRA_TASK_FEED_UPDATE:
case FEED_UPDATE:
if (!TextUtils.isEmpty(intent.getStringExtra(EXTRA_TASK_FEED_ID))) {
StoriesResponse storiesForFeed = apiManager.getStoriesForFeed(intent.getStringExtra(EXTRA_TASK_FEED_ID), intent.getStringExtra(EXTRA_TASK_PAGE_NUMBER), (StoryOrder) intent.getSerializableExtra(EXTRA_TASK_ORDER), (ReadFilter) intent.getSerializableExtra(EXTRA_TASK_READ_FILTER));
if (storiesForFeed == null || storiesForFeed.stories.length == 0) {
@ -120,7 +122,7 @@ public class SyncService extends IntentService {
}
break;
case EXTRA_TASK_MULTIFEED_UPDATE:
case MULTIFEED_UPDATE:
if (intent.getStringArrayExtra(EXTRA_TASK_MULTIFEED_IDS) != null) {
StoriesResponse storiesForFeeds = apiManager.getStoriesForFeeds(intent.getStringArrayExtra(EXTRA_TASK_MULTIFEED_IDS), intent.getStringExtra(EXTRA_TASK_PAGE_NUMBER), (StoryOrder) intent.getSerializableExtra(EXTRA_TASK_ORDER), (ReadFilter) intent.getSerializableExtra(EXTRA_TASK_READ_FILTER));
if (storiesForFeeds == null || storiesForFeeds.stories.length == 0) {
@ -131,7 +133,7 @@ public class SyncService extends IntentService {
}
break;
case EXTRA_TASK_MULTISOCIALFEED_UPDATE:
case MULTISOCIALFEED_UPDATE:
if (intent.getStringArrayExtra(EXTRA_TASK_MULTIFEED_IDS) != null) {
SocialFeedResponse sharedStoriesForFeeds = apiManager.getSharedStoriesForFeeds(intent.getStringArrayExtra(EXTRA_TASK_MULTIFEED_IDS), intent.getStringExtra(EXTRA_TASK_PAGE_NUMBER), (StoryOrder) intent.getSerializableExtra(EXTRA_TASK_ORDER), (ReadFilter) intent.getSerializableExtra(EXTRA_TASK_READ_FILTER));
if (sharedStoriesForFeeds == null || sharedStoriesForFeeds.stories.length == 0) {
@ -142,14 +144,14 @@ public class SyncService extends IntentService {
}
break;
case EXTRA_TASK_STARRED_STORIES_UPDATE:
case STARRED_STORIES_UPDATE:
StoriesResponse starredStories = apiManager.getStarredStories(intent.getStringExtra(EXTRA_TASK_PAGE_NUMBER));
if (starredStories == null && starredStories.stories.length == 0) {
resultStatus = SyncStatus.STATUS_NO_MORE_UPDATES;
}
break;
case EXTRA_TASK_SOCIALFEED_UPDATE:
case SOCIALFEED_UPDATE:
if (!TextUtils.isEmpty(intent.getStringExtra(EXTRA_TASK_SOCIALFEED_ID)) && !TextUtils.isEmpty(intent.getStringExtra(EXTRA_TASK_SOCIALFEED_USERNAME))) {
SocialFeedResponse storiesForSocialFeed = apiManager.getStoriesForSocialFeed(intent.getStringExtra(EXTRA_TASK_SOCIALFEED_ID), intent.getStringExtra(EXTRA_TASK_SOCIALFEED_USERNAME), intent.getStringExtra(EXTRA_TASK_PAGE_NUMBER), (StoryOrder) intent.getSerializableExtra(EXTRA_TASK_ORDER), (ReadFilter) intent.getSerializableExtra(EXTRA_TASK_READ_FILTER));
if (storiesForSocialFeed == null || storiesForSocialFeed.stories.length == 0) {