Skip to content

Commit

Permalink
refactored program/group/upcoming download and storage locations
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Frey committed Oct 14, 2012
1 parent 9572bd9 commit 7134dd4
Show file tree
Hide file tree
Showing 29 changed files with 432 additions and 1,317 deletions.
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.mythtv"
android:versionCode="1"
android:versionCode="2"
android:versionName="1.0.2" >

<uses-permission android:name="android.permission.INTERNET" />
Expand Down
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<jdmns-version>3.4.1</jdmns-version>
<joda-time-version>2.1</joda-time-version>
<org.joda.joda-convert-version>1.2</org.joda.joda-convert-version>
<commons-io-version>1.3.2</commons-io-version>
<mythtv.service.api-version>0.0.15</mythtv.service.api-version>
</properties>

Expand Down Expand Up @@ -69,6 +70,13 @@
<version>${org.joda.joda-convert-version}</version>
</dependency>

<!-- Apache Commons IO -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io-version}</version>
</dependency>

<!-- MythTV Services Api -->
<dependency>
<groupId>org.mythtv.android</groupId>
Expand Down
46 changes: 20 additions & 26 deletions src/org/mythtv/client/ui/AbstractLocationAwareFragmentActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,40 +246,35 @@ private void startServices() {
startService( new Intent( ProgramGuideCleanupService.ACTION_CLEANUP ) );
}

