1 /*
2  * Copyright (C) 2023 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.settings.wifi.details;
18 
19 import android.app.Application;
20 
21 import androidx.annotation.VisibleForTesting;
22 import androidx.lifecycle.AndroidViewModel;
23 import androidx.lifecycle.LiveData;
24 import androidx.lifecycle.MutableLiveData;
25 
26 import com.android.settings.overlay.FeatureFactory;
27 import com.android.wifitrackerlib.HotspotNetworkEntry;
28 import com.android.wifitrackerlib.WifiEntry;
29 
30 import org.jetbrains.annotations.NotNull;
31 
32 /**
33  * Wi-Fi Network Details ViewModel
34  */
35 public class WifiNetworkDetailsViewModel extends AndroidViewModel {
36     private static final String TAG = "WifiNetworkDetailsViewModel";
37 
38     @VisibleForTesting
39     MutableLiveData<HotspotNetworkData> mHotspotNetworkData = new MutableLiveData<>();
40 
WifiNetworkDetailsViewModel(@otNull Application application)41     public WifiNetworkDetailsViewModel(@NotNull Application application) {
42         super(application);
43     }
44 
45     /** Sets the {@link WifiEntry} class */
setWifiEntry(WifiEntry wifiEntry)46     public void setWifiEntry(WifiEntry wifiEntry) {
47         if (!(wifiEntry instanceof HotspotNetworkEntry)) {
48             log("post HotspotNetworkData:null");
49             mHotspotNetworkData.postValue(null);
50             return;
51         }
52         HotspotNetworkEntry entry = (HotspotNetworkEntry) wifiEntry;
53         HotspotNetworkData data = new HotspotNetworkData(
54                 entry.getNetworkType(),
55                 entry.getUpstreamConnectionStrength(),
56                 entry.getBatteryPercentage(),
57                 entry.isBatteryCharging());
58         log("post HotspotNetworkData:" + data);
59         mHotspotNetworkData.postValue(data);
60     }
61 
62     /** Gets the {@link HotspotNetworkData} LiveData */
getHotspotNetworkData()63     public LiveData<HotspotNetworkData> getHotspotNetworkData() {
64         return mHotspotNetworkData;
65     }
66 
67     /** The {@link HotspotNetworkData} class */
68     static class HotspotNetworkData {
69         private int mNetworkType;
70         private int mUpstreamConnectionStrength;
71         private int mBatteryPercentage;
72         private boolean mIsBatteryCharging;
73 
HotspotNetworkData(int networkType, int upstreamConnectionStrength, int batteryPercentage, boolean isBatteryCharging)74         HotspotNetworkData(int networkType, int upstreamConnectionStrength,
75                 int batteryPercentage,
76                 boolean isBatteryCharging) {
77             mNetworkType = networkType;
78             mUpstreamConnectionStrength = upstreamConnectionStrength;
79             mBatteryPercentage = batteryPercentage;
80             mIsBatteryCharging = isBatteryCharging;
81         }
82 
83         /** Gets the network type */
getNetworkType()84         public int getNetworkType() {
85             return mNetworkType;
86         }
87 
88         /** Gets the upstream connection strength */
getUpstreamConnectionStrength()89         public int getUpstreamConnectionStrength() {
90             return mUpstreamConnectionStrength;
91         }
92 
93         /** Gets the battery percentage */
getBatteryPercentage()94         public int getBatteryPercentage() {
95             return mBatteryPercentage;
96         }
97 
98         /** Returns true if the battery is charging */
isBatteryCharging()99         public boolean isBatteryCharging() {
100             return mIsBatteryCharging;
101         }
102 
103         @Override
toString()104         public String toString() {
105             return getClass().getSimpleName()
106                     + ":{networkType:" + mNetworkType
107                     + ", upstreamConnectionStrength:" + mUpstreamConnectionStrength
108                     + ", batteryPercentage:" + mBatteryPercentage
109                     + ", isBatteryCharging:" + mIsBatteryCharging
110                     + " }";
111         }
112     }
113 
log(String msg)114     private void log(String msg) {
115         FeatureFactory.getFeatureFactory().getWifiFeatureProvider().verboseLog(TAG, msg);
116     }
117 }
118