1 /* 2 * Copyright (C) 2014 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; 18 19 import android.util.Log; 20 import android.content.Context; 21 import android.content.ContentResolver; 22 import android.database.Cursor; 23 24 import com.android.internal.telephony.HbpcdLookup; 25 import com.android.internal.telephony.HbpcdLookup.MccIdd; 26 import com.android.internal.telephony.HbpcdLookup.MccLookup; 27 import com.android.internal.telephony.HbpcdLookup.MccSidConflicts; 28 import com.android.internal.telephony.HbpcdLookup.MccSidRange; 29 import com.android.internal.telephony.HbpcdLookup.ArbitraryMccSidMatch; 30 31 public final class HbpcdUtils { 32 private static final String LOG_TAG = "HbpcdUtils"; 33 private static final boolean DBG = false; 34 private ContentResolver resolver = null; 35 HbpcdUtils(Context context)36 public HbpcdUtils(Context context) { 37 resolver = context.getContentResolver(); 38 } 39 40 /** 41 * Resolves the unknown MCC with SID and Timezone information. 42 */ getMcc(int sid, int tz, int DSTflag, boolean isNitzTimeZone)43 public int getMcc(int sid, int tz, int DSTflag, boolean isNitzTimeZone) { 44 int tmpMcc = 0; 45 46 // check if SID exists in arbitrary_mcc_sid_match table. 47 // these SIDs are assigned to more than 1 operators, but they are known to 48 // be used by a specific operator, other operators having the same SID are 49 // not using it currently, if that SID is in this table, we don't need to 50 // check other tables. 51 String projection2[] = {ArbitraryMccSidMatch.MCC}; 52 Cursor c2 = resolver.query(ArbitraryMccSidMatch.CONTENT_URI, projection2, 53 ArbitraryMccSidMatch.SID + "=" + sid, null, null); 54 55 if (c2 != null) { 56 int c2Counter = c2.getCount(); 57 if (DBG) { 58 Log.d(LOG_TAG, "Query unresolved arbitrary table, entries are " + c2Counter); 59 } 60 if (c2Counter == 1) { 61 if (DBG) { 62 Log.d(LOG_TAG, "Query Unresolved arbitrary returned the cursor " + c2 ); 63 } 64 c2.moveToFirst(); 65 tmpMcc = c2.getInt(0); 66 if (DBG) { 67 Log.d(LOG_TAG, "MCC found in arbitrary_mcc_sid_match: " + tmpMcc); 68 } 69 c2.close(); 70 return tmpMcc; 71 } 72 c2.close(); 73 } 74 75 // Then check if SID exists in mcc_sid_conflict table. 76 // and use the timezone in mcc_lookup table to check which MCC matches. 77 String projection3[] = {MccSidConflicts.MCC}; 78 Cursor c3 = resolver.query(MccSidConflicts.CONTENT_URI, projection3, 79 MccSidConflicts.SID_CONFLICT + "=" + sid + " and (((" + 80 MccLookup.GMT_OFFSET_LOW + "<=" + tz + ") and (" + tz + "<=" + 81 MccLookup.GMT_OFFSET_HIGH + ") and (" + "0=" + DSTflag + ")) or ((" + 82 MccLookup.GMT_DST_LOW + "<=" + tz + ") and (" + tz + "<=" + 83 MccLookup.GMT_DST_HIGH + ") and (" + "1=" + DSTflag + ")))", 84 null, null); 85 if (c3 != null) { 86 int c3Counter = c3.getCount(); 87 if (c3Counter > 0) { 88 if (c3Counter > 1) { 89 Log.w(LOG_TAG, "something wrong, get more results for 1 conflict SID: " + c3); 90 } 91 if (DBG) Log.d(LOG_TAG, "Query conflict sid returned the cursor " + c3 ); 92 c3.moveToFirst(); 93 tmpMcc = c3.getInt(0); 94 if (DBG) Log.d(LOG_TAG, 95 "MCC found in mcc_lookup_table. Return tmpMcc = " + tmpMcc); 96 c3.close(); 97 if (isNitzTimeZone) { 98 return tmpMcc; 99 } else { 100 // time zone is not accurate, it may get wrong mcc, ignore it. 101 if (DBG) Log.d(LOG_TAG, "time zone is not accurate, mcc may be " 102 + tmpMcc); 103 return 0; 104 } 105 } 106 } 107 108 // if there is no conflict, then check if SID is in mcc_sid_range. 109 String projection5[] = {MccSidRange.MCC}; 110 Cursor c5 = resolver.query(MccSidRange.CONTENT_URI, projection5, 111 MccSidRange.RANGE_LOW + "<=" + sid + " and " + 112 MccSidRange.RANGE_HIGH + ">=" + sid, 113 null, null); 114 if (c5 != null) { 115 if (c5.getCount() > 0) { 116 if (DBG) Log.d(LOG_TAG, "Query Range returned the cursor " + c5 ); 117 c5.moveToFirst(); 118 tmpMcc = c5.getInt(0); 119 if (DBG) Log.d(LOG_TAG, "SID found in mcc_sid_range. Return tmpMcc = " + tmpMcc); 120 c5.close(); 121 return tmpMcc; 122 } 123 c5.close(); 124 } 125 if (DBG) Log.d(LOG_TAG, "SID NOT found in mcc_sid_range."); 126 127 if (DBG) Log.d(LOG_TAG, "Exit getMccByOtherFactors. Return tmpMcc = " + tmpMcc ); 128 // If unknown MCC still could not be resolved, 129 return tmpMcc; 130 } 131 132 /** 133 * Gets country information with given MCC. 134 */ getIddByMcc(int mcc)135 public String getIddByMcc(int mcc) { 136 if (DBG) Log.d(LOG_TAG, "Enter getHbpcdInfoByMCC."); 137 String idd = ""; 138 139 Cursor c = null; 140 141 String projection[] = {MccIdd.IDD}; 142 Cursor cur = resolver.query(MccIdd.CONTENT_URI, projection, 143 MccIdd.MCC + "=" + mcc, null, null); 144 if (cur != null) { 145 if (cur.getCount() > 0) { 146 if (DBG) Log.d(LOG_TAG, "Query Idd returned the cursor " + cur ); 147 // TODO: for those country having more than 1 IDDs, need more information 148 // to decide which IDD would be used. currently just use the first 1. 149 cur.moveToFirst(); 150 idd = cur.getString(0); 151 if (DBG) Log.d(LOG_TAG, "IDD = " + idd); 152 153 } 154 cur.close(); 155 } 156 if (c != null) c.close(); 157 158 if (DBG) Log.d(LOG_TAG, "Exit getHbpcdInfoByMCC."); 159 return idd; 160 } 161 } 162