WifiSettings.java revision 0b4fdc49fba83ad2a950681ef014b6927e438007
1/* 2 * Copyright (C) 2010 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 17package com.android.settings.wifi; 18 19import android.app.Activity; 20import android.app.AlertDialog; 21import android.app.AppGlobals; 22import android.app.Dialog; 23import android.app.admin.DevicePolicyManager; 24import android.content.BroadcastReceiver; 25import android.content.ComponentName; 26import android.content.ContentResolver; 27import android.content.Context; 28import android.content.Intent; 29import android.content.IntentFilter; 30import android.content.pm.ApplicationInfo; 31import android.content.pm.IPackageManager; 32import android.content.pm.PackageManager; 33import android.content.pm.PackageManager.NameNotFoundException; 34import android.content.res.Resources; 35import android.graphics.drawable.Drawable; 36import android.net.ConnectivityManager; 37import android.net.NetworkInfo; 38import android.net.NetworkInfo.State; 39import android.net.wifi.WifiConfiguration; 40import android.net.wifi.WifiManager; 41import android.net.wifi.WpsInfo; 42import android.nfc.NfcAdapter; 43import android.os.Bundle; 44import android.os.HandlerThread; 45import android.os.Process; 46import android.os.RemoteException; 47import android.os.UserHandle; 48import android.provider.Settings; 49import android.support.v7.preference.Preference; 50import android.support.v7.preference.PreferenceViewHolder; 51import android.text.Spannable; 52import android.text.style.TextAppearanceSpan; 53import android.util.Log; 54import android.util.TypedValue; 55import android.view.ContextMenu; 56import android.view.ContextMenu.ContextMenuInfo; 57import android.view.Gravity; 58import android.view.Menu; 59import android.view.MenuInflater; 60import android.view.MenuItem; 61import android.view.View; 62import android.widget.ProgressBar; 63import android.widget.TextView; 64import android.widget.TextView.BufferType; 65import android.widget.Toast; 66import com.android.internal.logging.MetricsLogger; 67import com.android.settings.LinkifyUtils; 68import com.android.settings.R; 69import com.android.settings.RestrictedSettingsFragment; 70import com.android.settings.SettingsActivity; 71import com.android.settings.dashboard.SummaryLoader; 72import com.android.settings.location.ScanningSettings; 73import com.android.settings.search.BaseSearchIndexProvider; 74import com.android.settings.search.Indexable; 75import com.android.settings.search.SearchIndexableRaw; 76import com.android.settings.wifi.AccessPointPreference.UserBadgeCache; 77import com.android.settingslib.wifi.AccessPoint; 78import com.android.settingslib.wifi.AccessPoint.AccessPointListener; 79import com.android.settingslib.wifi.WifiStatusTracker; 80import com.android.settingslib.wifi.WifiTracker; 81 82import java.util.ArrayList; 83import java.util.Collection; 84import java.util.List; 85 86import static android.os.UserManager.DISALLOW_CONFIG_WIFI; 87 88/** 89 * Two types of UI are provided here. 90 * 91 * The first is for "usual Settings", appearing as any other Setup fragment. 92 * 93 * The second is for Setup Wizard, with a simplified interface that hides the action bar 94 * and menus. 95 */ 96public class WifiSettings extends RestrictedSettingsFragment 97 implements Indexable, WifiTracker.WifiListener, AccessPointListener, 98 WifiDialog.WifiDialogListener { 99 100 private static final String TAG = "WifiSettings"; 101 102 /* package */ static final int MENU_ID_WPS_PBC = Menu.FIRST; 103 private static final int MENU_ID_WPS_PIN = Menu.FIRST + 1; 104 private static final int MENU_ID_ADVANCED = Menu.FIRST + 4; 105 private static final int MENU_ID_SCAN = Menu.FIRST + 5; 106 private static final int MENU_ID_CONNECT = Menu.FIRST + 6; 107 private static final int MENU_ID_FORGET = Menu.FIRST + 7; 108 private static final int MENU_ID_MODIFY = Menu.FIRST + 8; 109 private static final int MENU_ID_WRITE_NFC = Menu.FIRST + 9; 110 private static final int MENU_ID_CONFIGURE = Menu.FIRST + 10; 111 112 public static final int WIFI_DIALOG_ID = 1; 113 /* package */ static final int WPS_PBC_DIALOG_ID = 2; 114 private static final int WPS_PIN_DIALOG_ID = 3; 115 private static final int WRITE_NFC_DIALOG_ID = 6; 116 117 // Instance state keys 118 private static final String SAVE_DIALOG_MODE = "dialog_mode"; 119 private static final String SAVE_DIALOG_ACCESS_POINT_STATE = "wifi_ap_state"; 120 private static final String SAVED_WIFI_NFC_DIALOG_STATE = "wifi_nfc_dlg_state"; 121 122 protected WifiManager mWifiManager; 123 private WifiManager.ActionListener mConnectListener; 124 private WifiManager.ActionListener mSaveListener; 125 private WifiManager.ActionListener mForgetListener; 126 127 private WifiEnabler mWifiEnabler; 128 // An access point being editted is stored here. 129 private AccessPoint mSelectedAccessPoint; 130 131 private WifiDialog mDialog; 132 private WriteWifiConfigToNfcDialog mWifiToNfcDialog; 133 134 private TextView mEmptyView; 135 private ProgressBar mProgressHeader; 136 137 // this boolean extra specifies whether to disable the Next button when not connected. Used by 138 // account creation outside of setup wizard. 139 private static final String EXTRA_ENABLE_NEXT_ON_CONNECT = "wifi_enable_next_on_connect"; 140 // This string extra specifies a network to open the connect dialog on, so the user can enter 141 // network credentials. This is used by quick settings for secured networks. 142 private static final String EXTRA_START_CONNECT_SSID = "wifi_start_connect_ssid"; 143 144 // should Next button only be enabled when we have a connection? 145 private boolean mEnableNextOnConnection; 146 147 // Save the dialog details 148 private int mDialogMode; 149 private AccessPoint mDlgAccessPoint; 150 private Bundle mAccessPointSavedState; 151 private Bundle mWifiNfcDialogSavedState; 152 153 private WifiTracker mWifiTracker; 154 private String mOpenSsid; 155 156 private HandlerThread mBgThread; 157 158 private UserBadgeCache mUserBadgeCache; 159 private Preference mAddPreference; 160 161 /* End of "used in Wifi Setup context" */ 162 163 public WifiSettings() { 164 super(DISALLOW_CONFIG_WIFI); 165 } 166 167 @Override 168 public void onViewCreated(View view, Bundle savedInstanceState) { 169 super.onViewCreated(view, savedInstanceState); 170 final Activity activity = getActivity(); 171 if (activity != null) { 172 mProgressHeader = (ProgressBar) setPinnedHeaderView(R.layout.wifi_progress_header); 173 } 174 } 175 176 @Override 177 public void onCreate(Bundle icicle) { 178 super.onCreate(icicle); 179 addPreferencesFromResource(R.xml.wifi_settings); 180 mAddPreference = new Preference(getContext()); 181 Drawable ic_add = getContext().getDrawable(R.drawable.ic_menu_add).mutate(); 182 TypedValue tv = new TypedValue(); 183 getContext().getTheme().resolveAttribute(android.R.attr.colorAccent, tv, true); 184 ic_add.setTint(getContext().getColor(tv.resourceId)); 185 mAddPreference.setIcon(ic_add); 186 mAddPreference.setTitle(R.string.wifi_add_network); 187 188 mUserBadgeCache = new UserBadgeCache(getPackageManager()); 189 190 mBgThread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND); 191 mBgThread.start(); 192 } 193 194 @Override 195 public void onDestroy() { 196 mBgThread.quit(); 197 super.onDestroy(); 198 } 199 200 @Override 201 public void onActivityCreated(Bundle savedInstanceState) { 202 super.onActivityCreated(savedInstanceState); 203 204 mWifiTracker = 205 new WifiTracker(getActivity(), this, mBgThread.getLooper(), true, true, false); 206 mWifiManager = mWifiTracker.getManager(); 207 208 mConnectListener = new WifiManager.ActionListener() { 209 @Override 210 public void onSuccess() { 211 } 212 @Override 213 public void onFailure(int reason) { 214 Activity activity = getActivity(); 215 if (activity != null) { 216 Toast.makeText(activity, 217 R.string.wifi_failed_connect_message, 218 Toast.LENGTH_SHORT).show(); 219 } 220 } 221 }; 222 223 mSaveListener = new WifiManager.ActionListener() { 224 @Override 225 public void onSuccess() { 226 } 227 @Override 228 public void onFailure(int reason) { 229 Activity activity = getActivity(); 230 if (activity != null) { 231 Toast.makeText(activity, 232 R.string.wifi_failed_save_message, 233 Toast.LENGTH_SHORT).show(); 234 } 235 } 236 }; 237 238 mForgetListener = new WifiManager.ActionListener() { 239 @Override 240 public void onSuccess() { 241 } 242 @Override 243 public void onFailure(int reason) { 244 Activity activity = getActivity(); 245 if (activity != null) { 246 Toast.makeText(activity, 247 R.string.wifi_failed_forget_message, 248 Toast.LENGTH_SHORT).show(); 249 } 250 } 251 }; 252 253 if (savedInstanceState != null) { 254 mDialogMode = savedInstanceState.getInt(SAVE_DIALOG_MODE); 255 if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) { 256 mAccessPointSavedState = 257 savedInstanceState.getBundle(SAVE_DIALOG_ACCESS_POINT_STATE); 258 } 259 260 if (savedInstanceState.containsKey(SAVED_WIFI_NFC_DIALOG_STATE)) { 261 mWifiNfcDialogSavedState = 262 savedInstanceState.getBundle(SAVED_WIFI_NFC_DIALOG_STATE); 263 } 264 } 265 266 // if we're supposed to enable/disable the Next button based on our current connection 267 // state, start it off in the right state 268 Intent intent = getActivity().getIntent(); 269 mEnableNextOnConnection = intent.getBooleanExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, false); 270 271 if (mEnableNextOnConnection) { 272 if (hasNextButton()) { 273 final ConnectivityManager connectivity = (ConnectivityManager) 274 getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); 275 if (connectivity != null) { 276 NetworkInfo info = connectivity.getNetworkInfo( 277 ConnectivityManager.TYPE_WIFI); 278 changeNextButtonState(info.isConnected()); 279 } 280 } 281 } 282 283 mEmptyView = initEmptyView(); 284 registerForContextMenu(getListView()); 285 setHasOptionsMenu(true); 286 287 if (intent.hasExtra(EXTRA_START_CONNECT_SSID)) { 288 mOpenSsid = intent.getStringExtra(EXTRA_START_CONNECT_SSID); 289 onAccessPointsChanged(); 290 } 291 } 292 293 @Override 294 public void onDestroyView() { 295 super.onDestroyView(); 296 297 if (mWifiEnabler != null) { 298 mWifiEnabler.teardownSwitchBar(); 299 } 300 } 301 302 @Override 303 public void onStart() { 304 super.onStart(); 305 306 // On/off switch is hidden for Setup Wizard (returns null) 307 mWifiEnabler = createWifiEnabler(); 308 } 309 310 /** 311 * @return new WifiEnabler or null (as overridden by WifiSettingsForSetupWizard) 312 */ 313 /* package */ WifiEnabler createWifiEnabler() { 314 final SettingsActivity activity = (SettingsActivity) getActivity(); 315 return new WifiEnabler(activity, activity.getSwitchBar()); 316 } 317 318 @Override 319 public void onResume() { 320 final Activity activity = getActivity(); 321 super.onResume(); 322 removePreference("dummy"); 323 if (mWifiEnabler != null) { 324 mWifiEnabler.resume(activity); 325 } 326 327 mWifiTracker.startTracking(); 328 } 329 330 @Override 331 public void onPause() { 332 super.onPause(); 333 if (mWifiEnabler != null) { 334 mWifiEnabler.pause(); 335 } 336 337 mWifiTracker.stopTracking(); 338 } 339 340 @Override 341 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 342 // If the user is not allowed to configure wifi, do not show the menu. 343 if (isUiRestricted()) return; 344 345 addOptionsMenuItems(menu); 346 super.onCreateOptionsMenu(menu, inflater); 347 } 348 349 /** 350 * @param menu 351 */ 352 void addOptionsMenuItems(Menu menu) { 353 final boolean wifiIsEnabled = mWifiTracker.isWifiEnabled(); 354 menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.menu_stats_refresh) 355 .setEnabled(wifiIsEnabled) 356 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); 357 menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced) 358 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); 359 menu.add(Menu.NONE, MENU_ID_CONFIGURE, 0, R.string.wifi_menu_configure) 360 .setIcon(R.drawable.ic_settings_24dp) 361 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 362 } 363 364 @Override 365 protected int getMetricsCategory() { 366 return MetricsLogger.WIFI; 367 } 368 369 @Override 370 public void onSaveInstanceState(Bundle outState) { 371 super.onSaveInstanceState(outState); 372 373 // If the dialog is showing, save its state. 374 if (mDialog != null && mDialog.isShowing()) { 375 outState.putInt(SAVE_DIALOG_MODE, mDialogMode); 376 if (mDlgAccessPoint != null) { 377 mAccessPointSavedState = new Bundle(); 378 mDlgAccessPoint.saveWifiState(mAccessPointSavedState); 379 outState.putBundle(SAVE_DIALOG_ACCESS_POINT_STATE, mAccessPointSavedState); 380 } 381 } 382 383 if (mWifiToNfcDialog != null && mWifiToNfcDialog.isShowing()) { 384 Bundle savedState = new Bundle(); 385 mWifiToNfcDialog.saveState(savedState); 386 outState.putBundle(SAVED_WIFI_NFC_DIALOG_STATE, savedState); 387 } 388 } 389 390 @Override 391 public boolean onOptionsItemSelected(MenuItem item) { 392 // If the user is not allowed to configure wifi, do not handle menu selections. 393 if (isUiRestricted()) return false; 394 395 switch (item.getItemId()) { 396 case MENU_ID_WPS_PBC: 397 showDialog(WPS_PBC_DIALOG_ID); 398 return true; 399 /* 400 case MENU_ID_P2P: 401 if (getActivity() instanceof SettingsActivity) { 402 ((SettingsActivity) getActivity()).startPreferencePanel( 403 WifiP2pSettings.class.getCanonicalName(), 404 null, 405 R.string.wifi_p2p_settings_title, null, 406 this, 0); 407 } else { 408 startFragment(this, WifiP2pSettings.class.getCanonicalName(), 409 R.string.wifi_p2p_settings_title, -1, null); 410 } 411 return true; 412 */ 413 case MENU_ID_WPS_PIN: 414 showDialog(WPS_PIN_DIALOG_ID); 415 return true; 416 case MENU_ID_SCAN: 417 MetricsLogger.action(getActivity(), MetricsLogger.ACTION_WIFI_FORCE_SCAN); 418 mWifiTracker.forceScan(); 419 return true; 420 case MENU_ID_ADVANCED: 421 if (getActivity() instanceof SettingsActivity) { 422 ((SettingsActivity) getActivity()).startPreferencePanel( 423 AdvancedWifiSettings.class.getCanonicalName(), null, 424 R.string.wifi_advanced_titlebar, null, this, 0); 425 } else { 426 startFragment(this, AdvancedWifiSettings.class.getCanonicalName(), 427 R.string.wifi_advanced_titlebar, -1 /* Do not request a results */, 428 null); 429 } 430 return true; 431 case MENU_ID_CONFIGURE: 432 if (getActivity() instanceof SettingsActivity) { 433 ((SettingsActivity) getActivity()).startPreferencePanel( 434 ConfigureWifiSettings.class.getCanonicalName(), null, 435 R.string.wifi_configure_titlebar, null, this, 0); 436 } else { 437 startFragment(this, ConfigureWifiSettings.class.getCanonicalName(), 438 R.string.wifi_configure_titlebar, -1 /* Do not request a results */, 439 null); 440 } 441 return true; 442 443 } 444 return super.onOptionsItemSelected(item); 445 } 446 447 @Override 448 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) { 449 Preference preference = (Preference) view.getTag(); 450 451 if (preference instanceof AccessPointPreference) { 452 mSelectedAccessPoint = ((AccessPointPreference) preference).getAccessPoint(); 453 menu.setHeaderTitle(mSelectedAccessPoint.getSsid()); 454 if (mSelectedAccessPoint.isConnectable()) { 455 menu.add(Menu.NONE, MENU_ID_CONNECT, 0, R.string.wifi_menu_connect); 456 } 457 458 WifiConfiguration config = mSelectedAccessPoint.getConfig(); 459 // Some configs are ineditable 460 if (isEditabilityLockedDown(getActivity(), config)) { 461 return; 462 } 463 464 if (mSelectedAccessPoint.isSaved() || mSelectedAccessPoint.isEphemeral()) { 465 // Allow forgetting a network if either the network is saved or ephemerally 466 // connected. (In the latter case, "forget" blacklists the network so it won't 467 // be used again, ephemerally). 468 menu.add(Menu.NONE, MENU_ID_FORGET, 0, R.string.wifi_menu_forget); 469 } 470 if (mSelectedAccessPoint.isSaved()) { 471 menu.add(Menu.NONE, MENU_ID_MODIFY, 0, R.string.wifi_menu_modify); 472 NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity()); 473 if (nfcAdapter != null && nfcAdapter.isEnabled() && 474 mSelectedAccessPoint.getSecurity() != AccessPoint.SECURITY_NONE) { 475 // Only allow writing of NFC tags for password-protected networks. 476 menu.add(Menu.NONE, MENU_ID_WRITE_NFC, 0, R.string.wifi_menu_write_to_nfc); 477 } 478 } 479 } 480 } 481 482 @Override 483 public boolean onContextItemSelected(MenuItem item) { 484 if (mSelectedAccessPoint == null) { 485 return super.onContextItemSelected(item); 486 } 487 switch (item.getItemId()) { 488 case MENU_ID_CONNECT: { 489 if (mSelectedAccessPoint.isSaved()) { 490 connect(mSelectedAccessPoint.getConfig()); 491 } else if (mSelectedAccessPoint.getSecurity() == AccessPoint.SECURITY_NONE) { 492 /** Bypass dialog for unsecured networks */ 493 mSelectedAccessPoint.generateOpenNetworkConfig(); 494 connect(mSelectedAccessPoint.getConfig()); 495 } else { 496 showDialog(mSelectedAccessPoint, WifiConfigUiBase.MODE_CONNECT); 497 } 498 return true; 499 } 500 case MENU_ID_FORGET: { 501 forget(); 502 return true; 503 } 504 case MENU_ID_MODIFY: { 505 showDialog(mSelectedAccessPoint, WifiConfigUiBase.MODE_MODIFY); 506 return true; 507 } 508 case MENU_ID_WRITE_NFC: 509 showDialog(WRITE_NFC_DIALOG_ID); 510 return true; 511 512 } 513 return super.onContextItemSelected(item); 514 } 515 516 @Override 517 public boolean onPreferenceTreeClick(Preference preference) { 518 if (preference instanceof AccessPointPreference) { 519 mSelectedAccessPoint = ((AccessPointPreference) preference).getAccessPoint(); 520 if (mSelectedAccessPoint == null) { 521 return false; 522 } 523 /** Bypass dialog for unsecured, unsaved, and inactive networks */ 524 if (mSelectedAccessPoint.getSecurity() == AccessPoint.SECURITY_NONE && 525 !mSelectedAccessPoint.isSaved() && !mSelectedAccessPoint.isActive()) { 526 mSelectedAccessPoint.generateOpenNetworkConfig(); 527 connect(mSelectedAccessPoint.getConfig()); 528 } else if (mSelectedAccessPoint.isSaved()) { 529 showDialog(mSelectedAccessPoint, WifiConfigUiBase.MODE_VIEW); 530 } else { 531 showDialog(mSelectedAccessPoint, WifiConfigUiBase.MODE_CONNECT); 532 } 533 } else if (preference == mAddPreference) { 534 onAddNetworkPressed(); 535 } else { 536 return super.onPreferenceTreeClick(preference); 537 } 538 return true; 539 } 540 541 private void showDialog(AccessPoint accessPoint, int dialogMode) { 542 if (accessPoint != null) { 543 WifiConfiguration config = accessPoint.getConfig(); 544 if (isEditabilityLockedDown(getActivity(), config) && accessPoint.isActive()) { 545 final int userId = UserHandle.getUserId(config.creatorUid); 546 final PackageManager pm = getActivity().getPackageManager(); 547 final IPackageManager ipm = AppGlobals.getPackageManager(); 548 String appName = pm.getNameForUid(config.creatorUid); 549 try { 550 final ApplicationInfo appInfo = ipm.getApplicationInfo(appName, /* flags */ 0, 551 userId); 552 final CharSequence label = pm.getApplicationLabel(appInfo); 553 if (label != null) { 554 appName = label.toString(); 555 } 556 } catch (RemoteException e) { 557 // leave appName as packageName 558 } 559 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 560 builder.setTitle(accessPoint.getSsid()) 561 .setMessage(getString(R.string.wifi_alert_lockdown_by_device_owner, 562 appName)) 563 .setPositiveButton(android.R.string.ok, null) 564 .show(); 565 return; 566 } 567 } 568 569 if (mDialog != null) { 570 removeDialog(WIFI_DIALOG_ID); 571 mDialog = null; 572 } 573 574 // Save the access point and edit mode 575 mDlgAccessPoint = accessPoint; 576 mDialogMode = dialogMode; 577 578 showDialog(WIFI_DIALOG_ID); 579 } 580 581 @Override 582 public Dialog onCreateDialog(int dialogId) { 583 switch (dialogId) { 584 case WIFI_DIALOG_ID: 585 AccessPoint ap = mDlgAccessPoint; // For manual launch 586 if (ap == null) { // For re-launch from saved state 587 if (mAccessPointSavedState != null) { 588 ap = new AccessPoint(getActivity(), mAccessPointSavedState); 589 // For repeated orientation changes 590 mDlgAccessPoint = ap; 591 // Reset the saved access point data 592 mAccessPointSavedState = null; 593 } 594 } 595 // If it's null, fine, it's for Add Network 596 mSelectedAccessPoint = ap; 597 final boolean hideForget = (ap == null || isEditabilityLockedDown(getActivity(), 598 ap.getConfig())); 599 mDialog = new WifiDialog(getActivity(), this, ap, mDialogMode, 600 /* no hide submit/connect */ false, 601 /* hide forget if config locked down */ hideForget); 602 return mDialog; 603 case WPS_PBC_DIALOG_ID: 604 return new WpsDialog(getActivity(), WpsInfo.PBC); 605 case WPS_PIN_DIALOG_ID: 606 return new WpsDialog(getActivity(), WpsInfo.DISPLAY); 607 case WRITE_NFC_DIALOG_ID: 608 if (mSelectedAccessPoint != null) { 609 mWifiToNfcDialog = new WriteWifiConfigToNfcDialog( 610 getActivity(), mSelectedAccessPoint.getConfig().networkId, 611 mSelectedAccessPoint.getSecurity(), 612 mWifiManager); 613 } else if (mWifiNfcDialogSavedState != null) { 614 mWifiToNfcDialog = new WriteWifiConfigToNfcDialog( 615 getActivity(), mWifiNfcDialogSavedState, mWifiManager); 616 } 617 618 return mWifiToNfcDialog; 619 } 620 return super.onCreateDialog(dialogId); 621 } 622 623 /** 624 * Shows the latest access points available with supplemental information like 625 * the strength of network and the security for it. 626 */ 627 @Override 628 public void onAccessPointsChanged() { 629 // Safeguard from some delayed event handling 630 if (getActivity() == null) return; 631 632 if (isUiRestricted()) { 633 addMessagePreference(R.string.wifi_empty_list_user_restricted); 634 return; 635 } 636 final int wifiState = mWifiManager.getWifiState(); 637 638 switch (wifiState) { 639 case WifiManager.WIFI_STATE_ENABLED: 640 // AccessPoints are automatically sorted with TreeSet. 641 final Collection<AccessPoint> accessPoints = 642 mWifiTracker.getAccessPoints(); 643 getPreferenceScreen().removeAll(); 644 645 boolean hasAvailableAccessPoints = false; 646 int index = 0; 647 for (AccessPoint accessPoint : accessPoints) { 648 // Ignore access points that are out of range. 649 if (accessPoint.getLevel() != -1) { 650 hasAvailableAccessPoints = true; 651 if (accessPoint.getTag() != null) { 652 final Preference pref = (Preference) accessPoint.getTag(); 653 pref.setOrder(index++); 654 getPreferenceScreen().addPreference(pref); 655 continue; 656 } 657 AccessPointPreference preference = new AccessPointPreference(accessPoint, 658 getPrefContext(), mUserBadgeCache, false, this); 659 preference.setOrder(index++); 660 661 if (mOpenSsid != null && mOpenSsid.equals(accessPoint.getSsidStr()) 662 && !accessPoint.isSaved() 663 && accessPoint.getSecurity() != AccessPoint.SECURITY_NONE) { 664 onPreferenceTreeClick(preference); 665 mOpenSsid = null; 666 } 667 getPreferenceScreen().addPreference(preference); 668 accessPoint.setListener(this); 669 } 670 } 671 if (!hasAvailableAccessPoints) { 672 setProgressBarVisible(true); 673 Preference pref = new Preference(getContext()) { 674 @Override 675 public void onBindViewHolder(PreferenceViewHolder holder) { 676 super.onBindViewHolder(holder); 677 // Show a line on each side of add network. 678 holder.setDividerAllowedBelow(true); 679 } 680 }; 681 pref.setSelectable(false); 682 pref.setSummary(R.string.wifi_empty_list_wifi_on); 683 pref.setOrder(0); 684 getPreferenceScreen().addPreference(pref); 685 mAddPreference.setOrder(1); 686 getPreferenceScreen().addPreference(mAddPreference); 687 } else { 688 mAddPreference.setOrder(index++); 689 getPreferenceScreen().addPreference(mAddPreference); 690 setProgressBarVisible(false); 691 } 692 break; 693 694 case WifiManager.WIFI_STATE_ENABLING: 695 getPreferenceScreen().removeAll(); 696 setProgressBarVisible(true); 697 break; 698 699 case WifiManager.WIFI_STATE_DISABLING: 700 addMessagePreference(R.string.wifi_stopping); 701 setProgressBarVisible(true); 702 break; 703 704 case WifiManager.WIFI_STATE_DISABLED: 705 setOffMessage(); 706 setProgressBarVisible(false); 707 break; 708 } 709 } 710 711 protected TextView initEmptyView() { 712 TextView emptyView = (TextView) getActivity().findViewById(android.R.id.empty); 713 emptyView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); 714 setEmptyView(emptyView); 715 return emptyView; 716 } 717 718 private void setOffMessage() { 719 if (mEmptyView == null) { 720 return; 721 } 722 723 final CharSequence briefText = getText(R.string.wifi_empty_list_wifi_off); 724 725 // Don't use WifiManager.isScanAlwaysAvailable() to check the Wi-Fi scanning mode. Instead, 726 // read the system settings directly. Because when the device is in Airplane mode, even if 727 // Wi-Fi scanning mode is on, WifiManager.isScanAlwaysAvailable() still returns "off". 728 final ContentResolver resolver = getActivity().getContentResolver(); 729 final boolean wifiScanningMode = Settings.Global.getInt( 730 resolver, Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1; 731 732 if (isUiRestricted() || !wifiScanningMode) { 733 // Show only the brief text if the user is not allowed to configure scanning settings, 734 // or the scanning mode has been turned off. 735 mEmptyView.setText(briefText, BufferType.SPANNABLE); 736 } else { 737 // Append the description of scanning settings with link. 738 final StringBuilder contentBuilder = new StringBuilder(); 739 contentBuilder.append(briefText); 740 contentBuilder.append("\n\n"); 741 contentBuilder.append(getText(R.string.wifi_scan_notify_text)); 742 LinkifyUtils.linkify(mEmptyView, contentBuilder, new LinkifyUtils.OnClickListener() { 743 @Override 744 public void onClick() { 745 final SettingsActivity activity = 746 (SettingsActivity) WifiSettings.this.getActivity(); 747 activity.startPreferencePanel(ScanningSettings.class.getName(), null, 748 R.string.location_scanning_screen_title, null, null, 0); 749 } 750 }); 751 } 752 // Embolden and enlarge the brief description anyway. 753 Spannable boldSpan = (Spannable) mEmptyView.getText(); 754 boldSpan.setSpan( 755 new TextAppearanceSpan(getActivity(), android.R.style.TextAppearance_Medium), 0, 756 briefText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 757 getPreferenceScreen().removeAll(); 758 } 759 760 private void addMessagePreference(int messageId) { 761 if (mEmptyView != null) mEmptyView.setText(messageId); 762 getPreferenceScreen().removeAll(); 763 } 764 765 protected void setProgressBarVisible(boolean visible) { 766 if (mProgressHeader != null) { 767 mProgressHeader.setVisibility(visible ? View.VISIBLE : View.GONE); 768 } 769 } 770 771 @Override 772 public void onWifiStateChanged(int state) { 773 switch (state) { 774 case WifiManager.WIFI_STATE_ENABLING: 775 addMessagePreference(R.string.wifi_starting); 776 setProgressBarVisible(true); 777 break; 778 779 case WifiManager.WIFI_STATE_DISABLED: 780 setOffMessage(); 781 setProgressBarVisible(false); 782 break; 783 } 784 } 785 786 @Override 787 public void onConnectedChanged() { 788 changeNextButtonState(mWifiTracker.isConnected()); 789 } 790 791 /** 792 * Renames/replaces "Next" button when appropriate. "Next" button usually exists in 793 * Wifi setup screens, not in usual wifi settings screen. 794 * 795 * @param enabled true when the device is connected to a wifi network. 796 */ 797 private void changeNextButtonState(boolean enabled) { 798 if (mEnableNextOnConnection && hasNextButton()) { 799 getNextButton().setEnabled(enabled); 800 } 801 } 802 803 @Override 804 public void onForget(WifiDialog dialog) { 805 forget(); 806 } 807 808 @Override 809 public void onSubmit(WifiDialog dialog) { 810 if (mDialog != null) { 811 submit(mDialog.getController()); 812 } 813 } 814 815 /* package */ void submit(WifiConfigController configController) { 816 817 final WifiConfiguration config = configController.getConfig(); 818 819 if (config == null) { 820 if (mSelectedAccessPoint != null 821 && mSelectedAccessPoint.isSaved()) { 822 connect(mSelectedAccessPoint.getConfig()); 823 } 824 } else if (configController.getMode() == WifiConfigUiBase.MODE_MODIFY) { 825 mWifiManager.save(config, mSaveListener); 826 } else { 827 mWifiManager.save(config, mSaveListener); 828 if (mSelectedAccessPoint != null) { // Not an "Add network" 829 connect(config); 830 } 831 } 832 833 mWifiTracker.resumeScanning(); 834 } 835 836 /* package */ void forget() { 837 MetricsLogger.action(getActivity(), MetricsLogger.ACTION_WIFI_FORGET); 838 if (!mSelectedAccessPoint.isSaved()) { 839 if (mSelectedAccessPoint.getNetworkInfo() != null && 840 mSelectedAccessPoint.getNetworkInfo().getState() != State.DISCONNECTED) { 841 // Network is active but has no network ID - must be ephemeral. 842 mWifiManager.disableEphemeralNetwork( 843 AccessPoint.convertToQuotedString(mSelectedAccessPoint.getSsidStr())); 844 } else { 845 // Should not happen, but a monkey seems to trigger it 846 Log.e(TAG, "Failed to forget invalid network " + mSelectedAccessPoint.getConfig()); 847 return; 848 } 849 } else { 850 mWifiManager.forget(mSelectedAccessPoint.getConfig().networkId, mForgetListener); 851 } 852 853 mWifiTracker.resumeScanning(); 854 855 // We need to rename/replace "Next" button in wifi setup context. 856 changeNextButtonState(false); 857 } 858 859 protected void connect(final WifiConfiguration config) { 860 MetricsLogger.action(getActivity(), MetricsLogger.ACTION_WIFI_CONNECT); 861 mWifiManager.connect(config, mConnectListener); 862 } 863 864 protected void connect(final int networkId) { 865 MetricsLogger.action(getActivity(), MetricsLogger.ACTION_WIFI_CONNECT); 866 mWifiManager.connect(networkId, mConnectListener); 867 } 868 869 /** 870 * Refreshes acccess points and ask Wifi module to scan networks again. 871 */ 872 /* package */ void refreshAccessPoints() { 873 mWifiTracker.resumeScanning(); 874 875 getPreferenceScreen().removeAll(); 876 } 877 878 /** 879 * Called when "add network" button is pressed. 880 */ 881 /* package */ void onAddNetworkPressed() { 882 MetricsLogger.action(getActivity(), MetricsLogger.ACTION_WIFI_ADD_NETWORK); 883 // No exact access point is selected. 884 mSelectedAccessPoint = null; 885 showDialog(null, WifiConfigUiBase.MODE_CONNECT); 886 } 887 888 /* package */ int getAccessPointsCount() { 889 final boolean wifiIsEnabled = mWifiTracker.isWifiEnabled(); 890 if (wifiIsEnabled) { 891 return getPreferenceScreen().getPreferenceCount(); 892 } else { 893 return 0; 894 } 895 } 896 897 /** 898 * Requests wifi module to pause wifi scan. May be ignored when the module is disabled. 899 */ 900 /* package */ void pauseWifiScan() { 901 mWifiTracker.pauseScanning(); 902 } 903 904 /** 905 * Requests wifi module to resume wifi scan. May be ignored when the module is disabled. 906 */ 907 /* package */ void resumeWifiScan() { 908 mWifiTracker.resumeScanning(); 909 } 910 911 @Override 912 protected int getHelpResource() { 913 return R.string.help_url_wifi; 914 } 915 916 @Override 917 public void onAccessPointChanged(AccessPoint accessPoint) { 918 ((AccessPointPreference) accessPoint.getTag()).refresh(); 919 } 920 921 @Override 922 public void onLevelChanged(AccessPoint accessPoint) { 923 ((AccessPointPreference) accessPoint.getTag()).onLevelChanged(); 924 } 925 926 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 927 new BaseSearchIndexProvider() { 928 @Override 929 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { 930 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); 931 final Resources res = context.getResources(); 932 933 // Add fragment title 934 SearchIndexableRaw data = new SearchIndexableRaw(context); 935 data.title = res.getString(R.string.wifi_settings); 936 data.screenTitle = res.getString(R.string.wifi_settings); 937 data.keywords = res.getString(R.string.keywords_wifi); 938 result.add(data); 939 940 // Add saved Wi-Fi access points 941 final Collection<AccessPoint> accessPoints = 942 WifiTracker.getCurrentAccessPoints(context, true, false, false); 943 for (AccessPoint accessPoint : accessPoints) { 944 data = new SearchIndexableRaw(context); 945 data.title = accessPoint.getSsidStr(); 946 data.screenTitle = res.getString(R.string.wifi_settings); 947 data.enabled = enabled; 948 result.add(data); 949 } 950 951 return result; 952 } 953 }; 954 955 /** 956 * Returns true if the config is not editable through Settings. 957 * @param context Context of caller 958 * @param config The WiFi config. 959 * @return true if the config is not editable through Settings. 960 */ 961 static boolean isEditabilityLockedDown(Context context, WifiConfiguration config) { 962 return !canModifyNetwork(context, config); 963 } 964 965 /** 966 * This method is a stripped version of WifiConfigStore.canModifyNetwork. 967 * TODO: refactor to have only one method. 968 * @param context Context of caller 969 * @param config The WiFi config. 970 * @return true if Settings can modify the config. 971 */ 972 static boolean canModifyNetwork(Context context, WifiConfiguration config) { 973 if (config == null) { 974 return true; 975 } 976 977 final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService( 978 Context.DEVICE_POLICY_SERVICE); 979 980 // Check if device has DPM capability. If it has and dpm is still null, then we 981 // treat this case with suspicion and bail out. 982 final PackageManager pm = context.getPackageManager(); 983 if (pm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN) && dpm == null) { 984 return false; 985 } 986 987 boolean isConfigEligibleForLockdown = false; 988 if (dpm != null) { 989 final ComponentName deviceOwner = dpm.getDeviceOwnerComponentOnAnyUser(); 990 if (deviceOwner != null) { 991 final int deviceOwnerUserId = dpm.getDeviceOwnerUserId(); 992 try { 993 final int deviceOwnerUid = pm.getPackageUid(deviceOwner.getPackageName(), 994 deviceOwnerUserId); 995 isConfigEligibleForLockdown = deviceOwnerUid == config.creatorUid; 996 } catch (NameNotFoundException e) { 997 // don't care 998 } 999 } 1000 } 1001 if (!isConfigEligibleForLockdown) { 1002 return true; 1003 } 1004 1005 final ContentResolver resolver = context.getContentResolver(); 1006 final boolean isLockdownFeatureEnabled = Settings.Global.getInt(resolver, 1007 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0) != 0; 1008 return !isLockdownFeatureEnabled; 1009 } 1010 1011 private static class SummaryProvider extends BroadcastReceiver 1012 implements SummaryLoader.SummaryProvider { 1013 1014 private final Context mContext; 1015 private final WifiManager mWifiManager; 1016 private final WifiStatusTracker mWifiTracker; 1017 private final SummaryLoader mSummaryLoader; 1018 1019 public SummaryProvider(Context context, SummaryLoader summaryLoader) { 1020 mContext = context; 1021 mSummaryLoader = summaryLoader; 1022 mWifiManager = context.getSystemService(WifiManager.class); 1023 mWifiTracker = new WifiStatusTracker(mWifiManager); 1024 } 1025 1026 private CharSequence getSummary() { 1027 if (!mWifiTracker.enabled) { 1028 return mContext.getString(R.string.disabled); 1029 } 1030 if (!mWifiTracker.connected) { 1031 return mContext.getString(R.string.disconnected); 1032 } 1033 return mWifiTracker.ssid; 1034 } 1035 1036 @Override 1037 public void setListening(boolean listening) { 1038 if (listening) { 1039 IntentFilter filter = new IntentFilter(); 1040 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); 1041 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 1042 filter.addAction(WifiManager.RSSI_CHANGED_ACTION); 1043 mContext.registerReceiver(this, filter); 1044 } else { 1045 mContext.unregisterReceiver(this); 1046 } 1047 } 1048 1049 @Override 1050 public void onReceive(Context context, Intent intent) { 1051 mWifiTracker.handleBroadcast(intent); 1052 mSummaryLoader.setSummary(this, getSummary()); 1053 } 1054 } 1055 1056 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY 1057 = new SummaryLoader.SummaryProviderFactory() { 1058 @Override 1059 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity, 1060 SummaryLoader summaryLoader) { 1061 return new SummaryProvider(activity, summaryLoader); 1062 } 1063 }; 1064} 1065