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.settings.wifi; 18 19 import android.app.Dialog; 20 import android.net.wifi.WifiConfiguration; 21 import android.os.Bundle; 22 import android.text.TextUtils; 23 import android.view.LayoutInflater; 24 import android.view.Menu; 25 import android.view.MenuInflater; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 import android.view.ViewGroup; 29 import android.widget.ListView; 30 import android.widget.TextView; 31 32 import com.android.settings.R; 33 import com.android.settings.SetupWizardUtils; 34 import com.android.setupwizardlib.SetupWizardListLayout; 35 import com.android.setupwizardlib.view.NavigationBar; 36 37 /** 38 * This customized version of WifiSettings is shown to the user only during Setup Wizard. Menu 39 * is not shown, clicking on an access point will auto-advance to the next screen (once connected), 40 * and, if the user opts to skip ahead without a wifi connection, a warning message alerts of 41 * possible carrier data charges or missing software updates. 42 */ 43 public class WifiSettingsForSetupWizard extends WifiSettings { 44 45 private static final String TAG = "WifiSettingsForSetupWizard"; 46 47 private SetupWizardListLayout mLayout; 48 private View mAddOtherNetworkItem; 49 private TextView mEmptyFooter; 50 private View mMacAddressFooter; 51 private boolean mListLastEmpty = false; 52 53 @Override onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)54 public View onCreateView(final LayoutInflater inflater, ViewGroup container, 55 Bundle savedInstanceState) { 56 mLayout = (SetupWizardListLayout) 57 inflater.inflate(R.layout.setup_wifi_layout, container, false); 58 final ListView list = mLayout.getListView(); 59 60 mAddOtherNetworkItem = inflater.inflate(R.layout.setup_wifi_add_network, list, false); 61 list.addFooterView(mAddOtherNetworkItem, null, true); 62 mAddOtherNetworkItem.setOnClickListener(new OnClickListener() { 63 @Override 64 public void onClick(View v) { 65 if (mWifiManager.isWifiEnabled()) { 66 onAddNetworkPressed(); 67 } 68 } 69 }); 70 71 mMacAddressFooter = inflater.inflate(R.layout.setup_wifi_mac_address, list, false); 72 list.addFooterView(mMacAddressFooter, null, false); 73 74 final NavigationBar navigationBar = mLayout.getNavigationBar(); 75 if (navigationBar != null) { 76 WifiSetupActivity activity = (WifiSetupActivity) getActivity(); 77 activity.onNavigationBarCreated(navigationBar); 78 } 79 80 return mLayout; 81 } 82 83 @Override onActivityCreated(Bundle savedInstanceState)84 public void onActivityCreated(Bundle savedInstanceState) { 85 super.onActivityCreated(savedInstanceState); 86 87 if (hasNextButton()) { 88 getNextButton().setVisibility(View.GONE); 89 } 90 91 updateMacAddress(); 92 } 93 94 @Override onAccessPointsChanged()95 public void onAccessPointsChanged() { 96 super.onAccessPointsChanged(); 97 updateFooter(getPreferenceScreen().getPreferenceCount() == 0); 98 } 99 100 @Override onWifiStateChanged(int state)101 public void onWifiStateChanged(int state) { 102 super.onWifiStateChanged(state); 103 updateMacAddress(); 104 } 105 106 @Override registerForContextMenu(View view)107 public void registerForContextMenu(View view) { 108 // Suppressed during setup wizard 109 } 110 111 @Override createWifiEnabler()112 /* package */ WifiEnabler createWifiEnabler() { 113 // Not shown during setup wizard 114 return null; 115 } 116 117 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)118 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 119 // Do not show menu during setup wizard 120 } 121 122 @Override onCreateDialog(int dialogId)123 public Dialog onCreateDialog(int dialogId) { 124 final Dialog dialog = super.onCreateDialog(dialogId); 125 SetupWizardUtils.applyImmersiveFlags(dialog); 126 return dialog; 127 } 128 129 @Override connect(final WifiConfiguration config)130 protected void connect(final WifiConfiguration config) { 131 WifiSetupActivity activity = (WifiSetupActivity) getActivity(); 132 activity.networkSelected(); 133 super.connect(config); 134 } 135 136 @Override connect(final int networkId)137 protected void connect(final int networkId) { 138 WifiSetupActivity activity = (WifiSetupActivity) getActivity(); 139 activity.networkSelected(); 140 super.connect(networkId); 141 } 142 143 @Override initEmptyView()144 protected TextView initEmptyView() { 145 final LayoutInflater inflater = LayoutInflater.from(getActivity()); 146 mEmptyFooter = (TextView) inflater.inflate(R.layout.setup_wifi_empty, getListView(), false); 147 return mEmptyFooter; 148 } 149 updateFooter(boolean isEmpty)150 protected void updateFooter(boolean isEmpty) { 151 if (isEmpty != mListLastEmpty && hasListView()) { 152 final ListView list = getListView(); 153 list.removeFooterView(mEmptyFooter); 154 list.removeFooterView(mAddOtherNetworkItem); 155 list.removeFooterView(mMacAddressFooter); 156 if (isEmpty) { 157 list.addFooterView(mEmptyFooter, null, false); 158 } else { 159 list.addFooterView(mAddOtherNetworkItem, null, true); 160 list.addFooterView(mMacAddressFooter, null, false); 161 } 162 mListLastEmpty = isEmpty; 163 } 164 } 165 166 @Override setPinnedHeaderView(int layoutResId)167 public View setPinnedHeaderView(int layoutResId) { 168 // Pinned header is not supported in setup wizard 169 return null; 170 } 171 172 @Override setPinnedHeaderView(View pinnedHeader)173 public void setPinnedHeaderView(View pinnedHeader) { 174 // Pinned header is not supported in setup wizard 175 } 176 177 @Override setProgressBarVisible(boolean visible)178 protected void setProgressBarVisible(boolean visible) { 179 if (mLayout != null) { 180 if (visible) { 181 mLayout.showProgressBar(); 182 } else { 183 mLayout.hideProgressBar(); 184 } 185 } 186 } 187 updateMacAddress()188 private void updateMacAddress() { 189 if (mMacAddressFooter != null) { 190 String macAddress = null; 191 if (mWifiManager != null) { 192 android.net.wifi.WifiInfo connectionInfo = mWifiManager.getConnectionInfo(); 193 if (connectionInfo != null) { 194 macAddress = connectionInfo.getMacAddress(); 195 } 196 } 197 final TextView macAddressTextView = 198 (TextView) mMacAddressFooter.findViewById(R.id.mac_address); 199 macAddressTextView.setText(!TextUtils.isEmpty(macAddress) ? 200 macAddress : getString(R.string.status_unavailable)); 201 } 202 } 203 } 204