• Home
  • 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.settings.network;
18 
19 import android.app.FragmentManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.provider.Settings;
25 import android.telephony.CarrierConfigManager;
26 import android.telephony.TelephonyManager;
27 import android.telephony.UiccPortInfo;
28 import android.telephony.UiccSlotInfo;
29 import android.util.ArraySet;
30 import android.util.Log;
31 
32 import com.android.settings.AsyncTaskSidecar;
33 import com.android.settings.SidecarFragment;
34 
35 import java.util.Collections;
36 import java.util.Set;
37 import java.util.concurrent.CountDownLatch;
38 import java.util.concurrent.TimeUnit;
39 
40 /**
41  * {@code EnableMultiSimSidecar} enables multi SIM on the device. It should only be called for
42  * Android R+. After {@code run} is called, it sets the configuration on modem side to enable
43  * multiple SIMs. Once the configuration is set successfully, it will listen to UICC card changes
44  * until {@code TelMan.EXTRA_ACTIVE_SIM_SUPPORTED_COUNT} matches {@code mNumOfActiveSim} or timeout.
45  */
46 public class EnableMultiSimSidecar extends AsyncTaskSidecar<Void, Boolean> {
47 
48     // Tags
49     private static final String TAG = "EnableMultiSimSidecar";
50 
51     private static final long DEFAULT_ENABLE_MULTI_SIM_TIMEOUT_MILLS = 40 * 1000L;
52 
get(FragmentManager fm)53     public static EnableMultiSimSidecar get(FragmentManager fm) {
54         return SidecarFragment.get(fm, TAG, EnableMultiSimSidecar.class, null /* args */);
55     }
56 
57     final CountDownLatch mSimCardStateChangedLatch = new CountDownLatch(1);
58     private TelephonyManager mTelephonyManager;
59     private int mNumOfActiveSim = 0;
60 
61     private final BroadcastReceiver mCarrierConfigChangeReceiver =
62             new BroadcastReceiver() {
63                 @Override
64                 public void onReceive(Context context, Intent intent) {
65                     int readySimsCount = getReadySimsCount();
66                     int activePortsCount = getActivePortsCount();
67                     // If the number of ready SIM count and active ports equal to the number of SIMs
68                     // need to be activated, the device is successfully switched to multiple active
69                     // SIM mode.
70                     if (readySimsCount == mNumOfActiveSim && activePortsCount == mNumOfActiveSim) {
71                         Log.i(
72                                 TAG,
73                                 String.format("%d ports are active and ready.", mNumOfActiveSim));
74                         mSimCardStateChangedLatch.countDown();
75                         return;
76                     }
77                     Log.i(
78                             TAG,
79                             String.format(
80                                     "%d ports are active and %d SIMs are ready. Keep waiting until"
81                                             + " timeout.",
82                                     activePortsCount, readySimsCount));
83                 }
84             };
85 
86     @Override
doInBackground(Void aVoid)87     protected Boolean doInBackground(Void aVoid) {
88         return updateMultiSimConfig();
89     }
90 
91     @Override
onPostExecute(Boolean isDsdsEnabled)92     protected void onPostExecute(Boolean isDsdsEnabled) {
93         if (isDsdsEnabled) {
94             setState(State.SUCCESS, Substate.UNUSED);
95         } else {
96             setState(State.ERROR, Substate.UNUSED);
97         }
98     }
99 
run(int numberOfSimToActivate)100     public void run(int numberOfSimToActivate) {
101         mTelephonyManager = getContext().getSystemService(TelephonyManager.class);
102         mNumOfActiveSim = numberOfSimToActivate;
103 
104         if (mNumOfActiveSim > mTelephonyManager.getSupportedModemCount()) {
105             Log.e(TAG, "Requested number of active SIM is greater than supported modem count.");
106             setState(State.ERROR, Substate.UNUSED);
107             return;
108         }
109         if (mTelephonyManager.doesSwitchMultiSimConfigTriggerReboot()) {
110             Log.e(TAG, "The device does not support reboot free DSDS.");
111             setState(State.ERROR, Substate.UNUSED);
112             return;
113         }
114         super.run(null /* param */);
115     }
116 
117     // This method registers a ACTION_SIM_CARD_STATE_CHANGED broadcast receiver and wait for slot
118     // changes. If multi SIMs have been successfully enabled, it returns true. Otherwise, returns
119     // false.
updateMultiSimConfig()120     private boolean updateMultiSimConfig() {
121         try {
122             getContext()
123                     .registerReceiver(
124                             mCarrierConfigChangeReceiver,
125                             new IntentFilter(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));
126             mTelephonyManager.switchMultiSimConfig(mNumOfActiveSim);
127             long waitingTimeMillis =
128                     Settings.Global.getLong(
129                             getContext().getContentResolver(),
130                             Settings.Global.ENABLE_MULTI_SLOT_TIMEOUT_MILLIS,
131                             DEFAULT_ENABLE_MULTI_SIM_TIMEOUT_MILLS);
132             if (mSimCardStateChangedLatch.await(waitingTimeMillis, TimeUnit.MILLISECONDS)) {
133                 Log.i(TAG, "Multi SIM were successfully enabled.");
134                 return true;
135             } else {
136                 Log.e(TAG, "Timeout for waiting SIM status.");
137                 return false;
138             }
139         } catch (InterruptedException e) {
140             Log.e(TAG, "Failed to enable multiple SIM due to InterruptedException", e);
141             return false;
142         } finally {
143             getContext().unregisterReceiver(mCarrierConfigChangeReceiver);
144         }
145     }
146 
147     // Returns how many SIMs have SIM ready state, not ready state, or removable slot with absent
148     // SIM state.
getReadySimsCount()149     private int getReadySimsCount() {
150         int readyCardsCount = 0;
151         int activeSlotCount = mTelephonyManager.getActiveModemCount();
152         Set<Integer> activeRemovableLogicalSlots = getActiveRemovableLogicalSlotIds();
153         for (int logicalSlotId = 0; logicalSlotId < activeSlotCount; logicalSlotId++) {
154             int simState = mTelephonyManager.getSimState(logicalSlotId);
155             if (simState == TelephonyManager.SIM_STATE_READY
156                     || simState == TelephonyManager.SIM_STATE_NOT_READY
157                     || simState == TelephonyManager.SIM_STATE_LOADED
158                     || (simState == TelephonyManager.SIM_STATE_ABSENT
159                             && activeRemovableLogicalSlots.contains(logicalSlotId))) {
160                 readyCardsCount++;
161             }
162         }
163         return readyCardsCount;
164     }
165 
166     // Get active port count from {@code TelephonyManager#getUiccSlotsInfo}.
getActivePortsCount()167     private int getActivePortsCount() {
168         UiccSlotInfo[] slotsInfo = mTelephonyManager.getUiccSlotsInfo();
169         if (slotsInfo == null) {
170             return 0;
171         }
172         int activePorts = 0;
173         for (UiccSlotInfo slotInfo : slotsInfo) {
174             if (slotInfo == null) {
175                 continue;
176             }
177             for (UiccPortInfo portInfo : slotInfo.getPorts()) {
178                 if (portInfo.isActive()) {
179                     activePorts++;
180                 }
181             }
182 
183         }
184         return activePorts;
185     }
186 
187     /** Returns a list of active removable logical slot ids. */
getActiveRemovableLogicalSlotIds()188     public Set<Integer> getActiveRemovableLogicalSlotIds() {
189         UiccSlotInfo[] infos = mTelephonyManager.getUiccSlotsInfo();
190         if (infos == null) {
191             return Collections.emptySet();
192         }
193         Set<Integer> activeRemovableLogicalSlotIds = new ArraySet<>();
194         for (UiccSlotInfo info : infos) {
195             if (info == null) {
196                 continue;
197             }
198             for (UiccPortInfo portInfo : info.getPorts()) {
199                 if (portInfo.isActive() && info.isRemovable()) {
200                     activeRemovableLogicalSlotIds.add(portInfo.getLogicalSlotIndex());
201                 }
202             }
203         }
204         return activeRemovableLogicalSlotIds;
205     }
206 }
207