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; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 import android.util.Log; 24 25 import java.util.Objects; 26 27 /** 28 * Aware of how to parcel different types of {@link QosFilter}s. Any new type of qos filter must 29 * have a specialized case written here. 30 * <p/> 31 * Specifically leveraged when transferring {@link QosFilter} from 32 * {@link com.android.server.ConnectivityService} to {@link NetworkAgent} when the filter is first 33 * registered. 34 * <p/> 35 * This is not meant to be used in other contexts. 36 * 37 * @hide 38 */ 39 public final class QosFilterParcelable implements Parcelable { 40 41 private static final String LOG_TAG = QosFilterParcelable.class.getSimpleName(); 42 43 // Indicates that the filter was not successfully written to the parcel. 44 private static final int NO_FILTER_PRESENT = 0; 45 46 // The parcel is of type qos socket filter. 47 private static final int QOS_SOCKET_FILTER = 1; 48 49 private final QosFilter mQosFilter; 50 51 /** 52 * The underlying qos filter. 53 * <p/> 54 * Null only in the case parceling failed. 55 */ 56 @Nullable getQosFilter()57 public QosFilter getQosFilter() { 58 return mQosFilter; 59 } 60 QosFilterParcelable(@onNull final QosFilter qosFilter)61 public QosFilterParcelable(@NonNull final QosFilter qosFilter) { 62 Objects.requireNonNull(qosFilter, "qosFilter must be non-null"); 63 64 // NOTE: Normally a type check would belong here, but doing so breaks unit tests that rely 65 // on mocking qos filter. 66 mQosFilter = qosFilter; 67 } 68 QosFilterParcelable(final Parcel in)69 private QosFilterParcelable(final Parcel in) { 70 final int filterParcelType = in.readInt(); 71 72 switch (filterParcelType) { 73 case QOS_SOCKET_FILTER: { 74 mQosFilter = new QosSocketFilter(QosSocketInfo.CREATOR.createFromParcel(in)); 75 break; 76 } 77 78 case NO_FILTER_PRESENT: 79 default: { 80 mQosFilter = null; 81 } 82 } 83 } 84 85 public static final Creator<QosFilterParcelable> CREATOR = new Creator<QosFilterParcelable>() { 86 @Override 87 public QosFilterParcelable createFromParcel(final Parcel in) { 88 return new QosFilterParcelable(in); 89 } 90 91 @Override 92 public QosFilterParcelable[] newArray(final int size) { 93 return new QosFilterParcelable[size]; 94 } 95 }; 96 97 @Override describeContents()98 public int describeContents() { 99 return 0; 100 } 101 102 @Override writeToParcel(final Parcel dest, final int flags)103 public void writeToParcel(final Parcel dest, final int flags) { 104 if (mQosFilter instanceof QosSocketFilter) { 105 dest.writeInt(QOS_SOCKET_FILTER); 106 final QosSocketFilter qosSocketFilter = (QosSocketFilter) mQosFilter; 107 qosSocketFilter.getQosSocketInfo().writeToParcelWithoutFd(dest, 0); 108 return; 109 } 110 dest.writeInt(NO_FILTER_PRESENT); 111 Log.e(LOG_TAG, "Parceling failed, unknown type of filter present: " + mQosFilter); 112 } 113 } 114