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