1 /* 2 * Copyright (C) 2014 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 libcore.net.event; 18 19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; 20 21 import android.annotation.SystemApi; 22 import android.compat.annotation.UnsupportedAppUsage; 23 import java.util.List; 24 import java.util.concurrent.CopyOnWriteArrayList; 25 26 /** 27 * A singleton used to dispatch network events to registered listeners. 28 * 29 * @hide 30 */ 31 @SystemApi(client = MODULE_LIBRARIES) 32 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE) 33 public final class NetworkEventDispatcher { 34 35 private static final NetworkEventDispatcher instance = new NetworkEventDispatcher(); 36 37 private final List<NetworkEventListener> listeners = 38 new CopyOnWriteArrayList<NetworkEventListener>(); 39 40 /** 41 * Returns the shared {@link NetworkEventDispatcher} instance. 42 * 43 * @return singleton instance of {@link NetworkEventDispatcher} 44 * 45 * @hide 46 */ 47 @UnsupportedAppUsage 48 @SystemApi(client = MODULE_LIBRARIES) 49 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE) getInstance()50 public static NetworkEventDispatcher getInstance() { 51 return instance; 52 } 53 54 /** @hide Visible for testing. Use {@link #getInstance()} instead. */ NetworkEventDispatcher()55 public NetworkEventDispatcher() { 56 } 57 58 /** 59 * Registers a listener to be notified when network events occur. 60 * It can be deregistered using {@link #removeListener(NetworkEventListener)} 61 * 62 * @hide 63 */ 64 @UnsupportedAppUsage addListener(NetworkEventListener toAdd)65 public void addListener(NetworkEventListener toAdd) { 66 if (toAdd == null) { 67 throw new NullPointerException("toAdd == null"); 68 } 69 listeners.add(toAdd); 70 } 71 72 /** 73 * De-registers a listener previously added with {@link #addListener(NetworkEventListener)}. If 74 * the listener was not previously registered this is a no-op. 75 * 76 * @hide 77 */ removeListener(NetworkEventListener toRemove)78 public void removeListener(NetworkEventListener toRemove) { 79 for (NetworkEventListener listener : listeners) { 80 if (listener == toRemove) { 81 listeners.remove(listener); 82 return; 83 } 84 } 85 } 86 87 /** 88 * Notifies registered listeners of a network configuration change. 89 * 90 * @hide 91 */ 92 @SystemApi(client = MODULE_LIBRARIES) 93 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE) dispatchNetworkConfigurationChange()94 public void dispatchNetworkConfigurationChange() { 95 for (NetworkEventListener listener : listeners) { 96 try { 97 listener.onNetworkConfigurationChanged(); 98 } catch (RuntimeException e) { 99 System.logI("Exception thrown during network event propagation", e); 100 } 101 } 102 } 103 } 104