• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.sim;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.telecom.PhoneAccountHandle;
23 import android.telecom.TelecomManager;
24 import android.telephony.SubscriptionManager;
25 import android.telephony.TelephonyManager;
26 import android.util.Log;
27 import android.widget.Toast;
28 
29 import androidx.fragment.app.Fragment;
30 import androidx.fragment.app.FragmentActivity;
31 import androidx.fragment.app.FragmentManager;
32 
33 import com.android.settings.R;
34 
35 import java.util.List;
36 
37 /**
38  * This activity provides singleton semantics per dialog type for showing various kinds of
39  * dialogs asking the user to make choices about which SIM to use for various services
40  * (calls, SMS, and data).
41  */
42 public class SimDialogActivity extends FragmentActivity {
43     private static String TAG = "SimDialogActivity";
44 
45     public static String PREFERRED_SIM = "preferred_sim";
46     public static String DIALOG_TYPE_KEY = "dialog_type";
47     // sub ID returned from startActivityForResult
48     public static String RESULT_SUB_ID = "result_sub_id";
49     public static final int INVALID_PICK = -1;
50     public static final int DATA_PICK = 0;
51     public static final int CALLS_PICK = 1;
52     public static final int SMS_PICK = 2;
53     public static final int PREFERRED_PICK = 3;
54     // Show the "select SMS subscription" dialog, but don't save as default, just return a result
55     public static final int SMS_PICK_FOR_MESSAGE = 4;
56     // Dismiss the current dialog and finish the activity.
57     public static final int PICK_DISMISS = 5;
58 
59     @Override
onCreate(Bundle savedInstanceState)60     protected void onCreate(Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62         showOrUpdateDialog();
63     }
64 
65     @Override
onNewIntent(Intent intent)66     protected void onNewIntent(Intent intent) {
67         super.onNewIntent(intent);
68         setIntent(intent);
69         showOrUpdateDialog();
70     }
71 
showOrUpdateDialog()72     private void showOrUpdateDialog() {
73         final int dialogType = getIntent().getIntExtra(DIALOG_TYPE_KEY, INVALID_PICK);
74 
75         if (dialogType == PICK_DISMISS) {
76             finishAndRemoveTask();
77             return;
78         }
79 
80         final String tag = Integer.toString(dialogType);
81         final FragmentManager fragmentManager = getSupportFragmentManager();
82         SimDialogFragment fragment = (SimDialogFragment) fragmentManager.findFragmentByTag(tag);
83 
84         if (fragment == null) {
85             fragment = createFragment(dialogType);
86             fragment.show(fragmentManager, tag);
87         } else {
88             fragment.updateDialog();
89         }
90     }
91 
createFragment(int dialogType)92     private SimDialogFragment createFragment(int dialogType) {
93         switch (dialogType) {
94             case DATA_PICK:
95                 return SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_data,
96                         false /* includeAskEveryTime */);
97             case CALLS_PICK:
98                 return CallsSimListDialogFragment.newInstance(dialogType,
99                         R.string.select_sim_for_calls,
100                         true /* includeAskEveryTime */);
101             case SMS_PICK:
102                 return SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_sms,
103                         true /* includeAskEveryTime */);
104             case PREFERRED_PICK:
105                 if (!getIntent().hasExtra(PREFERRED_SIM)) {
106                     throw new IllegalArgumentException("Missing required extra " + PREFERRED_SIM);
107                 }
108                 return PreferredSimDialogFragment.newInstance();
109             case SMS_PICK_FOR_MESSAGE:
110                 return SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_sms,
111                         false /* includeAskEveryTime */);
112             default:
113                 throw new IllegalArgumentException("Invalid dialog type " + dialogType + " sent.");
114         }
115     }
116 
onSubscriptionSelected(int dialogType, int subId)117     public void onSubscriptionSelected(int dialogType, int subId) {
118         if (getSupportFragmentManager().findFragmentByTag(Integer.toString(dialogType)) == null) {
119             Log.w(TAG, "onSubscriptionSelected ignored because stored fragment was null");
120             return;
121         }
122         switch (dialogType) {
123             case DATA_PICK:
124                 setDefaultDataSubId(subId);
125                 break;
126             case CALLS_PICK:
127                 setDefaultCallsSubId(subId);
128                 break;
129             case SMS_PICK:
130                 setDefaultSmsSubId(subId);
131                 break;
132             case PREFERRED_PICK:
133                 setPreferredSim(subId);
134                 break;
135             case SMS_PICK_FOR_MESSAGE:
136                 // Don't set a default here.
137                 // The caller has created this dialog waiting for a result.
138                 Intent intent = new Intent();
139                 intent.putExtra(RESULT_SUB_ID, subId);
140                 setResult(Activity.RESULT_OK, intent);
141                 break;
142             default:
143                 throw new IllegalArgumentException(
144                         "Invalid dialog type " + dialogType + " sent.");
145         }
146     }
147 
onFragmentDismissed(SimDialogFragment simDialogFragment)148     public void onFragmentDismissed(SimDialogFragment simDialogFragment) {
149         final List<Fragment> fragments = getSupportFragmentManager().getFragments();
150         if (fragments.size() == 1 && fragments.get(0) == simDialogFragment) {
151             finishAndRemoveTask();
152         }
153     }
154 
setDefaultDataSubId(final int subId)155     private void setDefaultDataSubId(final int subId) {
156         final SubscriptionManager subscriptionManager = getSystemService(SubscriptionManager.class);
157         final TelephonyManager telephonyManager = getSystemService(
158                 TelephonyManager.class).createForSubscriptionId(subId);
159         subscriptionManager.setDefaultDataSubId(subId);
160         telephonyManager.setDataEnabled(true);
161         Toast.makeText(this, R.string.data_switch_started, Toast.LENGTH_LONG).show();
162     }
163 
setDefaultCallsSubId(final int subId)164     private void setDefaultCallsSubId(final int subId) {
165         final PhoneAccountHandle phoneAccount = subscriptionIdToPhoneAccountHandle(subId);
166         final TelecomManager telecomManager = getSystemService(TelecomManager.class);
167         telecomManager.setUserSelectedOutgoingPhoneAccount(phoneAccount);
168     }
169 
setDefaultSmsSubId(final int subId)170     private void setDefaultSmsSubId(final int subId) {
171         final SubscriptionManager subscriptionManager = getSystemService(SubscriptionManager.class);
172         subscriptionManager.setDefaultSmsSubId(subId);
173     }
174 
setPreferredSim(final int subId)175     private void setPreferredSim(final int subId) {
176         setDefaultDataSubId(subId);
177         setDefaultSmsSubId(subId);
178         setDefaultCallsSubId(subId);
179     }
180 
subscriptionIdToPhoneAccountHandle(final int subId)181     private PhoneAccountHandle subscriptionIdToPhoneAccountHandle(final int subId) {
182         final TelecomManager telecomManager = getSystemService(TelecomManager.class);
183         final TelephonyManager telephonyManager = getSystemService(TelephonyManager.class);
184 
185         for (PhoneAccountHandle handle : telecomManager.getCallCapablePhoneAccounts()) {
186             if (subId == telephonyManager.getSubscriptionId(handle)) {
187                 return handle;
188             }
189         }
190         return null;
191     }
192 }
193