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
17package android.net;
18
19import android.annotation.SystemService;
20import android.content.Context;
21import android.net.IEthernetManager;
22import android.net.IEthernetServiceListener;
23import android.net.IpConfiguration;
24import android.os.Handler;
25import android.os.Message;
26import android.os.RemoteException;
27
28import java.util.ArrayList;
29
30/**
31 * A class representing the IP configuration of the Ethernet network.
32 *
33 * @hide
34 */
35@SystemService(Context.ETHERNET_SERVICE)
36public class EthernetManager {
37    private static final String TAG = "EthernetManager";
38    private static final int MSG_AVAILABILITY_CHANGED = 1000;
39
40    private final Context mContext;
41    private final IEthernetManager mService;
42    private final Handler mHandler = new Handler() {
43        @Override
44        public void handleMessage(Message msg) {
45            if (msg.what == MSG_AVAILABILITY_CHANGED) {
46                boolean isAvailable = (msg.arg1 == 1);
47                for (Listener listener : mListeners) {
48                    listener.onAvailabilityChanged(isAvailable);
49                }
50            }
51        }
52    };
53    private final ArrayList<Listener> mListeners = new ArrayList<Listener>();
54    private final IEthernetServiceListener.Stub mServiceListener =
55            new IEthernetServiceListener.Stub() {
56                @Override
57                public void onAvailabilityChanged(boolean isAvailable) {
58                    mHandler.obtainMessage(
59                            MSG_AVAILABILITY_CHANGED, isAvailable ? 1 : 0, 0, null).sendToTarget();
60                }
61            };
62
63    /**
64     * A listener interface to receive notification on changes in Ethernet.
65     */
66    public interface Listener {
67        /**
68         * Called when Ethernet port's availability is changed.
69         * @param isAvailable {@code true} if one or more Ethernet port exists.
70         */
71        public void onAvailabilityChanged(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    public IpConfiguration getConfiguration() {
90        try {
91            return mService.getConfiguration();
92        } catch (RemoteException e) {
93            throw e.rethrowFromSystemServer();
94        }
95    }
96
97    /**
98     * Set Ethernet configuration.
99     */
100    public void setConfiguration(IpConfiguration config) {
101        try {
102            mService.setConfiguration(config);
103        } catch (RemoteException e) {
104            throw e.rethrowFromSystemServer();
105        }
106    }
107
108    /**
109     * Indicates whether the system currently has one or more
110     * Ethernet interfaces.
111     */
112    public boolean isAvailable() {
113        try {
114            return mService.isAvailable();
115        } catch (RemoteException e) {
116            throw e.rethrowFromSystemServer();
117        }
118    }
119
120    /**
121     * Adds a listener.
122     * @param listener A {@link Listener} to add.
123     * @throws IllegalArgumentException If the listener is null.
124     */
125    public void addListener(Listener listener) {
126        if (listener == null) {
127            throw new IllegalArgumentException("listener must not be null");
128        }
129        mListeners.add(listener);
130        if (mListeners.size() == 1) {
131            try {
132                mService.addListener(mServiceListener);
133            } catch (RemoteException e) {
134                throw e.rethrowFromSystemServer();
135            }
136        }
137    }
138
139    /**
140     * Removes a listener.
141     * @param listener A {@link Listener} to remove.
142     * @throws IllegalArgumentException If the listener is null.
143     */
144    public void removeListener(Listener listener) {
145        if (listener == null) {
146            throw new IllegalArgumentException("listener must not be null");
147        }
148        mListeners.remove(listener);
149        if (mListeners.isEmpty()) {
150            try {
151                mService.removeListener(mServiceListener);
152            } catch (RemoteException e) {
153                throw e.rethrowFromSystemServer();
154            }
155        }
156    }
157}
158