| /* | |
| * Copyright (C) 2020 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| package com.android.phone; | |
| import android.app.ActionBar; | |
| import android.content.ContentProvider; | |
| import android.content.Intent; | |
| import android.database.Cursor; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.os.PersistableBundle; | |
| import android.os.Process; | |
| import android.os.UserHandle; | |
| import android.preference.Preference; | |
| import android.preference.PreferenceScreen; | |
| import android.telephony.CarrierConfigManager; | |
| import android.util.Log; | |
| import android.view.MenuItem; | |
| import com.android.internal.telephony.CallForwardInfo; | |
| import com.android.internal.telephony.CommandsInterface; | |
| import com.android.internal.telephony.Phone; | |
| import java.util.ArrayList; | |
| public class CdmaCallForwardOptions extends TimeConsumingPreferenceActivity { | |
| private static final String LOG_TAG = "CdmaCallForwardOptions"; | |
| private static final String NUM_PROJECTION[] = { | |
| android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER | |
| }; | |
| private static final String BUTTON_CFU_KEY = "button_cfu_key"; | |
| private static final String BUTTON_CFB_KEY = "button_cfb_key"; | |
| private static final String BUTTON_CFNRY_KEY = "button_cfnry_key"; | |
| private static final String BUTTON_CFNRC_KEY = "button_cfnrc_key"; | |
| private static final String KEY_TOGGLE = "toggle"; | |
| private static final String KEY_STATUS = "status"; | |
| private static final String KEY_NUMBER = "number"; | |
| private static final String KEY_ENABLE = "enable"; | |
| private CallForwardEditPreference mButtonCFU; | |
| private CallForwardEditPreference mButtonCFB; | |
| private CallForwardEditPreference mButtonCFNRy; | |
| private CallForwardEditPreference mButtonCFNRc; | |
| private final ArrayList<CallForwardEditPreference> mPreferences = | |
| new ArrayList<CallForwardEditPreference> (); | |
| private int mInitIndex= 0; | |
| private boolean mFirstResume; | |
| private Bundle mIcicle; | |
| private Phone mPhone; | |
| private SubscriptionInfoHelper mSubscriptionInfoHelper; | |
| private boolean mReplaceInvalidCFNumbers; | |
| private boolean mCallForwardByUssd; | |
| @Override | |
| protected void onCreate(Bundle icicle) { | |
| super.onCreate(icicle); | |
| getWindow().addSystemFlags( | |
| android.view.WindowManager.LayoutParams | |
| .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); | |
| addPreferencesFromResource(R.xml.callforward_options); | |
| mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent()); | |
| mSubscriptionInfoHelper.setActionBarTitle( | |
| getActionBar(), getResources(), R.string.call_forwarding_settings_with_label); | |
| mPhone = mSubscriptionInfoHelper.getPhone(); | |
| PersistableBundle b = null; | |
| boolean supportCFNRc = true; | |
| if (mSubscriptionInfoHelper.hasSubId()) { | |
| b = PhoneGlobals.getInstance().getCarrierConfigForSubId( | |
| mSubscriptionInfoHelper.getSubId()); | |
| } else { | |
| b = PhoneGlobals.getInstance().getCarrierConfig(); | |
| } | |
| if (b != null) { | |
| mReplaceInvalidCFNumbers = b.getBoolean( | |
| CarrierConfigManager.KEY_CALL_FORWARDING_MAP_NON_NUMBER_TO_VOICEMAIL_BOOL); | |
| mCallForwardByUssd = b.getBoolean( | |
| CarrierConfigManager.KEY_USE_CALL_FORWARDING_USSD_BOOL); | |
| supportCFNRc = b.getBoolean( | |
| CarrierConfigManager.KEY_CALL_FORWARDING_WHEN_UNREACHABLE_SUPPORTED_BOOL); | |
| } | |
| PreferenceScreen prefSet = getPreferenceScreen(); | |
| mButtonCFU = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFU_KEY); | |
| mButtonCFB = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFB_KEY); | |
| mButtonCFNRy = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRY_KEY); | |
| mButtonCFNRc = (CallForwardEditPreference) prefSet.findPreference(BUTTON_CFNRC_KEY); | |
| mButtonCFU.setParentActivity(this, mButtonCFU.reason); | |
| mButtonCFB.setParentActivity(this, mButtonCFB.reason); | |
| mButtonCFNRy.setParentActivity(this, mButtonCFNRy.reason); | |
| mButtonCFNRc.setParentActivity(this, mButtonCFNRc.reason); | |
| mPreferences.add(mButtonCFU); | |
| mPreferences.add(mButtonCFB); | |
| mPreferences.add(mButtonCFNRy); | |
| if (supportCFNRc) { | |
| mPreferences.add(mButtonCFNRc); | |
| } else { | |
| // When CFNRc is not supported, mButtonCFNRc is grayed out from the menu. | |
| // Default state for the preferences in this PreferenceScreen is disabled. | |
| // Only preferences listed in the ArrayList mPreferences will be enabled. | |
| // By not adding mButtonCFNRc to mPreferences it will be kept disabled. | |
| Log.d(LOG_TAG, "onCreate: CFNRc is not supported, grey out the item."); | |
| } | |
| if (mCallForwardByUssd) { | |
| //the call forwarding ussd command's behavior is similar to the call forwarding when | |
| //unanswered,so only display the call forwarding when unanswered item. | |
| prefSet.removePreference(mButtonCFU); | |
| prefSet.removePreference(mButtonCFB); | |
| prefSet.removePreference(mButtonCFNRc); | |
| mPreferences.remove(mButtonCFU); | |
| mPreferences.remove(mButtonCFB); | |
| mPreferences.remove(mButtonCFNRc); | |
| mButtonCFNRy.setDependency(null); | |
| } | |
| // we wait to do the initialization until onResume so that the | |
| // TimeConsumingPreferenceActivity dialog can display as it | |
| // relies on onResume / onPause to maintain its foreground state. | |
| mFirstResume = true; | |
| mIcicle = icicle; | |
| ActionBar actionBar = getActionBar(); | |
| if (actionBar != null) { | |
| // android.R.id.home will be triggered in onOptionsItemSelected() | |
| actionBar.setDisplayHomeAsUpEnabled(true); | |
| } | |
| } | |
| @Override | |
| public void onResume() { | |
| super.onResume(); | |
| if (mFirstResume) { | |
| if (mIcicle == null) { | |
| Log.d(LOG_TAG, "start to init "); | |
| CallForwardEditPreference pref = mPreferences.get(mInitIndex); | |
| pref.init(this, mPhone, mReplaceInvalidCFNumbers, mCallForwardByUssd); | |
| pref.startCallForwardOptionsQuery(); | |
| } else { | |
| mInitIndex = mPreferences.size(); | |
| for (CallForwardEditPreference pref : mPreferences) { | |
| Bundle bundle = mIcicle.getParcelable(pref.getKey()); | |
| pref.setToggled(bundle.getBoolean(KEY_TOGGLE)); | |
| pref.setEnabled(bundle.getBoolean(KEY_ENABLE)); | |
| CallForwardInfo cf = new CallForwardInfo(); | |
| cf.number = bundle.getString(KEY_NUMBER); | |
| cf.status = bundle.getInt(KEY_STATUS); | |
| pref.init(this, mPhone, mReplaceInvalidCFNumbers, mCallForwardByUssd); | |
| pref.restoreCallForwardInfo(cf); | |
| } | |
| } | |
| mFirstResume = false; | |
| mIcicle = null; | |
| } | |
| } | |
| @Override | |
| protected void onSaveInstanceState(Bundle outState) { | |
| super.onSaveInstanceState(outState); | |
| for (CallForwardEditPreference pref : mPreferences) { | |
| Bundle bundle = new Bundle(); | |
| bundle.putBoolean(KEY_TOGGLE, pref.isToggled()); | |
| bundle.putBoolean(KEY_ENABLE, pref.isEnabled()); | |
| if (pref.callForwardInfo != null) { | |
| bundle.putString(KEY_NUMBER, pref.callForwardInfo.number); | |
| bundle.putInt(KEY_STATUS, pref.callForwardInfo.status); | |
| } | |
| outState.putParcelable(pref.getKey(), bundle); | |
| } | |
| } | |
| @Override | |
| public void onFinished(Preference preference, boolean reading) { | |
| if (mInitIndex < mPreferences.size()-1 && !isFinishing()) { | |
| mInitIndex++; | |
| CallForwardEditPreference pref = mPreferences.get(mInitIndex); | |
| pref.init(this, mPhone, mReplaceInvalidCFNumbers, mCallForwardByUssd); | |
| pref.startCallForwardOptionsQuery(); | |
| } | |
| super.onFinished(preference, reading); | |
| } | |
| @Override | |
| protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| Log.d(LOG_TAG, "onActivityResult: done"); | |
| if (resultCode != RESULT_OK) { | |
| Log.d(LOG_TAG, "onActivityResult: contact picker result not OK."); | |
| return; | |
| } | |
| if (data == null) { | |
| Log.w(LOG_TAG, "onActivityResult: data is null"); | |
| return; | |
| } | |
| Uri contactUri = data.getData(); | |
| if (contactUri == null) { | |
| Log.w(LOG_TAG, "onActivityResult: contactUri is null"); | |
| return; | |
| } | |
| ContactPermissionUtils.checkContactUriPermission(getCurrentCaller(), contactUri); | |
| Cursor cursor = null; | |
| try { | |
| // check if the URI returned by the user belongs to the user | |
| final int currentUser = UserHandle.getUserId(Process.myUid()); | |
| if (currentUser | |
| != ContentProvider.getUserIdFromUri(data.getData(), currentUser)) { | |
| Log.w(LOG_TAG, "onActivityResult: Contact data of different user, " | |
| + "cannot access"); | |
| return; | |
| } | |
| cursor = getContentResolver().query(data.getData(), | |
| NUM_PROJECTION, null, null, null); | |
| if ((cursor == null) || (!cursor.moveToFirst())) { | |
| Log.d(LOG_TAG, "onActivityResult: bad contact data, no results found."); | |
| return; | |
| } | |
| switch (requestCode) { | |
| case CommandsInterface.CF_REASON_UNCONDITIONAL: | |
| mButtonCFU.onPickActivityResult(cursor.getString(0)); | |
| break; | |
| case CommandsInterface.CF_REASON_BUSY: | |
| mButtonCFB.onPickActivityResult(cursor.getString(0)); | |
| break; | |
| case CommandsInterface.CF_REASON_NO_REPLY: | |
| mButtonCFNRy.onPickActivityResult(cursor.getString(0)); | |
| break; | |
| case CommandsInterface.CF_REASON_NOT_REACHABLE: | |
| mButtonCFNRc.onPickActivityResult(cursor.getString(0)); | |
| break; | |
| default: | |
| // TODO: may need exception here. | |
| } | |
| } finally { | |
| if (cursor != null) { | |
| cursor.close(); | |
| } | |
| } | |
| } | |
| @Override | |
| public boolean onOptionsItemSelected(MenuItem item) { | |
| final int itemId = item.getItemId(); | |
| if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled() | |
| CallFeaturesSetting.goUpToTopLevelSetting(this, mSubscriptionInfoHelper); | |
| return true; | |
| } | |
| return super.onOptionsItemSelected(item); | |
| } | |
| } |