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.settings.wifi; 18 19 import android.app.Activity; 20 import android.app.settings.SettingsEnums; 21 import android.content.Intent; 22 import android.net.wifi.WifiConfiguration; 23 import android.os.Bundle; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.Button; 28 import android.widget.ImageButton; 29 import android.widget.TextView; 30 31 import androidx.annotation.VisibleForTesting; 32 33 import com.android.settings.R; 34 import com.android.settings.core.InstrumentedFragment; 35 import com.android.settings.wifi.dpp.WifiDppUtils; 36 37 public class AddNetworkFragment extends InstrumentedFragment implements WifiConfigUiBase, 38 View.OnClickListener { 39 40 final static String WIFI_CONFIG_KEY = "wifi_config_key"; 41 @VisibleForTesting 42 final static int SUBMIT_BUTTON_ID = android.R.id.button1; 43 @VisibleForTesting 44 final static int CANCEL_BUTTON_ID = android.R.id.button2; 45 final static int SSID_SCANNER_BUTTON_ID = R.id.ssid_scanner_button; 46 final static int PASSWORD_SCANNER_BUTTON_ID = R.id.password_scanner_button; 47 48 private static final int REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER = 0; 49 50 private WifiConfigController mUIController; 51 private Button mSubmitBtn; 52 private Button mCancelBtn; 53 54 @Override onCreate(Bundle savedInstanceState)55 public void onCreate(Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 } 58 59 @Override getMetricsCategory()60 public int getMetricsCategory() { 61 return SettingsEnums.SETTINGS_WIFI_ADD_NETWORK; 62 } 63 64 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)65 public View onCreateView(LayoutInflater inflater, ViewGroup container, 66 Bundle savedInstanceState) { 67 final View rootView = inflater.inflate(R.layout.wifi_add_network_view, container, false); 68 69 final Button neutral = rootView.findViewById(android.R.id.button3); 70 if (neutral != null) { 71 neutral.setVisibility(View.GONE); 72 } 73 74 mSubmitBtn = rootView.findViewById(SUBMIT_BUTTON_ID); 75 mCancelBtn = rootView.findViewById(CANCEL_BUTTON_ID); 76 final ImageButton ssidScannerButton = rootView.findViewById(SSID_SCANNER_BUTTON_ID); 77 final ImageButton passwordScannerButton = rootView.findViewById(PASSWORD_SCANNER_BUTTON_ID); 78 mSubmitBtn.setOnClickListener(this); 79 mCancelBtn.setOnClickListener(this); 80 ssidScannerButton.setOnClickListener(this); 81 passwordScannerButton.setOnClickListener(this); 82 mUIController = new WifiConfigController(this, rootView, null, getMode()); 83 84 return rootView; 85 } 86 87 @Override onViewStateRestored(Bundle savedInstanceState)88 public void onViewStateRestored(Bundle savedInstanceState) { 89 super.onViewStateRestored(savedInstanceState); 90 mUIController.updatePassword(); 91 } 92 93 @Override onClick(View view)94 public void onClick(View view) { 95 String ssid = null; 96 97 if (view.getId() == SUBMIT_BUTTON_ID) { 98 handleSubmitAction(); 99 } else if (view.getId() == CANCEL_BUTTON_ID) { 100 handleCancelAction(); 101 } else if (view.getId() == SSID_SCANNER_BUTTON_ID) { 102 final TextView ssidEditText = getView().findViewById(R.id.ssid); 103 ssid = ssidEditText.getText().toString(); 104 // No break and flows to case PASSWORD_SCANNER_BUTTON_ID 105 106 // Launch QR code scanner to join a network. 107 startActivityForResult(WifiDppUtils.getEnrolleeQrCodeScannerIntent(ssid), 108 REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER); 109 } else if (view.getId() 110 == PASSWORD_SCANNER_BUTTON_ID) {// Launch QR code scanner to join a network. 111 startActivityForResult(WifiDppUtils.getEnrolleeQrCodeScannerIntent(ssid), 112 REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER); 113 } 114 } 115 116 @Override onActivityResult(int requestCode, int resultCode, Intent data)117 public void onActivityResult(int requestCode, int resultCode, Intent data) { 118 super.onActivityResult(requestCode, resultCode, data); 119 120 if (requestCode == REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER) { 121 if (resultCode != Activity.RESULT_OK) { 122 return; 123 } 124 125 final WifiConfiguration config = data.getParcelableExtra( 126 WifiDialogActivity.KEY_WIFI_CONFIGURATION); 127 successfullyFinish(config); 128 } 129 } 130 131 @Override getMode()132 public int getMode() { 133 return WifiConfigUiBase.MODE_CONNECT; 134 } 135 136 @Override getController()137 public WifiConfigController getController() { 138 return mUIController; 139 } 140 141 @Override dispatchSubmit()142 public void dispatchSubmit() { 143 handleSubmitAction(); 144 } 145 146 @Override setTitle(int id)147 public void setTitle(int id) { 148 getActivity().setTitle(id); 149 } 150 151 @Override setTitle(CharSequence title)152 public void setTitle(CharSequence title) { 153 getActivity().setTitle(title); 154 } 155 156 @Override setSubmitButton(CharSequence text)157 public void setSubmitButton(CharSequence text) { 158 mSubmitBtn.setText(text); 159 } 160 161 @Override setCancelButton(CharSequence text)162 public void setCancelButton(CharSequence text) { 163 mCancelBtn.setText(text); 164 } 165 166 @Override setForgetButton(CharSequence text)167 public void setForgetButton(CharSequence text) { 168 // AddNetwork doesn't need forget button. 169 } 170 171 @Override getSubmitButton()172 public Button getSubmitButton() { 173 return mSubmitBtn; 174 } 175 176 @Override getCancelButton()177 public Button getCancelButton() { 178 return mCancelBtn; 179 } 180 181 @Override getForgetButton()182 public Button getForgetButton() { 183 // AddNetwork doesn't need forget button. 184 return null; 185 } 186 187 @VisibleForTesting handleSubmitAction()188 void handleSubmitAction() { 189 successfullyFinish(mUIController.getConfig()); 190 } 191 successfullyFinish(WifiConfiguration config)192 private void successfullyFinish(WifiConfiguration config) { 193 final Intent intent = new Intent(); 194 final Activity activity = getActivity(); 195 intent.putExtra(WIFI_CONFIG_KEY, config); 196 activity.setResult(Activity.RESULT_OK, intent); 197 activity.finish(); 198 } 199 200 @VisibleForTesting handleCancelAction()201 void handleCancelAction() { 202 final Activity activity = getActivity(); 203 activity.setResult(Activity.RESULT_CANCELED); 204 activity.finish(); 205 } 206 } 207