1 /* 2 * Copyright (C) 2017 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 package com.android.car.settings.wifi; 17 18 import android.net.wifi.WifiManager; 19 import android.os.Bundle; 20 import android.provider.Settings; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.Nullable; 24 import androidx.annotation.XmlRes; 25 26 import com.android.car.settings.R; 27 import com.android.car.settings.common.SettingsFragment; 28 import com.android.car.settings.search.CarBaseSearchIndexProvider; 29 import com.android.car.ui.toolbar.ProgressBarController; 30 import com.android.car.ui.toolbar.ToolbarController; 31 import com.android.settingslib.search.SearchIndexable; 32 import com.android.wifitrackerlib.WifiEntry; 33 34 import java.util.List; 35 36 /** 37 * Main page to host Wifi related preferences. 38 */ 39 @SearchIndexable 40 public class WifiSettingsFragment extends SettingsFragment 41 implements CarWifiManager.Listener { 42 43 private static final int SEARCHING_DELAY_MILLIS = 1700; 44 private static final String EXTRA_CONNECTED_WIFI_ENTRY_KEY = "connected_wifi_entry_key"; 45 46 private CarWifiManager mCarWifiManager; 47 private ProgressBarController mProgressBar; 48 @Nullable 49 private String mConnectedWifiEntryKey; 50 51 @Override 52 @XmlRes getPreferenceScreenResId()53 protected int getPreferenceScreenResId() { 54 return R.xml.wifi_list_fragment; 55 } 56 57 @Override onCreate(Bundle savedInstanceState)58 public void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 mCarWifiManager = new CarWifiManager(getContext(), getLifecycle()); 61 62 if (savedInstanceState != null) { 63 mConnectedWifiEntryKey = savedInstanceState.getString( 64 EXTRA_CONNECTED_WIFI_ENTRY_KEY); 65 } 66 } 67 68 @Override onSaveInstanceState(@onNull Bundle outState)69 public void onSaveInstanceState(@NonNull Bundle outState) { 70 super.onSaveInstanceState(outState); 71 outState.putString(EXTRA_CONNECTED_WIFI_ENTRY_KEY, mConnectedWifiEntryKey); 72 } 73 74 @Override setupToolbar(@onNull ToolbarController toolbar)75 protected void setupToolbar(@NonNull ToolbarController toolbar) { 76 super.setupToolbar(toolbar); 77 78 mProgressBar = toolbar.getProgressBar(); 79 } 80 81 @Override onStart()82 public void onStart() { 83 super.onStart(); 84 mCarWifiManager.addListener(this); 85 onWifiStateChanged(mCarWifiManager.getWifiState()); 86 } 87 88 @Override onStop()89 public void onStop() { 90 super.onStop(); 91 mCarWifiManager.removeListener(this); 92 mProgressBar.setVisible(false); 93 } 94 95 @Override onDestroy()96 public void onDestroy() { 97 super.onDestroy(); 98 } 99 100 @Override onWifiEntriesChanged()101 public void onWifiEntriesChanged() { 102 mProgressBar.setVisible(true); 103 getView().postDelayed(() -> mProgressBar.setVisible(false), SEARCHING_DELAY_MILLIS); 104 List<WifiEntry> connectedWifiEntries = mCarWifiManager.getConnectedWifiEntries(); 105 if (!connectedWifiEntries.isEmpty()) { 106 String connectedWifiEntryKey = connectedWifiEntries.get(0).getKey(); 107 if (!connectedWifiEntryKey.equals(mConnectedWifiEntryKey)) { 108 scrollToPreference(connectedWifiEntryKey); 109 mConnectedWifiEntryKey = connectedWifiEntryKey; 110 } 111 } else { 112 mConnectedWifiEntryKey = null; 113 } 114 } 115 116 @Override onWifiStateChanged(int state)117 public void onWifiStateChanged(int state) { 118 mProgressBar.setVisible(state == WifiManager.WIFI_STATE_ENABLING); 119 } 120 121 /** 122 * Data provider for Settings Search. 123 */ 124 public static final CarBaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 125 new CarBaseSearchIndexProvider(R.xml.wifi_list_fragment, Settings.ACTION_WIFI_SETTINGS); 126 } 127