roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 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 | |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 18 | import android.annotation.NonNull; |
kevinjm | cf8cef5 | 2017-09-18 14:53:28 -0700 | [diff] [blame] | 19 | import android.annotation.StringRes; |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 20 | import android.car.drivingstate.CarUxRestrictions; |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 21 | import android.net.wifi.WifiManager; |
| 22 | import android.os.Bundle; |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 23 | import android.view.View; |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 24 | import android.widget.ProgressBar; |
| 25 | import android.widget.Switch; |
| 26 | import android.widget.TextView; |
| 27 | import android.widget.ViewSwitcher; |
| 28 | |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 29 | import androidx.car.widget.PagedListView; |
| 30 | |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 31 | import com.android.car.settings.R; |
kevinjm | cf8cef5 | 2017-09-18 14:53:28 -0700 | [diff] [blame] | 32 | import com.android.car.settings.common.BaseFragment; |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 33 | import com.android.car.settings.common.CarUxRestrictionsHelper; |
Aurimas Liutikas | 0c01e08 | 2017-12-01 16:41:40 -0800 | [diff] [blame] | 34 | |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Main page to host Wifi related preferences. |
| 37 | */ |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 38 | public class WifiSettingsFragment extends BaseFragment implements CarWifiManager.Listener { |
| 39 | private static final String TAG = "WifiSettingsFragment"; |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 40 | |
| 41 | private CarWifiManager mCarWifiManager; |
| 42 | private AccessPointListAdapter mAdapter; |
| 43 | private Switch mWifiSwitch; |
| 44 | private ProgressBar mProgressBar; |
| 45 | private PagedListView mListView; |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 46 | private TextView mMessageView; |
| 47 | private ViewSwitcher mViewSwitcher; |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 48 | private boolean mShowSavedApOnly; |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 49 | |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 50 | public static WifiSettingsFragment getInstance() { |
| 51 | WifiSettingsFragment wifiSettingsFragment = new WifiSettingsFragment(); |
| 52 | Bundle bundle = BaseFragment.getBundle(); |
| 53 | bundle.putInt(EXTRA_TITLE_ID, R.string.wifi_settings); |
| 54 | bundle.putInt(EXTRA_LAYOUT, R.layout.wifi_list); |
| 55 | bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar_with_toggle); |
| 56 | wifiSettingsFragment.setArguments(bundle); |
| 57 | return wifiSettingsFragment; |
| 58 | } |
roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 59 | |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 60 | /** |
| 61 | * Shows only saved wifi network. |
| 62 | */ |
| 63 | public WifiSettingsFragment showSavedApOnly(boolean showSavedApOnly) { |
| 64 | mShowSavedApOnly = showSavedApOnly; |
| 65 | return this; |
| 66 | } |
| 67 | |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 68 | @Override |
| 69 | public void onActivityCreated(Bundle savedInstanceState) { |
| 70 | super.onActivityCreated(savedInstanceState); |
| 71 | mCarWifiManager = new CarWifiManager(getContext(), this /* listener */); |
| 72 | |
| 73 | mProgressBar = (ProgressBar) getView().findViewById(R.id.wifi_search_progress); |
| 74 | mListView = (PagedListView) getView().findViewById(R.id.list); |
| 75 | mMessageView = (TextView) getView().findViewById(R.id.message); |
| 76 | mViewSwitcher = (ViewSwitcher) getView().findViewById(R.id.view_switcher); |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 77 | setupWifiSwitch(); |
| 78 | if (mCarWifiManager.isWifiEnabled()) { |
| 79 | showList(); |
Lujiang Xue | a1d4aea | 2017-10-12 10:53:01 -0700 | [diff] [blame] | 80 | setProgressBarVisible(true); |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 81 | } else { |
| 82 | showMessage(R.string.wifi_disabled); |
| 83 | } |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 84 | mAdapter = new AccessPointListAdapter( |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 85 | getContext(), |
| 86 | mCarWifiManager, |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 87 | mShowSavedApOnly |
| 88 | ? mCarWifiManager.getSavedAccessPoints() |
| 89 | : mCarWifiManager.getAllAccessPoints(), |
| 90 | !mShowSavedApOnly, |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 91 | mFragmentController); |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 92 | mListView.setAdapter(mAdapter); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onStart() { |
| 97 | super.onStart(); |
| 98 | mCarWifiManager.start(); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void onStop() { |
| 103 | super.onStop(); |
| 104 | mCarWifiManager.stop(); |
| 105 | } |
| 106 | |
| 107 | @Override |
Tony Mantler | 9fe1a51 | 2017-09-28 15:03:35 -0700 | [diff] [blame] | 108 | public void onDestroy() { |
| 109 | super.onDestroy(); |
| 110 | mCarWifiManager.destroy(); |
| 111 | } |
| 112 | |
| 113 | @Override |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 114 | public void onAccessPointsChanged() { |
| 115 | refreshData(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void onWifiStateChanged(int state) { |
Fan Zhang | 9788bc7 | 2017-02-27 17:21:28 -0800 | [diff] [blame] | 120 | mWifiSwitch.setChecked(mCarWifiManager.isWifiEnabled()); |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 121 | switch (state) { |
| 122 | case WifiManager.WIFI_STATE_ENABLING: |
| 123 | showList(); |
| 124 | setProgressBarVisible(true); |
| 125 | break; |
| 126 | case WifiManager.WIFI_STATE_DISABLED: |
| 127 | setProgressBarVisible(false); |
| 128 | showMessage(R.string.wifi_disabled); |
| 129 | break; |
| 130 | default: |
| 131 | showList(); |
| 132 | } |
| 133 | } |
| 134 | |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 135 | /** |
| 136 | * This fragment will adapt to restriction, so can always be shown. |
| 137 | */ |
| 138 | @Override |
| 139 | public boolean canBeShown(CarUxRestrictions carUxRestrictions) { |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void onUxRestrictionChanged(@NonNull CarUxRestrictions carUxRestrictions) { |
| 145 | mShowSavedApOnly = CarUxRestrictionsHelper.isNoSetup(carUxRestrictions); |
| 146 | refreshData(); |
| 147 | } |
| 148 | |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 149 | private void setProgressBarVisible(boolean visible) { |
| 150 | if (mProgressBar != null) { |
| 151 | mProgressBar.setVisibility(visible ? View.VISIBLE : View.GONE); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private void refreshData() { |
| 156 | if (mAdapter != null) { |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 157 | mAdapter.updateAccessPoints(mShowSavedApOnly |
| 158 | ? mCarWifiManager.getSavedAccessPoints() |
| 159 | : mCarWifiManager.getAllAccessPoints()); |
roger xue | a76987e | 2017-05-08 15:49:39 -0700 | [diff] [blame] | 160 | // if the list is empty, keep showing the progress bar, the list should reset |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 161 | // every couple seconds. |
| 162 | // TODO: Consider show a message in the list view place. |
| 163 | if (!mAdapter.isEmpty()) { |
| 164 | setProgressBarVisible(false); |
| 165 | } |
| 166 | } |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 167 | if (mCarWifiManager != null) { |
| 168 | mWifiSwitch.setChecked(mCarWifiManager.isWifiEnabled()); |
| 169 | } |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 170 | } |
| 171 | |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 172 | private void showMessage(@StringRes int resId) { |
| 173 | if (mViewSwitcher.getCurrentView() != mMessageView) { |
| 174 | mViewSwitcher.showNext(); |
| 175 | } |
| 176 | mMessageView.setText(getResources().getString(resId)); |
| 177 | } |
| 178 | |
| 179 | private void showList() { |
roger xue | b346ade | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 180 | if (mViewSwitcher.getCurrentView() != mListView) { |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 181 | mViewSwitcher.showPrevious(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | private void setupWifiSwitch() { |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 186 | mWifiSwitch = (Switch) getActivity().findViewById(R.id.toggle_switch); |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 187 | mWifiSwitch.setChecked(mCarWifiManager.isWifiEnabled()); |
Lujiang Xue | 262c78b | 2017-07-12 10:45:22 -0700 | [diff] [blame] | 188 | mWifiSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { |
| 189 | if (mWifiSwitch.isChecked() != mCarWifiManager.isWifiEnabled()) { |
| 190 | mCarWifiManager.setWifiEnabled(mWifiSwitch.isChecked()); |
| 191 | } |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 192 | }); |
| 193 | } |
| 194 | } |