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.IpConfiguration;
21import android.net.wifi.WifiConfiguration;
22import android.net.wifi.WifiManager;
23import android.os.Parcelable;
24
25/**
26 * Wi-Fi configuration that implements NetworkConfiguration.
27 */
28class WifiConfig implements NetworkConfiguration {
29    private final WifiManager mWifiManager;
30    private WifiConfiguration mWifiConfiguration;
31
32    WifiConfig(Context context) {
33        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
34        mWifiConfiguration = new WifiConfiguration();
35    }
36
37    @Override
38    public void setIpConfiguration(IpConfiguration configuration) {
39        mWifiConfiguration.setIpConfiguration(configuration);
40    }
41
42    @Override
43    public IpConfiguration getIpConfiguration() {
44        return mWifiConfiguration.getIpConfiguration();
45    }
46
47    @Override
48    public void save(WifiManager.ActionListener listener) {
49        mWifiManager.save(mWifiConfiguration, listener);
50    }
51
52    /**
53     * Load IpConfiguration from system with the given networkId.
54     */
55    public void load(int networkId) {
56        mWifiConfiguration = WifiConfigHelper.getWifiConfiguration(mWifiManager, networkId);
57    }
58
59    @Override
60    public String getPrintableName() {
61        return mWifiConfiguration.getPrintableSsid();
62    }
63
64    @Override
65    public Parcelable toParcelable() {
66        return mWifiConfiguration;
67    }
68
69    @Override
70    public void fromParcelable(Parcelable parcelable) {
71        if (parcelable instanceof WifiConfiguration) {
72            mWifiConfiguration = (WifiConfiguration) parcelable;
73        } else {
74            throw new IllegalArgumentException("Invalid parcelable");
75        }
76    }
77
78    @Override
79    public int getNetworkType() {
80        return NetworkConfigurationFactory.TYPE_WIFI;
81    }
82}
83