16bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt/*
26bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt * Copyright (C) 2006 The Android Open Source Project
36bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt *
46bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
56bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt * you may not use this file except in compliance with the License.
66bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt * You may obtain a copy of the License at
76bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt *
86bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
96bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt *
106bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
116bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
126bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt * See the License for the specific language governing permissions and
146bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt * limitations under the License.
156bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt */
166bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltpackage com.android.internal.telephony.dataconnection;
176bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
186bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport static com.android.internal.telephony.DctConstants.APN_CBS_ID;
196bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport static com.android.internal.telephony.DctConstants.APN_DEFAULT_ID;
206bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport static com.android.internal.telephony.DctConstants.APN_DUN_ID;
216bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport static com.android.internal.telephony.DctConstants.APN_EMERGENCY_ID;
226bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport static com.android.internal.telephony.DctConstants.APN_FOTA_ID;
236bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport static com.android.internal.telephony.DctConstants.APN_IA_ID;
246bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport static com.android.internal.telephony.DctConstants.APN_IMS_ID;
256bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport static com.android.internal.telephony.DctConstants.APN_INVALID_ID;
266bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport static com.android.internal.telephony.DctConstants.APN_MMS_ID;
276bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport static com.android.internal.telephony.DctConstants.APN_SUPL_ID;
286bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
296bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport android.content.Context;
306bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport android.net.NetworkCapabilities;
316bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport android.net.NetworkConfig;
326bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport android.net.NetworkRequest;
336bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport android.telephony.Rlog;
346bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
356bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltimport java.util.HashMap;
366bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
376bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwaltpublic class DcRequest implements Comparable<DcRequest> {
386bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    private static final String LOG_TAG = "DcRequest";
396bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
406bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    public final NetworkRequest networkRequest;
416bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    public final int priority;
426bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    public final int apnId;
436bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
446bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    public DcRequest(NetworkRequest nr, Context context) {
456bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        initApnPriorities(context);
466bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        networkRequest = nr;
476bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        apnId = apnIdForNetworkRequest(networkRequest);
486bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        priority = priorityForApnId(apnId);
496bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    }
506bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
516bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    public String toString() {
526bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        return networkRequest.toString() + ", priority=" + priority + ", apnId=" + apnId;
536bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    }
546bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
556bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    public int hashCode() {
566bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        return networkRequest.hashCode();
576bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    }
586bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
596bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    public boolean equals(Object o) {
606bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (o instanceof DcRequest) {
616bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            return networkRequest.equals(((DcRequest)o).networkRequest);
626bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
636bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        return false;
646bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    }
656bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
666bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    public int compareTo(DcRequest o) {
676bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        return o.priority - priority;
686bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    }
696bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
706bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    private int apnIdForNetworkRequest(NetworkRequest nr) {
716bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        NetworkCapabilities nc = nr.networkCapabilities;
726bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        // For now, ignore the bandwidth stuff
736bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.getTransportTypes().length > 0 &&
746bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt                nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) == false) {
756bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            return APN_INVALID_ID;
766bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
776bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
786bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        // in the near term just do 1-1 matches.
796bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        // TODO - actually try to match the set of capabilities
806bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        int apnId = APN_INVALID_ID;
816bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
826bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        boolean error = false;
836bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
846bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
856bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_DEFAULT_ID;
866bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
876bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
886bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
896bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_MMS_ID;
906bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
916bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
926bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
936bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_SUPL_ID;
946bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
956bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
966bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
976bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_DUN_ID;
986bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
996bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1006bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
1016bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_FOTA_ID;
1026bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
1036bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1046bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
1056bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_IMS_ID;
1066bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
1076bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1086bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
1096bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_CBS_ID;
1106bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
1116bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_IA)) {
1126bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
1136bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_IA_ID;
1146bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
1156bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_RCS)) {
1166bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
1176bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_INVALID_ID;
1186bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            loge("RCS APN type not yet supported");
1196bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
1206bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_XCAP)) {
1216bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
1226bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_INVALID_ID;
1236bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            loge("XCAP APN type not yet supported");
1246bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
1256bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_EIMS)) {
1266bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (apnId != APN_INVALID_ID) error = true;
1276bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            apnId = APN_EMERGENCY_ID;
1286bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
1296bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (error) {
1306bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            // TODO: If this error condition is removed, the framework's handling of
1316bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            // NET_CAPABILITY_NOT_RESTRICTED will need to be updated so requests for
1326bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            // say FOTA and INTERNET are marked as restricted.  This is not how
1336bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            // NetworkCapabilities.maybeMarkCapabilitiesRestricted currently works.
1346bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            loge("Multiple apn types specified in request - result is unspecified!");
1356bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
1366bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        if (apnId == APN_INVALID_ID) {
1376bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            loge("Unsupported NetworkRequest in Telephony: nr=" + nr);
1386bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
1396bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        return apnId;
1406bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    }
1416bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
1426bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    private static final HashMap<Integer, Integer> sApnPriorityMap =
1436bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            new HashMap<Integer, Integer>();
1446bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
1456bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    private void initApnPriorities(Context context) {
1466bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        synchronized (sApnPriorityMap) {
1476bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            if (sApnPriorityMap.isEmpty()) {
1486bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt                String[] networkConfigStrings = context.getResources().getStringArray(
1496bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt                        com.android.internal.R.array.networkAttributes);
1506bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt                for (String networkConfigString : networkConfigStrings) {
1516bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt                    NetworkConfig networkConfig = new NetworkConfig(networkConfigString);
1526bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt                    final int apnId = ApnContext.apnIdForType(networkConfig.type);
1536bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt                    sApnPriorityMap.put(apnId, networkConfig.priority);
1546bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt                }
1556bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt            }
1566bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        }
1576bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    }
1586bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
1596bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    private int priorityForApnId(int apnId) {
1606bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        Integer priority = sApnPriorityMap.get(apnId);
1616bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        return (priority != null ? priority.intValue() : 0);
1626bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    }
1636bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt
1646bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    private void loge(String s) {
1656bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt        Rlog.e(LOG_TAG, s);
1666bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt    }
1676bfc71d2b5340f6274b3e63926a7068e364fc9ffRobert Greenwalt}
168