Support displaying a dialog when Wi-Fi loses Internet access.

Bug: 31075769
Change-Id: Ia0c76d80591937dc40765c437e5779f3b6dde217
This commit is contained in:
Lorenzo Colitti
2016-09-15 14:06:46 +09:00
parent 27aabcd4df
commit 1449ecd460
3 changed files with 55 additions and 24 deletions

View File

@@ -385,6 +385,10 @@
<action android:name="android.net.conn.PROMPT_UNVALIDATED" /> <action android:name="android.net.conn.PROMPT_UNVALIDATED" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.net.conn.PROMPT_LOST_VALIDATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED" <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
android:value="true" /> android:value="true" />
</activity> </activity>

View File

@@ -1746,7 +1746,14 @@
<!-- Dialog text to tell the user that the selected network does not have Internet access. --> <!-- Dialog text to tell the user that the selected network does not have Internet access. -->
<string name="no_internet_access_text">This network has no Internet access. Stay connected?</string> <string name="no_internet_access_text">This network has no Internet access. Stay connected?</string>
<string name="no_internet_access_remember">Don\u2019t ask again for this network</string> <string name="no_internet_access_remember">Don\u2019t ask again</string>
<!-- Dialog text to tell the user that the selected network has lost Internet access, and asking the usee whether they want to avoid this network. -->
<string name="lost_internet_access_title">Wi\u2011Fi has no Internet access</string>
<string name="lost_internet_access_text">Your device can automatically switch to other saved Wi\u2011Fi networks or cellular data. Cellular charges may apply.</string>
<string name="lost_internet_access_switch">Switch networks</string>
<string name="lost_internet_access_cancel">Cancel</string>
<!-- Button label to connect to a Wi-Fi network --> <!-- Button label to connect to a Wi-Fi network -->
<string name="wifi_connect">Connect</string> <string name="wifi_connect">Connect</string>

View File

@@ -26,6 +26,7 @@ import android.net.NetworkCapabilities;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.NetworkRequest; import android.net.NetworkRequest;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@@ -35,6 +36,8 @@ import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController; import com.android.internal.app.AlertController;
import com.android.settings.R; import com.android.settings.R;
import static android.net.ConnectivityManager.ACTION_PROMPT_LOST_VALIDATION;
import static android.net.ConnectivityManager.ACTION_PROMPT_UNVALIDATED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
public final class WifiNoInternetDialog extends AlertActivity implements public final class WifiNoInternetDialog extends AlertActivity implements
@@ -46,20 +49,26 @@ public final class WifiNoInternetDialog extends AlertActivity implements
private String mNetworkName; private String mNetworkName;
private ConnectivityManager.NetworkCallback mNetworkCallback; private ConnectivityManager.NetworkCallback mNetworkCallback;
private CheckBox mAlwaysAllow; private CheckBox mAlwaysAllow;
private String mAction;
private boolean isKnownAction(Intent intent) {
return intent.getAction().equals(ACTION_PROMPT_UNVALIDATED) ||
intent.getAction().equals(ACTION_PROMPT_LOST_VALIDATION);
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
final Intent intent = getIntent(); final Intent intent = getIntent();
if (intent == null || if (intent == null || !isKnownAction(intent) || !"netId".equals(intent.getScheme())) {
!intent.getAction().equals(ConnectivityManager.ACTION_PROMPT_UNVALIDATED) ||
!"netId".equals(intent.getScheme())) {
Log.e(TAG, "Unexpected intent " + intent + ", exiting"); Log.e(TAG, "Unexpected intent " + intent + ", exiting");
finish(); finish();
return; return;
} }
mAction = intent.getAction();
try { try {
mNetwork = new Network(Integer.parseInt(intent.getData().getSchemeSpecificPart())); mNetwork = new Network(Integer.parseInt(intent.getData().getSchemeSpecificPart()));
} catch (NullPointerException|NumberFormatException e) { } catch (NullPointerException|NumberFormatException e) {
@@ -115,20 +124,29 @@ public final class WifiNoInternetDialog extends AlertActivity implements
mAlert.setIcon(R.drawable.ic_settings_wireless); mAlert.setIcon(R.drawable.ic_settings_wireless);
final AlertController.AlertParams ap = mAlertParams; final AlertController.AlertParams ap = mAlertParams;
ap.mTitle = mNetworkName; if (ACTION_PROMPT_UNVALIDATED.equals(mAction)) {
ap.mMessage = getString(R.string.no_internet_access_text); ap.mTitle = mNetworkName;
ap.mPositiveButtonText = getString(R.string.yes); ap.mMessage = getString(R.string.no_internet_access_text);
ap.mNegativeButtonText = getString(R.string.no); ap.mPositiveButtonText = getString(R.string.yes);
ap.mNegativeButtonText = getString(R.string.no);
} else {
ap.mTitle = getString(R.string.lost_internet_access_title);
ap.mMessage = getString(R.string.lost_internet_access_text);
ap.mPositiveButtonText = getString(R.string.lost_internet_access_switch);
ap.mNegativeButtonText = getString(R.string.lost_internet_access_cancel);
}
ap.mPositiveButtonListener = this; ap.mPositiveButtonListener = this;
ap.mNegativeButtonListener = this; ap.mNegativeButtonListener = this;
final LayoutInflater inflater = LayoutInflater.from(ap.mContext); if (ACTION_PROMPT_UNVALIDATED.equals(mAction)) {
final View checkbox = inflater.inflate( final LayoutInflater inflater = LayoutInflater.from(ap.mContext);
com.android.internal.R.layout.always_use_checkbox, null); final View checkbox = inflater.inflate(
ap.mView = checkbox; com.android.internal.R.layout.always_use_checkbox, null);
ap.mView = checkbox;
mAlwaysAllow = (CheckBox) checkbox.findViewById(com.android.internal.R.id.alwaysUse); mAlwaysAllow = (CheckBox) checkbox.findViewById(com.android.internal.R.id.alwaysUse);
mAlwaysAllow.setText(getString(R.string.no_internet_access_remember)); mAlwaysAllow.setText(getString(R.string.no_internet_access_remember));
}
setupAlert(); setupAlert();
} }
@@ -143,18 +161,20 @@ public final class WifiNoInternetDialog extends AlertActivity implements
} }
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if (which != BUTTON_NEGATIVE && which != BUTTON_POSITIVE) return;
final boolean accept = (which == BUTTON_POSITIVE); final boolean accept = (which == BUTTON_POSITIVE);
final String action = (accept ? "Connect" : "Ignore");
final boolean always = mAlwaysAllow.isChecked();
switch (which) { if (ACTION_PROMPT_UNVALIDATED.equals(mAction)) {
case BUTTON_POSITIVE: final String action = (accept ? "Connect" : "Ignore");
case BUTTON_NEGATIVE: final boolean always = mAlwaysAllow.isChecked();
mCM.setAcceptUnvalidated(mNetwork, accept, always); mCM.setAcceptUnvalidated(mNetwork, accept, always);
Log.d(TAG, action + " network=" + mNetwork + (always ? " and remember" : "")); Log.d(TAG, "NO_INTERNET: " + action + " network=" + mNetwork +
break; (always ? " and remember" : ""));
default: } else {
break; final String action = (accept ? "Switch" : "Cancel");
Log.d(TAG, "LOST_INTERNET: " + action);
Settings.Global.putInt(mAlertParams.mContext.getContentResolver(),
Settings.Global.NETWORK_AVOID_BAD_WIFI, accept ? 1 : 0);
} }
} }
} }