• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
84 
85     @NonNull
getCarrierApnTypeMap(PersistableBundle carrierConfig)86     private Map<Integer, Integer> getCarrierApnTypeMap(PersistableBundle carrierConfig) {
87         if (carrierConfig == null) {
88             Rlog.w(TAG, "carrier config is null");
89             return new ArrayMap<>();
90         }
91 
92         final String[] apnTypeConfig =
93                 carrierConfig.getStringArray(CarrierConfigManager.KEY_APN_PRIORITY_STRING_ARRAY);
94 
95         final Map<Integer, Integer> apnTypeMap = new ArrayMap<>();
96         if (apnTypeConfig != null) {
97             for (final String entry : apnTypeConfig) {
98                 try {
99                     final String[] keyValue = entry.split(":");
100                     if (keyValue.length != 2) {
101                         Rlog.e(TAG, "Apn type entry must have exactly one ':'");
102                     } else if (keyValue[0].contains(",")) {
103                         //getApnTypesBitmaskFromString parses commas to a list, not valid here.
104                         Rlog.e(TAG, "Invalid apn type name, entry: " + entry);
105                     } else {
106                         int apnTypeBitmask = ApnSetting.getApnTypesBitmaskFromString(keyValue[0]);
107                         if (apnTypeBitmask > 0) {
108                             apnTypeMap.put(apnTypeBitmask, Integer.parseInt(keyValue[1]));
109                         } else {
110                             Rlog.e(TAG, "Invalid apn type name, entry: " + entry);
111                         }
112                     }
113 
114                 } catch (Exception ex) {
115                     Rlog.e(TAG, "Exception on apn type entry: " + entry + "\n", ex);
116                 }
117             }
118         }
119         return apnTypeMap;
120     }
121 
add(@nnotation.ApnType int type, Map<Integer, Integer> apnTypeMap)122     private void add(@Annotation.ApnType int type, Map<Integer, Integer> apnTypeMap) {
123         if (apnTypeMap.containsKey(type)) {
124             mConfigTypeMap.put(type, new ApnConfigType(type, apnTypeMap.get(type)));
125         }
126     }
127 }
128