1 /* 2 * Copyright (C) 2017 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.internal.telephony.dataconnection; 18 19 import com.android.internal.annotations.VisibleForTesting; 20 21 import java.util.HashSet; 22 23 /** 24 * The class to describe the reasons of allowing or disallowing to establish a data connection. 25 */ 26 public class DataConnectionReasons { 27 private HashSet<DataDisallowedReasonType> mDataDisallowedReasonSet = new HashSet<>(); 28 private DataAllowedReasonType mDataAllowedReason = DataAllowedReasonType.NONE; 29 DataConnectionReasons()30 public DataConnectionReasons() {} 31 add(DataDisallowedReasonType reason)32 void add(DataDisallowedReasonType reason) { 33 // Adding a disallowed reason will clean up the allowed reason because they are 34 // mutual exclusive. 35 mDataAllowedReason = DataAllowedReasonType.NONE; 36 mDataDisallowedReasonSet.add(reason); 37 } 38 add(DataAllowedReasonType reason)39 void add(DataAllowedReasonType reason) { 40 // Adding an allowed reason will clean up the disallowed reasons because they are 41 // mutual exclusive. 42 mDataDisallowedReasonSet.clear(); 43 44 // Only higher priority allowed reason can overwrite the old one. See 45 // DataAllowedReasonType for the oder. 46 if (reason.ordinal() > mDataAllowedReason.ordinal()) { 47 mDataAllowedReason = reason; 48 } 49 } 50 51 @Override toString()52 public String toString() { 53 StringBuilder reasonStr = new StringBuilder(); 54 if (mDataDisallowedReasonSet.size() > 0) { 55 reasonStr.append("Data disallowed reasons:"); 56 for (DataDisallowedReasonType reason : mDataDisallowedReasonSet) { 57 reasonStr.append(" ").append(reason); 58 } 59 } else { 60 reasonStr.append("Data allowed reason:"); 61 reasonStr.append(" ").append(mDataAllowedReason); 62 } 63 return reasonStr.toString(); 64 } 65 copyFrom(DataConnectionReasons reasons)66 void copyFrom(DataConnectionReasons reasons) { 67 this.mDataDisallowedReasonSet = reasons.mDataDisallowedReasonSet; 68 this.mDataAllowedReason = reasons.mDataAllowedReason; 69 } 70 allowed()71 boolean allowed() { 72 return mDataDisallowedReasonSet.size() == 0; 73 } 74 75 /** 76 * Check if it contains a certain disallowed reason. 77 * 78 * @param reason The disallowed reason to check. 79 * @return {@code true} if the provided reason matches one of the disallowed reasons. 80 */ 81 @VisibleForTesting contains(DataDisallowedReasonType reason)82 public boolean contains(DataDisallowedReasonType reason) { 83 return mDataDisallowedReasonSet.contains(reason); 84 } 85 86 /** 87 * Check if only one disallowed reason prevent data connection. 88 * 89 * @param reason The given reason to check 90 * @return True if the given reason is the only one that prevents data connection 91 */ containsOnly(DataDisallowedReasonType reason)92 public boolean containsOnly(DataDisallowedReasonType reason) { 93 return mDataDisallowedReasonSet.size() == 1 && contains(reason); 94 } 95 contains(DataAllowedReasonType reason)96 boolean contains(DataAllowedReasonType reason) { 97 return reason == mDataAllowedReason; 98 } 99 containsHardDisallowedReasons()100 boolean containsHardDisallowedReasons() { 101 for (DataDisallowedReasonType reason : mDataDisallowedReasonSet) { 102 if (reason.isHardReason()) { 103 return true; 104 } 105 } 106 return false; 107 } 108 109 // Disallowed reasons. There could be multiple reasons if data connection is not allowed. 110 public enum DataDisallowedReasonType { 111 // Soft failure reasons. Normally the reasons from users or policy settings. 112 113 // Data is disabled by the user or policy. 114 DATA_DISABLED(false), 115 // Data roaming is disabled by the user. 116 ROAMING_DISABLED(false), 117 // Default data not selected. 118 DEFAULT_DATA_UNSELECTED(false), 119 120 // Belows are all hard failure reasons. 121 NOT_ATTACHED(true), 122 SIM_NOT_READY(true), 123 INVALID_PHONE_STATE(true), 124 CONCURRENT_VOICE_DATA_NOT_ALLOWED(true), 125 PS_RESTRICTED(true), 126 UNDESIRED_POWER_STATE(true), 127 INTERNAL_DATA_DISABLED(true), 128 RADIO_DISABLED_BY_CARRIER(true), 129 // Not in the right state for data call setup. 130 APN_NOT_CONNECTABLE(true), 131 // Data is in connecting state. No need to send another setup request. 132 DATA_IS_CONNECTING(true), 133 // Data is being disconnected. Telephony will retry after disconnected. 134 DATA_IS_DISCONNECTING(true), 135 // Data is already connected. No need to setup data again. 136 DATA_ALREADY_CONNECTED(true), 137 // certain APNs are not allowed on IWLAN in legacy mode. 138 ON_IWLAN(true), 139 // certain APNs are only allowed when the device is camped on NR. 140 NOT_ON_NR(true), 141 // Data is not allowed while device is in emergency callback mode. 142 IN_ECBM(true), 143 // The given APN type's preferred transport has switched. 144 ON_OTHER_TRANSPORT(true), 145 // Underlying data service is not bound. 146 DATA_SERVICE_NOT_READY(true), 147 // Qualified networks service does not allow certain types of APN brought up on either 148 // cellular or IWLAN. 149 DISABLED_BY_QNS(true), 150 // Data is throttled. The network explicitly requested device not to establish data 151 // connection for a certain period. 152 DATA_THROTTLED(true); 153 154 private boolean mIsHardReason; 155 isHardReason()156 boolean isHardReason() { 157 return mIsHardReason; 158 } 159 DataDisallowedReasonType(boolean isHardReason)160 DataDisallowedReasonType(boolean isHardReason) { 161 mIsHardReason = isHardReason; 162 } 163 } 164 165 // Data allowed reasons. There will be only one reason if data is allowed. 166 enum DataAllowedReasonType { 167 // Note that unlike disallowed reasons, we only have one allowed reason every time 168 // when we check data is allowed or not. The order of these allowed reasons is very 169 // important. The lower ones take precedence over the upper ones. 170 NONE, 171 NORMAL, 172 UNMETERED_APN, 173 RESTRICTED_REQUEST, 174 EMERGENCY_APN, 175 } 176 } 177