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 com.android.tv.settings.connectivity;
18
19import android.content.Context;
20import android.net.EthernetManager;
21import android.net.IpConfiguration;
22import android.net.wifi.WifiManager;
23import android.os.Parcelable;
24
25import com.android.tv.settings.R;
26
27/**
28 * Ethernet configuration that implements NetworkConfiguration.
29 */
30class EthernetConfig implements NetworkConfiguration {
31    private final EthernetManager mEthernetManager;
32    private IpConfiguration mIpConfiguration;
33    private final String mName;
34
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
42    public void setIpConfiguration(IpConfiguration configuration) {
43        mIpConfiguration = configuration;
44    }
45
46    @Override
47    public IpConfiguration getIpConfiguration() {
48        return mIpConfiguration;
49    }
50
51    @Override
52    public void save(WifiManager.ActionListener listener) {
53        mEthernetManager.setConfiguration(mIpConfiguration);
54
55        if (listener != null) {
56            listener.onSuccess();
57        }
58    }
59
60    /**
61     * Load IpConfiguration from system.
62     */
63    public void load() {
64        mIpConfiguration = mEthernetManager.getConfiguration();
65    }
66
67    @Override
68    public String getPrintableName() {
69        return mName;
70    }
71
72    @Override
73    public Parcelable toParcelable() {
74        return mIpConfiguration;
75    }
76
77    public void fromParcelable(Parcelable parcelable) {
78        if (parcelable instanceof IpConfiguration) {
79            mIpConfiguration = (IpConfiguration) parcelable;
80        } else {
81            throw new IllegalArgumentException("Invalid parcelable");
82        }
83    }
84
85    @Override
86    public int getNetworkType() {
87        return NetworkConfigurationFactory.TYPE_ETHERNET;
88    }
89}
90