1 /* 2 * Copyright (c) 2011 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.cdma; 18 19 import java.util.concurrent.atomic.AtomicInteger; 20 21 import com.android.internal.telephony.CommandsInterface; 22 import android.content.Context; 23 import android.os.AsyncResult; 24 import android.os.Handler; 25 import android.os.Message; 26 import android.os.Registrant; 27 import android.os.RegistrantList; 28 import android.provider.Settings; 29 import android.telephony.Rlog; 30 31 /** 32 * Class that handles the CDMA subscription source changed events from RIL 33 */ 34 public class CdmaSubscriptionSourceManager extends Handler { 35 static final String LOG_TAG = "CdmaSSM"; 36 private static final int EVENT_CDMA_SUBSCRIPTION_SOURCE_CHANGED = 1; 37 private static final int EVENT_GET_CDMA_SUBSCRIPTION_SOURCE = 2; 38 private static final int EVENT_RADIO_ON = 3; 39 private static final int EVENT_SUBSCRIPTION_STATUS_CHANGED = 4; 40 41 // To know subscription is activated 42 private static final int SUBSCRIPTION_ACTIVATED = 1; 43 44 public static final int SUBSCRIPTION_SOURCE_UNKNOWN = -1; 45 public static final int SUBSCRIPTION_FROM_RUIM = 0; /* CDMA subscription from RUIM */ 46 public static final int SUBSCRIPTION_FROM_NV = 1; /* CDMA subscription from NV */ 47 public static final int PREFERRED_CDMA_SUBSCRIPTION = SUBSCRIPTION_FROM_NV; 48 49 private static CdmaSubscriptionSourceManager sInstance; 50 private static final Object sReferenceCountMonitor = new Object(); 51 private static int sReferenceCount = 0; 52 53 // ***** Instance Variables 54 private CommandsInterface mCi; 55 private Context mContext; 56 private RegistrantList mCdmaSubscriptionSourceChangedRegistrants = new RegistrantList(); 57 58 // Type of CDMA subscription source 59 private AtomicInteger mCdmaSubscriptionSource = new AtomicInteger(SUBSCRIPTION_FROM_NV); 60 61 // Constructor 62 private CdmaSubscriptionSourceManager(Context context, CommandsInterface ci) { 63 mContext = context; 64 mCi = ci; 65 mCi.registerForCdmaSubscriptionChanged(this, EVENT_CDMA_SUBSCRIPTION_SOURCE_CHANGED, null); 66 mCi.registerForOn(this, EVENT_RADIO_ON, null); 67 int subscriptionSource = getDefault(context); 68 mCdmaSubscriptionSource.set(subscriptionSource); 69 mCi.registerForSubscriptionStatusChanged(this, EVENT_SUBSCRIPTION_STATUS_CHANGED, null); 70 } 71 72 /** 73 * This function creates a single instance of this class 74 * 75 * @return object of type CdmaSubscriptionSourceManager 76 */ 77 public static CdmaSubscriptionSourceManager getInstance(Context context, 78 CommandsInterface ci, Handler h, int what, Object obj) { 79 synchronized (sReferenceCountMonitor) { 80 if (null == sInstance) { 81 sInstance = new CdmaSubscriptionSourceManager(context, ci); 82 } 83 CdmaSubscriptionSourceManager.sReferenceCount++; 84 } 85 sInstance.registerForCdmaSubscriptionSourceChanged(h, what, obj); 86 return sInstance; 87 } 88 89 /** 90 * Unregisters for the registered event with RIL 91 */ 92 public void dispose(Handler h) { 93 mCdmaSubscriptionSourceChangedRegistrants.remove(h); 94 synchronized (sReferenceCountMonitor) { 95 sReferenceCount--; 96 if (sReferenceCount <= 0) { 97 mCi.unregisterForCdmaSubscriptionChanged(this); 98 mCi.unregisterForOn(this); 99 mCi.unregisterForSubscriptionStatusChanged(this); 100 sInstance = null; 101 } 102 } 103 } 104 105 /* 106 * (non-Javadoc) 107 * @see android.os.Handler#handleMessage(android.os.Message) 108 */ 109 @Override 110 public void handleMessage(Message msg) { 111 AsyncResult ar; 112 switch (msg.what) { 113 case EVENT_CDMA_SUBSCRIPTION_SOURCE_CHANGED: 114 case EVENT_GET_CDMA_SUBSCRIPTION_SOURCE: 115 { 116 log("CDMA_SUBSCRIPTION_SOURCE event = " + msg.what); 117 ar = (AsyncResult) msg.obj; 118 handleGetCdmaSubscriptionSource(ar); 119 } 120 break; 121 case EVENT_RADIO_ON: { 122 mCi.getCdmaSubscriptionSource(obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_SOURCE)); 123 } 124 break; 125 case EVENT_SUBSCRIPTION_STATUS_CHANGED: { 126 log("EVENT_SUBSCRIPTION_STATUS_CHANGED"); 127 ar = (AsyncResult)msg.obj; 128 if (ar.exception == null) { 129 int actStatus = ((int[])ar.result)[0]; 130 log("actStatus = " + actStatus); 131 if (actStatus == SUBSCRIPTION_ACTIVATED) { // Subscription Activated 132 // In case of multi-SIM, framework should wait for the subscription ready 133 // to send any request to RIL. Otherwise it will return failure. 134 Rlog.v(LOG_TAG,"get Cdma Subscription Source"); 135 mCi.getCdmaSubscriptionSource( 136 obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_SOURCE)); 137 } 138 } else { 139 logw("EVENT_SUBSCRIPTION_STATUS_CHANGED, Exception:" + ar.exception); 140 } 141 } 142 break; 143 default: 144 super.handleMessage(msg); 145 } 146 } 147 148 /** 149 * Returns the current CDMA subscription source value 150 * @return CDMA subscription source value 151 */ 152 public int getCdmaSubscriptionSource() { 153 return mCdmaSubscriptionSource.get(); 154 } 155 156 /** 157 * Gets the default CDMA subscription source 158 * 159 * @return Default CDMA subscription source from Settings DB if present. 160 */ 161 public static int getDefault(Context context) { 162 // Get the default value from the Settings 163 int subscriptionSource = Settings.Global.getInt(context.getContentResolver(), 164 Settings.Global.CDMA_SUBSCRIPTION_MODE, PREFERRED_CDMA_SUBSCRIPTION); 165 return subscriptionSource; 166 } 167 168 /** 169 * Clients automatically register for CDMA subscription source changed event 170 * when they get an instance of this object. 171 */ 172 private void registerForCdmaSubscriptionSourceChanged(Handler h, int what, Object obj) { 173 Registrant r = new Registrant (h, what, obj); 174 mCdmaSubscriptionSourceChangedRegistrants.add(r); 175 } 176 177 /** 178 * Handles the call to get the subscription source 179 * 180 * @param ar AsyncResult object that contains the result of get CDMA 181 * subscription source call 182 */ 183 private void handleGetCdmaSubscriptionSource(AsyncResult ar) { 184 if ((ar.exception == null) && (ar.result != null)) { 185 int newSubscriptionSource = ((int[]) ar.result)[0]; 186 187 if (newSubscriptionSource != mCdmaSubscriptionSource.get()) { 188 log("Subscription Source Changed : " + mCdmaSubscriptionSource + " >> " 189 + newSubscriptionSource); 190 mCdmaSubscriptionSource.set(newSubscriptionSource); 191 192 // Notify registrants of the new CDMA subscription source 193 mCdmaSubscriptionSourceChangedRegistrants.notifyRegistrants(new AsyncResult(null, 194 null, null)); 195 } 196 } else { 197 // GET_CDMA_SUBSCRIPTION is returning Failure. Probably 198 // because modem created GSM Phone. If modem created 199 // GSMPhone, then PhoneProxy will trigger a change in 200 // Phone objects and this object will be destroyed. 201 logw("Unable to get CDMA Subscription Source, Exception: " + ar.exception 202 + ", result: " + ar.result); 203 } 204 } 205 206 private void log(String s) { 207 Rlog.d(LOG_TAG, s); 208 } 209 210 private void logw(String s) { 211 Rlog.w(LOG_TAG, s); 212 } 213 214 } 215