• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.content;
18 
19 import android.annotation.NonNull;
20 import android.content.res.Configuration;
21 
22 import com.android.internal.annotations.GuardedBy;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.function.Consumer;
27 
28 /**
29  * A helper class to manage {@link ComponentCallbacks} and {@link ComponentCallbacks2}, such as
30  * registering ,unregistering {@link ComponentCallbacks} and sending callbacks to all registered
31  * {@link ComponentCallbacks}.
32  *
33  * @see Context#registerComponentCallbacks(ComponentCallbacks)
34  * @see Context#unregisterComponentCallbacks(ComponentCallbacks)
35  * @see ComponentCallbacks
36  * @see ComponentCallbacks2
37  *
38  * @hide
39  */
40 public class ComponentCallbacksController {
41     @GuardedBy("mLock")
42     private List<ComponentCallbacks> mComponentCallbacks;
43 
44     private final Object mLock = new Object();
45 
46     /**
47      * Register the {@link ComponentCallbacks}.
48      *
49      * @see Context#registerComponentCallbacks(ComponentCallbacks)
50      */
registerCallbacks(@onNull ComponentCallbacks callbacks)51     public void registerCallbacks(@NonNull ComponentCallbacks callbacks) {
52         synchronized (mLock) {
53             if (mComponentCallbacks == null) {
54                 mComponentCallbacks = new ArrayList<>();
55             }
56             mComponentCallbacks.add(callbacks);
57         }
58     }
59 
60     /**
61      * Unregister the {@link ComponentCallbacks}.
62      *
63      * @see Context#unregisterComponentCallbacks(ComponentCallbacks)
64      */
unregisterCallbacks(@onNull ComponentCallbacks callbacks)65     public void unregisterCallbacks(@NonNull ComponentCallbacks callbacks) {
66         synchronized (mLock) {
67             if (mComponentCallbacks == null || mComponentCallbacks.isEmpty()) {
68                 return;
69             }
70             mComponentCallbacks.remove(callbacks);
71         }
72     }
73 
74     /**
75      * Clear all registered {@link ComponentCallbacks}.
76      * It is useful when the associated {@link Context} is going to be released.
77      */
clearCallbacks()78     public void clearCallbacks() {
79         synchronized (mLock) {
80             if (mComponentCallbacks != null) {
81                 mComponentCallbacks.clear();
82             }
83         }
84     }
85 
86     /**
87      * Sending {@link ComponentCallbacks#onConfigurationChanged(Configuration)} to all registered
88      * {@link ComponentCallbacks}.
89      */
dispatchConfigurationChanged(@onNull Configuration newConfig)90     public void dispatchConfigurationChanged(@NonNull Configuration newConfig) {
91         forAllComponentCallbacks(callbacks -> callbacks.onConfigurationChanged(newConfig));
92     }
93 
94     /**
95      * Sending {@link ComponentCallbacks#onLowMemory()} to all registered
96      * {@link ComponentCallbacks}.
97      */
dispatchLowMemory()98     public void dispatchLowMemory() {
99         forAllComponentCallbacks(ComponentCallbacks::onLowMemory);
100     }
101 
102     /**
103      * Sending {@link ComponentCallbacks2#onTrimMemory(int)} to all registered
104      * {@link ComponentCallbacks2}.
105      */
dispatchTrimMemory(int level)106     public void dispatchTrimMemory(int level) {
107         forAllComponentCallbacks(callbacks -> {
108             if (callbacks instanceof ComponentCallbacks2) {
109                 ((ComponentCallbacks2) callbacks).onTrimMemory(level);
110             }
111         });
112     }
113 
forAllComponentCallbacks(Consumer<ComponentCallbacks> callbacksConsumer)114     private void forAllComponentCallbacks(Consumer<ComponentCallbacks> callbacksConsumer) {
115         final ComponentCallbacks[] callbacksArray;
116         synchronized (mLock) {
117             if (mComponentCallbacks == null || mComponentCallbacks.isEmpty()) {
118                 return;
119             }
120             callbacksArray = new ComponentCallbacks[mComponentCallbacks.size()];
121             mComponentCallbacks.toArray(callbacksArray);
122         }
123         for (ComponentCallbacks callbacks : callbacksArray) {
124             callbacksConsumer.accept(callbacks);
125         }
126     }
127 }
128