Update oom score reference state if activity state is changed

Previously, the update of reference state may be scheduled in
display thread. That causes a race for the case of activity stopped:
when ActivityTaskManagerService#activityStopped is called, it will
first set STOPPED state and then invoke trimApplications which will
trigger a full oom-adj calculation. But if the scheduled reference
state is updated later than the calculation, the adj and process
state of the stopped activity may stay at a stale perceptible state
temporally. That seems somewhat inconsistent between ATMS and AMS.

This change ensures that any activity states change applies to the
oom reference state immediately. So any direct or posted oom-adj
calculation can refer to the latest state.

Also correct the place of setting activity state flags that should
be outside of the loop.

Bug: 159104503
Bug: 170685888
Test: ActivityLifecycleTests#testRestoreFromKill
      WindowProcessControllerTests#testComputeOomAdjFromActivities
Change-Id: I42bd43f71315dbf8f5d6abc7ab2f742419f0b6da
4 files changed