File programCache = mFileHelper.getProgramDataDirectory();
if( null != programCache && programCache.exists() ) {

File upcoming = new File( programCache, UpcomingDownloadService.UPCOMING_FILE );
if( upcoming.exists() ) {
File upcoming = new File( mFileHelper.getProgramUpcomingDataDirectory(), UpcomingDownloadService.UPCOMING_FILE );
if( upcoming.exists() ) {

DateTime today = new DateTime().withTime( 0, 0, 0, 0 );
DateTime lastModified = new DateTime( upcoming.lastModified() );
DateTime today = new DateTime().withTime( 0, 0, 0, 0 );
DateTime lastModified = new DateTime( upcoming.lastModified() );

if( lastModified.isBefore( today ) ) {
startService( new Intent( UpcomingDownloadService.ACTION_DOWNLOAD ) );
startService( new Intent( BannerCleanupService.ACTION_CLEANUP ) );
} else {
Log.i( TAG, "onResume : not time to update 'upcoming' episodes" );
}
} else {
if( lastModified.isBefore( today ) ) {
startService( new Intent( UpcomingDownloadService.ACTION_DOWNLOAD ) );
startService( new Intent( BannerCleanupService.ACTION_CLEANUP ) );
} else {
Log.i( TAG, "onResume : not time to update 'upcoming' episodes" );
}
} else {
startService( new Intent( UpcomingDownloadService.ACTION_DOWNLOAD ) );
}

File recorded = new File( programCache, RecordedDownloadService.RECORDED_FILE );
if( recorded.exists() ) {
File recorded = new File( mFileHelper.getProgramRecordedDataDirectory(), RecordedDownloadService.RECORDED_FILE );
if( recorded.exists() ) {

DateTime lastHour = new DateTime().minusHours( 1 );
DateTime lastModified = new DateTime( recorded.lastModified() );
DateTime lastHour = new DateTime().minusHours( 1 );
DateTime lastModified = new DateTime( recorded.lastModified() );

if( lastModified.isBefore( lastHour ) ) {
startService( new Intent( RecordedCleanupService.ACTION_CLEANUP ) );
} else {
Log.i( TAG, "onResume : not time to update 'recorded' episodes" );
}
if( lastModified.isBefore( lastHour ) ) {
startService( new Intent( RecordedCleanupService.ACTION_CLEANUP ) );
} else {
startService( new Intent( RecordedDownloadService.ACTION_DOWNLOAD ) );
Log.i( TAG, "onResume : not time to update 'recorded' episodes" );
}

} else {
startService( new Intent( RecordedDownloadService.ACTION_DOWNLOAD ) );
}

Log.v( TAG, "startServices : exit" );
Expand Down Expand Up @@ -338,7 +333,6 @@ public void onReceive( Context context, Intent intent ) {

if ( intent.getAction().equals( RecordedCleanupService.ACTION_COMPLETE ) ) {
Log.i( TAG, "RecordedCleanupReceiver.onReceive : " + intent.getStringExtra( RecordedCleanupService.EXTRA_COMPLETE ) );
Log.i( TAG, "RecordedCleanupReceiver.onReceive : " + intent.getIntExtra( RecordedCleanupService.EXTRA_COMPLETE_COUNT, 0 ) + " files cleaned up" );

startService( new Intent( RecordedDownloadService.ACTION_DOWNLOAD ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/mythtv/client/ui/LocationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void onResume() {
away.setEnabled( false );
away.setVisibility( View.VISIBLE );

startService( new Intent( UpgradeCleanupService.ACTION_PROGRAM_GUIDE_CLEANUP ) );
startService( new Intent( UpgradeCleanupService.ACTION_PROGRAMS_CLEANUP ) );

Log.d( TAG, "onResume : exit" );
}
Expand Down
5 changes: 5 additions & 0 deletions src/org/mythtv/client/ui/dvr/AbstractDvrActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.mythtv.client.ui.AbstractMythtvFragmentActivity;
import org.mythtv.client.ui.AwayActivity;
import org.mythtv.client.ui.HomeActivity;
import org.mythtv.service.util.FileHelper;

import android.content.Intent;
import android.os.Bundle;
Expand All @@ -36,6 +37,8 @@ public abstract class AbstractDvrActivity extends AbstractMythtvFragmentActivity

protected static final String TAG = AbstractDvrActivity.class.getSimpleName();

protected FileHelper mFileHelper = null;

/* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
Expand All @@ -45,6 +48,8 @@ protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );

setupActionBar();

mFileHelper = new FileHelper( this );

Log.v( TAG, "onCreate : exit" );
}
Expand Down
48 changes: 38 additions & 10 deletions src/org/mythtv/client/ui/dvr/ProgramGroupActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@
*/
package org.mythtv.client.ui.dvr;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.mythtv.R;
import org.mythtv.service.dvr.cache.ProgramGroupLruMemoryCache;
import org.mythtv.service.dvr.ProgramGroupRecordedDownloadService;
import org.mythtv.service.util.UrlUtils;
import org.mythtv.services.api.dvr.Programs;

Expand All @@ -28,6 +35,9 @@
import android.util.Log;
import android.view.MenuItem;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
* @author Daniel Frey
* @author John Baab
Expand All @@ -41,8 +51,6 @@ public class ProgramGroupActivity extends AbstractDvrActivity {

private ProgramGroupFragment programGroupFragment = null;

private ProgramGroupLruMemoryCache cache;

// ***************************************
// Activity methods
// ***************************************
Expand All @@ -57,15 +65,35 @@ protected void onCreate( Bundle savedInstanceState ) {
Log.v( TAG, "onCreate : enter" );
super.onCreate( savedInstanceState );

cache = new ProgramGroupLruMemoryCache( this );

Bundle extras = getIntent().getExtras();
String name = extras.getString( EXTRA_PROGRAM_GROUP_KEY );
String cleaned = UrlUtils.encodeUrl( name );
String programGroup = extras.getString( EXTRA_PROGRAM_GROUP_KEY );

String encodedTitle = UrlUtils.encodeUrl( programGroup );

File programGroupDirectory = mFileHelper.getProgramGroupDirectory( programGroup );
File programGroupJson = new File( programGroupDirectory, encodedTitle + ProgramGroupRecordedDownloadService.RECORDED_FILE );

Programs programs = null;
InputStream is = null;
try {
is = new BufferedInputStream( new FileInputStream( programGroupJson ), 8192 );
programs = getMainApplication().getObjectMapper().readValue( is, Programs.class );
} catch( FileNotFoundException e ) {
Log.e( TAG, "onProgramGroupSelected : error, json could not be found", e );

programs = RecordingsActivity.getDownloadingPrograms( programGroup );
} catch( JsonParseException e ) {
Log.e( TAG, "onProgramGroupSelected : error, json could not be parsed", e );

programs = RecordingsActivity.getDownloadingPrograms( programGroup );
} catch( JsonMappingException e ) {
Log.e( TAG, "onProgramGroupSelected : error, json could not be mapped", e );
programs = RecordingsActivity.getDownloadingPrograms( programGroup );

} catch( IOException e ) {
Log.e( TAG, "onProgramGroupSelected : error, io exception reading file", e );

Programs programs = cache.get( cleaned );
if( null == programs || ( null == programs.getPrograms() || programs.getPrograms().isEmpty() ) ) {
programs = ProgramGroupLruMemoryCache.getDownloadingPrograms( name );
programs = RecordingsActivity.getDownloadingPrograms( programGroup );
}

Log.v( TAG, "onCreate : programs=" + programs.toString() );
Expand Down
2 changes: 1 addition & 1 deletion src/org/mythtv/client/ui/dvr/ProgramGroupFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void onListItemClick( ListView l, View v, int position, long id ) {
Intent i = new Intent( getActivity(), VideoActivity.class );
i.putExtra( VideoActivity.EXTRA_PROGRAM_CHANNEL_ID, program.getChannelInfo().getChannelId() );
i.putExtra( VideoActivity.EXTRA_PROGRAM_START_TIME, DateUtils.dateTimeFormatter.print( program.getStartTime() ) );
i.putExtra( VideoActivity.EXTRA_PROGRAM_CLEANED_TITLE, UrlUtils.encodeUrl( program.getTitle() ) );
i.putExtra( VideoActivity.EXTRA_PROGRAM_GROUP, program.getTitle() );
startActivity( i );

Log.v( TAG, "onListItemClick : exit" );
Expand Down
82 changes: 64 additions & 18 deletions src/org/mythtv/client/ui/dvr/RecordingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@
*/
package org.mythtv.client.ui.dvr;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.mythtv.R;
import org.mythtv.service.dvr.cache.ProgramGroupLruMemoryCache;
import org.mythtv.service.dvr.ProgramGroupRecordedDownloadService;
import org.mythtv.service.util.UrlUtils;
import org.mythtv.services.api.dvr.Program;
import org.mythtv.services.api.dvr.Programs;

import android.content.Intent;
Expand All @@ -29,6 +39,9 @@
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
* @author Daniel Frey
*
Expand All @@ -37,15 +50,11 @@ public class RecordingsActivity extends AbstractDvrActivity implements Recording

private static final String TAG = RecordingsActivity.class.getSimpleName();

private ProgramGroupLruMemoryCache cache;

@Override
public void onCreate( Bundle savedInstanceState ) {
Log.v( TAG, "onCreate : enter" );
super.onCreate( savedInstanceState );

cache = new ProgramGroupLruMemoryCache( this );

setContentView( R.layout.activity_dvr_recordings );

RecordingsFragment recordingsFragment = (RecordingsFragment) getSupportFragmentManager().findFragmentById( R.id.fragment_dvr_program_groups );
Expand All @@ -58,31 +67,51 @@ public void onProgramGroupSelected( String programGroup ) {
Log.d( TAG, "onProgramGroupSelected : enter" );

if( null != findViewById( R.id.fragment_dvr_program_group ) ) {
Log.v( TAG, "onProgramGroupSelected : adding program group to pane" );
Log.v( TAG, "onProgramGroupSelected : adding program group to pane '" + programGroup + "'" );
FragmentManager manager = getSupportFragmentManager();

String cleaned = UrlUtils.encodeUrl( programGroup );
String encodedTitle = UrlUtils.encodeUrl( programGroup );

File programGroupDirectory = mFileHelper.getProgramGroupDirectory( programGroup );
File programGroupJson = new File( programGroupDirectory, encodedTitle + ProgramGroupRecordedDownloadService.RECORDED_FILE );

Programs programs = cache.get( cleaned );
if( null == programs || null == programs.getPrograms() || programs.getPrograms().isEmpty() ) {
programs = ProgramGroupLruMemoryCache.getDownloadingPrograms( programGroup );
Programs programs = null;
InputStream is = null;
try {
is = new BufferedInputStream( new FileInputStream( programGroupJson ), 8192 );
programs = getMainApplication().getObjectMapper().readValue( is, Programs.class );
} catch( FileNotFoundException e ) {
Log.e( TAG, "onProgramGroupSelected : error, json could not be found", e );

programs = getDownloadingPrograms( programGroup );
} catch( JsonParseException e ) {
Log.e( TAG, "onProgramGroupSelected : error, json could not be parsed", e );

programs = getDownloadingPrograms( programGroup );
} catch( JsonMappingException e ) {
Log.e( TAG, "onProgramGroupSelected : error, json could not be mapped", e );
programs = getDownloadingPrograms( programGroup );

} catch( IOException e ) {
Log.e( TAG, "onProgramGroupSelected : error, io exception reading file", e );

programs = getDownloadingPrograms( programGroup );
}
Log.d( TAG, "onProgramGroupSelected : programs=" + programs.toString() );


ProgramGroupFragment programGroupFragment = (ProgramGroupFragment) manager.findFragmentById( R.id.fragment_dvr_program_group );
FragmentTransaction transaction = manager.beginTransaction();

if( null == programGroupFragment ) {
Log.v( TAG, "onProgramGroupSelected : creating new programGroupFragment" );
programGroupFragment = new ProgramGroupFragment();

transaction
.add( R.id.fragment_dvr_program_group, programGroupFragment )
.setTransition( FragmentTransaction.TRANSIT_FRAGMENT_OPEN )
.addToBackStack( null )
.commit();
.add( R.id.fragment_dvr_program_group, programGroupFragment )
.setTransition( FragmentTransaction.TRANSIT_FRAGMENT_OPEN )
.addToBackStack( null )
.commit();
}

Log.v( TAG, "onProgramGroupSelected : setting program group to display" );
programGroupFragment.loadPrograms( this, programs );
} else {
Expand All @@ -96,4 +125,21 @@ public void onProgramGroupSelected( String programGroup ) {
Log.d( TAG, "onProgramGroupSelected : exit" );
}

public static Programs getDownloadingPrograms( String title ) {

Programs programs = new Programs();

List<Program> programList = new ArrayList<Program>();
Program program = new Program();
program.setTitle( title + " is currently downloading" );
program.setSubTitle( "Please try again later." );
programList.add( program );
programs.setPrograms( programList );

Log.i( TAG, "getDownloadingPrograms : programs=" + programs.toString() );

return programs;

}

}
Loading

0 comments on commit 7134dd4

Please sign in to comment.