1 /* 2 * Copyright (C) 2006 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 package com.android.internal.telephony.dataconnection; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.net.NetworkRequest; 21 import android.net.NetworkSpecifier; 22 import android.net.TelephonyNetworkSpecifier; 23 import android.telephony.Annotation.ApnType; 24 25 import com.android.telephony.Rlog; 26 27 /** 28 * Wraps cellular network requests to configured apn types. 29 */ 30 public class DcRequest implements Comparable<DcRequest> { 31 private static final String LOG_TAG = "DcRequest"; 32 33 @NonNull 34 public final NetworkRequest networkRequest; 35 public final int priority; 36 public final @ApnType int apnType; 37 DcRequest(@onNull final NetworkRequest nr, @ApnType final int type, int apnPriority)38 private DcRequest(@NonNull final NetworkRequest nr, @ApnType final int type, 39 int apnPriority) { 40 networkRequest = nr; 41 priority = apnPriority; 42 apnType = type; 43 } 44 45 /** 46 * Create a DcRequest based off of the network request. If the network request is not cellular, 47 * then null is returned and a warning is generated. 48 * @param networkRequest sets the type of dc request 49 * @param apnConfigTypeRepository apn config types to match on the network request 50 * @return corresponding DcRequest 51 * 52 */ 53 @Nullable create(@onNull final NetworkRequest networkRequest, @NonNull final ApnConfigTypeRepository apnConfigTypeRepository)54 public static DcRequest create(@NonNull final NetworkRequest networkRequest, 55 @NonNull final ApnConfigTypeRepository apnConfigTypeRepository) { 56 final int apnType = ApnContext.getApnTypeFromNetworkRequest(networkRequest); 57 final ApnConfigType apnConfigType = apnConfigTypeRepository.getByType(apnType); 58 if (apnConfigType == null) { 59 Rlog.d(LOG_TAG, "Non cellular request ignored: " + networkRequest.toString()); 60 checkForAnomalousNetworkRequest(networkRequest); 61 return null; 62 } else { 63 Rlog.d(LOG_TAG, "Cellular request confirmed: " + networkRequest.toString()); 64 return new DcRequest(networkRequest, apnType, apnConfigType.getPriority()); 65 } 66 } 67 checkForAnomalousNetworkRequest(NetworkRequest networkRequest)68 private static void checkForAnomalousNetworkRequest(NetworkRequest networkRequest) { 69 NetworkSpecifier specifier = networkRequest.getNetworkSpecifier(); 70 if (specifier != null) { 71 if (specifier instanceof TelephonyNetworkSpecifier) { 72 reportAnomalousNetworkRequest(networkRequest); 73 } 74 } 75 } 76 reportAnomalousNetworkRequest(NetworkRequest networkRequest)77 private static void reportAnomalousNetworkRequest(NetworkRequest networkRequest) { 78 //TODO: Report anomaly if this happens 79 Rlog.w(LOG_TAG, "A TelephonyNetworkSpecifier for a non-cellular request is invalid: " 80 + networkRequest.toString()); 81 82 } 83 toString()84 public String toString() { 85 return networkRequest.toString() + ", priority=" + priority + ", apnType=" + apnType; 86 } 87 hashCode()88 public int hashCode() { 89 return networkRequest.hashCode(); 90 } 91 equals(Object o)92 public boolean equals(Object o) { 93 if (o instanceof DcRequest) { 94 return networkRequest.equals(((DcRequest)o).networkRequest); 95 } 96 return false; 97 } 98 compareTo(DcRequest o)99 public int compareTo(DcRequest o) { 100 return o.priority - priority; 101 } 102 } 103