@@ -67,19 +67,29 @@ object ProfileManager {
6767 PROFILE_AUTO -> false
6868 PROFILE_REAL -> false
6969 PROFILE_NATIVE -> true
70- else -> getProfileXml(context, profile)?.use {
71- var next = it.next()
72- while (next != XmlPullParser .END_DOCUMENT ) {
73- when (next) {
74- XmlPullParser .START_TAG -> when (it.name) {
75- " profile" -> {
76- return @use it.getAttributeBooleanValue(null , " auto" , false )
70+ else -> {
71+ val parser = getProfileXml(context, profile)
72+ if (parser != null ) {
73+ try {
74+ var next = parser.next()
75+ while (next != XmlPullParser .END_DOCUMENT ) {
76+ when (next) {
77+ XmlPullParser .START_TAG -> when (parser.name) {
78+ " profile" -> {
79+ return @runCatching parser.getAttributeBooleanValue(null , " auto" , false )
80+ }
81+ }
7782 }
83+ next = parser.next()
7884 }
85+ } finally {
86+ parser.close()
7987 }
80- next = it.next()
88+ false
89+ } else {
90+ false
8191 }
82- } == true
92+ }
8393 }
8494 }.getOrDefault(false )
8595
@@ -90,20 +100,25 @@ object ProfileManager {
90100 if (profileResId == 0 ) return realData
91101 val resultData = mutableMapOf<String , String >()
92102 resultData.putAll(realData)
93- getProfileXml(context, profile)?.use {
94- var next = it.next()
95- while (next != XmlPullParser .END_DOCUMENT ) {
96- when (next) {
97- XmlPullParser .START_TAG -> when (it.name) {
98- " data" -> {
99- val key = it.getAttributeValue(null , " key" )
100- val value = it.getAttributeValue(null , " value" )
101- resultData[key] = value
102- Log .d(TAG , " Overwrite from profile: $key = $value " )
103+ val parser = getProfileXml(context, profile)
104+ if (parser != null ) {
105+ try {
106+ var next = parser.next()
107+ while (next != XmlPullParser .END_DOCUMENT ) {
108+ when (next) {
109+ XmlPullParser .START_TAG -> when (parser.name) {
110+ " data" -> {
111+ val key = parser.getAttributeValue(null , " key" )
112+ val value = parser.getAttributeValue(null , " value" )
113+ resultData[key] = value
114+ Log .d(TAG , " Overwrite from profile: $key = $value " )
115+ }
103116 }
104117 }
118+ next = parser.next()
105119 }
106- next = it.next()
120+ } finally {
121+ parser.close()
107122 }
108123 }
109124 return resultData
@@ -151,15 +166,20 @@ object ProfileManager {
151166
152167 // From profile
153168 try {
154- getProfileXml(context, profile)?.use {
155- var next = it.next()
156- while (next != XmlPullParser .END_DOCUMENT ) {
157- when (next) {
158- XmlPullParser .START_TAG -> when (it.name) {
159- " serial" -> return it.getAttributeValue(null , " template" )
169+ val parser = getProfileXml(context, profile)
170+ if (parser != null ) {
171+ try {
172+ var next = parser.next()
173+ while (next != XmlPullParser .END_DOCUMENT ) {
174+ when (next) {
175+ XmlPullParser .START_TAG -> when (parser.name) {
176+ " serial" -> return parser.getAttributeValue(null , " template" )
177+ }
160178 }
179+ next = parser.next()
161180 }
162- next = it.next()
181+ } finally {
182+ parser.close()
163183 }
164184 }
165185 } catch (e: Exception ) {
@@ -274,19 +294,26 @@ object ProfileManager {
274294
275295 fun getProfileName (context : Context , profile : String ): String? = getProfileName { getProfileXml(context, profile) }
276296
277- private fun getProfileName (parserCreator : () -> XmlResourceParser ? ): String? = parserCreator()?.use {
278- var next = it.next()
279- while (next != XmlPullParser .END_DOCUMENT ) {
280- when (next) {
281- XmlPullParser .START_TAG -> when (it.name) {
282- " profile" -> {
283- return @use it.getAttributeValue(null , " name" )
297+ private fun getProfileName (parserCreator : () -> XmlResourceParser ? ): String? {
298+ val parser = parserCreator()
299+ if (parser != null ) {
300+ try {
301+ var next = parser.next()
302+ while (next != XmlPullParser .END_DOCUMENT ) {
303+ when (next) {
304+ XmlPullParser .START_TAG -> when (parser.name) {
305+ " profile" -> {
306+ return parser.getAttributeValue(null , " name" )
307+ }
308+ }
284309 }
310+ next = parser.next()
285311 }
312+ } finally {
313+ parser.close()
286314 }
287- next = it.next()
288315 }
289- null
316+ return null
290317 }
291318
292319 fun setProfile (context : Context , profile : String? ) {
0 commit comments