Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 75eb42b9 authored by Mateus Azis's avatar Mateus Azis
Browse files

Use try-with-resources to avoid accidentaly leaking unclosed objects.

Try-with-resources guarantees that objects will be closed even if an
exception happens after their creation. It also handles nulls and avoids
throwing NPE.

Test: built and launched Cuttlefish.
Change-Id: I80e767d1fbbca25a724bfe94e1752f88891a6bb0
parent a1355bc2
Loading
Loading
Loading
Loading
+9 −12
Original line number Diff line number Diff line
@@ -98,9 +98,8 @@ public final class WallpaperInfo implements Parcelable {
        ServiceInfo si = service.serviceInfo;

        final PackageManager pm = context.getPackageManager();
        XmlResourceParser parser = null;
        try {
            parser = si.loadXmlMetaData(pm, WallpaperService.SERVICE_META_DATA);
        try (XmlResourceParser parser = si.loadXmlMetaData(pm,
                WallpaperService.SERVICE_META_DATA)) {
            if (parser == null) {
                throw new XmlPullParserException("No "
                        + WallpaperService.SERVICE_META_DATA + " meta-data");
@@ -159,8 +158,6 @@ public final class WallpaperInfo implements Parcelable {
        } catch (NameNotFoundException e) {
            throw new XmlPullParserException(
                    "Unable to create context for: " + si.packageName);
        } finally {
            if (parser != null) parser.close();
        }
    }