• 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;
18 
19 import android.app.ActionBar;
20 import android.app.Activity;
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.net.ConnectivityManager;
25 import android.net.NetworkScoreManager;
26 import android.net.wifi.WifiManager;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.HandlerThread;
30 import android.os.Looper;
31 import android.os.Process;
32 import android.os.SimpleClock;
33 import android.os.SystemClock;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.Button;
38 
39 import androidx.annotation.VisibleForTesting;
40 
41 import com.android.settings.R;
42 import com.android.settings.core.InstrumentedFragment;
43 import com.android.settings.wifi.details2.WifiNetworkDetailsFragment2;
44 import com.android.wifitrackerlib.NetworkDetailsTracker;
45 import com.android.wifitrackerlib.WifiEntry;
46 
47 import java.time.Clock;
48 import java.time.ZoneOffset;
49 
50 /**
51  * Detail page for configuring Wi-Fi network.
52  *
53  * The WifiEntry should be saved to the argument when launching this class in order to properly
54  * render this page.
55  */
56 public class ConfigureWifiEntryFragment extends InstrumentedFragment implements WifiConfigUiBase2 {
57 
58     private static final String TAG = "ConfigureWifiEntryFragment";
59 
60     public static final String NETWORK_CONFIG_KEY = "network_config_key";
61 
62     private static final int SUBMIT_BUTTON_ID = android.R.id.button1;
63     private static final int CANCEL_BUTTON_ID = android.R.id.button2;
64 
65     // Max age of tracked WifiEntries
66     private static final long MAX_SCAN_AGE_MILLIS = 15_000;
67     // Interval between initiating SavedNetworkTracker scans
68     private static final long SCAN_INTERVAL_MILLIS = 10_000;
69 
70     private WifiConfigController2 mUiController;
71     private Button mSubmitBtn;
72     private Button mCancelBtn;
73     private WifiEntry mWifiEntry;
74     @VisibleForTesting
75     NetworkDetailsTracker mNetworkDetailsTracker;
76     private HandlerThread mWorkerThread;
77 
78     @Override
onAttach(Context context)79     public void onAttach(Context context) {
80         super.onAttach(context);
81 
82         setupNetworkDetailsTracker();
83         mWifiEntry = mNetworkDetailsTracker.getWifiEntry();
84     }
85 
86     @Override
onDestroy()87     public void onDestroy() {
88         if (mWorkerThread != null) {
89             mWorkerThread.quit();
90         }
91 
92         super.onDestroy();
93     }
94 
95     @Override
getMetricsCategory()96     public int getMetricsCategory() {
97         return SettingsEnums.SETTINGS_WIFI_CONFIGURE_NETWORK;
98     }
99 
100     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)101     public View onCreateView(LayoutInflater inflater, ViewGroup container,
102             Bundle savedInstanceState) {
103         final View rootView = inflater.inflate(R.layout.wifi_add_network_view,
104                 container, false /* attachToRoot */);
105 
106         final Button neutral = rootView.findViewById(android.R.id.button3);
107         if (neutral != null) {
108             neutral.setVisibility(View.GONE);
109         }
110 
111         mSubmitBtn = rootView.findViewById(SUBMIT_BUTTON_ID);
112         mCancelBtn = rootView.findViewById(CANCEL_BUTTON_ID);
113         mSubmitBtn.setOnClickListener(view -> handleSubmitAction());
114         mCancelBtn.setOnClickListener(view -> handleCancelAction());
115 
116         mUiController = new WifiConfigController2(this, rootView, mWifiEntry, getMode());
117 
118         /**
119          * For this add WifiEntry UI, need to remove the Home button, so set related feature as
120          * false.
121          */
122         final ActionBar actionBar = getActivity().getActionBar();
123         if (actionBar != null) {
124             actionBar.setDisplayHomeAsUpEnabled(false);
125             actionBar.setHomeButtonEnabled(false);
126             actionBar.setDisplayShowHomeEnabled(false);
127         }
128 
129         return rootView;
130     }
131 
132     @Override
onViewStateRestored(Bundle savedInstanceState)133     public void onViewStateRestored(Bundle savedInstanceState) {
134         super.onViewStateRestored(savedInstanceState);
135         mUiController.updatePassword();
136     }
137 
138     @Override
getMode()139     public int getMode() {
140         return WifiConfigUiBase2.MODE_CONNECT;
141     }
142 
143     @Override
getController()144     public WifiConfigController2 getController() {
145         return mUiController;
146     }
147 
148     @Override
dispatchSubmit()149     public void dispatchSubmit() {
150         handleSubmitAction();
151     }
152 
153     @Override
setTitle(int id)154     public void setTitle(int id) {
155         getActivity().setTitle(id);
156     }
157 
158     @Override
setTitle(CharSequence title)159     public void setTitle(CharSequence title) {
160         getActivity().setTitle(title);
161     }
162 
163     @Override
setSubmitButton(CharSequence text)164     public void setSubmitButton(CharSequence text) {
165         mSubmitBtn.setText(text);
166     }
167 
168     @Override
setCancelButton(CharSequence text)169     public void setCancelButton(CharSequence text) {
170         mCancelBtn.setText(text);
171     }
172 
173     @Override
setForgetButton(CharSequence text)174     public void setForgetButton(CharSequence text) {
175         // AddNetwork doesn't need forget button.
176     }
177 
178     @Override
getSubmitButton()179     public Button getSubmitButton() {
180         return mSubmitBtn;
181     }
182 
183     @Override
getCancelButton()184     public Button getCancelButton() {
185         return mCancelBtn;
186     }
187 
188     @Override
getForgetButton()189     public Button getForgetButton() {
190         // AddNetwork doesn't need forget button.
191         return null;
192     }
193 
194     @VisibleForTesting
handleSubmitAction()195     void handleSubmitAction() {
196         final Intent intent = new Intent();
197         final Activity activity = getActivity();
198         intent.putExtra(NETWORK_CONFIG_KEY, mUiController.getConfig());
199         activity.setResult(Activity.RESULT_OK, intent);
200         activity.finish();
201     }
202 
203     @VisibleForTesting
handleCancelAction()204     void handleCancelAction() {
205         getActivity().finish();
206     }
207 
setupNetworkDetailsTracker()208     private void setupNetworkDetailsTracker() {
209         if (mNetworkDetailsTracker != null) {
210             return;
211         }
212 
213         final Context context = getContext();
214         mWorkerThread = new HandlerThread(TAG
215                 + "{" + Integer.toHexString(System.identityHashCode(this)) + "}",
216                 Process.THREAD_PRIORITY_BACKGROUND);
217         mWorkerThread.start();
218         final Clock elapsedRealtimeClock = new SimpleClock(ZoneOffset.UTC) {
219             @Override
220             public long millis() {
221                 return SystemClock.elapsedRealtime();
222             }
223         };
224 
225         mNetworkDetailsTracker = NetworkDetailsTracker.createNetworkDetailsTracker(
226                 getSettingsLifecycle(),
227                 context,
228                 context.getSystemService(WifiManager.class),
229                 context.getSystemService(ConnectivityManager.class),
230                 context.getSystemService(NetworkScoreManager.class),
231                 new Handler(Looper.getMainLooper()),
232                 mWorkerThread.getThreadHandler(),
233                 elapsedRealtimeClock,
234                 MAX_SCAN_AGE_MILLIS,
235                 SCAN_INTERVAL_MILLIS,
236                 getArguments().getString(WifiNetworkDetailsFragment2.KEY_CHOSEN_WIFIENTRY_KEY));
237     }
238 }
239