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 /** 35 * Main page to host Wifi related preferences. 36 */ 37 @SearchIndexable 38 public class WifiSettingsFragment extends SettingsFragment 39 implements CarWifiManager.Listener { 40 41 private static final int SEARCHING_DELAY_MILLIS = 1700; 42 private static final String EXTRA_CONNECTED_WIFI_ENTRY_KEY = "connected_wifi_entry_key"; 43 44 private CarWifiManager mCarWifiManager; 45 private ProgressBarController mProgressBar; 46 @Nullable 47 private String mConnectedWifiEntryKey; 48 49 @Override 50 @XmlRes getPreferenceScreenResId()51 protected int getPreferenceScreenResId() { 52 return R.xml.wifi_list_fragment; 53 } 54 55 @Override onCreate(Bundle savedInstanceState)56 public void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 mCarWifiManager = new CarWifiManager(getContext(), getLifecycle()); 59 60 if (savedInstanceState != null) { 61 mConnectedWifiEntryKey = savedInstanceState.getString( 62 EXTRA_CONNECTED_WIFI_ENTRY_KEY); 63 } 64 } 65 66 @Override onSaveInstanceState(@onNull Bundle outState)67 public void onSaveInstanceState(@NonNull Bundle outState) { 68 super.onSaveInstanceState(outState); 69 outState.putString(EXTRA_CONNECTED_WIFI_ENTRY_KEY, mConnectedWifiEntryKey); 70 } 71 72 @Override setupToolbar(@onNull ToolbarController toolbar)73 protected void setupToolbar(@NonNull ToolbarController toolbar) { 74 super.setupToolbar(toolbar); 75 76 mProgressBar = toolbar.getProgressBar(); 77 } 78 79 @Override onStart()80 public void onStart() { 81 super.onStart(); 82 mCarWifiManager.addListener(this); 83 onWifiStateChanged(mCarWifiManager.getWifiState()); 84 } 85 86 @Override onStop()87 public void onStop() { 88 super.onStop(); 89 mCarWifiManager.removeListener(this); 90 mProgressBar.setVisible(false); 91 } 92 93 @Override onDestroy()94 public void onDestroy() { 95 super.onDestroy(); 96 } 97 98 @Override onWifiEntriesChanged()99 public void onWifiEntriesChanged() { 100 mProgressBar.setVisible(true); 101 getView().postDelayed(() -> mProgressBar.setVisible(false), SEARCHING_DELAY_MILLIS); 102 WifiEntry connectedWifiEntry = mCarWifiManager.getConnectedWifiEntry(); 103 if (connectedWifiEntry != null) { 104 String connectedWifiEntryKey = connectedWifiEntry.getKey(); 105 if (!connectedWifiEntryKey.equals(mConnectedWifiEntryKey)) { 106 scrollToPreference(connectedWifiEntryKey); 107 mConnectedWifiEntryKey = connectedWifiEntryKey; 108 } 109 } else { 110 mConnectedWifiEntryKey = null; 111 } 112 } 113 114 @Override onWifiStateChanged(int state)115 public void onWifiStateChanged(int state) { 116 mProgressBar.setVisible(state == WifiManager.WIFI_STATE_ENABLING); 117 } 118 119 /** 120 * Data provider for Settings Search. 121 */ 122 public static final CarBaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 123 new CarBaseSearchIndexProvider(R.xml.wifi_list_fragment, Settings.ACTION_WIFI_SETTINGS); 124 } 125