• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.util.Log;
23 import android.view.View;
24 import android.widget.Button;
25 import android.widget.TextView;
26 import android.widget.Toast;
27 
28 import com.android.car.settings.common.ListSettingsActivity;
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 WifiDetailActivity extends ListSettingsActivity {
43     private static final String TAG = "WifiDetailActivity";
44     private AccessPoint mAccessPoint;
45     private WifiManager mWifiManager;
46 
47     private class ActionFailListener implements WifiManager.ActionListener {
48         @StringRes private final int mMessageResId;
49 
ActionFailListener(@tringRes int messageResId)50         public ActionFailListener(@StringRes int messageResId) {
51             mMessageResId = messageResId;
52         }
53 
54         @Override
onSuccess()55         public void onSuccess() {
56         }
57         @Override
onFailure(int reason)58         public void onFailure(int reason) {
59             Toast.makeText(WifiDetailActivity.this,
60                     R.string.wifi_failed_connect_message,
61                     Toast.LENGTH_SHORT).show();
62         }
63     }
64 
65     @Override
setupActionBar()66     public void setupActionBar() {
67         super.setupActionBar();
68         getActionBar().setCustomView(R.layout.action_bar_with_button);
69         getActionBar().setDisplayShowCustomEnabled(true);
70     }
71 
72     @Override
onCreate(Bundle savedInstanceState)73     protected void onCreate(Bundle savedInstanceState) {
74         mWifiManager = (WifiManager) getSystemService(WifiManager.class);
75         mAccessPoint = new AccessPoint(this, getIntent().getExtras());
76 
77         super.onCreate(savedInstanceState);
78         ((TextView) findViewById(R.id.title)).setText(mAccessPoint.getSsid());
79         Button forgetButton = (Button) findViewById(R.id.action_button1);
80         forgetButton.setText(R.string.forget);
81         forgetButton.setOnClickListener(v -> {
82                 forget();
83                 finish();
84             });
85 
86         if (mAccessPoint.isSaved() && !mAccessPoint.isActive()) {
87             Button connectButton = (Button) findViewById(R.id.action_button2);
88             connectButton.setVisibility(View.VISIBLE);
89             connectButton.setText(R.string.wifi_setup_connect);
90             connectButton.setOnClickListener(v -> {
91                 mWifiManager.connect(mAccessPoint.getConfig(),
92                         new ActionFailListener(R.string.wifi_failed_connect_message));
93                 finish();
94             });
95         }
96     }
97 
98     @Override
getLineItems()99     public ArrayList<TypedPagedListAdapter.LineItem> getLineItems() {
100         ArrayList<TypedPagedListAdapter.LineItem> lineItems = new ArrayList<>();
101         lineItems.add(
102                 new SimpleTextLineItem(getText(R.string.wifi_status), mAccessPoint.getSummary()));
103         lineItems.add(
104                 new SimpleTextLineItem(getText(R.string.wifi_signal), getSignalString()));
105         lineItems.add(new SimpleTextLineItem(getText(R.string.wifi_security),
106                 mAccessPoint.getSecurityString(true /* concise*/)));
107         return lineItems;
108     }
109 
getSignalString()110     private String getSignalString() {
111         String[] signalStrings = getResources().getStringArray(R.array.wifi_signals);
112 
113         int level = WifiManager.calculateSignalLevel(
114                 mAccessPoint.getRssi(), signalStrings.length);
115         return signalStrings[level];
116     }
117 
forget()118     private void forget() {
119         if (!mAccessPoint.isSaved()) {
120             if (mAccessPoint.getNetworkInfo() != null &&
121                     mAccessPoint.getNetworkInfo().getState() != State.DISCONNECTED) {
122                 // Network is active but has no network ID - must be ephemeral.
123                 mWifiManager.disableEphemeralNetwork(
124                         AccessPoint.convertToQuotedString(mAccessPoint.getSsidStr()));
125             } else {
126                 // Should not happen, but a monkey seems to trigger it
127                 Log.e(TAG, "Failed to forget invalid network " + mAccessPoint.getConfig());
128                 return;
129             }
130         } else {
131             mWifiManager.forget(mAccessPoint.getConfig().networkId,
132                     new ActionFailListener(R.string.wifi_failed_forget_message));
133         }
134     }
135 }
136