@@ -713,6 +713,10 @@ private void removeSessionStateLocked(IBinder sessionToken, int userId) {
713713 // Remove the session state from the global session state map of the current user.
714714 SessionState sessionState = userState .sessionStateMap .remove (sessionToken );
715715
716+ if (sessionState == null ) {
717+ return ;
718+ }
719+
716720 // Close the open log entry, if any.
717721 if (sessionState .mLogUri != null ) {
718722 SomeArgs args = SomeArgs .obtain ();
@@ -2150,6 +2154,7 @@ private void onOpenEntry(Uri logUri, long channelId, long watchStarttime,
21502154 args .arg1 = logUri ;
21512155 args .arg2 = channelId ;
21522156 args .arg3 = endTime ;
2157+ args .arg4 = sessionState ;
21532158 Message msg = obtainMessage (LogHandler .MSG_UPDATE_ENTRY , args );
21542159 sendMessageDelayed (msg , endTime - System .currentTimeMillis ());
21552160 }
@@ -2162,49 +2167,64 @@ private void onOpenEntry(Uri logUri, long channelId, long watchStarttime,
21622167
21632168 private void onUpdateEntry (Uri uri , long channelId , long time , SessionState sessionState ) {
21642169 String [] projection = {
2165- TvContract .WatchedPrograms .COLUMN_WATCH_START_TIME_UTC_MILLIS ,
2166- TvContract .WatchedPrograms .COLUMN_WATCH_END_TIME_UTC_MILLIS ,
2167- TvContract .WatchedPrograms .COLUMN_TITLE ,
2168- TvContract .WatchedPrograms .COLUMN_START_TIME_UTC_MILLIS ,
2169- TvContract .WatchedPrograms .COLUMN_END_TIME_UTC_MILLIS ,
2170- TvContract .WatchedPrograms .COLUMN_DESCRIPTION
2170+ TvContract .WatchedPrograms .COLUMN_WATCH_END_TIME_UTC_MILLIS
21712171 };
21722172 Cursor cursor = null ;
21732173 try {
21742174 cursor = mContentResolver .query (uri , projection , null , null , null );
21752175 if (cursor != null && cursor .moveToNext ()) {
2176- long watchStartTime = cursor .getLong (0 );
2177- long watchEndTime = cursor .getLong (1 );
2178- String title = cursor .getString (2 );
2179- long startTime = cursor .getLong (3 );
2180- long endTime = cursor .getLong (4 );
2181- String description = cursor .getString (5 );
2182-
2176+ long watchEndTime = cursor .getLong (0 );
21832177 // Do nothing if the current log entry is already closed.
21842178 if (watchEndTime > 0 ) {
21852179 return ;
21862180 }
21872181
2188- // The current program has just ended. Create a (complete) log entry off the
2189- // current entry.
2182+ // Update the watch end time for the current log entry.
21902183 ContentValues values = new ContentValues ();
2191- values .put (TvContract .WatchedPrograms .COLUMN_WATCH_START_TIME_UTC_MILLIS ,
2192- watchStartTime );
21932184 values .put (TvContract .WatchedPrograms .COLUMN_WATCH_END_TIME_UTC_MILLIS , time );
2185+ int c = mContentResolver .update (uri , values , null , null );
2186+ } else {
2187+ // The record has been deleted.
2188+ synchronized (mLock ) {
2189+ if (!uri .equals (sessionState .mLogUri )) {
2190+ // If the deleted record is not for the current channel, do not re-open
2191+ // a log entry for the next program.
2192+ return ;
2193+ }
2194+ }
2195+ }
2196+ if (cursor != null ) {
2197+ cursor .close ();
2198+ cursor = null ;
2199+ }
2200+
2201+ // The current program has just ended. Create a new log entry for the next program.
2202+ uri = ContentUris .withAppendedId (TvContract .Channels .CONTENT_URI , channelId );
2203+ projection = new String [] {
2204+ TvContract .Channels .COLUMN_PACKAGE_NAME
2205+ };
2206+ cursor = mContentResolver .query (uri , projection , null , null , null );
2207+ if (cursor != null && cursor .moveToNext ()) {
2208+ ContentValues values = new ContentValues ();
2209+ values .put (TvContract .WatchedPrograms .COLUMN_PACKAGE_NAME , cursor .getString (0 ));
2210+ values .put (TvContract .WatchedPrograms .COLUMN_WATCH_START_TIME_UTC_MILLIS , time );
2211+ values .put (TvContract .WatchedPrograms .COLUMN_WATCH_END_TIME_UTC_MILLIS , 0 );
21942212 values .put (TvContract .WatchedPrograms .COLUMN_CHANNEL_ID , channelId );
2195- values .put (TvContract .WatchedPrograms .COLUMN_TITLE , title );
2196- values .put (TvContract .WatchedPrograms .COLUMN_START_TIME_UTC_MILLIS , startTime );
2197- values .put (TvContract .WatchedPrograms .COLUMN_END_TIME_UTC_MILLIS , endTime );
2198- values .put (TvContract .WatchedPrograms .COLUMN_DESCRIPTION , description );
2199- mContentResolver .insert (TvContract .WatchedPrograms .CONTENT_URI , values );
2213+ Uri newUri = mContentResolver .insert (TvContract .WatchedPrograms .CONTENT_URI ,
2214+ values );
2215+
2216+ synchronized (mLock ) {
2217+ sessionState .mLogUri = newUri ;
2218+ }
2219+
2220+ // Re-open the current log entry with the next program information.
2221+ onOpenEntry (newUri , channelId , time , sessionState );
22002222 }
22012223 } finally {
22022224 if (cursor != null ) {
22032225 cursor .close ();
22042226 }
22052227 }
2206- // Re-open the current log entry with the next program information.
2207- onOpenEntry (uri , channelId , time , sessionState );
22082228 }
22092229
22102230 private void onCloseEntry (Uri uri , long watchEndTime ) {
0 commit comments