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.content.Context;
19 import android.content.Intent;
20 import android.graphics.Canvas;
21 import android.net.wifi.WifiManager;
22 import android.os.Bundle;
23 import android.support.car.ui.PagedListView;
24 import android.support.v7.widget.RecyclerView;
25 import android.view.View;
26 import android.widget.LinearLayout;
27 import android.widget.ProgressBar;
28 import android.widget.Switch;
29 import android.widget.TextView;
30 import android.widget.ViewSwitcher;
31 
32 import android.annotation.StringRes;
33 
34 import com.android.car.settings.common.CarSettingActivity;
35 import com.android.car.settings.R;
36 
37 import com.android.settingslib.wifi.AccessPoint;
38 
39 
40 /**
41  * Main page to host Wifi related preferences.
42  */
43 public class WifiSettingsActivity extends CarSettingActivity implements CarWifiManager.Listener {
44     private static final String TAG = "WifiSettingsActivity";
45 
46     private CarWifiManager mCarWifiManager;
47     private AccessPointListAdapter mAdapter;
48     private Switch mWifiSwitch;
49     private ProgressBar mProgressBar;
50     private PagedListView mListView;
51     private LinearLayout mWifiListContainer;
52     private TextView mMessageView;
53     private ViewSwitcher mViewSwitcher;
54     private TextView mAddWifiTextView;
55 
56     @Override
onCreate(Bundle savedInstanceState)57     public void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59         mCarWifiManager = new CarWifiManager(this /* context */ , this /* listener */);
60         setContentView(R.layout.wifi_list);
61 
62         ((TextView) findViewById(R.id.action_bar_title)).setText(R.string.wifi_settings);
63         mProgressBar = (ProgressBar) findViewById(R.id.wifi_search_progress);
64         mListView = (PagedListView) findViewById(R.id.list);
65         mMessageView = (TextView) findViewById(R.id.message);
66         mViewSwitcher = (ViewSwitcher) findViewById(R.id.view_switcher);
67         mAddWifiTextView = (TextView) findViewById(R.id.add_wifi);
68         mWifiListContainer = (LinearLayout) findViewById(R.id.wifi_list_container);
69         mAddWifiTextView.setOnClickListener(v -> {
70             Intent intent = new Intent(this /* context */, AddWifiActivity.class);
71             intent.putExtra(AddWifiActivity.ADD_NETWORK_MODE, true);
72             startActivity(intent);
73         });
74         setupWifiSwitch();
75         if (mCarWifiManager.isWifiEnabled()) {
76             showList();
77         } else {
78             showMessage(R.string.wifi_disabled);
79         }
80         mListView.setDefaultItemDecoration(new ItemDecoration(this));
81         // Set this to light mode, since the scroll bar buttons always appear
82         // on top of a dark scrim.
83         mListView.setDarkMode();
84         mAdapter = new AccessPointListAdapter(
85                 this, mCarWifiManager, mCarWifiManager.getAccessPoints());
86         mListView.setAdapter(mAdapter);
87     }
88 
89     @Override
setupActionBar()90     public void setupActionBar() {
91         getActionBar().setCustomView(R.layout.action_bar_with_toggle);
92         getActionBar().setDisplayShowCustomEnabled(true);
93         getActionBar().setDisplayHomeAsUpEnabled(true);
94     }
95 
96     @Override
onStart()97     public void onStart() {
98         super.onStart();
99         mCarWifiManager.start();
100     }
101 
102     @Override
onStop()103     public void onStop() {
104         super.onStop();
105         mCarWifiManager.stop();
106     }
107 
108     @Override
onAccessPointsChanged()109     public void onAccessPointsChanged() {
110         refreshData();
111     }
112 
113     @Override
onWifiStateChanged(int state)114     public void onWifiStateChanged(int state) {
115         mWifiSwitch.setChecked(mCarWifiManager.isWifiEnabled());
116         switch (state) {
117             case WifiManager.WIFI_STATE_ENABLING:
118                 showList();
119                 setProgressBarVisible(true);
120                 break;
121             case WifiManager.WIFI_STATE_DISABLED:
122                 setProgressBarVisible(false);
123                 showMessage(R.string.wifi_disabled);
124                 break;
125             default:
126                 showList();
127         }
128     }
129 
setProgressBarVisible(boolean visible)130     private  void setProgressBarVisible(boolean visible) {
131         if (mProgressBar != null) {
132             mProgressBar.setVisibility(visible ? View.VISIBLE : View.GONE);
133         }
134     }
135 
refreshData()136     private void refreshData() {
137         if (mAdapter != null) {
138             mAdapter.updateAccessPoints(mCarWifiManager.getAccessPoints());
139             // if the list is empty, keep showing the progress bar, the list should refresh
140             // every couple seconds.
141             // TODO: Consider show a message in the list view place.
142             if (!mAdapter.isEmpty()) {
143                 setProgressBarVisible(false);
144             }
145         }
146         mWifiSwitch.setChecked(mCarWifiManager.isWifiEnabled());
147     }
148 
149     /**
150      * Default {@link android.support.car.ui.PagedListView.Decoration} for the {@link PagedListView}
151      * that removes the dividing lines between items.
152      */
153     private static class ItemDecoration extends PagedListView.Decoration {
ItemDecoration(Context context)154         public ItemDecoration(Context context) {
155             super(context);
156         }
157 
158         @Override
onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)159         public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {}
160     }
161 
showMessage(@tringRes int resId)162     private void showMessage(@StringRes int resId) {
163         if (mViewSwitcher.getCurrentView() != mMessageView) {
164             mViewSwitcher.showNext();
165         }
166         mMessageView.setText(getResources().getString(resId));
167     }
168 
showList()169     private void showList() {
170         if (mViewSwitcher.getCurrentView() != mWifiListContainer) {
171             mViewSwitcher.showPrevious();
172         }
173     }
174 
setupWifiSwitch()175     private void setupWifiSwitch() {
176         mWifiSwitch = (Switch) findViewById(R.id.toggle_switch);
177         mWifiSwitch.setChecked(mCarWifiManager.isWifiEnabled());
178         mWifiSwitch.setOnClickListener(v -> {
179             mCarWifiManager.setWifiEnabled(mWifiSwitch.isChecked());
180         });
181     }
182 }
183