1 /*
2  * Copyright (C) 2006 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.mocks;
18 
19 import static android.telephony.SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
20 import static android.telephony.SubscriptionManager.INVALID_PHONE_INDEX;
21 import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
22 
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.RemoteException;
26 import android.os.UserHandle;
27 import android.telephony.SubscriptionInfo;
28 
29 import com.android.internal.telephony.CommandsInterface;
30 import com.android.internal.telephony.ITelephonyRegistry;
31 import com.android.internal.telephony.Phone;
32 import com.android.internal.telephony.PhoneConstants;
33 import com.android.internal.telephony.SubscriptionController;
34 import com.android.internal.telephony.TelephonyIntents;
35 
36 import java.io.FileDescriptor;
37 import java.io.PrintWriter;
38 import java.util.List;
39 import java.util.concurrent.atomic.AtomicInteger;
40 
41 // must extend SubscriptionController as some people use it directly within-process
42 public class SubscriptionControllerMock extends SubscriptionController {
43     final AtomicInteger mDefaultDataSubId = new AtomicInteger(INVALID_SUBSCRIPTION_ID);
44     final AtomicInteger mDefaultVoiceSubId = new AtomicInteger(INVALID_SUBSCRIPTION_ID);
45     final ITelephonyRegistry.Stub mTelephonyRegistry;
46     final int[][] mSlotIndexToSubId;
47 
init(Phone phone)48     public static SubscriptionController init(Phone phone) {
49         throw new RuntimeException("not implemented");
50     }
init(Context c, CommandsInterface[] ci)51     public static SubscriptionController init(Context c, CommandsInterface[] ci) {
52         throw new RuntimeException("not implemented");
53     }
getInstance()54     public static SubscriptionController getInstance() {
55         throw new RuntimeException("not implemented");
56     }
57 
SubscriptionControllerMock(Context c, ITelephonyRegistry.Stub tr, int phoneCount)58     public SubscriptionControllerMock(Context c, ITelephonyRegistry.Stub tr, int phoneCount) {
59         super(c);
60         mTelephonyRegistry = tr;
61         mSlotIndexToSubId = new int[phoneCount][];
62         for (int i = 0; i < phoneCount; i++) {
63             mSlotIndexToSubId[i] = new int[1];
64             mSlotIndexToSubId[i][0] = INVALID_SUBSCRIPTION_ID;
65         }
66     }
67 
init(Context c)68     protected void init(Context c) {
69         mContext = c;
70     }
71 
72     @Override
getDefaultDataSubId()73     public int getDefaultDataSubId() {
74         return mDefaultDataSubId.get();
75     }
76 
77     @Override
setDefaultDataSubId(int subId)78     public void setDefaultDataSubId(int subId) {
79         if (subId == DEFAULT_SUBSCRIPTION_ID) {
80             throw new RuntimeException("setDefaultDataSubId called with DEFAULT_SUB_ID");
81         }
82 
83         mDefaultDataSubId.set(subId);
84         broadcastDefaultDataSubIdChanged(subId);
85     }
86 
broadcastDefaultDataSubIdChanged(int subId)87     private void broadcastDefaultDataSubIdChanged(int subId) {
88         // Broadcast an Intent for default data sub change
89         Intent intent = new Intent(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
90         intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
91         intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, subId);
92         mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
93     }
94 
95     @Override
getSubIdUsingPhoneId(int phoneId)96     public int getSubIdUsingPhoneId(int phoneId) {
97         int[] subIds = getSubId(phoneId);
98         if (subIds == null || subIds.length == 0) {
99             return INVALID_SUBSCRIPTION_ID;
100         }
101         return subIds[0];
102     }
103 
104     @Override
notifySubscriptionInfoChanged()105     public void notifySubscriptionInfoChanged() {
106         try {
107             mTelephonyRegistry.notifySubscriptionInfoChanged();
108         } catch (RemoteException ex) {}
109     }
110     @Override
getActiveSubscriptionInfo(int subId, String callingPackage)111     public SubscriptionInfo getActiveSubscriptionInfo(int subId, String callingPackage) {
112         throw new RuntimeException("not implemented");
113     }
114     @Override
getActiveSubscriptionInfoForIccId(String iccId, String callingPackage)115     public SubscriptionInfo getActiveSubscriptionInfoForIccId(String iccId, String callingPackage) {
116         throw new RuntimeException("not implemented");
117     }
118     @Override
getActiveSubscriptionInfoForSimSlotIndex(int slotIndex, String cp)119     public SubscriptionInfo getActiveSubscriptionInfoForSimSlotIndex(int slotIndex, String cp){
120         throw new RuntimeException("not implemented");
121     }
122     @Override
getAllSubInfoList(String callingPackage)123     public List<SubscriptionInfo> getAllSubInfoList(String callingPackage) {
124         throw new RuntimeException("not implemented");
125     }
126     @Override
getActiveSubscriptionInfoList(String callingPackage)127     public List<SubscriptionInfo> getActiveSubscriptionInfoList(String callingPackage) {
128         throw new RuntimeException("not implemented");
129     }
130     @Override
getActiveSubInfoCount(String callingPackage)131     public int getActiveSubInfoCount(String callingPackage) {
132         throw new RuntimeException("not implemented");
133     }
134     @Override
getAllSubInfoCount(String callingPackage)135     public int getAllSubInfoCount(String callingPackage) {
136         throw new RuntimeException("not implemented");
137     }
138     @Override
getActiveSubInfoCountMax()139     public int getActiveSubInfoCountMax() {
140         throw new RuntimeException("not implemented");
141     }
142     @Override
addSubInfoRecord(String iccId, int slotIndex)143     public int addSubInfoRecord(String iccId, int slotIndex) {
144         throw new RuntimeException("not implemented");
145     }
146     @Override
setPlmnSpn(int slotIndex, boolean showPlmn, String plmn, boolean showSpn, String spn)147     public boolean setPlmnSpn(int slotIndex, boolean showPlmn, String plmn, boolean showSpn,
148             String spn) {
149         throw new RuntimeException("not implemented");
150     }
151     @Override
setIconTint(int tint, int subId)152     public int setIconTint(int tint, int subId) {
153         throw new RuntimeException("not implemented");
154     }
155     @Override
setDisplayName(String displayName, int subId)156     public int setDisplayName(String displayName, int subId) {
157         throw new RuntimeException("not implemented");
158     }
159     @Override
setDisplayNameUsingSrc(String displayName, int subId, long nameSource)160     public int setDisplayNameUsingSrc(String displayName, int subId, long nameSource) {
161         throw new RuntimeException("not implemented");
162     }
163     @Override
setDisplayNumber(String number, int subId)164     public int setDisplayNumber(String number, int subId) {
165         throw new RuntimeException("not implemented");
166     }
167     @Override
setDataRoaming(int roaming, int subId)168     public int setDataRoaming(int roaming, int subId) {
169         throw new RuntimeException("not implemented");
170     }
171     @Override
setMccMnc(String mccMnc, int subId)172     public int setMccMnc(String mccMnc, int subId) {
173         throw new RuntimeException("not implemented");
174     }
175     @Override
getSlotIndex(int subId)176     public int getSlotIndex(int subId) {
177         throw new RuntimeException("not implemented");
178     }
179 
isInvalidslotIndex(int slotIndex)180     private boolean isInvalidslotIndex(int slotIndex) {
181         if (slotIndex < 0 || slotIndex >= mSlotIndexToSubId.length) return true;
182         return false;
183     }
184 
185     @Override
getSubId(int slotIndex)186     public int[] getSubId(int slotIndex) {
187         if (isInvalidslotIndex(slotIndex)) {
188             return null;
189         }
190         return mSlotIndexToSubId[slotIndex];
191     }
setSlotSubId(int slotIndex, int subId)192     public void setSlotSubId(int slotIndex, int subId) {
193         if (isInvalidslotIndex(slotIndex)) {
194             throw new RuntimeException("invalid slot specified" + slotIndex);
195         }
196         if (mSlotIndexToSubId[slotIndex][0] != subId) {
197             mSlotIndexToSubId[slotIndex][0] = subId;
198             try {
199                 mTelephonyRegistry.notifySubscriptionInfoChanged();
200             } catch (RemoteException ex) {}
201         }
202     }
203     @Override
getPhoneId(int subId)204     public int getPhoneId(int subId) {
205         if (subId == DEFAULT_SUBSCRIPTION_ID) {
206             subId = getDefaultSubId();
207         }
208 
209         if (subId <= INVALID_SUBSCRIPTION_ID) return INVALID_PHONE_INDEX;
210 
211         for (int i = 0; i < mSlotIndexToSubId.length; i++) {
212             if (mSlotIndexToSubId[i][0] == subId) return i;
213         }
214         return INVALID_PHONE_INDEX;
215     }
216     @Override
clearSubInfo()217     public int clearSubInfo() {
218         throw new RuntimeException("not implemented");
219     }
220     @Override
getDefaultSubId()221     public int getDefaultSubId() {
222         throw new RuntimeException("not implemented");
223     }
224     @Override
setDefaultSmsSubId(int subId)225     public void setDefaultSmsSubId(int subId) {
226         throw new RuntimeException("not implemented");
227     }
228     @Override
getDefaultSmsSubId()229     public int getDefaultSmsSubId() {
230         throw new RuntimeException("not implemented");
231     }
232     @Override
setDefaultVoiceSubId(int subId)233     public void setDefaultVoiceSubId(int subId) {
234         if (subId == DEFAULT_SUBSCRIPTION_ID) {
235             throw new RuntimeException("setDefaultDataSubId called with DEFAULT_SUB_ID");
236         }
237         mDefaultVoiceSubId.set(subId);
238         broadcastDefaultVoiceSubIdChanged(subId);
239     }
240     @Override
getDefaultVoiceSubId()241     public int getDefaultVoiceSubId() {
242         if (mDefaultVoiceSubId != null) {
243             return mDefaultVoiceSubId.get();
244         } else {
245             return INVALID_SUBSCRIPTION_ID;
246         }
247     }
248     @Override
clearDefaultsForInactiveSubIds()249     public void clearDefaultsForInactiveSubIds() {
250         throw new RuntimeException("not implemented");
251     }
252     @Override
updatePhonesAvailability(Phone[] phones)253     public void updatePhonesAvailability(Phone[] phones) {
254         throw new RuntimeException("not implemented");
255     }
256     @Override
getActiveSubIdList()257     public int[] getActiveSubIdList() {
258         throw new RuntimeException("not implemented");
259     }
260     @Override
isActiveSubId(int subId)261     public boolean isActiveSubId(int subId) {
262         throw new RuntimeException("not implemented");
263     }
264     @Override
getSimStateForSlotIndex(int slotIndex)265     public int getSimStateForSlotIndex(int slotIndex) {
266         throw new RuntimeException("not implemented");
267     }
268     @Override
setSubscriptionProperty(int subId, String propKey, String propValue)269     public void setSubscriptionProperty(int subId, String propKey, String propValue) {
270         throw new RuntimeException("not implemented");
271     }
272     @Override
getSubscriptionProperty(int subId, String propKey, String callingPackage)273     public String getSubscriptionProperty(int subId, String propKey, String callingPackage) {
274         throw new RuntimeException("not implemented");
275     }
276     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)277     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
278         throw new RuntimeException("not implemented");
279     }
280 }
281