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