Skip to content

Commit

Permalink
Perform a Quick Sync during setup
Browse files Browse the repository at this point in the history
Pull down only 1 hour of EPG, then restart the service to allow the HTSP
connection to be re-established with a longer EPG max time.

This delays the logo download until the end of the second sync, but that's
OK in my opinion.
  • Loading branch information
kiall committed Jan 20, 2017
1 parent 53e713c commit d8f35ac
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
8 changes: 8 additions & 0 deletions app/src/debug/java/ie/macinnes/tvheadend/DevTestActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public void accountInfo(View view) {

public void deleteChannels(View view) {
setRunning();

Context context = getBaseContext();
Intent i = new Intent(context, EpgSyncService.class);
context.stopService(i);

i = new Intent(context, TvInputService.class);
context.stopService(i);

TvContractUtils.removeChannels(getBaseContext());
setOk();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@ public void run() {
GuidedStepFragment fragment = new CompletedFragment();
fragment.setArguments(getArguments());
add(getFragmentManager(), fragment);

// Re-Start EPG sync service (removing quick-sync)
Context context = getActivity().getBaseContext();

Intent intent = new Intent(context, EpgSyncService.class);
context.stopService(intent);
context.startService(intent);
}
};

Expand All @@ -360,10 +367,14 @@ public void onDestroy() {
public void onStart() {
super.onStart();

// Start EPG sync service
// Re-Start EPG sync service
Context context = getActivity().getBaseContext();
context.stopService(new Intent(context, EpgSyncService.class));
context.startService(new Intent(context, EpgSyncService.class));

Intent intent = new Intent(context, EpgSyncService.class);
intent.putExtra(EpgSyncService.SYNC_QUICK, true);

context.stopService(intent);
context.startService(intent);
}

@Override
Expand Down Expand Up @@ -414,7 +425,7 @@ public static class CompletedFragment extends BaseGuidedStepFragment {
public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
GuidanceStylist.Guidance guidance = new GuidanceStylist.Guidance(
"Setup Complete",
"Enjoy!",
"More EPG data, channel logs, etc are downloading in the background",
"TVHeadend",
null);

Expand Down
19 changes: 18 additions & 1 deletion app/src/main/java/ie/macinnes/tvheadend/sync/EpgSyncService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
public class EpgSyncService extends Service {
private static final String TAG = EpgSyncService.class.getName();

public static final String SYNC_QUICK = "sync-quick";

protected HandlerThread mHandlerThread;
protected Handler mHandler;

Expand All @@ -55,6 +57,7 @@ public class EpgSyncService extends Service {
protected GetFileTask mGetFileTask;

protected static List<Runnable> sInitialSyncCompleteCallbacks = new ArrayList<>();
protected boolean mQuickSyncRequested = false;

protected final Runnable mInitialSyncCompleteCallback = new Runnable() {
@Override
Expand Down Expand Up @@ -115,6 +118,9 @@ public void onCreate() {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
mQuickSyncRequested = intent.getBooleanExtra(SYNC_QUICK, false);
}
if (mAccount == null) {
return START_NOT_STICKY;
}
Expand Down Expand Up @@ -173,7 +179,13 @@ public void onStateChange(int state, int previous) {
} else if (state == Connection.STATE_READY) {
Log.d(TAG, "HTSP Connection Ready");
installTasks();
enableAsyncMetadata();
if (mQuickSyncRequested) {
// Sync 1 hour of EPG
enableAsyncMetadata(3600);
mQuickSyncRequested = false;
} else {
enableAsyncMetadata();
}
} else if (state == Connection.STATE_FAILED) {
Log.e(TAG, "HTSP Connection Failed, Reconnecting");
closeConnection();
Expand Down Expand Up @@ -207,6 +219,11 @@ protected void enableAsyncMetadata() {
mEpgSyncTask.enableAsyncMetadata(mInitialSyncCompleteCallback);
}

protected void enableAsyncMetadata(long epgMaxTime) {
Log.d(TAG, "Enabling Async Metadata");
mEpgSyncTask.enableAsyncMetadata(mInitialSyncCompleteCallback, epgMaxTime);
}

protected void closeConnection() {
if (mConnection != null) {
Log.d(TAG, "Closing HTSP connection");
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/ie/macinnes/tvheadend/sync/EpgSyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,20 @@ public EpgSyncTask(Context context, Handler handler, Account account, GetFileTas
Constants.PREFERENCE_TVHEADEND, Context.MODE_PRIVATE);
}


public void enableAsyncMetadata(Runnable initialSyncCompleteCallback) {
long epgMaxTime = Long.parseLong(mSharedPreferences.getString(Constants.KEY_EPG_MAX_TIME, "3600"));

enableAsyncMetadata(initialSyncCompleteCallback, epgMaxTime);
}

public void enableAsyncMetadata(Runnable initialSyncCompleteCallback, long epgMaxTime) {
long lastUpdate = mSharedPreferences.getLong(Constants.KEY_EPG_LAST_UPDATE, 0);
long epgMaxTime = mSharedPreferences.getLong(Constants.KEY_EPG_MAX_TIME, 3600);
epgMaxTime = epgMaxTime + (System.currentTimeMillis() / 1000L);

Log.i(TAG, "Enabling Async Metadata. Last Update: " + lastUpdate + ", EPG max time: " + epgMaxTime);

epgMaxTime = epgMaxTime + (System.currentTimeMillis() / 1000L);

mInitialSyncCompleteCallback = initialSyncCompleteCallback;

EnableAsyncMetadataRequest enableAsyncMetadataRequest = new EnableAsyncMetadataRequest();
Expand Down

0 comments on commit d8f35ac

Please sign in to comment.