1 /* 2 * Copyright (C) 2019 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.savedaccesspoints2; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.net.ConnectivityManager; 22 import android.net.wifi.WifiManager; 23 import android.os.Bundle; 24 import android.os.Handler; 25 import android.os.HandlerThread; 26 import android.os.Looper; 27 import android.os.Process; 28 import android.os.SimpleClock; 29 import android.os.SystemClock; 30 import android.text.TextUtils; 31 import android.util.Log; 32 33 import androidx.annotation.NonNull; 34 import androidx.annotation.VisibleForTesting; 35 import androidx.preference.PreferenceScreen; 36 37 import com.android.settings.R; 38 import com.android.settings.core.SubSettingLauncher; 39 import com.android.settings.dashboard.DashboardFragment; 40 import com.android.settings.network.NetworkProviderSettings; 41 import com.android.settings.wifi.details.WifiNetworkDetailsFragment; 42 import com.android.wifitrackerlib.SavedNetworkTracker; 43 44 import java.time.Clock; 45 import java.time.ZoneOffset; 46 47 /** 48 * UI to manage saved networks/access points. 49 */ 50 public class SavedAccessPointsWifiSettings2 extends DashboardFragment 51 implements SavedNetworkTracker.SavedNetworkTrackerCallback { 52 53 @VisibleForTesting static final String TAG = "SavedAccessPoints2"; 54 55 // Max age of tracked WifiEntries 56 private static final long MAX_SCAN_AGE_MILLIS = 15_000; 57 // Interval between initiating SavedNetworkTracker scans 58 private static final long SCAN_INTERVAL_MILLIS = 10_000; 59 60 @VisibleForTesting SavedNetworkTracker mSavedNetworkTracker; 61 @VisibleForTesting HandlerThread mWorkerThread; 62 63 @Override getMetricsCategory()64 public int getMetricsCategory() { 65 return SettingsEnums.WIFI_SAVED_ACCESS_POINTS; 66 } 67 68 @Override getPreferenceScreenResId()69 protected int getPreferenceScreenResId() { 70 return R.xml.wifi_display_saved_access_points2; 71 } 72 73 @Override getLogTag()74 protected String getLogTag() { 75 return TAG; 76 } 77 78 @Override onAttach(Context context)79 public void onAttach(Context context) { 80 super.onAttach(context); 81 use(SavedAccessPointsPreferenceController2.class).setHost(this); 82 use(SubscribedAccessPointsPreferenceController2.class).setHost(this); 83 } 84 85 @Override onCreate(Bundle savedInstanceState)86 public void onCreate(Bundle savedInstanceState) { 87 super.onCreate(savedInstanceState); 88 89 final Context context = getContext(); 90 mWorkerThread = new HandlerThread(TAG 91 + "{" + Integer.toHexString(System.identityHashCode(this)) + "}", 92 Process.THREAD_PRIORITY_BACKGROUND); 93 mWorkerThread.start(); 94 final Clock elapsedRealtimeClock = new SimpleClock(ZoneOffset.UTC) { 95 @Override 96 public long millis() { 97 return SystemClock.elapsedRealtime(); 98 } 99 }; 100 mSavedNetworkTracker = new SavedNetworkTracker(getSettingsLifecycle(), context, 101 context.getSystemService(WifiManager.class), 102 context.getSystemService(ConnectivityManager.class), 103 new Handler(Looper.getMainLooper()), 104 mWorkerThread.getThreadHandler(), 105 elapsedRealtimeClock, 106 MAX_SCAN_AGE_MILLIS, 107 SCAN_INTERVAL_MILLIS, 108 this); 109 } 110 111 @Override onStart()112 public void onStart() { 113 super.onStart(); 114 115 onSavedWifiEntriesChanged(); 116 onSubscriptionWifiEntriesChanged(); 117 } 118 119 @Override onDestroy()120 public void onDestroy() { 121 mWorkerThread.quit(); 122 123 super.onDestroy(); 124 } 125 126 /** 127 * Shows {@link WifiNetworkDetailsFragment} for assigned key of {@link WifiEntry}. 128 */ showWifiPage(@onNull String key, CharSequence title)129 public void showWifiPage(@NonNull String key, CharSequence title) { 130 removeDialog(NetworkProviderSettings.WIFI_DIALOG_ID); 131 132 if (TextUtils.isEmpty(key)) { 133 Log.e(TAG, "Not able to show WifiEntry of an empty key"); 134 return; 135 } 136 137 final Bundle bundle = new Bundle(); 138 bundle.putString(WifiNetworkDetailsFragment.KEY_CHOSEN_WIFIENTRY_KEY, key); 139 140 new SubSettingLauncher(getContext()) 141 .setTitleText(title) 142 .setDestination(WifiNetworkDetailsFragment.class.getName()) 143 .setArguments(bundle) 144 .setSourceMetricsCategory(getMetricsCategory()) 145 .launch(); 146 } 147 148 @Override onWifiStateChanged()149 public void onWifiStateChanged() { 150 // Do nothing. 151 } 152 153 @Override onSavedWifiEntriesChanged()154 public void onSavedWifiEntriesChanged() { 155 if (isFinishingOrDestroyed()) { 156 return; 157 } 158 final PreferenceScreen screen = getPreferenceScreen(); 159 use(SavedAccessPointsPreferenceController2.class) 160 .displayPreference(screen, mSavedNetworkTracker.getSavedWifiEntries()); 161 } 162 163 @Override onSubscriptionWifiEntriesChanged()164 public void onSubscriptionWifiEntriesChanged() { 165 if (isFinishingOrDestroyed()) { 166 return; 167 } 168 final PreferenceScreen screen = getPreferenceScreen(); 169 use(SubscribedAccessPointsPreferenceController2.class) 170 .displayPreference(screen, mSavedNetworkTracker.getSubscriptionWifiEntries()); 171 } 172 } 173