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