huangcm
2025-04-11 48566d1cda2d109a94496c806286f47b8984166d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
 
package com.android.server.wm;
 
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
 
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.function.pooled.PooledLambda;
 
import java.util.ArrayList;
 
/**
 * Multi-cast implementation of {@link ActivityMetricsLaunchObserver}.
 *
 * <br /><br />
 * If this class is called through the {@link ActivityMetricsLaunchObserver} interface,
 * then the call is forwarded to all registered observers at the time.
 *
 * <br /><br />
 * All calls are invoked asynchronously in-order on a background thread. This fulfills the
 * sequential ordering guarantee in {@link ActivityMetricsLaunchObserverRegistry}.
 *
 * @see ActivityTaskManagerInternal#getLaunchObserverRegistry()
 */
class LaunchObserverRegistryImpl implements
        ActivityMetricsLaunchObserverRegistry, ActivityMetricsLaunchObserver {
    private final ArrayList<ActivityMetricsLaunchObserver> mList = new ArrayList<>();
 
    /**
     * All calls are posted to a handler because:
     *
     * 1. We don't know how long the observer will take to handle this call and we don't want
     *    to block the WM critical section on it.
     * 2. We don't know the lock ordering of the observer so we don't want to expose a chance
     *    of deadlock.
     */
    private final Handler mHandler;
 
    public LaunchObserverRegistryImpl(Looper looper) {
        mHandler = new Handler(looper);
    }
 
    @Override
    public void registerLaunchObserver(ActivityMetricsLaunchObserver launchObserver) {
        mHandler.sendMessage(PooledLambda.obtainMessage(
                LaunchObserverRegistryImpl::handleRegisterLaunchObserver, this, launchObserver));
    }
 
    @Override
    public void unregisterLaunchObserver(ActivityMetricsLaunchObserver launchObserver) {
        mHandler.sendMessage(PooledLambda.obtainMessage(
                LaunchObserverRegistryImpl::handleUnregisterLaunchObserver, this, launchObserver));
    }
 
    @Override
    public void onIntentStarted(Intent intent) {
        mHandler.sendMessage(PooledLambda.obtainMessage(
                LaunchObserverRegistryImpl::handleOnIntentStarted, this, intent));
    }
 
    @Override
    public void onIntentFailed() {
        mHandler.sendMessage(PooledLambda.obtainMessage(
                LaunchObserverRegistryImpl::handleOnIntentFailed, this));
    }
 
    @Override
    public void onActivityLaunched(
            @ActivityRecordProto byte[] activity,
            int temperature) {
        mHandler.sendMessage(PooledLambda.obtainMessage(
                LaunchObserverRegistryImpl::handleOnActivityLaunched,
                this, activity, temperature));
    }
 
    @Override
    public void onActivityLaunchCancelled(
        @ActivityRecordProto byte[] activity) {
        mHandler.sendMessage(PooledLambda.obtainMessage(
                LaunchObserverRegistryImpl::handleOnActivityLaunchCancelled, this, activity));
    }
 
    @Override
    public void onActivityLaunchFinished(
        @ActivityRecordProto byte[] activity) {
        mHandler.sendMessage(PooledLambda.obtainMessage(
                LaunchObserverRegistryImpl::handleOnActivityLaunchFinished, this, activity));
    }
 
    // Use PooledLambda.obtainMessage to invoke below methods. Every method reference must be
    // unbound (i.e. not capture any variables explicitly or implicitly) to fulfill the
    // singleton-lambda requirement.
 
    private void handleRegisterLaunchObserver(ActivityMetricsLaunchObserver observer) {
        mList.add(observer);
    }
 
    private void handleUnregisterLaunchObserver(ActivityMetricsLaunchObserver observer) {
        mList.remove(observer);
    }
 
    private void handleOnIntentStarted(Intent intent) {
        // Traverse start-to-end to meet the registerLaunchObserver multi-cast order guarantee.
        for (int i = 0; i < mList.size(); i++) {
             ActivityMetricsLaunchObserver o = mList.get(i);
             o.onIntentStarted(intent);
        }
    }
 
    private void handleOnIntentFailed() {
        // Traverse start-to-end to meet the registerLaunchObserver multi-cast order guarantee.
        for (int i = 0; i < mList.size(); i++) {
             ActivityMetricsLaunchObserver o = mList.get(i);
             o.onIntentFailed();
        }
    }
 
    private void handleOnActivityLaunched(
            @ActivityRecordProto byte[] activity,
            @Temperature int temperature) {
        // Traverse start-to-end to meet the registerLaunchObserver multi-cast order guarantee.
        for (int i = 0; i < mList.size(); i++) {
             ActivityMetricsLaunchObserver o = mList.get(i);
             o.onActivityLaunched(activity, temperature);
        }
    }
 
    private void handleOnActivityLaunchCancelled(
            @ActivityRecordProto byte[] activity) {
        // Traverse start-to-end to meet the registerLaunchObserver multi-cast order guarantee.
        for (int i = 0; i < mList.size(); i++) {
             ActivityMetricsLaunchObserver o = mList.get(i);
             o.onActivityLaunchCancelled(activity);
        }
    }
 
    private void handleOnActivityLaunchFinished(
            @ActivityRecordProto byte[] activity) {
        // Traverse start-to-end to meet the registerLaunchObserver multi-cast order guarantee.
        for (int i = 0; i < mList.size(); i++) {
            ActivityMetricsLaunchObserver o = mList.get(i);
            o.onActivityLaunchFinished(activity);
        }
    }
}