1 /* 2 * Copyright (C) 2016 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.stk; 18 19 import android.content.Context; 20 import android.content.res.XmlResourceParser; 21 import android.graphics.Bitmap; 22 import android.graphics.BitmapFactory; 23 import android.os.Build; 24 import android.telephony.SubscriptionInfo; 25 import android.telephony.SubscriptionManager; 26 import android.telephony.TelephonyManager; 27 import android.text.TextUtils; 28 29 import com.android.internal.telephony.PhoneConstants; 30 import com.android.internal.telephony.cat.CatLog; 31 import com.android.internal.util.XmlUtils; 32 33 import java.util.ArrayList; 34 35 /** 36 * Provides preset label and/or icon in accordance with mcc/mnc 37 * conbination of the inserted SIM card for Multi-SIM model. 38 */ 39 public class StkMenuConfig { 40 private static final String LOG_TAG = "StkMenuConfig"; 41 private static final boolean DBG = Build.IS_DEBUGGABLE; 42 43 private static final String XML_OPERATORS_TAG = "operators"; 44 private static final String XML_OPERATOR_TAG = "operator"; 45 46 private static final String XML_MCC_ATTR = "mcc"; 47 private static final String XML_MNC_ATTR = "mnc"; 48 private static final String XML_LABEL_ATTR = "label"; 49 private static final String XML_ICON_ATTR = "icon"; 50 private static final String RESOURCE_TYPE = "drawable"; 51 52 private static final int UNSPECIFIED = -1; 53 54 private static final Config NO_CONFIG = new Config(0, 0, null, null); 55 56 private static final Object sLock = new Object(); 57 private static StkMenuConfig sInstance; 58 59 private Context mContext; 60 private ArrayList<Config> mArray; 61 private Config mConfigs[] = null; 62 63 private static class Config { 64 public int mcc; 65 public int mnc; 66 public String label; 67 public String icon; 68 Config(int mcc, int mnc, String label, String icon)69 public Config(int mcc, int mnc, String label, String icon) { 70 this.mcc = mcc; 71 this.mnc = mnc; 72 this.label = label; 73 this.icon = icon; 74 } 75 } 76 getInstance(Context applicationContext)77 public static StkMenuConfig getInstance(Context applicationContext) { 78 synchronized (sLock) { 79 if (sInstance == null) { 80 sInstance = new StkMenuConfig(); 81 sInstance.initialize(applicationContext); 82 } 83 return sInstance; 84 } 85 } 86 87 /** 88 * Returns a preset label, if exists. 89 */ getLabel(int slotId)90 public String getLabel(int slotId) { 91 findConfig(slotId); 92 93 if (DBG) CatLog.d(LOG_TAG, "getLabel: " + mConfigs[slotId].label + ", slot id: " + slotId); 94 return mConfigs[slotId].label; 95 } 96 97 /** 98 * Returns a preset icon, if exists. 99 */ getIcon(int slotId)100 public Bitmap getIcon(int slotId) { 101 findConfig(slotId); 102 103 Bitmap bitmap = null; 104 if (mConfigs[slotId].icon != null) { 105 int resId = mContext.getResources().getIdentifier(mConfigs[slotId].icon, 106 RESOURCE_TYPE, mContext.getPackageName()); 107 bitmap = resId == UNSPECIFIED ? null : 108 BitmapFactory.decodeResource(mContext.getResources(), resId); 109 } 110 if (DBG) CatLog.d(LOG_TAG, "getIcon: " + mConfigs[slotId].icon + ", slot id: " + slotId); 111 return bitmap; 112 } 113 findConfig(int slotId)114 private void findConfig(int slotId) { 115 SubscriptionManager sm = (SubscriptionManager) mContext.getSystemService( 116 Context.TELEPHONY_SUBSCRIPTION_SERVICE); 117 SubscriptionInfo info = (sm != null) ? sm.getActiveSubscriptionInfoForSimSlotIndex(slotId) 118 : null; 119 if (info == null) { 120 mConfigs[slotId] = NO_CONFIG; 121 return; 122 } 123 124 TelephonyManager telephony = 125 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 126 String operator = telephony.getSimOperator(info.getSubscriptionId()); 127 if (TextUtils.isEmpty(operator) || (operator.length() < 5)) { 128 mConfigs[slotId] = NO_CONFIG; 129 return; 130 } 131 132 int mcc = Integer.parseInt(operator.substring(0, 3)); 133 int mnc = Integer.parseInt(operator.substring(3)); 134 135 if (mConfigs[slotId] != null && mConfigs[slotId].mcc == mcc 136 && mConfigs[slotId].mnc == mnc) { 137 if (DBG) CatLog.d(LOG_TAG, "Return the cached config, slot id: " + slotId); 138 return; 139 } 140 141 if (DBG) CatLog.d(LOG_TAG, "Find config and create the cached config, slot id: " + slotId); 142 for (Config config : mArray) { 143 if ((config.mcc == mcc) && (config.mnc == mnc)) { 144 mConfigs[slotId] = config; 145 return; 146 } 147 } 148 149 mConfigs[slotId] = new Config(mcc, mnc, null, null); 150 } 151 initialize(Context context)152 private void initialize(Context context) { 153 mContext = context; 154 mArray = new ArrayList<Config>(); 155 mConfigs = new Config[TelephonyManager.from(mContext).getSimCount()]; 156 157 XmlResourceParser parser = mContext.getResources().getXml(R.xml.menu_conf); 158 159 try { 160 XmlUtils.beginDocument(parser, XML_OPERATORS_TAG); 161 162 do { 163 XmlUtils.nextElement(parser); 164 165 if (!XML_OPERATOR_TAG.equals(parser.getName())) { 166 break; 167 } 168 169 int mcc = parser.getAttributeIntValue(null, XML_MCC_ATTR, UNSPECIFIED); 170 int mnc = parser.getAttributeIntValue(null, XML_MNC_ATTR, UNSPECIFIED); 171 172 if ((mcc == UNSPECIFIED) || (mnc == UNSPECIFIED)) { 173 continue; 174 } 175 176 String label = parser.getAttributeValue(null, XML_LABEL_ATTR); 177 String icon = parser.getAttributeValue(null, XML_ICON_ATTR); 178 179 Config config = new Config(mcc, mnc, label, icon); 180 mArray.add(config); 181 } while (true); 182 } catch (Exception e) { 183 CatLog.e(LOG_TAG, "Something wrong happened while interpreting the xml file" + e); 184 } finally { 185 parser.close(); 186 } 187 } 188 } 189