Lorenzo Colitti | 20c1c99 | 2014-05-20 16:58:34 -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 com.android.server.ethernet; |
| 18 | |
| 19 | import android.net.IpConfiguration; |
| 20 | import android.net.IpConfiguration.IpAssignment; |
| 21 | import android.net.IpConfiguration.ProxySettings; |
Lorenzo Colitti | 20c1c99 | 2014-05-20 16:58:34 -0700 | [diff] [blame] | 22 | import android.os.Environment; |
| 23 | import android.util.Log; |
| 24 | import android.util.SparseArray; |
| 25 | |
| 26 | import com.android.server.net.IpConfigStore; |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * This class provides an API to store and manage Ethernet network configuration. |
| 31 | */ |
| 32 | public class EthernetConfigStore extends IpConfigStore { |
| 33 | private static final String TAG = "EthernetConfigStore"; |
| 34 | |
| 35 | private static final String ipConfigFile = Environment.getDataDirectory() + |
| 36 | "/misc/ethernet/ipconfig.txt"; |
| 37 | |
| 38 | public EthernetConfigStore() { |
| 39 | } |
| 40 | |
| 41 | public IpConfiguration readIpAndProxyConfigurations() { |
| 42 | SparseArray<IpConfiguration> networks = readIpAndProxyConfigurations(ipConfigFile); |
| 43 | |
| 44 | if (networks.size() == 0) { |
| 45 | Log.w(TAG, "No Ethernet configuration found. Using default."); |
Lorenzo Colitti | 41a372f | 2014-08-12 10:49:39 +0900 | [diff] [blame] | 46 | return new IpConfiguration(IpAssignment.DHCP, ProxySettings.NONE, null, null); |
Lorenzo Colitti | 20c1c99 | 2014-05-20 16:58:34 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | if (networks.size() > 1) { |
| 50 | // Currently we only support a single Ethernet interface. |
| 51 | Log.w(TAG, "Multiple Ethernet configurations detected. Only reading first one."); |
| 52 | } |
| 53 | |
| 54 | return networks.valueAt(0); |
| 55 | } |
| 56 | |
| 57 | public void writeIpAndProxyConfigurations(IpConfiguration config) { |
| 58 | SparseArray<IpConfiguration> networks = new SparseArray<IpConfiguration>(); |
| 59 | networks.put(0, config); |
| 60 | writeIpAndProxyConfigurations(ipConfigFile, networks); |
| 61 | } |
| 62 | } |