165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.connectivity;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.IpConfiguration;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiConfiguration;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiManager;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Parcelable;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Wi-Fi configuration that implements NetworkConfiguration.
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneclass WifiConfig implements NetworkConfiguration {
296e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantler    private final WifiManager mWifiManager;
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private WifiConfiguration mWifiConfiguration;
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    WifiConfig(Context context) {
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mWifiConfiguration = new WifiConfiguration();
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void setIpConfiguration(IpConfiguration configuration) {
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mWifiConfiguration.setIpConfiguration(configuration);
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public IpConfiguration getIpConfiguration() {
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mWifiConfiguration.getIpConfiguration();
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void save(WifiManager.ActionListener listener) {
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mWifiManager.save(mWifiConfiguration, listener);
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    /**
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * Load IpConfiguration from system with the given networkId.
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     */
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void load(int networkId) {
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mWifiConfiguration = WifiConfigHelper.getWifiConfiguration(mWifiManager, networkId);
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public String getPrintableName() {
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mWifiConfiguration.getPrintableSsid();
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public Parcelable toParcelable() {
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mWifiConfiguration;
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void fromParcelable(Parcelable parcelable) {
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (parcelable instanceof WifiConfiguration) {
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mWifiConfiguration = (WifiConfiguration) parcelable;
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            throw new IllegalArgumentException("Invalid parcelable");
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public int getNetworkType() {
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return NetworkConfigurationFactory.TYPE_WIFI;
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
83