Home | History | Annotate | Download | only in wifi
      1 /*
      2  * Copyright (C) 2017 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 package com.android.car.settings.wifi;
     17 
     18 import android.net.NetworkInfo.State;
     19 import android.net.wifi.WifiManager;
     20 import android.os.Bundle;
     21 import android.support.annotation.StringRes;
     22 import android.support.v7.app.AppCompatActivity;
     23 import android.util.Log;
     24 import android.view.View;
     25 import android.widget.TextView;
     26 import android.widget.Toast;
     27 
     28 import com.android.car.settings.common.ListSettingsFragment;
     29 import com.android.car.settings.common.SimpleTextLineItem;
     30 import com.android.car.settings.common.TypedPagedListAdapter;
     31 import com.android.settingslib.wifi.AccessPoint;
     32 
     33 import com.android.car.settings.R;
     34 
     35 import java.util.ArrayList;
     36 
     37 /**
     38  * Shows details about a wifi network, including actions related to the network,
     39  * e.g. ignore, disconnect, etc. The intent should include information about
     40  * access point, use that to render UI, e.g. show SSID etc.
     41  */
     42 public class WifiDetailFragment extends ListSettingsFragment {
     43     public static final String EXTRA_AP_STATE = "extra_ap_state";
     44     private static final String TAG = "WifiDetailFragment";
     45 
     46     private AccessPoint mAccessPoint;
     47     private WifiManager mWifiManager;
     48 
     49     private class ActionFailListener implements WifiManager.ActionListener {
     50         @StringRes private final int mMessageResId;
     51 
     52         public ActionFailListener(@StringRes int messageResId) {
     53             mMessageResId = messageResId;
     54         }
     55 
     56         @Override
     57         public void onSuccess() {
     58         }
     59         @Override
     60         public void onFailure(int reason) {
     61             Toast.makeText(getContext(),
     62                     R.string.wifi_failed_connect_message,
     63                     Toast.LENGTH_SHORT).show();
     64         }
     65     }
     66 
     67     public static WifiDetailFragment getInstance(AccessPoint accessPoint) {
     68         WifiDetailFragment wifiDetailFragment = new WifiDetailFragment();
     69         Bundle bundle = ListSettingsFragment.getBundle();
     70         bundle.putInt(EXTRA_TITLE_ID, R.string.wifi_settings);
     71         bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar_with_button);
     72         Bundle accessPointState = new Bundle();
     73         accessPoint.saveWifiState(accessPointState);
     74         bundle.putBundle(EXTRA_AP_STATE, accessPointState);
     75         wifiDetailFragment.setArguments(bundle);
     76         return wifiDetailFragment;
     77     }
     78 
     79     @Override
     80     public void onCreate(Bundle savedInstanceState) {
     81         super.onCreate(savedInstanceState);
     82         mAccessPoint = new AccessPoint(getContext(), getArguments().getBundle(EXTRA_AP_STATE));
     83     }
     84 
     85     @Override
     86     public void onActivityCreated(Bundle savedInstanceState) {
     87         mWifiManager = getContext().getSystemService(WifiManager.class);
     88 
     89         super.onActivityCreated(savedInstanceState);
     90         ((TextView) getActivity().findViewById(R.id.title)).setText(mAccessPoint.getSsid());
     91         TextView forgetButton = (TextView) getActivity().findViewById(R.id.action_button1);
     92         forgetButton.setText(R.string.forget);
     93         forgetButton.setOnClickListener(v -> {
     94                 forget();
     95                 mFragmentController.goBack();
     96             });
     97 
     98         if (mAccessPoint.isSaved() && !mAccessPoint.isActive()) {
     99             TextView connectButton = (TextView) getActivity().findViewById(R.id.action_button2);
    100             connectButton.setVisibility(View.VISIBLE);
    101             connectButton.setText(R.string.wifi_setup_connect);
    102             connectButton.setOnClickListener(v -> {
    103                 mWifiManager.connect(mAccessPoint.getConfig(),
    104                         new ActionFailListener(R.string.wifi_failed_connect_message));
    105                 mFragmentController.goBack();
    106             });
    107         }
    108     }
    109 
    110     @Override
    111     public ArrayList<TypedPagedListAdapter.LineItem> getLineItems() {
    112         ArrayList<TypedPagedListAdapter.LineItem> lineItems = new ArrayList<>();
    113         lineItems.add(
    114                 new SimpleTextLineItem(getText(R.string.wifi_status), mAccessPoint.getSummary()));
    115         lineItems.add(
    116                 new SimpleTextLineItem(getText(R.string.wifi_signal), getSignalString()));
    117         lineItems.add(new SimpleTextLineItem(getText(R.string.wifi_security),
    118                 mAccessPoint.getSecurityString(true /* concise*/)));
    119         return lineItems;
    120     }
    121 
    122     private String getSignalString() {
    123         String[] signalStrings = getResources().getStringArray(R.array.wifi_signals);
    124 
    125         int level = WifiManager.calculateSignalLevel(
    126                 mAccessPoint.getRssi(), signalStrings.length);
    127         return signalStrings[level];
    128     }
    129 
    130     private void forget() {
    131         if (!mAccessPoint.isSaved()) {
    132             if (mAccessPoint.getNetworkInfo() != null &&
    133                     mAccessPoint.getNetworkInfo().getState() != State.DISCONNECTED) {
    134                 // Network is active but has no network ID - must be ephemeral.
    135                 mWifiManager.disableEphemeralNetwork(
    136                         AccessPoint.convertToQuotedString(mAccessPoint.getSsidStr()));
    137             } else {
    138                 // Should not happen, but a monkey seems to trigger it
    139                 Log.e(TAG, "Failed to forget invalid network " + mAccessPoint.getConfig());
    140                 return;
    141             }
    142         } else {
    143             mWifiManager.forget(mAccessPoint.getConfig().networkId,
    144                     new ActionFailListener(R.string.wifi_failed_forget_message));
    145         }
    146     }
    147 }
    148