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