Commits

Daniel Frey  committed 3372ef7

refactord program group display to use content provider for data

  • Participants
  • Parent commits 28bdb4d
  • Branches master

Comments (0)

Files changed (4)

File src/org/mythtv/client/ui/dvr/ProgramGroupActivity.java

 	@Override
 	protected void onCreate( Bundle savedInstanceState ) {
 		Log.v( TAG, "onCreate : enter" );
-
 		super.onCreate( savedInstanceState );
 
+		Bundle extras = getIntent().getExtras(); 
+		String name = extras.getString( EXTRA_PROGRAM_GROUP_KEY );
+		
 		setContentView( R.layout.fragment_dvr_program_group );
 
 		programGroupFragment = (ProgramGroupFragment) getSupportFragmentManager().findFragmentById( R.id.fragment_dvr_program_group );
-		programGroupFragment.setActivity( this );
-		programGroupFragment.loadPrograms();
+		programGroupFragment.loadPrograms( name );
 		
 		Log.v( TAG, "onCreate : exit" );
 	}

File src/org/mythtv/client/ui/dvr/ProgramGroupFragment.java

  */
 package org.mythtv.client.ui.dvr;
 
-import java.util.List;
-
 import org.mythtv.R;
-import org.mythtv.client.MainApplication;
 import org.mythtv.client.ui.util.MythtvListFragment;
-import org.mythtv.services.api.dvr.Program;
+import org.mythtv.db.dvr.ProgramConstants;
 
-import android.app.Activity;
+import android.content.Context;
 import android.content.Intent;
+import android.database.Cursor;
 import android.os.Bundle;
+import android.provider.BaseColumns;
+import android.support.v4.app.LoaderManager;
+import android.support.v4.content.CursorLoader;
+import android.support.v4.content.Loader;
+import android.support.v4.widget.CursorAdapter;
+import android.support.v4.widget.SimpleCursorAdapter;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.BaseAdapter;
 import android.widget.ListView;
 import android.widget.TextView;
 
  * @author John Baab
  * 
  */
