Skip to content

Commit

Permalink
Use IntentService to update latest episode values.
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Apr 17, 2014
1 parent 160cd42 commit d401026
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 66 deletions.
2 changes: 2 additions & 0 deletions SeriesGuide/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@
<!-- Services -->
<service android:name="com.battlelancer.seriesguide.service.TraktFlagService" >
</service>
<service android:name="com.battlelancer.seriesguide.util.LatestEpisodeUpdateService">
</service>

<!-- Notification service -->
<service android:name="com.battlelancer.seriesguide.service.NotificationService" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.battlelancer.seriesguide.ui.FirstRunFragment.OnFirstRunDismissedListener;
import com.battlelancer.seriesguide.ui.dialogs.AddDialogFragment;
import com.battlelancer.seriesguide.util.ImageProvider;
import com.battlelancer.seriesguide.util.LatestEpisodeUpdateService;
import com.battlelancer.seriesguide.util.TaskManager;
import com.battlelancer.seriesguide.util.Utils;
import com.battlelancer.thetvdbapi.TheTVDB;
Expand Down Expand Up @@ -252,7 +253,9 @@ protected void onNewIntent(Intent intent) {
@Override
protected void onResume() {
super.onResume();
Utils.updateLatestEpisodes(this);

startService(new Intent(this, LatestEpisodeUpdateService.class));

if (mSavedState != null) {
restoreLocalState(mSavedState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.battlelancer.seriesguide.util.DBUtils;
import com.battlelancer.seriesguide.util.FlagTask.FlagTaskCompletedEvent;
import com.battlelancer.seriesguide.util.ImageProvider;
import com.battlelancer.seriesguide.util.LatestEpisodeUpdateService;
import com.battlelancer.seriesguide.util.ShowTools;
import com.battlelancer.seriesguide.util.TimeTools;
import com.battlelancer.seriesguide.util.Utils;
Expand Down Expand Up @@ -721,7 +722,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin

public void onEvent(FlagTaskCompletedEvent event) {
if (isAdded()) {
Utils.updateLatestEpisode(getActivity(), event.mType.getShowTvdbId());
getActivity().startService(new Intent(getActivity(), LatestEpisodeUpdateService.class)
.putExtra(LatestEpisodeUpdateService.InitBundle.SHOW_TVDB_ID,
event.mType.getShowTvdbId()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2014 Uwe Trottmann
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.battlelancer.seriesguide.util;

import android.app.IntentService;
import android.content.Intent;
import android.database.Cursor;
import com.battlelancer.seriesguide.provider.SeriesGuideContract;
import com.battlelancer.seriesguide.settings.DisplaySettings;
import java.util.HashSet;

/**
* Updates the latest episode value for a given show or all shows.
*/
public class LatestEpisodeUpdateService extends IntentService {

public interface InitBundle {
/** TVDb id of the show to update or 0 to update all shows. */
String SHOW_TVDB_ID = "show_tvdbid";
}

public LatestEpisodeUpdateService() {
super("LatestEpisodeUpdateService");
}

@Override
protected void onHandleIntent(Intent intent) {
int showTvdbId = intent.getIntExtra(InitBundle.SHOW_TVDB_ID, 0);

boolean isNoReleasedEpisodes = DisplaySettings.isNoReleasedEpisodes(
getApplicationContext());
boolean isNoSpecials = DisplaySettings.isHidingSpecials(getApplicationContext());

if (showTvdbId > 0) {
// update single show
DBUtils.updateLatestEpisode(getApplicationContext(), showTvdbId, isNoReleasedEpisodes,
isNoSpecials);
} else {
// update all shows
HashSet<Integer> shows = ShowTools.getShowTvdbIdsAsSet(getApplicationContext());
for (int tvdbId : shows) {
DBUtils.updateLatestEpisode(getApplicationContext(), tvdbId,
isNoReleasedEpisodes, isNoSpecials);
}
}

// Show adapter gets notified by ContentProvider
// Lists adapter needs to be notified manually
getContentResolver().notifyChange(SeriesGuideContract.ListItems.CONTENT_WITH_DETAILS_URI,
null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,70 +122,6 @@ public static String splitAndKitTVDBStrings(String tvdbstring) {
return tvdbstring;
}

/**
* Update the latest episode fields for all existing shows.
*/
public static void updateLatestEpisodes(Context context) {
Thread t = new UpdateLatestEpisodeThread(context);
t.start();
}

/**
* Update the latest episode field for a specific show.
*/
public static void updateLatestEpisode(Context context, int showTvdbId) {
Thread t = new UpdateLatestEpisodeThread(context, showTvdbId);
t.start();
}

public static class UpdateLatestEpisodeThread extends Thread {

private Context mContext;

private int mShowTvdbId;

public UpdateLatestEpisodeThread(Context context) {
mContext = context;
this.setName("UpdateLatestEpisode");
}

public UpdateLatestEpisodeThread(Context context, int showTvdbId) {
this(context);
mShowTvdbId = showTvdbId;
}

public void run() {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
final boolean isNoReleasedEpisodes = DisplaySettings.isNoReleasedEpisodes(mContext);
final boolean isNoSpecials = DisplaySettings.isHidingSpecials(mContext);

if (mShowTvdbId > 0) {
// update single show
DBUtils.updateLatestEpisode(mContext, mShowTvdbId, isNoReleasedEpisodes,
isNoSpecials);
} else {
// update all shows
final Cursor shows = mContext.getContentResolver().query(Shows.CONTENT_URI,
new String[] {
Shows._ID
}, null, null, null
);
if (shows != null) {
while (shows.moveToNext()) {
int showTvdbId = shows.getInt(0);
DBUtils.updateLatestEpisode(mContext, showTvdbId, isNoReleasedEpisodes,
isNoSpecials);
}
shows.close();
}
}

// Show adapter gets notified by ContentProvider
// Lists adapter needs to be notified manually
mContext.getContentResolver().notifyChange(ListItems.CONTENT_WITH_DETAILS_URI, null);
}
}

public static String getVersion(Context context) {
String version;
try {
Expand Down

0 comments on commit d401026

Please sign in to comment.