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