|
91 | 91 | import java.util.HashSet; |
92 | 92 | import java.util.List; |
93 | 93 | import java.util.Set; |
94 | | -import java.util.concurrent.Executors; |
95 | 94 |
|
96 | 95 | public class SearchHelper { |
97 | 96 |
|
98 | 97 | public static final int MARK_COLOR_VIBRANT = Color.rgb(255, 0, 0); |
99 | 98 | public static final String NEW_MODS_SEARCH_QUERY = "\uD83C\uDD95"; |
100 | | - public static List<ModData> allModsList = new ArrayList<>(); |
| 99 | + public static final List<ModData> allModsList = new ArrayList<>(); |
101 | 100 |
|
102 | 101 | public static String TAG = "SearchHelper"; |
103 | 102 |
|
@@ -360,100 +359,103 @@ private static void parsePrefXml(Context context, Class<?> catPrefsFragment, int |
360 | 359 | parsePrefXml(context, catPrefsFragment.getName(), xmlResId, internalId); |
361 | 360 | } |
362 | 361 |
|
363 | | -private static void parsePrefXml(Context context, String catPrefsFragment, int xmlResId, int... internalId) { |
364 | | - Executors.newSingleThreadExecutor().execute(() -> { |
365 | | - Resources res = context.getResources(); |
366 | | - try (XmlResourceParser xml = res.getXml(xmlResId)) { |
367 | | - int order = 0; |
368 | | - String location = null, locationHyper = null, locationPad = null; |
369 | | - int locationId = 0, locationHyperId = 0, locationPadId = 0; |
370 | | - boolean isPad = isPad(); |
371 | | - StringBuilder internalName = null; |
372 | | - int eventType = xml.getEventType(); |
373 | | - |
374 | | - if (internalId.length != 0) { |
375 | | - internalName = new StringBuilder(); |
376 | | - for (int id : internalId) { |
377 | | - if (internalName.length() > 0) { |
378 | | - internalName.append("/"); |
| 362 | + private static void parsePrefXml(Context context, String catPrefsFragment, int xmlResId, int... internalId) { |
| 363 | + ThreadPoolManager.getInstance().submit(() -> { |
| 364 | + Resources res = context.getResources(); |
| 365 | + try (XmlResourceParser xml = res.getXml(xmlResId)) { |
| 366 | + int order = 0; |
| 367 | + String location = null, locationPad = null; |
| 368 | + int locationId = 0, locationPadId = 0; |
| 369 | + boolean isPadDevice = isPad(); |
| 370 | + StringBuilder internalName = null; |
| 371 | + int eventType = xml.getEventType(); |
| 372 | + |
| 373 | + if (internalId.length != 0) { |
| 374 | + internalName = new StringBuilder(); |
| 375 | + for (int id : internalId) { |
| 376 | + if (internalName.length() > 0) { |
| 377 | + internalName.append("/"); |
| 378 | + } |
| 379 | + |
| 380 | + internalName.append(res.getString(id)); |
379 | 381 | } |
380 | | - internalName.append(res.getString(id)); |
381 | 382 | } |
382 | | - } |
383 | 383 |
|
384 | | - List<ModData> localList = new ArrayList<>(); |
| 384 | + List<ModData> localList = new ArrayList<>(32); |
385 | 385 |
|
386 | | - while (eventType != XmlPullParser.END_DOCUMENT) { |
387 | | - if (eventType == XmlPullParser.START_TAG && !xml.getName().equals("PreferenceCategory")) { |
388 | | - try { |
389 | | - ModData modData = new ModData(); |
390 | | - modData.title = getModTitle(res, xml.getAttributeValue(ANDROID_NS, "title")); |
391 | | - boolean isPreferenceVisible = Boolean.parseBoolean(xml.getAttributeValue(APP_NS, "isPreferenceVisible")); |
| 386 | + while (eventType != XmlPullParser.END_DOCUMENT) { |
| 387 | + if (eventType == XmlPullParser.START_TAG && !"PreferenceCategory".equals(xml.getName())) { |
| 388 | + try { |
| 389 | + String titleAttr = xml.getAttributeValue(ANDROID_NS, "title"); |
| 390 | + String keyAttr = xml.getAttributeValue(ANDROID_NS, "key"); |
| 391 | + String isPrefVisibleAttr = xml.getAttributeValue(APP_NS, "isPreferenceVisible"); |
| 392 | + String myLocationPadAttr = xml.getAttributeValue(APP_NS, "myLocationPad"); |
| 393 | + String myLocationAttr = xml.getAttributeValue(APP_NS, "myLocation"); |
392 | 394 |
|
393 | | - if (locationPad == null) { |
394 | | - locationPad = getModTitle(res, xml.getAttributeValue(APP_NS, "myLocationPad")); |
395 | | - locationPadId = getModId(xml.getAttributeValue(APP_NS, "myLocationPad")); |
396 | | - } |
397 | | - if (location == null) { |
398 | | - location = getModTitle(res, xml.getAttributeValue(APP_NS, "myLocation")); |
399 | | - locationId = getModId(xml.getAttributeValue(APP_NS, "myLocation")); |
400 | | - } |
| 395 | + String modTitle = getModTitle(res, titleAttr); |
| 396 | + boolean isPreferenceVisible = Boolean.parseBoolean(isPrefVisibleAttr); |
| 397 | + |
| 398 | + if (locationPad == null && myLocationPadAttr != null) { |
| 399 | + locationPad = getModTitle(res, myLocationPadAttr); |
| 400 | + locationPadId = getModId(myLocationPadAttr); |
| 401 | + } |
| 402 | + if (location == null && myLocationAttr != null) { |
| 403 | + location = getModTitle(res, myLocationAttr); |
| 404 | + locationId = getModId(myLocationAttr); |
| 405 | + } |
401 | 406 |
|
402 | | - if (!TextUtils.isEmpty(modData.title) && !isPreferenceVisible) { |
403 | | - String internalPad = internalName == null ? locationPad : internalName + "/" + locationPad; |
404 | | - String internal = internalName == null ? location : internalName + "/" + location; |
| 407 | + if (!TextUtils.isEmpty(modTitle) && !isPreferenceVisible) { |
| 408 | + String internalPad = internalName == null ? locationPad : internalName + "/" + locationPad; |
| 409 | + String internal = internalName == null ? location : internalName + "/" + location; |
405 | 410 |
|
406 | | - if (locationHyper == null || location == null || (isPad && locationPad == null)) { |
407 | | - if (location != null) { |
| 411 | + ModData modData = new ModData(); |
| 412 | + modData.title = modTitle; |
| 413 | + if (location != null && (!isPadDevice || locationPad == null)) { |
408 | 414 | modData.breadcrumbs = internal; |
409 | 415 | modData.catTitleResId = locationId; |
410 | 416 | } else if (locationPad != null) { |
411 | 417 | modData.breadcrumbs = internalPad; |
412 | 418 | modData.catTitleResId = locationPadId; |
413 | 419 | } |
414 | | - } else { |
415 | | - if (!isPad) { |
416 | | - modData.breadcrumbs = internal; |
417 | | - modData.catTitleResId = locationId; |
418 | | - } else { |
419 | | - modData.breadcrumbs = internalPad; |
420 | | - modData.catTitleResId = locationPadId; |
421 | | - } |
| 420 | + modData.xml = xmlResId; |
| 421 | + modData.key = keyAttr; |
| 422 | + modData.order = order; |
| 423 | + modData.fragment = catPrefsFragment; |
| 424 | + localList.add(modData); |
422 | 425 | } |
423 | | - |
424 | | - modData.xml = xmlResId; |
425 | | - modData.key = xml.getAttributeValue(ANDROID_NS, "key"); |
426 | | - modData.order = order; |
427 | | - modData.fragment = catPrefsFragment; |
428 | | - localList.add(modData); |
| 426 | + order++; |
| 427 | + } catch (Throwable t) { |
| 428 | + AndroidLogUtils.logE(TAG, "Failed to get xml keyword object!", t); |
429 | 429 | } |
430 | | - order++; |
431 | | - } catch (Throwable t) { |
432 | | - AndroidLogUtils.logE(TAG, "Failed to get xml keyword object!", t); |
433 | 430 | } |
| 431 | + eventType = xml.next(); |
434 | 432 | } |
435 | | - eventType = xml.next(); |
436 | | - } |
437 | 433 |
|
438 | | - synchronized (allModsList) { |
439 | | - allModsList.addAll(localList); |
440 | | - } |
| 434 | + if (!localList.isEmpty()) { |
| 435 | + synchronized (allModsList) { |
| 436 | + allModsList.addAll(localList); |
| 437 | + } |
| 438 | + } |
441 | 439 |
|
442 | | - } catch (Throwable t) { |
443 | | - AndroidLogUtils.logE(TAG, "Failed to access XML resource!", t); |
444 | | - } |
445 | | - }); |
446 | | -} |
| 440 | + } catch (Throwable t) { |
| 441 | + AndroidLogUtils.logE(TAG, "Failed to access XML resource!", t); |
| 442 | + } |
| 443 | + }); |
| 444 | + } |
447 | 445 |
|
448 | 446 | private static int getModId(String title) { |
449 | 447 | if (title == null) { |
450 | 448 | return -1; |
451 | 449 | } |
452 | | - int titleResId = Integer.parseInt(title.substring(1)); |
453 | | - if (titleResId <= 0) { |
| 450 | + try { |
| 451 | + int titleResId = Integer.parseInt(title.substring(1)); |
| 452 | + if (titleResId <= 0) { |
| 453 | + return -1; |
| 454 | + } |
| 455 | + return titleResId; |
| 456 | + } catch (NumberFormatException e) { |
454 | 457 | return -1; |
455 | 458 | } |
456 | | - return titleResId; |
457 | 459 | } |
458 | 460 |
|
459 | 461 | private static String getModTitle(Resources res, String title) { |
|
0 commit comments