1 /*
2  * Copyright (C) 2018 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.car.settings.testutils;
18 
19 import static android.telephony.PhoneStateListener.LISTEN_NONE;
20 
21 import android.telephony.PhoneStateListener;
22 import android.telephony.SubscriptionManager;
23 import android.telephony.TelephonyManager;
24 
25 import org.robolectric.annotation.Implementation;
26 import org.robolectric.annotation.Implements;
27 import org.robolectric.annotation.Resetter;
28 
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 
34 @Implements(TelephonyManager.class)
35 public class ShadowTelephonyManager extends org.robolectric.shadows.ShadowTelephonyManager {
36 
37     public static final String SUBSCRIBER_ID = "test_id";
38     private static Map<Integer, Integer> sSubIdsWithResetCalledCount = new HashMap<>();
39     private static int sSimCount = 1;
40     private final Map<PhoneStateListener, Integer> mPhoneStateRegistrations = new HashMap<>();
41     private boolean mIsDataEnabled = false;
42     private boolean mIsRoamingEnabled = false;
43 
verifyFactoryResetCalled(int subId, int numTimes)44     public static boolean verifyFactoryResetCalled(int subId, int numTimes) {
45         if (!sSubIdsWithResetCalledCount.containsKey(subId)) return false;
46         return sSubIdsWithResetCalledCount.get(subId) == numTimes;
47     }
48 
49     @Implementation
listen(PhoneStateListener listener, int flags)50     protected void listen(PhoneStateListener listener, int flags) {
51         super.listen(listener, flags);
52 
53         if (flags == LISTEN_NONE) {
54             mPhoneStateRegistrations.remove(listener);
55         } else {
56             mPhoneStateRegistrations.put(listener, flags);
57         }
58     }
59 
getListenersForFlags(int flags)60     public List<PhoneStateListener> getListenersForFlags(int flags) {
61         List<PhoneStateListener> listeners = new ArrayList<>();
62         for (PhoneStateListener listener : mPhoneStateRegistrations.keySet()) {
63             if ((mPhoneStateRegistrations.get(listener) & flags) != 0) {
64                 listeners.add(listener);
65             }
66         }
67         return listeners;
68     }
69 
70     @Implementation
setDataEnabled(boolean enable)71     protected void setDataEnabled(boolean enable) {
72         mIsDataEnabled = enable;
73     }
74 
75     @Implementation
isDataEnabled()76     protected boolean isDataEnabled() {
77         return mIsDataEnabled;
78     }
79 
80     @Implementation
factoryReset(int subId)81     protected void factoryReset(int subId) {
82         sSubIdsWithResetCalledCount.put(subId,
83                 sSubIdsWithResetCalledCount.getOrDefault(subId, 0) + 1);
84     }
85 
86     @Implementation
getSubscriberId(int subId)87     protected String getSubscriberId(int subId) {
88         return subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID ? null : SUBSCRIBER_ID;
89     }
90 
91     @Implementation
getSimCount()92     protected int getSimCount() {
93         return sSimCount;
94     }
95 
setSimCount(int simCount)96     public static void setSimCount(int simCount) {
97         sSimCount = simCount;
98     }
99 
100     @Implementation
setDataRoamingEnabled(boolean isEnabled)101     protected void setDataRoamingEnabled(boolean isEnabled) {
102         mIsRoamingEnabled = isEnabled;
103     }
104 
105     @Implementation
isDataRoamingEnabled()106     protected boolean isDataRoamingEnabled() {
107         return mIsRoamingEnabled;
108     }
109 
110     @Resetter
reset()111     public static void reset() {
112         sSubIdsWithResetCalledCount.clear();
113         sSimCount = 1;
114     }
115 }
116