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