-public class ProgramGroupFragment extends MythtvListFragment {
+public class ProgramGroupFragment extends MythtvListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
 
 	private static final String TAG = ProgramGroupFragment.class.getSimpleName();
 
-	private ProgramAdapter adapter = null;
+	private ProgramCursorAdapter adapter;
 	
-	private Activity activity;
+	public ProgramGroupFragment() { }
 	
-	public ProgramGroupFragment() {
-		this( null );
+	/* (non-Javadoc)
+	 * @see android.support.v4.app.LoaderManager.LoaderCallbacks#onCreateLoader(int, android.os.Bundle)
+	 */
+	@Override
+	public Loader<Cursor> onCreateLoader( int id, Bundle args ) {
+		Log.v( TAG, "onCreateLoader : enter" );
+		
+		String name = args.getString( ProgramGroupActivity.EXTRA_PROGRAM_GROUP_KEY );
+		Log.v( TAG, "onCreateLoader : name=" + name );
+		
+		String[] projection = { BaseColumns._ID, ProgramConstants.FIELD_TITLE, ProgramConstants.FIELD_SUB_TITLE };
+		String[] selectionArgs = { name };
+		 
+	    CursorLoader cursorLoader = new CursorLoader( getActivity(), ProgramConstants.CONTENT_URI, projection, ProgramConstants.FIELD_TITLE + "=?", selectionArgs, ProgramConstants.FIELD_SUB_TITLE );
+	    Log.v( TAG, "onCreateLoader : cursorLoader=" + cursorLoader.toString() );
+	    
+	    Log.v( TAG, "onCreateLoader : exit" );
+		return cursorLoader;
+	}
+
+	/* (non-Javadoc)
+	 * @see android.support.v4.app.LoaderManager.LoaderCallbacks#onLoadFinished(android.support.v4.content.Loader, java.lang.Object)
+	 */
+	@Override
+	public void onLoadFinished( Loader<Cursor> loader, Cursor cursor ) {
+		Log.v( TAG, "onLoadFinished : enter" );
+		
+		adapter.swapCursor( cursor );
+		
+		Log.v( TAG, "onLoadFinished : exit" );
 	}
-	
-	public ProgramGroupFragment( Activity activity ) {
-		super();
-		Log.i( TAG, "initialize : enter" );
 
-		this.activity = activity;
+	/* (non-Javadoc)
+	 * @see android.support.v4.app.LoaderManager.LoaderCallbacks#onLoaderReset(android.support.v4.content.Loader)
+	 */
+	@Override
+	public void onLoaderReset( Loader<Cursor> loader ) {
+		Log.v( TAG, "onLoaderReset : enter" );
 		
-		Log.i( TAG, "initialize : exit" );
+		adapter.swapCursor( null );
+				
+		Log.v( TAG, "onLoaderReset : exit" );
+	}
+
+	/* (non-Javadoc)
+	 * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
+	 */
+	@Override
+	public void onCreate( Bundle savedInstanceState ) {
+		Log.i( TAG, "onCreate : enter" );
+		super.onCreate( savedInstanceState );
+
+		Log.i( TAG, "onCreate : exit" );
 	}
 
 	@Override
 	public void onActivityCreated( Bundle savedInstanceState ) {
 		Log.i( TAG, "onActivityCreated : enter" );
-
 		super.onActivityCreated( savedInstanceState );
 	    
-   		loadPrograms();
-
 	    Log.i( TAG, "onActivityCreated : exit" );
 	}
 
-	public void setActivity( Activity activity ) {
-		this.activity = activity;
-	}
-	
-	public void loadPrograms() {
+	public void loadPrograms( String name ) {
 		Log.i( TAG, "loadPrograms : enter" );
 
-		if( null != activity ) {
-			adapter = new ProgramAdapter( ( (MainApplication) activity.getApplicationContext() ).getCurrentRecordingsInProgramGroup() );
-			setListAdapter( adapter );
-		}
+		Log.i( TAG, "loadPrograms : name=" + name );
+
+		Bundle args = new Bundle();
+		args.putString( ProgramGroupActivity.EXTRA_PROGRAM_GROUP_KEY, name );
 		
-		Log.i( TAG, "loadPrograms : exit" );
+		getLoaderManager().initLoader( 0, args, this );
+
+	    adapter = new ProgramCursorAdapter(
+	            getActivity().getApplicationContext(), R.layout.program_row,
+	            null, new String[] { ProgramConstants.FIELD_SUB_TITLE }, new int[] { R.id.program_sub_title },
+	            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER );
+
+	    setListAdapter( adapter );
+	    
+	    Log.i( TAG, "loadPrograms : exit" );
 	}
 	
 	@Override
 
 		super.onListItemClick( l, v, position, id );
 		
-		Log.v (TAG, "position : " + position);
+		Log.v (TAG, "onListItemClick : position=" + position + ", id=" + id );
 	    
-		Program program = adapter.getItem( position );
-		getApplicationContext().setCurrentProgram( program );
-		
 		Intent i = new Intent( getActivity(), VideoActivity.class );
 		startActivity( i );
 
 		Log.v( TAG, "onListItemClick : exit" );
 	}
 	
-	private class ProgramAdapter extends BaseAdapter {
-		List<Program> programs = null;
-
-		ProgramAdapter( List<Program> programs ) {
-			super();
+	private class ProgramCursorAdapter extends SimpleCursorAdapter {
 
-			this.programs = programs;
-		}
-
-		@Override
-		public int getCount() {
-			return programs.size();
-		}
-
-		@Override
-		public Program getItem( int position ) {
-			return programs.get( position );
-		}
-
-		@Override
-		public long getItemId( int position ) {
-			return position;
+		public ProgramCursorAdapter( Context context, int layout, Cursor c, String[] from, int[] to, int flags ) {
+			super( context, layout, c, from, to, flags );
 		}
 
 		@Override
 		public View getView( int position, View convertView, ViewGroup parent ) {
-			View row = convertView;
-
-			if( row == null ) {
-				LayoutInflater inflater = getActivity().getLayoutInflater();
-
-				row = inflater.inflate( R.layout.program_row, parent, false );
-			}
-
-			Program program = getItem( position );
-
-			TextView title = (TextView) row.findViewById( R.id.program_sub_title );
-			title.setText( !"".equals( program.getSubTitle().trim() ) ? program.getSubTitle() : program.getTitle() );
-
-			//TextView description = (TextView) row.findViewById( R.id.program_description );
-			//description.setText( program.getDescription() );
-
+			Log.v( TAG, "getView : enter" );
+
+			View row =  super.getView( position, convertView, parent );
+			
+			getCursor().moveToPosition( position );
+		    try {
+		        int idIndex = getCursor().getColumnIndexOrThrow( BaseColumns._ID );
+		        int titleIndex = getCursor().getColumnIndexOrThrow( ProgramConstants.FIELD_TITLE );
+				int subTitleIndex = getCursor().getColumnIndexOrThrow( ProgramConstants.FIELD_SUB_TITLE );
+
+		        int id = getCursor().getInt( idIndex );
+		        String title = getCursor().getString( titleIndex );
+		        String subTitle = getCursor().getString( subTitleIndex );
+
+		        Log.v( TAG, "getView : id=" + id + ", title=" + title + ", subTitle=" + subTitle );
+		        
+				if( row == null ) {
+					LayoutInflater inflater = getActivity().getLayoutInflater();
+
+					row = inflater.inflate( R.layout.program_row, parent, false );
+				}
+
+				TextView t = (TextView) row.findViewById( R.id.program_sub_title );
+				t.setText( !"".equals( subTitle ) ? subTitle : title );
+
+		    } catch( Exception e ) {
+				Log.e( TAG, "getView : error", e );
+		    }
+		    
 			return row;
 		}
+
 	}
 
 }

File src/org/mythtv/client/ui/dvr/RecordingsActivity.java

 
 		RecordingsFragment recordings = (RecordingsFragment) getSupportFragmentManager().findFragmentById( R.id.fragment_dvr_program_groups );
 
-//		recordings.setOnProgramGroupListener( this );
+		recordings.setOnProgramGroupListener( this );
 
 		isTwoPane = ( null != findViewById( R.id.fragment_dvr_program_group ) );
 		
 		Log.i( TAG, "onCreate : exit" );
 	}
 
-	public void addProgramGroupFragment( ProgramGroup programGroup ) {
+	public void addProgramGroupFragment( String programGroup ) {
 		Log.d( TAG, "addProgramGroupFragment : enter" );
 
 		FragmentManager fragMgr = getSupportFragmentManager();
+		
 		ProgramGroupFragment programGroupFragment = (ProgramGroupFragment) fragMgr.findFragmentById( R.id.fragment_dvr_program_group );
+		
 		FragmentTransaction xaction = fragMgr.beginTransaction();
 
 		if( null == programGroupFragment ) {
 			Log.v( TAG, "addProgramGroupFragment : creating new program group fragment" );
 
-			programGroupFragment = new ProgramGroupFragment( this );
+			programGroupFragment = new ProgramGroupFragment();
 
 			xaction
 				.add( R.id.fragment_dvr_program_group, programGroupFragment )
 				.commit();
 		}
 
-		programGroupFragment.loadPrograms();
+		programGroupFragment.loadPrograms( programGroup );
 		
 		Log.d( TAG, "addProgramGroupFragment : exit" );
 	}
 
-	public void onProgramGroupSelected( ProgramGroup programGroup ) {
+	public void onProgramGroupSelected( String programGroup ) {
 		Log.d( TAG, "onProgramGroupSelected : enter" );
 
-		getApplicationContext().setCurrentRecordingsInProgramGroup( programGroup.getRecordings() );
-
+		
 		if( isTwoPane ) {
 			Log.v( TAG, "onProgramGroupSelected : adding program group to pane" );
 
 			Log.v( TAG, "onProgramGroupSelected : starting program group activity" );
 
 			Intent i = new Intent( this, ProgramGroupActivity.class );
+			i.putExtra( ProgramGroupActivity.EXTRA_PROGRAM_GROUP_KEY, programGroup );
 			startActivity( i );
 		}
 

File src/org/mythtv/client/ui/dvr/RecordingsFragment.java

 import org.mythtv.client.ui.util.MythtvListFragment;
 import org.mythtv.db.dvr.ProgramGroupConstants;
 
-import android.app.AlertDialog;
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.Context;
 
 	private static final String TAG = RecordingsFragment.class.getSimpleName();
 
+	private OnProgramGroupListener listener = null;
 	private ProgramGroupCursorAdapter adapter;
 	
 	/* (non-Javadoc)
 		super.onListItemClick( l, v, position, id );
 		Log.v( TAG, "onListItemClick : position=" + position + ", id=" + id );
 
+		Cursor cursor = getActivity().getApplicationContext().getContentResolver().query( ContentUris.withAppendedId( ProgramGroupConstants.CONTENT_URI, id ), new String[] { ProgramGroupConstants.FIELD_PROGRAM_GROUP }, null, null, null );
+		if( cursor.moveToFirst() ) {
+			int nameIndex = cursor.getColumnIndexOrThrow( ProgramGroupConstants.FIELD_PROGRAM_GROUP );
+			String name = cursor.getString( nameIndex );
+		
+			listener.onProgramGroupSelected( name );
+		}
+		cursor.close();
+		
 		Log.v( TAG, "onListItemClick : exit" );
 	}
 	  
-	public interface OnProgramGroupListener {
-		void onProgramGroupSelected( ProgramGroup programGroup );
-	}
+	public void setOnProgramGroupListener( OnProgramGroupListener listener ) {
+		Log.v( TAG, "setOnProgramGroupListener : enter" );
 
-	private void exceptionDialolg( Throwable t ) {
-		AlertDialog.Builder builder = new AlertDialog.Builder( getActivity() );
+		this.listener = listener;
 
-		builder
-			.setTitle( R.string.exception )
-			.setMessage( t.toString() )
-			.setPositiveButton( R.string.close, null )
-				.show();
+		Log.v( TAG, "setOnProgramGroupListener : exit" );
 	}
 
+	public interface OnProgramGroupListener {
+		void onProgramGroupSelected( String programGroup );
+	}
+
+//	private void exceptionDialolg( Throwable t ) {
+//		AlertDialog.Builder builder = new AlertDialog.Builder( getActivity() );
+//
+//		builder
+//			.setTitle( R.string.exception )
+//			.setMessage( t.toString() )
+//			.setPositiveButton( R.string.close, null )
+//				.show();
+//	}
+
 	private class ProgramGroupCursorAdapter extends SimpleCursorAdapter {
 
 		public ProgramGroupCursorAdapter( Context context, int layout, Cursor c, String[] from, int[] to, int flags ) {
 		    
 			return row;
 		}
-
 		
 	}