1#!/usr/bin/env python3.4
2#
3#   Copyright 2016 - Google
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17# This is test util for subscription setup.
18# It will be deleted once we have better solution for subscription ids.
19from future import standard_library
20standard_library.install_aliases()
21from acts.test_utils.tel.tel_defines import INVALID_SUB_ID
22from acts.test_utils.tel.tel_defines import WAIT_TIME_CHANGE_DATA_SUB_ID
23import time
24
25
26def initial_set_up_for_subid_infomation(log, ad):
27    """Initial subid setup for voice, message and data according to ad's
28    attribute.
29
30    Setup sub id properties for android device. Including the followings:
31        incoming_voice_sub_id
32        incoming_message_sub_id
33        outgoing_voice_sub_id
34        outgoing_message_sub_id
35        default_data_sub_id
36
37    Args:
38        log: log object
39        ad: android device object
40
41    Returns:
42        None
43    """
44    # outgoing_voice_sub_id
45    # If default_voice_sim_slot_index is set in config file, then use sub_id
46    # of this SIM as default_outgoing_sub_id. If default_voice_sim_slot_index
47    # is not set, then use default voice sub_id as default_outgoing_sub_id.
48    # Outgoing voice call will be made on default_outgoing_sub_id by default.
49    if hasattr(ad, "default_voice_sim_slot_index"):
50        outgoing_voice_sub_id = get_subid_from_slot_index(
51            log, ad, ad.default_voice_sim_slot_index)
52        set_subid_for_outgoing_call(ad, outgoing_voice_sub_id)
53    else:
54        outgoing_voice_sub_id = ad.droid.subscriptionGetDefaultVoiceSubId()
55    setattr(ad, "outgoing_voice_sub_id", outgoing_voice_sub_id)
56
57    # outgoing_message_sub_id
58    # If default_message_sim_slot_index is set in config file, then use sub_id
59    # of this SIM as outgoing_message_sub_id. If default_message_sim_slot_index
60    # is not set, then use default Sms sub_id as outgoing_message_sub_id.
61    # Outgoing SMS will be sent on outgoing_message_sub_id by default.
62    if hasattr(ad, "default_message_sim_slot_index"):
63        outgoing_message_sub_id = get_subid_from_slot_index(
64            log, ad, ad.default_message_sim_slot_index)
65        set_subid_for_message(ad, outgoing_message_sub_id)
66    else:
67        outgoing_message_sub_id = ad.droid.subscriptionGetDefaultSmsSubId()
68    setattr(ad, "outgoing_message_sub_id", outgoing_message_sub_id)
69
70    # default_data_sub_id
71    # If default_data_sim_slot_index is set in config file, then use sub_id
72    # of this SIM as default_data_sub_id. If default_data_sim_slot_index
73    # is not set, then use default Data sub_id as default_data_sub_id.
74    # Data connection will be established on default_data_sub_id by default.
75    if hasattr(ad, "default_data_sim_slot_index"):
76        default_data_sub_id = get_subid_from_slot_index(
77            log, ad, ad.default_data_sim_slot_index)
78        set_subid_for_data(ad, default_data_sub_id, 0)
79    else:
80        default_data_sub_id = ad.droid.subscriptionGetDefaultDataSubId()
81    setattr(ad, "default_data_sub_id", default_data_sub_id)
82
83    # This is for Incoming Voice Sub ID
84    # If "incoming_voice_sim_slot_index" is set in config file, then
85    # incoming voice call will call to the phone number of the SIM in
86    # "incoming_voice_sim_slot_index".
87    # If "incoming_voice_sim_slot_index" is NOT set in config file,
88    # then incoming voice call will call to the phone number of default
89    # subId.
90    if hasattr(ad, "incoming_voice_sim_slot_index"):
91        incoming_voice_sub_id = get_subid_from_slot_index(
92            log, ad, ad.incoming_voice_sim_slot_index)
93    else:
94        incoming_voice_sub_id = ad.droid.subscriptionGetDefaultVoiceSubId()
95    setattr(ad, "incoming_voice_sub_id", incoming_voice_sub_id)
96
97    # This is for Incoming SMS Sub ID
98    # If "incoming_message_sim_slot_index" is set in config file, then
99    # incoming SMS be sent to the phone number of the SIM in
100    # "incoming_message_sim_slot_index".
101    # If "incoming_message_sim_slot_index" is NOT set in config file,
102    # then incoming SMS be sent to the phone number of default
103    # subId.
104    if hasattr(ad, "incoming_message_sim_slot_index"):
105        incoming_message_sub_id = get_subid_from_slot_index(
106            log, ad, ad.incoming_message_sim_slot_index)
107    else:
108        incoming_message_sub_id = ad.droid.subscriptionGetDefaultSmsSubId()
109    setattr(ad, "incoming_message_sub_id", incoming_message_sub_id)
110
111
112def get_default_data_sub_id(ad):
113    """ Get default data subscription id
114    """
115    if hasattr(ad, "default_data_sub_id"):
116        return ad.default_data_sub_id
117    else:
118        return ad.droid.subscriptionGetDefaultDataSubId()
119
120
121def get_outgoing_message_sub_id(ad):
122    """ Get outgoing message subscription id
123    """
124    if hasattr(ad, "outgoing_message_sub_id"):
125        return ad.outgoing_message_sub_id
126    else:
127        return ad.droid.subscriptionGetDefaultSmsSubId()
128
129
130def get_outgoing_voice_sub_id(ad):
131    """ Get outgoing voice subscription id
132    """
133    if hasattr(ad, "outgoing_voice_sub_id"):
134        return ad.outgoing_voice_sub_id
135    else:
136        return ad.droid.subscriptionGetDefaultVoiceSubId()
137
138
139def get_incoming_voice_sub_id(ad):
140    """ Get incoming voice subscription id
141    """
142    if hasattr(ad, "incoming_voice_sub_id"):
143        return ad.incoming_voice_sub_id
144    else:
145        return ad.droid.subscriptionGetDefaultVoiceSubId()
146
147
148def get_incoming_message_sub_id(ad):
149    """ Get incoming message subscription id
150    """
151    if hasattr(ad, "incoming_message_sub_id"):
152        return ad.incoming_message_sub_id
153    else:
154        return ad.droid.subscriptionGetDefaultSmsSubId()
155
156
157def get_subid_from_slot_index(log, ad, sim_slot_index):
158    """ Get the subscription ID for a SIM at a particular slot
159
160    Args:
161        ad: android_device object.
162
163    Returns:
164        result: Subscription ID
165    """
166    subInfo = ad.droid.subscriptionGetAllSubInfoList()
167    for info in subInfo:
168        if info['simSlotIndex'] == sim_slot_index:
169            return info['subscriptionId']
170    return INVALID_SUB_ID
171
172
173def set_subid_for_data(ad, sub_id, time_to_sleep=WAIT_TIME_CHANGE_DATA_SUB_ID):
174    """Set subId for data
175
176    Args:
177        ad: android device object.
178        sub_id: subscription id (integer)
179
180    Returns:
181        None
182    """
183    # TODO: Need to check onSubscriptionChanged event. b/27843365
184    if ad.droid.subscriptionGetDefaultDataSubId() != sub_id:
185        ad.droid.subscriptionSetDefaultDataSubId(sub_id)
186        time.sleep(time_to_sleep)
187
188
189def set_subid_for_message(ad, sub_id):
190    """Set subId for outgoing message
191
192    Args:
193        ad: android device object.
194        sub_id: subscription id (integer)
195
196    Returns:
197        None
198    """
199    ad.droid.subscriptionSetDefaultSmsSubId(sub_id)
200
201
202def set_subid_for_outgoing_call(ad, sub_id):
203    """Set subId for outgoing voice call
204
205    Args:
206        ad: android device object.
207        sub_id: subscription id (integer)
208
209    Returns:
210        None
211    """
212    ad.droid.telecomSetUserSelectedOutgoingPhoneAccountBySubId(sub_id)
213