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.tv.settings.connectivity;
18 
19 import android.content.Context;
20 import android.net.EthernetManager;
21 import android.net.IpConfiguration;
22 import android.net.wifi.WifiManager;
23 
24 import com.android.tv.settings.R;
25 
26 /**
27  * Ethernet configuration that implements NetworkConfiguration.
28  */
29 class EthernetConfig implements NetworkConfiguration {
30     private final EthernetManager mEthernetManager;
31     private IpConfiguration mIpConfiguration;
32     private final String mName;
33     private String mInterfaceName;
34 
EthernetConfig(Context context)35     EthernetConfig(Context context) {
36         mEthernetManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE);
37         mIpConfiguration = new IpConfiguration();
38         mName = context.getResources().getString(R.string.connectivity_ethernet);
39     }
40 
41     @Override
setIpConfiguration(IpConfiguration configuration)42     public void setIpConfiguration(IpConfiguration configuration) {
43         mIpConfiguration = configuration;
44     }
45 
46     @Override
getIpConfiguration()47     public IpConfiguration getIpConfiguration() {
48         return mIpConfiguration;
49     }
50 
51     @Override
save(WifiManager.ActionListener listener)52     public void save(WifiManager.ActionListener listener) {
53         if (mInterfaceName != null) {
54             mEthernetManager.setConfiguration(mInterfaceName, mIpConfiguration);
55         }
56 
57         if (listener != null) {
58             listener.onSuccess();
59         }
60     }
61 
62     /**
63      * Load IpConfiguration from system.
64      */
load()65     public void load() {
66         String[] ifaces = mEthernetManager.getAvailableInterfaces();
67         if (ifaces.length > 0) {
68             mInterfaceName = ifaces[0];
69             mIpConfiguration = mEthernetManager.getConfiguration(mInterfaceName);
70         }
71     }
72 
73     @Override
getPrintableName()74     public String getPrintableName() {
75         return mName;
76     }
77 
78 }
79