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.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.wifi.WifiSettings;
41 import com.android.settings.wifi.details2.WifiNetworkDetailsFragment2;
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     private 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     private SavedNetworkTracker mSavedNetworkTracker;
61     private 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                 context.getSystemService(NetworkScoreManager.class),
104                 new Handler(Looper.getMainLooper()),
105                 mWorkerThread.getThreadHandler(),
106                 elapsedRealtimeClock,
107                 MAX_SCAN_AGE_MILLIS,
108                 SCAN_INTERVAL_MILLIS,
109                 this);
110     }
111 
112     @Override
onStart()113     public void onStart() {
114         super.onStart();
115 
116         onSavedWifiEntriesChanged();
117         onSubscriptionWifiEntriesChanged();
118     }
119 
120     @Override
onDestroy()121     public void onDestroy() {
122         mWorkerThread.quit();
123 
124         super.onDestroy();
125     }
126 
127     /**
128      * Shows {@link WifiNetworkDetailsFragment2} for assigned key of {@link WifiEntry}.
129      */
showWifiPage(@onNull String key, CharSequence title)130     public void showWifiPage(@NonNull String key, CharSequence title) {
131         removeDialog(WifiSettings.WIFI_DIALOG_ID);
132 
133         if (TextUtils.isEmpty(key)) {
134             Log.e(TAG, "Not able to show WifiEntry of an empty key");
135             return;
136         }
137 
138         final Bundle bundle = new Bundle();
139         bundle.putString(WifiNetworkDetailsFragment2.KEY_CHOSEN_WIFIENTRY_KEY, key);
140 
141         new SubSettingLauncher(getContext())
142                 .setTitleText(title)
143                 .setDestination(WifiNetworkDetailsFragment2.class.getName())
144                 .setArguments(bundle)
145                 .setSourceMetricsCategory(getMetricsCategory())
146                 .launch();
147     }
148 
149     @Override
onWifiStateChanged()150     public void onWifiStateChanged() {
151         // Do nothing.
152     }
153 
154     @Override
onSavedWifiEntriesChanged()155     public void onSavedWifiEntriesChanged() {
156         if (isFinishingOrDestroyed()) {
157             return;
158         }
159         final PreferenceScreen screen = getPreferenceScreen();
160         use(SavedAccessPointsPreferenceController2.class)
161                 .displayPreference(screen, mSavedNetworkTracker.getSavedWifiEntries());
162     }
163 
164     @Override
onSubscriptionWifiEntriesChanged()165     public void onSubscriptionWifiEntriesChanged() {
166         if (isFinishingOrDestroyed()) {
167             return;
168         }
169         final PreferenceScreen screen = getPreferenceScreen();
170         use(SubscribedAccessPointsPreferenceController2.class)
171                 .displayPreference(screen, mSavedNetworkTracker.getSubscriptionWifiEntries());
172     }
173 }
174