1 /* 2 * Copyright (C) 2020 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 android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.os.PersistableBundle; 22 import android.telephony.Annotation; 23 import android.telephony.CarrierConfigManager; 24 import android.telephony.Rlog; 25 import android.telephony.data.ApnSetting; 26 import android.util.ArrayMap; 27 28 import java.util.Collection; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 /** 33 * Hard coded configuration of specific network types that the telephony module needs. 34 * Formerly stored in network attributes within the resources file. 35 */ 36 public class ApnConfigTypeRepository { 37 38 private static final String TAG = ApnConfigTypeRepository.class.getSimpleName(); 39 40 private final Map<Integer, ApnConfigType> mConfigTypeMap; 41 ApnConfigTypeRepository(PersistableBundle carrierConfig)42 public ApnConfigTypeRepository(PersistableBundle carrierConfig) { 43 mConfigTypeMap = new HashMap<>(); 44 setup(carrierConfig); 45 } 46 47 /** 48 * Gets list of apn config types. 49 * @return All apn config types. 50 */ getTypes()51 public Collection<ApnConfigType> getTypes() { 52 return mConfigTypeMap.values(); 53 } 54 55 /** 56 * Gets the apn config type by apn type. 57 * @param type The ApnType to search for. 58 * @return The config type matching the given apn type. 59 */ 60 @Nullable getByType(@nnotation.ApnType int type)61 public ApnConfigType getByType(@Annotation.ApnType int type) { 62 return mConfigTypeMap.get(type); 63 } 64 setup(PersistableBundle carrierConfig)65 private void setup(PersistableBundle carrierConfig) { 66 addApns(getCarrierApnTypeMap(CarrierConfigManager.getDefaultConfig())); 67 addApns(getCarrierApnTypeMap(carrierConfig)); 68 } 69 addApns(Map<Integer, Integer> apnTypeMap)70 private void addApns(Map<Integer, Integer> apnTypeMap) { 71 add(ApnSetting.TYPE_DEFAULT, apnTypeMap); 72 add(ApnSetting.TYPE_MMS, apnTypeMap); 73 add(ApnSetting.TYPE_SUPL, apnTypeMap); 74 add(ApnSetting.TYPE_DUN, apnTypeMap); 75 add(ApnSetting.TYPE_HIPRI, apnTypeMap); 76 add(ApnSetting.TYPE_FOTA, apnTypeMap); 77 add(ApnSetting.TYPE_IMS, apnTypeMap); 78 add(ApnSetting.TYPE_CBS, apnTypeMap); 79 add(ApnSetting.TYPE_IA, apnTypeMap); 80 add(ApnSetting.TYPE_EMERGENCY, apnTypeMap); 81 add(ApnSetting.TYPE_MCX, apnTypeMap); 82 add(ApnSetting.TYPE_XCAP, apnTypeMap); 83 add(ApnSetting.TYPE_ENTERPRISE, apnTypeMap); 84 } 85 86 @NonNull getCarrierApnTypeMap(PersistableBundle carrierConfig)87 private Map<Integer, Integer> getCarrierApnTypeMap(PersistableBundle carrierConfig) { 88 if (carrierConfig == null) { 89 Rlog.w(TAG, "carrier config is null"); 90 return new ArrayMap<>(); 91 } 92 93 final String[] apnTypeConfig = 94 carrierConfig.getStringArray(CarrierConfigManager.KEY_APN_PRIORITY_STRING_ARRAY); 95 96 final Map<Integer, Integer> apnTypeMap = new ArrayMap<>(); 97 if (apnTypeConfig != null) { 98 for (final String entry : apnTypeConfig) { 99 try { 100 final String[] keyValue = entry.split(":"); 101 if (keyValue.length != 2) { 102 Rlog.e(TAG, "Apn type entry must have exactly one ':'"); 103 } else if (keyValue[0].contains(",")) { 104 //getApnTypesBitmaskFromString parses commas to a list, not valid here. 105 Rlog.e(TAG, "Invalid apn type name, entry: " + entry); 106 } else { 107 int apnTypeBitmask = ApnSetting.getApnTypesBitmaskFromString(keyValue[0]); 108 if (apnTypeBitmask > 0) { 109 apnTypeMap.put(apnTypeBitmask, Integer.parseInt(keyValue[1])); 110 } else { 111 Rlog.e(TAG, "Invalid apn type name, entry: " + entry); 112 } 113 } 114 115 } catch (Exception ex) { 116 Rlog.e(TAG, "Exception on apn type entry: " + entry + "\n", ex); 117 } 118 } 119 } 120 return apnTypeMap; 121 } 122 add(@nnotation.ApnType int type, Map<Integer, Integer> apnTypeMap)123 private void add(@Annotation.ApnType int type, Map<Integer, Integer> apnTypeMap) { 124 if (apnTypeMap.containsKey(type)) { 125 mConfigTypeMap.put(type, new ApnConfigType(type, apnTypeMap.get(type))); 126 } 127 } 128 } 129