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