• 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 
17 package com.android.car.settings.home;
18 
19 import android.annotation.DrawableRes;
20 import android.content.Context;
21 import android.view.MotionEvent;
22 import android.view.View;
23 import android.widget.Switch;
24 
25 import com.android.car.list.IconToggleLineItem;
26 import com.android.car.settings.R;
27 import com.android.car.settings.common.BaseFragment;
28 import com.android.car.settings.wifi.CarWifiManager;
29 import com.android.car.settings.wifi.WifiSettingsFragment;
30 import com.android.car.settings.wifi.WifiUtil;
31 
32 
33 /**
34  * Represents the wifi line item on settings home page.
35  */
36 public class WifiLineItem extends IconToggleLineItem {
37     private final Context mContext;
38     private final CarWifiManager mCarWifiManager;
39     private BaseFragment.FragmentController mFragmentController;
40 
WifiLineItem( Context context, CarWifiManager carWifiManager, BaseFragment.FragmentController fragmentController)41     public WifiLineItem(
42             Context context,
43             CarWifiManager carWifiManager,
44             BaseFragment.FragmentController fragmentController) {
45         super(context.getText(R.string.wifi_settings), context);
46         mContext = context;
47         mCarWifiManager = carWifiManager;
48         mFragmentController = fragmentController;
49     }
50 
51     @Override
onToggleTouched(Switch toggleSwitch, MotionEvent event)52     public boolean onToggleTouched(Switch toggleSwitch, MotionEvent event) {
53         // intercept touch event, so we can process the request and update the switch
54         // state accordingly
55         if (event.getAction() == MotionEvent.ACTION_DOWN) {
56             mCarWifiManager.setWifiEnabled(!isChecked());
57         }
58         return true;
59     }
60 
61     @Override
onClick(View view)62     public void onClick(View view) {
63         mFragmentController.launchFragment(WifiSettingsFragment.newInstance());
64     }
65 
66     @Override
getDesc()67     public CharSequence getDesc() {
68         return mContext.getText(R.string.wifi_settings_summary);
69     }
70 
71     @Override
isChecked()72     public boolean isChecked() {
73         return mCarWifiManager.isWifiEnabled();
74     }
75 
76     @Override
isExpandable()77     public boolean isExpandable() {
78         return true;
79     }
80 
81     @Override
getIcon()82     public @DrawableRes int getIcon() {
83         return WifiUtil.getIconRes(mCarWifiManager.getWifiState());
84     }
85 
onWifiStateChanged(int state)86     public void onWifiStateChanged(int state) {
87         if (mIconUpdateListener != null) {
88             mIconUpdateListener.onUpdateIcon(WifiUtil.getIconRes(state));
89         }
90         if (mSwitchStateUpdateListener != null) {
91             mSwitchStateUpdateListener.onToggleChanged(WifiUtil.isWifiOn(state));
92         }
93     }
94 
95 }
96