Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 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.content.Context; |
| 20 | import android.net.IEthernetManager; |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 21 | import android.net.IEthernetServiceListener; |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 22 | import android.net.IpConfiguration; |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.Message; |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 29 | /** |
| 30 | * A class representing the IP configuration of the Ethernet network. |
| 31 | * |
| 32 | * @hide |
| 33 | */ |
| 34 | public class EthernetManager { |
| 35 | private static final String TAG = "EthernetManager"; |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 36 | private static final int MSG_AVAILABILITY_CHANGED = 1000; |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 37 | |
| 38 | private final Context mContext; |
| 39 | private final IEthernetManager mService; |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 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(isAvailable); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | }; |
| 51 | private final ArrayList<Listener> mListeners = new ArrayList<Listener>(); |
| 52 | private final IEthernetServiceListener.Stub mServiceListener = |
| 53 | new IEthernetServiceListener.Stub() { |
| 54 | @Override |
| 55 | public void onAvailabilityChanged(boolean isAvailable) { |
| 56 | mHandler.obtainMessage( |
| 57 | MSG_AVAILABILITY_CHANGED, isAvailable ? 1 : 0, 0, null).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 isAvailable {@code true} if one or more Ethernet port exists. |
| 68 | */ |
| 69 | public void onAvailabilityChanged(boolean isAvailable); |
| 70 | } |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 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 | */ |
| 78 | public EthernetManager(Context context, IEthernetManager service) { |
| 79 | mContext = context; |
| 80 | mService = service; |
| 81 | } |
| 82 | |
| 83 | /** |
Lorenzo Colitti | 9c1ccc5 | 2014-05-22 11:51:27 -0700 | [diff] [blame] | 84 | * Get Ethernet configuration. |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 85 | * @return the Ethernet Configuration, contained in {@link IpConfiguration}. |
| 86 | */ |
| 87 | public IpConfiguration getConfiguration() { |
| 88 | try { |
| 89 | return mService.getConfiguration(); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 90 | } catch (RemoteException e) { |
| 91 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
Lorenzo Colitti | 9c1ccc5 | 2014-05-22 11:51:27 -0700 | [diff] [blame] | 96 | * Set Ethernet configuration. |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 97 | */ |
| 98 | public void setConfiguration(IpConfiguration config) { |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 99 | try { |
| 100 | mService.setConfiguration(config); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 101 | } catch (RemoteException e) { |
| 102 | throw e.rethrowFromSystemServer(); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Indicates whether the system currently has one or more |
| 108 | * Ethernet interfaces. |
| 109 | */ |
| 110 | public boolean isAvailable() { |
| 111 | try { |
| 112 | return mService.isAvailable(); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 113 | } catch (RemoteException e) { |
| 114 | throw e.rethrowFromSystemServer(); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Adds a listener. |
| 120 | * @param listener A {@link Listener} to add. |
| 121 | * @throws IllegalArgumentException If the listener is null. |
| 122 | */ |
| 123 | public void addListener(Listener listener) { |
| 124 | if (listener == null) { |
| 125 | throw new IllegalArgumentException("listener must not be null"); |
| 126 | } |
| 127 | mListeners.add(listener); |
| 128 | if (mListeners.size() == 1) { |
| 129 | try { |
| 130 | mService.addListener(mServiceListener); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 131 | } catch (RemoteException e) { |
| 132 | throw e.rethrowFromSystemServer(); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Removes a listener. |
| 139 | * @param listener A {@link Listener} to remove. |
| 140 | * @throws IllegalArgumentException If the listener is null. |
| 141 | */ |
| 142 | public void removeListener(Listener listener) { |
| 143 | if (listener == null) { |
| 144 | throw new IllegalArgumentException("listener must not be null"); |
| 145 | } |
| 146 | mListeners.remove(listener); |
| 147 | if (mListeners.isEmpty()) { |
| 148 | try { |
| 149 | mService.removeListener(mServiceListener); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 150 | } catch (RemoteException e) { |
| 151 | throw e.rethrowFromSystemServer(); |
Jaewan Kim | d109a7c | 2014-10-20 12:04:13 +0900 | [diff] [blame] | 152 | } |
Lorenzo Colitti | 4e5aa2c | 2014-05-21 16:23:43 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |