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.systemui.statusbar.pipeline.shared.data.model
18 
19 import android.net.NetworkCapabilities
20 import com.android.systemui.log.core.LogMessage
21 
22 /**
23  * A model for all of the current default connections(s).
24  *
25  * Uses different classes for each connection type to ensure type safety when setting the values.
26  *
27  * Important: We generally expect there to be only *one* default network at a time (with the
28  * exception of carrier merged). Specifically, we don't expect to ever have both wifi *and* cellular
29  * as default at the same time. However, the framework network callbacks don't provide any
30  * guarantees about why types of network could be default at the same time, so we don't enforce any
31  * guarantees on this class.
32  */
33 data class DefaultConnectionModel(
34     /** Wifi's status as default or not. */
35     val wifi: Wifi = Wifi(isDefault = false),
36 
37     /** Mobile's status as default or not. */
38     val mobile: Mobile = Mobile(isDefault = false),
39 
40     /**
41      * True if the current default network represents a carrier merged network, and false otherwise.
42      * See [android.net.wifi.WifiInfo.isCarrierMerged] for more information.
43      *
44      * Important: A carrier merged network can come in as either a
45      * [NetworkCapabilities.TRANSPORT_CELLULAR] *or* as a [NetworkCapabilities.TRANSPORT_WIFI]. This
46      * means that when carrier merged is in effect, either:
47      * - [wifi] *and* [carrierMerged] will be marked as default; or
48      * - [mobile] *and* [carrierMerged] will be marked as default
49      *
50      * Specifically, [carrierMerged] will never be the *only* default connection.
51      */
52     val carrierMerged: CarrierMerged = CarrierMerged(isDefault = false),
53 
54     /** Ethernet's status as default or not. */
55     val ethernet: Ethernet = Ethernet(isDefault = false),
56 
57     /** True if the default connection is currently validated and false otherwise. */
58     val isValidated: Boolean = false,
59 ) {
60     data class Wifi(val isDefault: Boolean)
61     data class Mobile(val isDefault: Boolean)
62     data class CarrierMerged(val isDefault: Boolean)
63     data class Ethernet(val isDefault: Boolean)
64 
65     /**
66      * Used in conjunction with [ConnectivityInputLogger] to log this class without calling
67      * [toString] on it.
68      *
69      * Be sure to change [messagePrinter] whenever this method is changed.
70      */
messageInitializernull71     fun messageInitializer(message: LogMessage) {
72         message.bool1 = wifi.isDefault
73         message.bool2 = mobile.isDefault
74         message.bool3 = carrierMerged.isDefault
75         message.bool4 = ethernet.isDefault
76         message.int1 = if (isValidated) 1 else 0
77     }
78 
messagePrinternull79     fun messagePrinter(message: LogMessage): String {
80         return "DefaultConnectionModel(" +
81             "wifi.isDefault=${message.bool1}, " +
82             "mobile.isDefault=${message.bool2}, " +
83             "carrierMerged.isDefault=${message.bool3}, " +
84             "ethernet.isDefault=${message.bool4}, " +
85             "isValidated=${if (message.int1 == 1) "true" else "false"})"
86     }
87 }
88