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.text.TextUtils; 25 26 import androidx.localbroadcastmanager.content.LocalBroadcastManager; 27 28 import com.android.car.settings.R; 29 import com.android.car.settings.common.FragmentController; 30 import com.android.car.settings.common.Logger; 31 import com.android.car.settings.common.PreferenceController; 32 import com.android.settingslib.wifi.AccessPoint; 33 34 /** Business logic relating to the security type and associated password. */ 35 public class NetworkPasswordPreferenceController extends 36 PreferenceController<NetworkNameRestrictedPasswordEditTextPreference> { 37 38 private static final Logger LOG = new Logger(NetworkPasswordPreferenceController.class); 39 40 private final BroadcastReceiver mNameChangeReceiver = new BroadcastReceiver() { 41 @Override 42 public void onReceive(Context context, Intent intent) { 43 mNetworkName = intent.getStringExtra(NetworkNamePreferenceController.KEY_NETWORK_NAME); 44 getPreference().setNetworkName(mNetworkName); 45 refreshUi(); 46 } 47 }; 48 49 private final BroadcastReceiver mSecurityChangeReceiver = new BroadcastReceiver() { 50 @Override 51 public void onReceive(Context context, Intent intent) { 52 mSecurityType = intent.getIntExtra( 53 NetworkSecurityPreferenceController.KEY_SECURITY_TYPE, 54 AccessPoint.SECURITY_NONE); 55 refreshUi(); 56 } 57 }; 58 59 private String mNetworkName; 60 private int mSecurityType = AccessPoint.SECURITY_NONE; 61 NetworkPasswordPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)62 public NetworkPasswordPreferenceController(Context context, String preferenceKey, 63 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 64 super(context, preferenceKey, fragmentController, uxRestrictions); 65 } 66 67 @Override getPreferenceType()68 protected Class<NetworkNameRestrictedPasswordEditTextPreference> getPreferenceType() { 69 return NetworkNameRestrictedPasswordEditTextPreference.class; 70 } 71 72 @Override onStartInternal()73 protected void onStartInternal() { 74 LocalBroadcastManager.getInstance(getContext()).registerReceiver(mNameChangeReceiver, 75 new IntentFilter(NetworkNamePreferenceController.ACTION_NAME_CHANGE)); 76 LocalBroadcastManager.getInstance(getContext()).registerReceiver(mSecurityChangeReceiver, 77 new IntentFilter(NetworkSecurityPreferenceController.ACTION_SECURITY_CHANGE)); 78 } 79 80 @Override onStopInternal()81 protected void onStopInternal() { 82 LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mNameChangeReceiver); 83 LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mSecurityChangeReceiver); 84 } 85 86 @Override updateState(NetworkNameRestrictedPasswordEditTextPreference preference)87 protected void updateState(NetworkNameRestrictedPasswordEditTextPreference preference) { 88 if (TextUtils.isEmpty(mNetworkName)) { 89 getPreference().setDialogTitle(R.string.wifi_password); 90 } else { 91 getPreference().setDialogTitle(mNetworkName); 92 } 93 preference.setVisible(mSecurityType != AccessPoint.SECURITY_NONE); 94 } 95 96 @Override handlePreferenceChanged( NetworkNameRestrictedPasswordEditTextPreference preference, Object newValue)97 protected boolean handlePreferenceChanged( 98 NetworkNameRestrictedPasswordEditTextPreference preference, Object newValue) { 99 String password = newValue.toString(); 100 int netId = WifiUtil.connectToAccessPoint(getContext(), mNetworkName, mSecurityType, 101 password, /* hidden= */ true); 102 103 LOG.d("connected to netId: " + netId); 104 if (netId != WifiUtil.INVALID_NET_ID) { 105 getFragmentController().goBack(); 106 } 107 108 return true; 109 } 110 } 111