• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.net;
18 
19 import android.annotation.SystemService;
20 import android.content.Context;
21 import android.os.Handler;
22 import android.os.Message;
23 import android.os.RemoteException;
24 
25 import java.util.ArrayList;
26 
27 /**
28  * A class representing the IP configuration of the Ethernet network.
29  *
30  * @hide
31  */
32 @SystemService(Context.ETHERNET_SERVICE)
33 public class EthernetManager {
34     private static final String TAG = "EthernetManager";
35     private static final int MSG_AVAILABILITY_CHANGED = 1000;
36 
37     private final Context mContext;
38     private final IEthernetManager mService;
39     private final Handler mHandler = new Handler() {
40         @Override
41         public void handleMessage(Message msg) {
42             if (msg.what == MSG_AVAILABILITY_CHANGED) {
43                 boolean isAvailable = (msg.arg1 == 1);
44                 for (Listener listener : mListeners) {
45                     listener.onAvailabilityChanged((String) msg.obj, isAvailable);
46                 }
47             }
48         }
49     };
50     private final ArrayList<Listener> mListeners = new ArrayList<>();
51     private final IEthernetServiceListener.Stub mServiceListener =
52             new IEthernetServiceListener.Stub() {
53                 @Override
54                 public void onAvailabilityChanged(String iface, boolean isAvailable) {
55                     mHandler.obtainMessage(
56                             MSG_AVAILABILITY_CHANGED, isAvailable ? 1 : 0, 0, iface).sendToTarget();
57                 }
58             };
59 
60     /**
61      * A listener interface to receive notification on changes in Ethernet.
62      */
63     public interface Listener {
64         /**
65          * Called when Ethernet port's availability is changed.
66          * @param iface Ethernet interface name
67          * @param isAvailable {@code true} if Ethernet port exists.
68          */
onAvailabilityChanged(String iface, boolean isAvailable)69         void onAvailabilityChanged(String iface, boolean isAvailable);
70     }
71 
72     /**
73      * Create a new EthernetManager instance.
74      * Applications will almost always want to use
75      * {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
76      * the standard {@link android.content.Context#ETHERNET_SERVICE Context.ETHERNET_SERVICE}.
77      */
EthernetManager(Context context, IEthernetManager service)78     public EthernetManager(Context context, IEthernetManager service) {
79         mContext = context;
80         mService = service;
81     }
82 
83     /**
84      * Get Ethernet configuration.
85      * @return the Ethernet Configuration, contained in {@link IpConfiguration}.
86      */
getConfiguration(String iface)87     public IpConfiguration getConfiguration(String iface) {
88         try {
89             return mService.getConfiguration(iface);
90         } catch (RemoteException e) {
91             throw e.rethrowFromSystemServer();
92         }
93     }
94 
95     /**
96      * Set Ethernet configuration.
97      */
setConfiguration(String iface, IpConfiguration config)98     public void setConfiguration(String iface, IpConfiguration config) {
99         try {
100             mService.setConfiguration(iface, config);
101         } catch (RemoteException e) {
102             throw e.rethrowFromSystemServer();
103         }
104     }
105 
106     /**
107      * Indicates whether the system currently has one or more Ethernet interfaces.
108      */
isAvailable()109     public boolean isAvailable() {
110         return getAvailableInterfaces().length > 0;
111     }
112 
113     /**
114      * Indicates whether the system has given interface.
115      *
116      * @param iface Ethernet interface name
117      */
isAvailable(String iface)118     public boolean isAvailable(String iface) {
119         try {
120             return mService.isAvailable(iface);
121         } catch (RemoteException e) {
122             throw e.rethrowFromSystemServer();
123         }
124     }
125 
126     /**
127      * Adds a listener.
128      * @param listener A {@link Listener} to add.
129      * @throws IllegalArgumentException If the listener is null.
130      */
addListener(Listener listener)131     public void addListener(Listener listener) {
132         if (listener == null) {
133             throw new IllegalArgumentException("listener must not be null");
134         }
135         mListeners.add(listener);
136         if (mListeners.size() == 1) {
137             try {
138                 mService.addListener(mServiceListener);
139             } catch (RemoteException e) {
140                 throw e.rethrowFromSystemServer();
141             }
142         }
143     }
144 
145     /**
146      * Returns an array of available Ethernet interface names.
147      */
getAvailableInterfaces()148     public String[] getAvailableInterfaces() {
149         try {
150             return mService.getAvailableInterfaces();
151         } catch (RemoteException e) {
152             throw e.rethrowAsRuntimeException();
153         }
154     }
155 
156     /**
157      * Removes a listener.
158      * @param listener A {@link Listener} to remove.
159      * @throws IllegalArgumentException If the listener is null.
160      */
removeListener(Listener listener)161     public void removeListener(Listener listener) {
162         if (listener == null) {
163             throw new IllegalArgumentException("listener must not be null");
164         }
165         mListeners.remove(listener);
166         if (mListeners.isEmpty()) {
167             try {
168                 mService.removeListener(mServiceListener);
169             } catch (RemoteException e) {
170                 throw e.rethrowFromSystemServer();
171             }
172         }
173     }
174 }
175