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