1 /* 2 * Copyright (C) 2015 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 static android.net.ConnectivityManager.ACTION_PROMPT_LOST_VALIDATION; 20 import static android.net.ConnectivityManager.ACTION_PROMPT_PARTIAL_CONNECTIVITY; 21 import static android.net.ConnectivityManager.ACTION_PROMPT_UNVALIDATED; 22 import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; 23 24 import android.content.Context; 25 import android.content.DialogInterface; 26 import android.content.Intent; 27 import android.net.ConnectivityManager; 28 import android.net.ConnectivityManager.NetworkCallback; 29 import android.net.Network; 30 import android.net.NetworkCapabilities; 31 import android.net.NetworkInfo; 32 import android.net.NetworkRequest; 33 import android.net.wifi.WifiInfo; 34 import android.os.Bundle; 35 import android.provider.Settings; 36 import android.util.Log; 37 import android.view.LayoutInflater; 38 import android.view.View; 39 import android.widget.CheckBox; 40 41 import androidx.annotation.VisibleForTesting; 42 43 import com.android.internal.app.AlertActivity; 44 import com.android.internal.app.AlertController; 45 import com.android.settings.R; 46 47 /** 48 * To display a dialog that asks the user whether to connect to a network that is not validated. 49 */ 50 public class WifiNoInternetDialog extends AlertActivity implements 51 DialogInterface.OnClickListener { 52 private static final String TAG = "WifiNoInternetDialog"; 53 54 private ConnectivityManager mCM; 55 private Network mNetwork; 56 private String mNetworkName; 57 private ConnectivityManager.NetworkCallback mNetworkCallback; 58 @VisibleForTesting CheckBox mAlwaysAllow; 59 private String mAction; 60 private boolean mButtonClicked; 61 isKnownAction(Intent intent)62 private boolean isKnownAction(Intent intent) { 63 return ACTION_PROMPT_UNVALIDATED.equals(intent.getAction()) 64 || ACTION_PROMPT_LOST_VALIDATION.equals(intent.getAction()) 65 || ACTION_PROMPT_PARTIAL_CONNECTIVITY.equals(intent.getAction()); 66 } 67 68 @Override onCreate(Bundle savedInstanceState)69 public void onCreate(Bundle savedInstanceState) { 70 super.onCreate(savedInstanceState); 71 72 final Intent intent = getIntent(); 73 if (intent == null || !isKnownAction(intent)) { 74 Log.e(TAG, "Unexpected intent " + intent + ", exiting"); 75 finish(); 76 return; 77 } 78 79 mAction = intent.getAction(); 80 mNetwork = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK); 81 82 if (mNetwork == null) { 83 Log.e(TAG, "Can't determine network from intent extra, exiting"); 84 finish(); 85 return; 86 } 87 88 // TODO: add a registerNetworkCallback(Network network, NetworkCallback networkCallback) and 89 // simplify this. 90 final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build(); 91 mNetworkCallback = new NetworkCallback() { 92 @Override 93 public void onLost(Network network) { 94 // Close the dialog if the network disconnects. 95 if (mNetwork.equals(network)) { 96 Log.d(TAG, "Network " + mNetwork + " disconnected"); 97 finish(); 98 } 99 } 100 @Override 101 public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) { 102 // Close the dialog if the network validates. 103 if (mNetwork.equals(network) && nc.hasCapability(NET_CAPABILITY_VALIDATED)) { 104 Log.d(TAG, "Network " + mNetwork + " validated"); 105 finish(); 106 } 107 } 108 }; 109 110 mCM = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 111 mCM.registerNetworkCallback(request, mNetworkCallback); 112 113 final NetworkInfo ni = mCM.getNetworkInfo(mNetwork); 114 final NetworkCapabilities nc = mCM.getNetworkCapabilities(mNetwork); 115 if (ni == null || !ni.isConnectedOrConnecting() || nc == null) { 116 Log.d(TAG, "Network " + mNetwork + " is not connected: " + ni); 117 finish(); 118 return; 119 } 120 mNetworkName = nc.getSsid(); 121 if (mNetworkName != null) { 122 mNetworkName = WifiInfo.sanitizeSsid(mNetworkName); 123 } 124 125 createDialog(); 126 } 127 createDialog()128 private void createDialog() { 129 mAlert.setIcon(R.drawable.ic_settings_wireless); 130 131 final AlertController.AlertParams ap = mAlertParams; 132 if (ACTION_PROMPT_UNVALIDATED.equals(mAction)) { 133 ap.mTitle = mNetworkName; 134 ap.mMessage = getString(R.string.no_internet_access_text); 135 ap.mPositiveButtonText = getString(R.string.yes); 136 ap.mNegativeButtonText = getString(R.string.no); 137 } else if (ACTION_PROMPT_PARTIAL_CONNECTIVITY.equals(mAction)) { 138 ap.mTitle = mNetworkName; 139 ap.mMessage = getString(R.string.partial_connectivity_text); 140 ap.mPositiveButtonText = getString(R.string.yes); 141 ap.mNegativeButtonText = getString(R.string.no); 142 } else { 143 ap.mTitle = getString(R.string.lost_internet_access_title); 144 ap.mMessage = getString(R.string.lost_internet_access_text); 145 ap.mPositiveButtonText = getString(R.string.lost_internet_access_switch); 146 ap.mNegativeButtonText = getString(R.string.lost_internet_access_cancel); 147 } 148 ap.mPositiveButtonListener = this; 149 ap.mNegativeButtonListener = this; 150 151 final LayoutInflater inflater = LayoutInflater.from(ap.mContext); 152 final View checkbox = inflater.inflate( 153 com.android.internal.R.layout.always_use_checkbox, null); 154 ap.mView = checkbox; 155 mAlwaysAllow = (CheckBox) checkbox.findViewById(com.android.internal.R.id.alwaysUse); 156 157 if (ACTION_PROMPT_UNVALIDATED.equals(mAction) 158 || ACTION_PROMPT_PARTIAL_CONNECTIVITY.equals(mAction)) { 159 mAlwaysAllow.setText(getString(R.string.no_internet_access_remember)); 160 } else { 161 mAlwaysAllow.setText(getString(R.string.lost_internet_access_persist)); 162 } 163 164 setupAlert(); 165 } 166 167 @Override onDestroy()168 protected void onDestroy() { 169 if (mNetworkCallback != null) { 170 mCM.unregisterNetworkCallback(mNetworkCallback); 171 mNetworkCallback = null; 172 } 173 174 // If the user exits the no Internet or partial connectivity dialog without taking any 175 // action, disconnect the network, because once the dialog has been dismissed there is no 176 // way to use the network. 177 // 178 // Unfortunately, AlertDialog does not seem to offer any good way to get an onCancel or 179 // onDismiss callback. So we implement this ourselves. 180 if (isFinishing() && !mButtonClicked) { 181 if (ACTION_PROMPT_PARTIAL_CONNECTIVITY.equals(mAction)) { 182 mCM.setAcceptPartialConnectivity(mNetwork, false /* accept */, false /* always */); 183 } else if (ACTION_PROMPT_UNVALIDATED.equals(mAction)) { 184 mCM.setAcceptUnvalidated(mNetwork, false /* accept */, false /* always */); 185 } 186 } 187 super.onDestroy(); 188 } 189 190 @Override onClick(DialogInterface dialog, int which)191 public void onClick(DialogInterface dialog, int which) { 192 if (which != BUTTON_NEGATIVE && which != BUTTON_POSITIVE) return; 193 final boolean always = mAlwaysAllow.isChecked(); 194 final String what, action; 195 196 mButtonClicked = true; 197 198 if (ACTION_PROMPT_UNVALIDATED.equals(mAction)) { 199 what = "NO_INTERNET"; 200 final boolean accept = (which == BUTTON_POSITIVE); 201 action = (accept ? "Connect" : "Ignore"); 202 mCM.setAcceptUnvalidated(mNetwork, accept, always); 203 } else if (ACTION_PROMPT_PARTIAL_CONNECTIVITY.equals(mAction)) { 204 what = "PARTIAL_CONNECTIVITY"; 205 final boolean accept = (which == BUTTON_POSITIVE); 206 action = (accept ? "Connect" : "Ignore"); 207 mCM.setAcceptPartialConnectivity(mNetwork, accept, always); 208 } else { 209 what = "LOST_INTERNET"; 210 final boolean avoid = (which == BUTTON_POSITIVE); 211 action = (avoid ? "Switch away" : "Get stuck"); 212 if (always) { 213 Settings.Global.putString(mAlertParams.mContext.getContentResolver(), 214 Settings.Global.NETWORK_AVOID_BAD_WIFI, avoid ? "1" : "0"); 215 } else if (avoid) { 216 mCM.setAvoidUnvalidated(mNetwork); 217 } 218 } 219 Log.d(TAG, what + ": " + action + " network=" + mNetwork + 220 (always ? " and remember" : "")); 221 } 222 } 223