Home | History | Annotate | Download | only in wifi
      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 android.content.Context;
     20 import android.content.DialogInterface;
     21 import android.content.Intent;
     22 import android.net.ConnectivityManager;
     23 import android.net.ConnectivityManager.NetworkCallback;
     24 import android.net.Network;
     25 import android.net.NetworkInfo;
     26 import android.net.NetworkCapabilities;
     27 import android.net.NetworkRequest;
     28 import android.os.Bundle;
     29 import android.util.Log;
     30 import android.view.LayoutInflater;
     31 import android.view.View;
     32 import android.widget.CheckBox;
     33 import android.widget.TextView;
     34 
     35 import com.android.internal.app.AlertActivity;
     36 import com.android.internal.app.AlertController;
     37 import com.android.settings.R;
     38 
     39 import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
     40 
     41 public final class WifiNoInternetDialog extends AlertActivity implements
     42         DialogInterface.OnClickListener {
     43     private static final String TAG = "WifiNoInternetDialog";
     44 
     45     private ConnectivityManager mCM;
     46     private Network mNetwork;
     47     private String mNetworkName;
     48     private ConnectivityManager.NetworkCallback mNetworkCallback;
     49     private CheckBox mAlwaysAllow;
     50 
     51     @Override
     52     public void onCreate(Bundle savedInstanceState) {
     53         super.onCreate(savedInstanceState);
     54 
     55         final Intent intent = getIntent();
     56         if (intent == null ||
     57                 !intent.getAction().equals(ConnectivityManager.ACTION_PROMPT_UNVALIDATED) ||
     58                 !"netId".equals(intent.getScheme())) {
     59             Log.e(TAG, "Unexpected intent " + intent + ", exiting");
     60             finish();
     61             return;
     62         }
     63 
     64         try {
     65             mNetwork = new Network(Integer.parseInt(intent.getData().getSchemeSpecificPart()));
     66         } catch (NullPointerException|NumberFormatException e) {
     67             mNetwork = null;
     68         }
     69 
     70         if (mNetwork == null) {
     71             Log.e(TAG, "Can't determine network from '" + intent.getData() + "' , exiting");
     72             finish();
     73             return;
     74         }
     75 
     76         // TODO: add a registerNetworkCallback(Network network, NetworkCallback networkCallback) and
     77         // simplify this.
     78         final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
     79         mNetworkCallback = new NetworkCallback() {
     80             @Override
     81             public void onLost(Network network) {
     82                 // Close the dialog if the network disconnects.
     83                 if (mNetwork.equals(network)) {
     84                     Log.d(TAG, "Network " + mNetwork + " disconnected");
     85                     finish();
     86                 }
     87             }
     88             @Override
     89             public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) {
     90                 // Close the dialog if the network validates.
     91                 if (mNetwork.equals(network) && nc.hasCapability(NET_CAPABILITY_VALIDATED)) {
     92                     Log.d(TAG, "Network " + mNetwork + " validated");
     93                     finish();
     94                 }
     95             }
     96         };
     97 
     98         mCM = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     99         mCM.registerNetworkCallback(request, mNetworkCallback);
    100 
    101         final NetworkInfo ni = mCM.getNetworkInfo(mNetwork);
    102         if (ni == null || !ni.isConnectedOrConnecting()) {
    103             Log.d(TAG, "Network " + mNetwork + " is not connected: " + ni);
    104             finish();
    105             return;
    106         }
    107         mNetworkName = ni.getExtraInfo();
    108         if (mNetworkName != null) {
    109             mNetworkName = mNetworkName.replaceAll("^\"|\"$", "");  // Remove double quotes
    110         }
    111 
    112         createDialog();
    113     }
    114 
    115     private void createDialog() {
    116         mAlert.setIcon(R.drawable.ic_settings_wireless);
    117 
    118         final AlertController.AlertParams ap = mAlertParams;
    119         ap.mTitle = mNetworkName;
    120         ap.mMessage = getString(R.string.no_internet_access_text);
    121         ap.mPositiveButtonText = getString(R.string.yes);
    122         ap.mNegativeButtonText = getString(R.string.no);
    123         ap.mPositiveButtonListener = this;
    124         ap.mNegativeButtonListener = this;
    125 
    126         final LayoutInflater inflater = LayoutInflater.from(ap.mContext);
    127         final View checkbox = inflater.inflate(
    128                 com.android.internal.R.layout.always_use_checkbox, null);
    129         ap.mView = checkbox;
    130 
    131         mAlwaysAllow = (CheckBox) checkbox.findViewById(com.android.internal.R.id.alwaysUse);
    132         mAlwaysAllow.setText(getString(R.string.no_internet_access_remember));
    133 
    134         setupAlert();
    135     }
    136 
    137     @Override
    138     protected void onDestroy() {
    139         if (mNetworkCallback != null) {
    140             mCM.unregisterNetworkCallback(mNetworkCallback);
    141             mNetworkCallback = null;
    142         }
    143         super.onDestroy();
    144     }
    145 
    146     public void onClick(DialogInterface dialog, int which) {
    147         final boolean accept = (which == BUTTON_POSITIVE);
    148         final String action = (accept ? "Connect" : "Ignore");
    149         final boolean always = mAlwaysAllow.isChecked();
    150 
    151         switch (which) {
    152             case BUTTON_POSITIVE:
    153             case BUTTON_NEGATIVE:
    154                 mCM.setAcceptUnvalidated(mNetwork, accept, always);
    155                 Log.d(TAG, action +  " network=" + mNetwork + (always ? " and remember" : ""));
    156                 break;
    157             default:
    158                 break;
    159         }
    160     }
    161 }
    162