1 /*
2  * Copyright (C) 2018 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.car.settings.quicksettings;
18 
19 import android.annotation.DrawableRes;
20 import android.annotation.Nullable;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.graphics.drawable.Drawable;
24 import android.view.View;
25 
26 import androidx.lifecycle.Lifecycle;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.CarSettingActivities;
30 import com.android.car.settings.wifi.CarWifiManager;
31 import com.android.car.settings.wifi.WifiUtil;
32 import com.android.wifitrackerlib.WifiEntry;
33 
34 /**
35  * Controls Wifi tile on quick setting page.
36  */
37 public class WifiTile implements QuickSettingGridAdapter.Tile, CarWifiManager.Listener {
38     private final StateChangedListener mStateChangedListener;
39     private final CarWifiManager mCarWifiManager;
40     private final Context mContext;
41 
42     private final View.OnLongClickListener mLaunchWifiSettings;
43 
44     @DrawableRes
45     private int mIconRes = R.drawable.ic_settings_wifi;
46 
47     private String mText;
48 
49     private State mState = State.OFF;
50 
WifiTile( Context context, StateChangedListener stateChangedListener, Lifecycle lifecycle)51     WifiTile(
52             Context context,
53             StateChangedListener stateChangedListener,
54             Lifecycle lifecycle) {
55         mContext = context;
56         mLaunchWifiSettings = v -> {
57             context.startActivity(new Intent(context,
58                     CarSettingActivities.WifiSettingsActivity.class));
59             return true;
60         };
61         mCarWifiManager = new CarWifiManager(context, lifecycle);
62         mStateChangedListener = stateChangedListener;
63         // init icon and text etc.
64         updateWifiEntrySsid();
65         onWifiStateChanged(mCarWifiManager.getWifiState());
66     }
67 
68     @Nullable
getOnLongClickListener()69     public View.OnLongClickListener getOnLongClickListener() {
70         return mLaunchWifiSettings;
71     }
72 
73     @Override
isAvailable()74     public boolean isAvailable() {
75         return WifiUtil.isWifiAvailable(mContext);
76     }
77 
78     @Override
getIcon()79     public Drawable getIcon() {
80         return mContext.getDrawable(mIconRes);
81     }
82 
83     @Override
84     @Nullable
getText()85     public String getText() {
86         return mText;
87     }
88 
89     @Override
getState()90     public State getState() {
91         return mState;
92     }
93 
94     @Override
start()95     public void start() {
96         mCarWifiManager.addListener(this);
97     }
98 
99     @Override
stop()100     public void stop() {
101         mCarWifiManager.removeListener(this);
102     }
103 
104     @Override
onWifiEntriesChanged()105     public void onWifiEntriesChanged() {
106         if (updateWifiEntrySsid()) {
107             mStateChangedListener.onStateChanged();
108         }
109     }
110 
111     @Override
onWifiStateChanged(int state)112     public void onWifiStateChanged(int state) {
113         mIconRes = WifiUtil.getIconRes(state);
114         int stringId = WifiUtil.getStateDesc(state);
115         if (stringId != 0) {
116             mText = mContext.getString(stringId);
117         } else if (!updateWifiEntrySsid()) {
118             if (wifiEnabledNotConnected()) {
119                 mText = mContext.getString(R.string.wifi_settings);
120             }
121         }
122         mState = WifiUtil.isWifiOn(state) ? State.ON : State.OFF;
123         mStateChangedListener.onStateChanged();
124     }
125 
126     @Override
onClick(View v)127     public void onClick(View v) {
128         mCarWifiManager.setWifiEnabled(!mCarWifiManager.isWifiEnabled());
129     }
130 
wifiEnabledNotConnected()131     private boolean wifiEnabledNotConnected() {
132         return mCarWifiManager.isWifiEnabled() && mCarWifiManager.getConnectedWifiEntry() == null;
133     }
134 
135     /**
136      * Updates the text with Wi-Fi entry connected, if any
137      *
138      * @return {@code true} if the text was updated, {@code false} otherwise
139      */
updateWifiEntrySsid()140     private boolean updateWifiEntrySsid() {
141         WifiEntry wifiEntry = mCarWifiManager.getConnectedWifiEntry();
142         if (wifiEntry != null) {
143             mText = wifiEntry.getSsid();
144             return true;
145         }
146         return false;
147     }
148 }
149