1 /* 2 * Copyright (C) 2021 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 android.net.vcn; 18 19 import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS; 20 import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; 21 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.net.NetworkCapabilities; 25 import android.net.TransportInfo; 26 import android.net.wifi.WifiInfo; 27 import android.os.Parcel; 28 import android.os.Parcelable; 29 import android.telephony.SubscriptionManager; 30 31 import java.util.Objects; 32 33 /** 34 * VcnTransportInfo contains information about the VCN's underlying transports for SysUi. 35 * 36 * <p>Presence of this class in the NetworkCapabilities.TransportInfo implies that the network is a 37 * VCN. 38 * 39 * <p>VcnTransportInfo must exist on top of either an underlying Wifi or Cellular Network. If the 40 * underlying Network is WiFi, the subId will be {@link 41 * SubscriptionManager#INVALID_SUBSCRIPTION_ID}. If the underlying Network is Cellular, the WifiInfo 42 * will be {@code null}. 43 * 44 * <p>Receipt of a VcnTransportInfo requires the NETWORK_SETTINGS permission; else the entire 45 * VcnTransportInfo instance will be redacted. 46 * 47 * @hide 48 */ 49 public class VcnTransportInfo implements TransportInfo, Parcelable { 50 @Nullable private final WifiInfo mWifiInfo; 51 private final int mSubId; 52 VcnTransportInfo(@onNull WifiInfo wifiInfo)53 public VcnTransportInfo(@NonNull WifiInfo wifiInfo) { 54 this(wifiInfo, INVALID_SUBSCRIPTION_ID); 55 } 56 VcnTransportInfo(int subId)57 public VcnTransportInfo(int subId) { 58 this(null /* wifiInfo */, subId); 59 } 60 VcnTransportInfo(@ullable WifiInfo wifiInfo, int subId)61 private VcnTransportInfo(@Nullable WifiInfo wifiInfo, int subId) { 62 mWifiInfo = wifiInfo; 63 mSubId = subId; 64 } 65 66 /** 67 * Get the {@link WifiInfo} for this VcnTransportInfo. 68 * 69 * <p>If the underlying Network for the associated VCN is Cellular, returns null. 70 * 71 * @return the WifiInfo if there is an underlying WiFi connection, else null. 72 */ 73 @Nullable getWifiInfo()74 public WifiInfo getWifiInfo() { 75 return mWifiInfo; 76 } 77 78 /** 79 * Get the subId for the VCN Network associated with this VcnTransportInfo. 80 * 81 * <p>If the underlying Network for the associated VCN is WiFi, returns {@link 82 * SubscriptionManager#INVALID_SUBSCRIPTION_ID}. 83 * 84 * @return the Subscription ID if a cellular underlying Network is present, else {@link 85 * android.telephony.SubscriptionManager#INVALID_SUBSCRIPTION_ID}. 86 */ getSubId()87 public int getSubId() { 88 return mSubId; 89 } 90 91 @Override hashCode()92 public int hashCode() { 93 return Objects.hash(mWifiInfo, mSubId); 94 } 95 96 @Override equals(Object o)97 public boolean equals(Object o) { 98 if (!(o instanceof VcnTransportInfo)) return false; 99 final VcnTransportInfo that = (VcnTransportInfo) o; 100 return Objects.equals(mWifiInfo, that.mWifiInfo) && mSubId == that.mSubId; 101 } 102 103 /** {@inheritDoc} */ 104 @Override describeContents()105 public int describeContents() { 106 return 0; 107 } 108 109 @Override 110 @NonNull makeCopy(long redactions)111 public TransportInfo makeCopy(long redactions) { 112 if ((redactions & NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS) != 0) { 113 return new VcnTransportInfo(null, INVALID_SUBSCRIPTION_ID); 114 } 115 116 return new VcnTransportInfo( 117 (mWifiInfo == null) ? null : mWifiInfo.makeCopy(redactions), mSubId); 118 } 119 120 @Override getApplicableRedactions()121 public long getApplicableRedactions() { 122 long redactions = REDACT_FOR_NETWORK_SETTINGS; 123 124 // Add additional wifi redactions if necessary 125 if (mWifiInfo != null) { 126 redactions |= mWifiInfo.getApplicableRedactions(); 127 } 128 129 return redactions; 130 } 131 132 /** {@inheritDoc} */ 133 @Override writeToParcel(@onNull Parcel dest, int flags)134 public void writeToParcel(@NonNull Parcel dest, int flags) { 135 dest.writeInt(mSubId); 136 dest.writeParcelable(mWifiInfo, flags); 137 } 138 139 @Override toString()140 public String toString() { 141 return "VcnTransportInfo { mWifiInfo = " + mWifiInfo + ", mSubId = " + mSubId + " }"; 142 } 143 144 /** Implement the Parcelable interface */ 145 public static final @NonNull Creator<VcnTransportInfo> CREATOR = 146 new Creator<VcnTransportInfo>() { 147 public VcnTransportInfo createFromParcel(Parcel in) { 148 final int subId = in.readInt(); 149 final WifiInfo wifiInfo = in.readParcelable(null, android.net.wifi.WifiInfo.class); 150 151 // If all fields are their null values, return null TransportInfo to avoid 152 // leaking information about this being a VCN Network (instead of macro 153 // cellular, etc) 154 if (wifiInfo == null && subId == INVALID_SUBSCRIPTION_ID) { 155 return null; 156 } 157 158 return new VcnTransportInfo(wifiInfo, subId); 159 } 160 161 public VcnTransportInfo[] newArray(int size) { 162 return new VcnTransportInfo[size]; 163 } 164 }; 165 } 166