1 /* 2 * Copyright (C) 2020 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.phone; 18 19 import android.app.ActionBar; 20 import android.content.Intent; 21 import android.database.Cursor; 22 import android.os.Bundle; 23 import android.os.PersistableBundle; 24 import android.preference.Preference; 25 import android.preference.PreferenceScreen; 26 import android.telephony.CarrierConfigManager; 27 import android.util.Log; 28 import android.view.MenuItem; 29 30 import com.android.internal.telephony.CallForwardInfo; 31 import com.android.internal.telephony.CommandsInterface; 32 import com.android.internal.telephony.Phone; 33 34 import java.util.ArrayList; 35 36 public class CdmaCallForwardOptions extends TimeConsumingPreferenceActivity { 37 private static final String LOG_TAG = "CdmaCallForwardOptions"; 38 39 private static final String NUM_PROJECTION[] = { 40 android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER 41 }; 42 43 private static final String BUTTON_CFU_KEY = "button_cfu_key"; 44 private static final String BUTTON_CFB_KEY = "button_cfb_key"; 45 private static final String BUTTON_CFNRY_KEY = "button_cfnry_key"; 46 private static final String BUTTON_CFNRC_KEY = "button_cfnrc_key"; 47 48 private static final String KEY_TOGGLE = "toggle"; 49 private static final String KEY_STATUS = "status"; 50 private static final String KEY_NUMBER = "number"; 51 private static final String KEY_ENABLE = "enable"; 52 53 private CallForwardEditPreference mButtonCFU; 54 private CallForwardEditPreference mButtonCFB; 55 private CallForwardEditPreference mButtonCFNRy; 56 private CallForwardEditPreference mButtonCFNRc; 57 58 private final ArrayList<CallForwardEditPreference> mPreferences = 59 new ArrayList<CallForwardEditPreference> (); 60 private int mInitIndex= 0; 61 62 private boolean mFirstResume; 63 private Bundle mIcicle; 64 private Phone mPhone; 65 private SubscriptionInfoHelper mSubscriptionInfoHelper; 66 private boolean mReplaceInvalidCFNumbers; 67 private boolean mCallForwardByUssd; 68 69 @Override onCreate(Bundle icicle)70 protected void onCreate(Bundle icicle) { 71 super.onCreate(icicle); 72 73 addPreferencesFromResource(R.xml.callforward_options); 74 75 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent()); 76 mSubscriptionInfoHelper.setActionBarTitle( 77 getActionBar(), getResources(), R.string.call_forwarding_settings_with_label); 78 mPhone = mSubscriptionInfoHelper.getPhone(); 79 80 PersistableBundle b = null; 81 boolean supportCFNRc = true; 82 if (mSubscriptionInfoHelper.hasSubId()) { 83 b = PhoneGlobals.getInstance().getCarrierConfigForSubId( 84 mSubscriptionInfoHelper.getSubId()); 85 } else { 86 b = PhoneGlobals.getInstance().getCarrierConfig(); 87 } 88 if (b != null) { 89 mReplaceInvalidCFNumbers = b.getBoolean( 90 CarrierConfigManager.KEY_CALL_FORWARDING_MAP_NON_NUMBER_TO_VOICEMAIL_BOOL); 91 mCallForwardByUssd = b.getBoolean( 92 CarrierConfigManager.KEY_USE_CALL_FORWARDING_USSD_BOOL); 93 supportCFNRc = b.getBoolean( 94 CarrierConfigManager.KEY_CALL_FORWARDING_WHEN_UNREACHABLE_SUPPORTED_BOOL); 95 } 96 97 PreferenceScreen prefSet = getPreferenceScreen(); 98 mButtonCFU = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFU_KEY); 99 mButtonCFB = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFB_KEY); 100 mButtonCFNRy = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRY_KEY); 101 mButtonCFNRc = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRC_KEY); 102 103 mButtonCFU.setParentActivity(this, mButtonCFU.reason); 104 mButtonCFB.setParentActivity(this, mButtonCFB.reason); 105 mButtonCFNRy.setParentActivity(this, mButtonCFNRy.reason); 106 mButtonCFNRc.setParentActivity(this, mButtonCFNRc.reason); 107 108 mPreferences.add(mButtonCFU); 109 mPreferences.add(mButtonCFB); 110 mPreferences.add(mButtonCFNRy); 111 112 if (supportCFNRc) { 113 mPreferences.add(mButtonCFNRc); 114 } else { 115 // When CFNRc is not supported, mButtonCFNRc is grayed out from the menu. 116 // Default state for the preferences in this PreferenceScreen is disabled. 117 // Only preferences listed in the ArrayList mPreferences will be enabled. 118 // By not adding mButtonCFNRc to mPreferences it will be kept disabled. 119 Log.d(LOG_TAG, "onCreate: CFNRc is not supported, grey out the item."); 120 } 121 122 if (mCallForwardByUssd) { 123 //the call forwarding ussd command's behavior is similar to the call forwarding when 124 //unanswered,so only display the call forwarding when unanswered item. 125 prefSet.removePreference(mButtonCFU); 126 prefSet.removePreference(mButtonCFB); 127 prefSet.removePreference(mButtonCFNRc); 128 mPreferences.remove(mButtonCFU); 129 mPreferences.remove(mButtonCFB); 130 mPreferences.remove(mButtonCFNRc); 131 mButtonCFNRy.setDependency(null); 132 } 133 134 // we wait to do the initialization until onResume so that the 135 // TimeConsumingPreferenceActivity dialog can display as it 136 // relies on onResume / onPause to maintain its foreground state. 137 138 mFirstResume = true; 139 mIcicle = icicle; 140 141 ActionBar actionBar = getActionBar(); 142 if (actionBar != null) { 143 // android.R.id.home will be triggered in onOptionsItemSelected() 144 actionBar.setDisplayHomeAsUpEnabled(true); 145 } 146 } 147 148 @Override onResume()149 public void onResume() { 150 super.onResume(); 151 152 if (mFirstResume) { 153 if (mIcicle == null) { 154 Log.d(LOG_TAG, "start to init "); 155 CallForwardEditPreference pref = mPreferences.get(mInitIndex); 156 pref.init(this, mPhone, mReplaceInvalidCFNumbers, mCallForwardByUssd); 157 pref.startCallForwardOptionsQuery(); 158 159 } else { 160 mInitIndex = mPreferences.size(); 161 162 for (CallForwardEditPreference pref : mPreferences) { 163 Bundle bundle = mIcicle.getParcelable(pref.getKey()); 164 pref.setToggled(bundle.getBoolean(KEY_TOGGLE)); 165 pref.setEnabled(bundle.getBoolean(KEY_ENABLE)); 166 CallForwardInfo cf = new CallForwardInfo(); 167 cf.number = bundle.getString(KEY_NUMBER); 168 cf.status = bundle.getInt(KEY_STATUS); 169 pref.init(this, mPhone, mReplaceInvalidCFNumbers, mCallForwardByUssd); 170 pref.restoreCallForwardInfo(cf); 171 } 172 } 173 mFirstResume = false; 174 mIcicle = null; 175 } 176 } 177 178 @Override onSaveInstanceState(Bundle outState)179 protected void onSaveInstanceState(Bundle outState) { 180 super.onSaveInstanceState(outState); 181 182 for (CallForwardEditPreference pref : mPreferences) { 183 Bundle bundle = new Bundle(); 184 bundle.putBoolean(KEY_TOGGLE, pref.isToggled()); 185 bundle.putBoolean(KEY_ENABLE, pref.isEnabled()); 186 if (pref.callForwardInfo != null) { 187 bundle.putString(KEY_NUMBER, pref.callForwardInfo.number); 188 bundle.putInt(KEY_STATUS, pref.callForwardInfo.status); 189 } 190 outState.putParcelable(pref.getKey(), bundle); 191 } 192 } 193 194 @Override onFinished(Preference preference, boolean reading)195 public void onFinished(Preference preference, boolean reading) { 196 if (mInitIndex < mPreferences.size()-1 && !isFinishing()) { 197 mInitIndex++; 198 CallForwardEditPreference pref = mPreferences.get(mInitIndex); 199 pref.init(this, mPhone, mReplaceInvalidCFNumbers, mCallForwardByUssd); 200 pref.startCallForwardOptionsQuery(); 201 } 202 203 super.onFinished(preference, reading); 204 } 205 206 @Override onActivityResult(int requestCode, int resultCode, Intent data)207 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 208 Log.d(LOG_TAG, "onActivityResult: done"); 209 if (resultCode != RESULT_OK) { 210 Log.d(LOG_TAG, "onActivityResult: contact picker result not OK."); 211 return; 212 } 213 Cursor cursor = null; 214 try { 215 cursor = getContentResolver().query(data.getData(), 216 NUM_PROJECTION, null, null, null); 217 if ((cursor == null) || (!cursor.moveToFirst())) { 218 Log.d(LOG_TAG, "onActivityResult: bad contact data, no results found."); 219 return; 220 } 221 222 switch (requestCode) { 223 case CommandsInterface.CF_REASON_UNCONDITIONAL: 224 mButtonCFU.onPickActivityResult(cursor.getString(0)); 225 break; 226 case CommandsInterface.CF_REASON_BUSY: 227 mButtonCFB.onPickActivityResult(cursor.getString(0)); 228 break; 229 case CommandsInterface.CF_REASON_NO_REPLY: 230 mButtonCFNRy.onPickActivityResult(cursor.getString(0)); 231 break; 232 case CommandsInterface.CF_REASON_NOT_REACHABLE: 233 mButtonCFNRc.onPickActivityResult(cursor.getString(0)); 234 break; 235 default: 236 // TODO: may need exception here. 237 } 238 } finally { 239 if (cursor != null) { 240 cursor.close(); 241 } 242 } 243 } 244 245 @Override onOptionsItemSelected(MenuItem item)246 public boolean onOptionsItemSelected(MenuItem item) { 247 final int itemId = item.getItemId(); 248 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled() 249 CallFeaturesSetting.goUpToTopLevelSetting(this, mSubscriptionInfoHelper); 250 return true; 251 } 252 return super.onOptionsItemSelected(item); 253 } 254 } 255