1 /* 2 * Copyright (C) 2019 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.net; 18 19 import android.annotation.Hide; 20 import android.annotation.NonNull; 21 import android.os.Binder; 22 import android.os.RemoteException; 23 import android.util.Log; 24 25 /** 26 * A convenience wrapper for INetworkMonitor. 27 * 28 * Wraps INetworkMonitor calls, making them a bit more friendly to use. Currently handles: 29 * - Clearing calling identity 30 * - Ignoring RemoteExceptions 31 * - Converting to stable parcelables 32 * 33 * By design, all methods on INetworkMonitor are asynchronous oneway IPCs and are thus void. All the 34 * wrapper methods in this class return a boolean that callers can use to determine whether 35 * RemoteException was thrown. 36 */ 37 @Hide 38 public class NetworkMonitorManager { 39 40 @NonNull private final INetworkMonitor mNetworkMonitor; 41 @NonNull private final String mTag; 42 NetworkMonitorManager(@onNull INetworkMonitor networkMonitorManager, @NonNull String tag)43 public NetworkMonitorManager(@NonNull INetworkMonitor networkMonitorManager, 44 @NonNull String tag) { 45 mNetworkMonitor = networkMonitorManager; 46 mTag = tag; 47 } 48 NetworkMonitorManager(@onNull INetworkMonitor networkMonitorManager)49 public NetworkMonitorManager(@NonNull INetworkMonitor networkMonitorManager) { 50 this(networkMonitorManager, NetworkMonitorManager.class.getSimpleName()); 51 } 52 log(String s, Throwable e)53 private void log(String s, Throwable e) { 54 Log.e(mTag, s, e); 55 } 56 57 // CHECKSTYLE:OFF Generated code 58 start()59 public boolean start() { 60 final long token = Binder.clearCallingIdentity(); 61 try { 62 mNetworkMonitor.start(); 63 return true; 64 } catch (RemoteException e) { 65 log("Error in start", e); 66 return false; 67 } finally { 68 Binder.restoreCallingIdentity(token); 69 } 70 } 71 launchCaptivePortalApp()72 public boolean launchCaptivePortalApp() { 73 final long token = Binder.clearCallingIdentity(); 74 try { 75 mNetworkMonitor.launchCaptivePortalApp(); 76 return true; 77 } catch (RemoteException e) { 78 log("Error in launchCaptivePortalApp", e); 79 return false; 80 } finally { 81 Binder.restoreCallingIdentity(token); 82 } 83 } 84 notifyCaptivePortalAppFinished(int response)85 public boolean notifyCaptivePortalAppFinished(int response) { 86 final long token = Binder.clearCallingIdentity(); 87 try { 88 mNetworkMonitor.notifyCaptivePortalAppFinished(response); 89 return true; 90 } catch (RemoteException e) { 91 log("Error in notifyCaptivePortalAppFinished", e); 92 return false; 93 } finally { 94 Binder.restoreCallingIdentity(token); 95 } 96 } 97 setAcceptPartialConnectivity()98 public boolean setAcceptPartialConnectivity() { 99 final long token = Binder.clearCallingIdentity(); 100 try { 101 mNetworkMonitor.setAcceptPartialConnectivity(); 102 return true; 103 } catch (RemoteException e) { 104 log("Error in setAcceptPartialConnectivity", e); 105 return false; 106 } finally { 107 Binder.restoreCallingIdentity(token); 108 } 109 } 110 forceReevaluation(int uid)111 public boolean forceReevaluation(int uid) { 112 final long token = Binder.clearCallingIdentity(); 113 try { 114 mNetworkMonitor.forceReevaluation(uid); 115 return true; 116 } catch (RemoteException e) { 117 log("Error in forceReevaluation", e); 118 return false; 119 } finally { 120 Binder.restoreCallingIdentity(token); 121 } 122 } 123 notifyPrivateDnsChanged(PrivateDnsConfigParcel config)124 public boolean notifyPrivateDnsChanged(PrivateDnsConfigParcel config) { 125 final long token = Binder.clearCallingIdentity(); 126 try { 127 mNetworkMonitor.notifyPrivateDnsChanged(config); 128 return true; 129 } catch (RemoteException e) { 130 log("Error in notifyPrivateDnsChanged", e); 131 return false; 132 } finally { 133 Binder.restoreCallingIdentity(token); 134 } 135 } 136 notifyDnsResponse(int returnCode)137 public boolean notifyDnsResponse(int returnCode) { 138 final long token = Binder.clearCallingIdentity(); 139 try { 140 mNetworkMonitor.notifyDnsResponse(returnCode); 141 return true; 142 } catch (RemoteException e) { 143 log("Error in notifyDnsResponse", e); 144 return false; 145 } finally { 146 Binder.restoreCallingIdentity(token); 147 } 148 } 149 notifyNetworkConnected(LinkProperties lp, NetworkCapabilities nc)150 public boolean notifyNetworkConnected(LinkProperties lp, NetworkCapabilities nc) { 151 final long token = Binder.clearCallingIdentity(); 152 try { 153 mNetworkMonitor.notifyNetworkConnected(lp, nc); 154 return true; 155 } catch (RemoteException e) { 156 log("Error in notifyNetworkConnected", e); 157 return false; 158 } finally { 159 Binder.restoreCallingIdentity(token); 160 } 161 } 162 notifyNetworkDisconnected()163 public boolean notifyNetworkDisconnected() { 164 final long token = Binder.clearCallingIdentity(); 165 try { 166 mNetworkMonitor.notifyNetworkDisconnected(); 167 return true; 168 } catch (RemoteException e) { 169 log("Error in notifyNetworkDisconnected", e); 170 return false; 171 } finally { 172 Binder.restoreCallingIdentity(token); 173 } 174 } 175 notifyLinkPropertiesChanged(LinkProperties lp)176 public boolean notifyLinkPropertiesChanged(LinkProperties lp) { 177 final long token = Binder.clearCallingIdentity(); 178 try { 179 mNetworkMonitor.notifyLinkPropertiesChanged(lp); 180 return true; 181 } catch (RemoteException e) { 182 log("Error in notifyLinkPropertiesChanged", e); 183 return false; 184 } finally { 185 Binder.restoreCallingIdentity(token); 186 } 187 } 188 notifyNetworkCapabilitiesChanged(NetworkCapabilities nc)189 public boolean notifyNetworkCapabilitiesChanged(NetworkCapabilities nc) { 190 final long token = Binder.clearCallingIdentity(); 191 try { 192 mNetworkMonitor.notifyNetworkCapabilitiesChanged(nc); 193 return true; 194 } catch (RemoteException e) { 195 log("Error in notifyNetworkCapabilitiesChanged", e); 196 return false; 197 } finally { 198 Binder.restoreCallingIdentity(token); 199 } 200 } 201 202 // CHECKSTYLE:ON Generated code 203 } 204