Refactor ActivityRecord#finish* methods

Updated ActivityRecord#finish* methods and to separate their purposes
and use better suited names.

ActivityRecord#finishIfPossible - start of activity finishing. In
most cases will first pause activity and wait for it to report back.

ActivityRecord#completeFinishing - second stage of the process, when
activity is no longer resumed. Usually will be called from 'paused'
callback on the server. If the next activity that should be shown in
place of the current one is not visible yet - it will defer the
finishing process and add the current activity to stopping list. If
next is already visible - will finish immediately.

ActivityRecord#destroyIfPossible - called when activity is ready to
be destroyed, or when the caller does not want to wait. Will initiate
the client destruction immediately, unless it's the last activity on
display.

Bug: 137329632
Test: atest CtsWindowManagerDeviceTestCases:android.server.wm.lifecycle
Test: atest WmTests:ActivityRecordTests
Change-Id: Id6e6dae306e2af91e9e00d9e66c416b13810ad93
diff --git a/services/core/java/com/android/server/wm/TaskRecord.java b/services/core/java/com/android/server/wm/TaskRecord.java
index 882f411..ede2f56 100644
--- a/services/core/java/com/android/server/wm/TaskRecord.java
+++ b/services/core/java/com/android/server/wm/TaskRecord.java
@@ -1419,8 +1419,9 @@
                 mActivities.remove(activityNdx);
                 --activityNdx;
                 --numActivities;
-            } else if (r.finishActivityLocked(Activity.RESULT_CANCELED, null,
-                    reason, false /* oomAdj */, pauseImmediately) == FINISH_RESULT_REMOVED) {
+            } else if (r.finishIfPossible(Activity.RESULT_CANCELED,
+                    null /* resultData */, reason, false /* oomAdj */, pauseImmediately)
+                    == FINISH_RESULT_REMOVED) {
                 --activityNdx;
                 --numActivities;
             }
@@ -1474,8 +1475,8 @@
                     if (opts != null) {
                         ret.updateOptionsLocked(opts);
                     }
-                    if (r.finishActivityLocked(Activity.RESULT_CANCELED, null /* resultData */,
-                            "clear-task-stack", false /* oomAdj */) == FINISH_RESULT_REMOVED) {
+                    if (r.finishIfPossible("clear-task-stack", false /* oomAdj */)
+                            == FINISH_RESULT_REMOVED) {
                         --activityNdx;
                         --numActivities;
                     }
@@ -1488,8 +1489,7 @@
                         && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0
                         && !ActivityStarter.isDocumentLaunchesIntoExisting(launchFlags)) {
                     if (!ret.finishing) {
-                        ret.finishActivityLocked(Activity.RESULT_CANCELED, null /* resultData */,
-                                "clear-task-top", false /* oomAdj */);
+                        ret.finishIfPossible("clear-task-top", false /* oomAdj */);
                         return null;
                     }
                 }