I have a file output like this and want to do parsing and put the output into a List. The biggest problem here is the name of a wake-lock which appears after ID and runtime, in some cases, it contains spaces, ":" or () which made me hard to parse. Some cases also don't have runtime neither counts, instead all of that there is just word realtime. I have put my approach below, maybe it needs just a little improvement which I cannot see, or it a whole miss.
1000 ActivityManager-Sleep: 4s 493ms (0 times) max=4991 actual=4991 realtime u0a149 AudioIn: 1s 274ms (1 times) max=1274 realtime u0a138 *job*/com.android.vending/com.google.android.finsky.scheduler.process.mainimpl.PhoneskyJobServiceMain: 257ms (1 times) max=547 actual=547 realtime 1041 AudioIn: 133ms (3 times) max=126 realtime u0a136 GCoreFlp: 81ms (1 times) max=153 actual=153 realtime 1001 *telephony-radio*: 76ms (3 times) max=155 actual=157 realtime u0a188 Doze: 39ms (1 times) max=124 actual=124 realtime u0a188 Scrims: 36ms (1 times) max=115 actual=115 realtime u0a257 *job*/com.liverpool.echo/com.urbanairship.job.AndroidJobService: 32ms (1 times) max=96 actual=96 realtime 1000 startDream: 25ms (1 times) max=59 actual=59 realtime 1000 GCoreFlp: 23ms (1 times) max=84 actual=84 realtime u0a188 show keyguard: 22ms (0 times) max=48 actual=48 realtime u0a136 GCM_READ: 17ms (2 times) max=12 realtime 1000 AnyMotionDetector: 17ms (1 times) max=24 actual=24 realtime u0a145 *job*/com.google.android.apps.turbo/.nudges.broadcasts.BatteryHistoryLoggerJobService: 11ms (1 times) max=47 actual=47 realtime 1000 deviceidle_going_idle: 10ms (1 times) max=32 actual=32 realtime u0a136 NlpWakeLock: 9ms (6 times) max=11 actual=36 realtime u0a136 CMWakeLock: 8ms (1 times) max=13 actual=13 realtime 1002 bluetooth_timer: 8ms (4 times) max=8 actual=13 realtime u0a136 UlrDispSvcFastWL: 6ms (3 times) max=19 actual=23 realtime 1000 *alarm*: 6ms (1 times) max=6 realtime u0a136 *alarm*: 6ms (1 times) max=9 actual=9 realtime 1000 NlpWakeLock: 4ms (3 times) max=8 actual=13 realtime u0a136 GCM_HB_ALARM: 3ms (1 times) max=6 actual=6 realtime u0a166 GCoreFlp: 3ms (2 times) max=3 actual=6 realtime u0a145 *job*/com.google.android.apps.turbo/.deadline.library.DeadlineUpdateJobService: 3ms (1 times) max=11 actual=11 realtime u0a147 NlpWakeLock: 2ms (3 times) max=4 actual=8 realtime 1000 GnssLocationProvider: 2ms (4 times) max=4 actual=7 realtime 1000 WifiSuspend: 2ms (1 times) max=7 actual=7 realtime u0a136 *gms_scheduler*:internal: 1ms (1 times) max=5 actual=5 realtime u0a136 Wakeful StateMachine: GeofencerStateMachine: 1ms (1 times) max=2 actual=2 realtime 1027 NfcService:mRoutingWakeLock realtime
I need an Array output for each line with ID, name, runTime, timesTriggered, max, actual
I tried to do this with split and substring, but I don't like my approach, also it won't work at some cases.
for (int k = 0; k < wakelocksToParse.size(); k++) { String wakelockStat = wakelocksToParse.get(k); utils.writeFile(Data.PARTIAL_WAKELOCKS_NEW, wakelockStat, true); if (wakelockStat.lastIndexOf(":") != -1) { String UID; String wakelockName; String wakelockTime; String wakelockCount; long timeInMs = 0; UID = wakelockStat.substring(0, wakelockStat.indexOf(" ")); wakelockName = wakelockStat.substring(wakelockStat.indexOf(" "), wakelockStat.lastIndexOf(":")).trim(); if (wakelockName.length() == 0) wakelockName = "unknown"; wakelockTime = wakelockStat.substring(wakelockStat.lastIndexOf(":") + 2, wakelockStat.indexOf("(") - 1); String[] time = wakelockTime.replaceAll("[^0-9 ]", "").split(" "); if (time.length == 5) { int days = utils.parseIntWithDefault(time[0], 0); int hours = utils.parseIntWithDefault(time[1], 0); int minutes = utils.parseIntWithDefault(time[2], 0); int seconds = utils.parseIntWithDefault(time[3], 0); int ms = utils.parseIntWithDefault(time[4], 0); timeInMs = (days * 86400000) + (hours * 3600000) + (minutes * 60000) + (seconds * 1000) + ms; } else if (time.length == 4) { int hours = utils.parseIntWithDefault(time[0], 0); int minutes = utils.parseIntWithDefault(time[1], 0); int seconds = utils.parseIntWithDefault(time[2], 0); int ms = utils.parseIntWithDefault(time[3], 0); timeInMs = (hours * 3600000) + (minutes * 60000) + (seconds * 1000) + ms; } else if (time.length == 3) { int minutes = utils.parseIntWithDefault(time[0], 0); int seconds = utils.parseIntWithDefault(time[1], 0); int ms = utils.parseIntWithDefault(time[2], 0); timeInMs = (minutes * 60000) + (seconds * 1000) + ms; } else if (time.length == 2) { int seconds = utils.parseIntWithDefault(time[0], 0); int ms = utils.parseIntWithDefault(time[1], 0); timeInMs = (seconds * 1000) + ms; } else if (time.length == 1) { timeInMs = utils.parseIntWithDefault(time[0], 0); } wakelockCount = wakelockStat.substring(wakelockStat.lastIndexOf(':') + 1).trim(); wakelockCount = wakelockCount.substring(wakelockCount.indexOf("(") + 1); wakelockCount = wakelockCount.substring(0, wakelockCount.indexOf(" ")).trim(); Log.d("aaaa", UID + " " + wakelockName + " " + timeInMs + " " + wakelockCount); if (timeInMs >= 1000) data.add(new WakelocksData(UID, wakelockName, timeInMs, wakelockCount)); utils.writeFile(Data.PARTIAL_WAKELOCKS_OLD, UID + " " + wakelockName + " " + timeInMs + " " + wakelockCount, true); } } ```