• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.car.settings.wifi;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.net.wifi.WifiManager;
25 import android.os.Handler;
26 import android.os.Looper;
27 import android.text.TextUtils;
28 import android.widget.Toast;
29 
30 import androidx.annotation.VisibleForTesting;
31 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
32 
33 import com.android.car.settings.R;
34 import com.android.car.settings.common.FragmentController;
35 import com.android.car.settings.common.Logger;
36 import com.android.car.settings.common.PreferenceController;
37 import com.android.wifitrackerlib.WifiEntry;
38 
39 /** Business logic relating to the security type and associated password. */
40 public class NetworkPasswordPreferenceController extends
41         PreferenceController<NetworkNameRestrictedPasswordEditTextPreference> {
42 
43     private static final Logger LOG = new Logger(NetworkPasswordPreferenceController.class);
44 
45     @VisibleForTesting
46     final BroadcastReceiver mNameChangeReceiver = new BroadcastReceiver() {
47         @Override
48         public void onReceive(Context context, Intent intent) {
49             mNetworkName = intent.getStringExtra(NetworkNamePreferenceController.KEY_NETWORK_NAME);
50             getPreference().setNetworkName(mNetworkName);
51             refreshUi();
52         }
53     };
54 
55     @VisibleForTesting
56     final BroadcastReceiver mSecurityChangeReceiver = new BroadcastReceiver() {
57         @Override
58         public void onReceive(Context context, Intent intent) {
59             mSecurityType = intent.getIntExtra(
60                     NetworkSecurityPreferenceController.KEY_SECURITY_TYPE,
61                     WifiEntry.SECURITY_NONE);
62             refreshUi();
63         }
64     };
65 
66     private final Handler mUiHandler = new Handler(Looper.getMainLooper());
67     private final WifiManager.ActionListener mConnectionListener =
68             new WifiManager.ActionListener() {
69                 @Override
70                 public void onSuccess() {
71                     LOG.d("connected to network");
72                     mUiHandler.post(() -> getFragmentController().goBack());
73                 }
74 
75                 @Override
76                 public void onFailure(int reason) {
77                     LOG.d("Failed to connect to network. Failure code: " + reason);
78                     Toast.makeText(getContext(), R.string.wifi_failed_connect_message,
79                             Toast.LENGTH_SHORT).show();
80                 }
81             };
82 
83     private String mNetworkName;
84     private int mSecurityType = WifiEntry.SECURITY_NONE;
85 
NetworkPasswordPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)86     public NetworkPasswordPreferenceController(Context context, String preferenceKey,
87             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
88         super(context, preferenceKey, fragmentController, uxRestrictions);
89     }
90 
91     @Override
getPreferenceType()92     protected Class<NetworkNameRestrictedPasswordEditTextPreference> getPreferenceType() {
93         return NetworkNameRestrictedPasswordEditTextPreference.class;
94     }
95 
96     @Override
onCreateInternal()97     protected void onCreateInternal() {
98         LocalBroadcastManager.getInstance(getContext()).registerReceiver(mNameChangeReceiver,
99                 new IntentFilter(NetworkNamePreferenceController.ACTION_NAME_CHANGE));
100         LocalBroadcastManager.getInstance(getContext()).registerReceiver(mSecurityChangeReceiver,
101                 new IntentFilter(NetworkSecurityPreferenceController.ACTION_SECURITY_CHANGE));
102     }
103 
104     @Override
onDestroyInternal()105     protected void onDestroyInternal() {
106         LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mNameChangeReceiver);
107         LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mSecurityChangeReceiver);
108     }
109 
110     @Override
updateState(NetworkNameRestrictedPasswordEditTextPreference preference)111     protected void updateState(NetworkNameRestrictedPasswordEditTextPreference preference) {
112         if (TextUtils.isEmpty(mNetworkName)) {
113             getPreference().setDialogTitle(R.string.wifi_password);
114         } else {
115             getPreference().setDialogTitle(mNetworkName);
116         }
117         preference.setVisible(!WifiUtil.isOpenNetwork(mSecurityType));
118     }
119 
120     @Override
handlePreferenceChanged( NetworkNameRestrictedPasswordEditTextPreference preference, Object newValue)121     protected boolean handlePreferenceChanged(
122             NetworkNameRestrictedPasswordEditTextPreference preference, Object newValue) {
123         String password = newValue.toString();
124         WifiUtil.connectToWifiEntry(getContext(), mNetworkName, mSecurityType,
125                 password, /* hidden= */ true, mConnectionListener);
126         return true;
127     }
128 }
129