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.app.Activity;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiManager;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
236e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantlerimport com.android.tv.settings.connectivity.setup.MessageWizardFragment;
246e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantler
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Saves a wifi network's configuration.
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class SaveWifiConfigurationFragment extends MessageWizardFragment
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        implements WifiManager.ActionListener {
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public interface Listener {
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        void onSaveWifiConfigurationCompleted(int result);
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int RESULT_SUCCESS = 0;
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int RESULT_FAILURE = 1;
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_NETWORK_TYPE = "networkType";
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CONFIGURATION = "configuration";
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static SaveWifiConfigurationFragment newInstance(String title,
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            NetworkConfiguration configuration) {
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        SaveWifiConfigurationFragment fragment = new SaveWifiConfigurationFragment();
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle args = new Bundle();
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putInt(EXTRA_NETWORK_TYPE, configuration.getNetworkType());
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putParcelable(EXTRA_CONFIGURATION, configuration.toParcelable());
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        addArguments(args, title, true);
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        fragment.setArguments(args);
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return fragment;
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private Listener mListener;
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onAttach(Activity activity) {
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (activity instanceof Listener) {
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mListener = (Listener) activity;
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            throw new IllegalArgumentException("Activity must implement "
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    + "SaveWifiConfigurationFragment.Listener to use this fragment.");
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onAttach(activity);
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onDetach() {
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onDetach();
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mListener = null;
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onCreate(Bundle icicle) {
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onCreate(icicle);
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int networkType = getArguments().getInt(EXTRA_NETWORK_TYPE);
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        NetworkConfiguration configuration =
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                NetworkConfigurationFactory.createNetworkConfiguration(getActivity(), networkType);
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        configuration.fromParcelable(getArguments().getParcelable(EXTRA_CONFIGURATION));
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        configuration.save(this);
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onSuccess() {
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mListener != null) {
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mListener.onSaveWifiConfigurationCompleted(RESULT_SUCCESS);
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onFailure(int reason) {
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mListener != null) {
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mListener.onSaveWifiConfigurationCompleted(RESULT_FAILURE);
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
96