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 com.android.tv.settings.connectivity.setup.MessageWizardFragment; 20 import com.android.tv.settings.form.FormPage; 21 22 import android.app.Activity; 23 import android.app.Fragment; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.net.wifi.WifiManager; 27 import android.os.Bundle; 28 29 /** 30 * Saves a wifi network's configuration. 31 */ 32 public class SaveWifiConfigurationFragment extends MessageWizardFragment 33 implements WifiManager.ActionListener { 34 35 public interface Listener { onSaveWifiConfigurationCompleted(int result)36 void onSaveWifiConfigurationCompleted(int result); 37 } 38 39 public static final int RESULT_SUCCESS = 0; 40 public static final int RESULT_FAILURE = 1; 41 42 private static final String EXTRA_NETWORK_TYPE = "networkType"; 43 private static final String EXTRA_CONFIGURATION = "configuration"; 44 newInstance(String title, NetworkConfiguration configuration)45 public static SaveWifiConfigurationFragment newInstance(String title, 46 NetworkConfiguration configuration) { 47 SaveWifiConfigurationFragment fragment = new SaveWifiConfigurationFragment(); 48 Bundle args = new Bundle(); 49 args.putInt(EXTRA_NETWORK_TYPE, configuration.getNetworkType()); 50 args.putParcelable(EXTRA_CONFIGURATION, configuration.toParcelable()); 51 addArguments(args, title, true); 52 fragment.setArguments(args); 53 return fragment; 54 } 55 56 private Listener mListener; 57 58 @Override onAttach(Activity activity)59 public void onAttach(Activity activity) { 60 if (activity instanceof Listener) { 61 mListener = (Listener) activity; 62 } else { 63 throw new IllegalArgumentException("Activity must implement " 64 + "SaveWifiConfigurationFragment.Listener to use this fragment."); 65 } 66 super.onAttach(activity); 67 } 68 69 @Override onDetach()70 public void onDetach() { 71 super.onDetach(); 72 mListener = null; 73 } 74 75 @Override onCreate(Bundle icicle)76 public void onCreate(Bundle icicle) { 77 super.onCreate(icicle); 78 79 int networkType = getArguments().getInt(EXTRA_NETWORK_TYPE); 80 NetworkConfiguration configuration = 81 NetworkConfigurationFactory.createNetworkConfiguration(getActivity(), networkType); 82 configuration.fromParcelable(getArguments().getParcelable(EXTRA_CONFIGURATION)); 83 configuration.save(this); 84 } 85 86 @Override onSuccess()87 public void onSuccess() { 88 if (mListener != null) { 89 mListener.onSaveWifiConfigurationCompleted(RESULT_SUCCESS); 90 } 91 } 92 93 @Override onFailure(int reason)94 public void onFailure(int reason) { 95 if (mListener != null) { 96 mListener.onSaveWifiConfigurationCompleted(RESULT_FAILURE); 97 } 98 } 99 } 100