Skip to content

Commit 6b42600

Browse files
committedApr 6, 2023
Tvprovider : Fix resource leak
To prevent resource leaks and strict mode policy violation LeakedClosableViolation added try -with-resources statement to ensure that each resource is closed at the end of the statement. Test: build only Bug: 154293770 Change-Id: Id12a9ada5f12991121c1374ce4ed8d7bdaa98d7c
1 parent 93d84b9 commit 6b42600

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed
 

‎tvprovider/tvprovider/src/main/java/androidx/tvprovider/media/tv/PreviewChannelHelper.java

+29-18
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,19 @@ public List<PreviewChannel> getAllChannels() {
195195
return Collections.emptyList();
196196
}
197197

198-
Cursor cursor = mContext.getContentResolver()
198+
List<PreviewChannel> channels = new ArrayList<>();
199+
try (Cursor cursor = mContext.getContentResolver()
199200
.query(
200201
TvContractCompat.Channels.CONTENT_URI,
201202
PreviewChannel.Columns.PROJECTION,
202203
null,
203204
null,
204-
null);
205-
206-
List<PreviewChannel> channels = new ArrayList<>();
207-
if (cursor != null && cursor.moveToFirst()) {
208-
do {
209-
channels.add(PreviewChannel.fromCursor(cursor));
210-
} while (cursor.moveToNext());
205+
null)) {
206+
if (cursor != null && cursor.moveToFirst()) {
207+
do {
208+
channels.add(PreviewChannel.fromCursor(cursor));
209+
} while (cursor.moveToNext());
210+
}
211211
}
212212
return channels;
213213
}
@@ -228,10 +228,15 @@ public PreviewChannel getPreviewChannel(long channelId) {
228228

229229
PreviewChannel channel = null;
230230
Uri channelUri = TvContractCompat.buildChannelUri(channelId);
231-
Cursor cursor = mContext.getContentResolver()
232-
.query(channelUri, PreviewChannel.Columns.PROJECTION, null, null, null);
233-
if (cursor != null && cursor.moveToFirst()) {
234-
channel = PreviewChannel.fromCursor(cursor);
231+
try (Cursor cursor = mContext.getContentResolver()
232+
.query(channelUri,
233+
PreviewChannel.Columns.PROJECTION,
234+
null,
235+
null,
236+
null)) {
237+
if (cursor != null && cursor.moveToFirst()) {
238+
channel = PreviewChannel.fromCursor(cursor);
239+
}
235240
}
236241
return channel;
237242
}
@@ -416,9 +421,12 @@ public PreviewProgram getPreviewProgram(long programId) {
416421

417422
PreviewProgram program = null;
418423
Uri programUri = TvContractCompat.buildPreviewProgramUri(programId);
419-
Cursor cursor = mContext.getContentResolver().query(programUri, null, null, null, null);
420-
if (cursor != null && cursor.moveToFirst()) {
421-
program = PreviewProgram.fromCursor(cursor);
424+
try (Cursor cursor = mContext.getContentResolver()
425+
.query(programUri, null, null, null, null);
426+
) {
427+
if (cursor != null && cursor.moveToFirst()) {
428+
program = PreviewProgram.fromCursor(cursor);
429+
}
422430
}
423431
return program;
424432
}
@@ -492,9 +500,12 @@ public WatchNextProgram getWatchNextProgram(long programId) {
492500

493501
WatchNextProgram program = null;
494502
Uri programUri = TvContractCompat.buildWatchNextProgramUri(programId);
495-
Cursor cursor = mContext.getContentResolver().query(programUri, null, null, null, null);
496-
if (cursor != null && cursor.moveToFirst()) {
497-
program = WatchNextProgram.fromCursor(cursor);
503+
try (Cursor cursor = mContext.getContentResolver()
504+
.query(programUri, null, null, null, null)
505+
) {
506+
if (cursor != null && cursor.moveToFirst()) {
507+
program = WatchNextProgram.fromCursor(cursor);
508+
}
498509
}
499510
return program;
500511
}

0 commit comments

Comments
 (0)
Please sign in to comment.