14e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti/*
24e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * Copyright (C) 2014 The Android Open Source Project
34e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti *
44e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * Licensed under the Apache License, Version 2.0 (the "License");
54e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * you may not use this file except in compliance with the License.
64e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * You may obtain a copy of the License at
74e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti *
84e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti *      http://www.apache.org/licenses/LICENSE-2.0
94e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti *
104e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * Unless required by applicable law or agreed to in writing, software
114e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * distributed under the License is distributed on an "AS IS" BASIS,
124e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * See the License for the specific language governing permissions and
144e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * limitations under the License.
154e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti */
164e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti
174e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colittipackage android.net;
184e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti
194e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colittiimport android.content.Context;
204e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colittiimport android.net.IEthernetManager;
21d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kimimport android.net.IEthernetServiceListener;
224e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colittiimport android.net.IpConfiguration;
23d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kimimport android.os.Handler;
24d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kimimport android.os.Message;
254e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colittiimport android.os.RemoteException;
264e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti
27d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kimimport java.util.ArrayList;
28d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim
294e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti/**
304e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * A class representing the IP configuration of the Ethernet network.
314e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti *
324e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti * @hide
334e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti */
344e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colittipublic class EthernetManager {
354e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    private static final String TAG = "EthernetManager";
36d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    private static final int MSG_AVAILABILITY_CHANGED = 1000;
374e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti
384e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    private final Context mContext;
394e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    private final IEthernetManager mService;
40d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    private final Handler mHandler = new Handler() {
41d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        @Override
42d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        public void handleMessage(Message msg) {
43d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            if (msg.what == MSG_AVAILABILITY_CHANGED) {
44d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                boolean isAvailable = (msg.arg1 == 1);
45d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                for (Listener listener : mListeners) {
46d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                    listener.onAvailabilityChanged(isAvailable);
47d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                }
48d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            }
49d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        }
50d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    };
51d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    private final ArrayList<Listener> mListeners = new ArrayList<Listener>();
52d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    private final IEthernetServiceListener.Stub mServiceListener =
53d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            new IEthernetServiceListener.Stub() {
54d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                @Override
55d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                public void onAvailabilityChanged(boolean isAvailable) {
56d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                    mHandler.obtainMessage(
57d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                            MSG_AVAILABILITY_CHANGED, isAvailable ? 1 : 0, 0, null).sendToTarget();
58d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                }
59d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            };
60d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim
61d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    /**
62d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     * A listener interface to receive notification on changes in Ethernet.
63d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     */
64d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    public interface Listener {
65d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        /**
66d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim         * Called when Ethernet port's availability is changed.
67d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim         * @param isAvailable {@code true} if one or more Ethernet port exists.
68d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim         */
69d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        public void onAvailabilityChanged(boolean isAvailable);
70d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    }
714e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti
724e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    /**
734e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti     * Create a new EthernetManager instance.
744e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti     * Applications will almost always want to use
754e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti     * {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
764e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti     * the standard {@link android.content.Context#ETHERNET_SERVICE Context.ETHERNET_SERVICE}.
774e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti     */
784e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    public EthernetManager(Context context, IEthernetManager service) {
794e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti        mContext = context;
804e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti        mService = service;
814e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    }
824e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti
834e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    /**
849c1ccc54a582d0ae4230dde249ba439921165e12Lorenzo Colitti     * Get Ethernet configuration.
854e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti     * @return the Ethernet Configuration, contained in {@link IpConfiguration}.
864e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti     */
874e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    public IpConfiguration getConfiguration() {
884e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti        try {
894e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti            return mService.getConfiguration();
90d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        } catch (NullPointerException | RemoteException e) {
910a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti            return new IpConfiguration();
924e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti        }
934e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    }
944e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti
954e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    /**
969c1ccc54a582d0ae4230dde249ba439921165e12Lorenzo Colitti     * Set Ethernet configuration.
974e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti     */
984e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    public void setConfiguration(IpConfiguration config) {
994e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti        try {
1004e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti            mService.setConfiguration(config);
101d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        } catch (NullPointerException | RemoteException e) {
102d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        }
103d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    }
104d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim
105d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    /**
106d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     * Indicates whether the system currently has one or more
107d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     * Ethernet interfaces.
108d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     */
109d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    public boolean isAvailable() {
110d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        try {
111d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            return mService.isAvailable();
112d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        } catch (NullPointerException | RemoteException e) {
113d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            return false;
114d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        }
115d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    }
116d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim
117d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    /**
118d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     * Adds a listener.
119d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     * @param listener A {@link Listener} to add.
120d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     * @throws IllegalArgumentException If the listener is null.
121d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     */
122d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    public void addListener(Listener listener) {
123d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        if (listener == null) {
124d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            throw new IllegalArgumentException("listener must not be null");
125d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        }
126d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        mListeners.add(listener);
127d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        if (mListeners.size() == 1) {
128d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            try {
129d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                mService.addListener(mServiceListener);
130d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            } catch (NullPointerException | RemoteException e) {
131d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            }
132d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        }
133d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    }
134d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim
135d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    /**
136d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     * Removes a listener.
137d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     * @param listener A {@link Listener} to remove.
138d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     * @throws IllegalArgumentException If the listener is null.
139d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim     */
140d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim    public void removeListener(Listener listener) {
141d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        if (listener == null) {
142d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            throw new IllegalArgumentException("listener must not be null");
143d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        }
144d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        mListeners.remove(listener);
145d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim        if (mListeners.isEmpty()) {
146d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            try {
147d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim                mService.removeListener(mServiceListener);
148d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            } catch (NullPointerException | RemoteException e) {
149d109a7cf695b1d208bd69ddb013b9fbdd5658255Jaewan Kim            }
1504e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti        }
1514e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti    }
1524e5aa2cee69f6791f8001aeb1fc12389863fab8fLorenzo Colitti}
153