Merge "Prevent Erase SIM during call" into main

This commit is contained in:
Chaohui Wang
2023-11-08 06:11:30 +00:00
committed by Android (Google) Code Review
7 changed files with 331 additions and 238 deletions

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 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.settings.network.telephony
import android.content.Context
import android.telephony.TelephonyCallback
import android.telephony.TelephonyManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn
/**
* Flow for call state.
*/
fun Context.callStateFlow(subId: Int): Flow<Int> = callbackFlow {
val telephonyManager = getSystemService(TelephonyManager::class.java)!!
.createForSubscriptionId(subId)
val callback = object : TelephonyCallback(), TelephonyCallback.CallStateListener {
override fun onCallStateChanged(state: Int) {
trySend(state)
}
}
telephonyManager.registerTelephonyCallback(Dispatchers.Default.asExecutor(), callback)
awaitClose { telephonyManager.unregisterTelephonyCallback(callback) }
}.conflate().flowOn(Dispatchers.Default)

View File

@@ -1,97 +0,0 @@
/*
* Copyright (C) 2019 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.settings.network.telephony;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.euicc.EuiccManager;
import android.text.TextUtils;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.network.SubscriptionUtil;
import com.android.settings.security.ConfirmSimDeletionPreferenceController;
import com.android.settings.wifi.dpp.WifiDppUtils;
/** This controls a preference allowing the user to delete the profile for an eSIM. */
public class DeleteSimProfilePreferenceController extends BasePreferenceController {
private SubscriptionInfo mSubscriptionInfo;
private Fragment mParentFragment;
private int mRequestCode;
private boolean mConfirmationDefaultOn;
public DeleteSimProfilePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mConfirmationDefaultOn =
context.getResources()
.getBoolean(R.bool.config_sim_deletion_confirmation_default_on);
}
public void init(int subscriptionId, Fragment parentFragment, int requestCode) {
mParentFragment = parentFragment;
for (SubscriptionInfo info : SubscriptionUtil.getAvailableSubscriptions(mContext)) {
if (info.getSubscriptionId() == subscriptionId && info.isEmbedded()) {
mSubscriptionInfo = info;
break;
}
}
mRequestCode = requestCode;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
boolean confirmDeletion =
Settings.Global.getInt(
mContext.getContentResolver(),
ConfirmSimDeletionPreferenceController.KEY_CONFIRM_SIM_DELETION,
mConfirmationDefaultOn ? 1 : 0)
== 1;
if (confirmDeletion) {
WifiDppUtils.showLockScreen(mContext, () -> deleteSim());
} else {
deleteSim();
}
return true;
}
return false;
}
private void deleteSim() {
SubscriptionUtil.startDeleteEuiccSubscriptionDialogActivity(
mContext, mSubscriptionInfo.getSubscriptionId());
// result handled in MobileNetworkSettings
}
@Override
public int getAvailabilityStatus() {
if (mSubscriptionInfo != null) {
return AVAILABLE;
} else {
return CONDITIONALLY_UNAVAILABLE;
}
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright (C) 2023 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.settings.network.telephony
import android.content.Context
import android.telephony.SubscriptionInfo
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import androidx.lifecycle.LifecycleOwner
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import com.android.settings.R
import com.android.settings.core.BasePreferenceController
import com.android.settings.network.SubscriptionUtil
import com.android.settings.security.ConfirmSimDeletionPreferenceController.KEY_CONFIRM_SIM_DELETION
import com.android.settings.wifi.dpp.WifiDppUtils
import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle
import com.android.settingslib.spaprivileged.settingsprovider.settingsGlobalBoolean
/** This controls a preference allowing the user to delete the profile for an eSIM. */
class DeleteSimProfilePreferenceController(context: Context, preferenceKey: String) :
BasePreferenceController(context, preferenceKey) {
private var subscriptionId: Int = SubscriptionManager.INVALID_SUBSCRIPTION_ID
private var subscriptionInfo: SubscriptionInfo? = null
private lateinit var preference: Preference
fun init(subscriptionId: Int) {
this.subscriptionId = subscriptionId
subscriptionInfo = SubscriptionUtil.getAvailableSubscriptions(mContext)
.find { it.subscriptionId == subscriptionId && it.isEmbedded }
}
override fun getAvailabilityStatus() = when (subscriptionInfo) {
null -> CONDITIONALLY_UNAVAILABLE
else -> AVAILABLE
}
override fun displayPreference(screen: PreferenceScreen) {
super.displayPreference(screen)
preference = screen.findPreference(preferenceKey)!!
}
override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) {
mContext.callStateFlow(subscriptionId).collectLatestWithLifecycle(viewLifecycleOwner) {
preference.isEnabled = (it == TelephonyManager.CALL_STATE_IDLE)
}
}
override fun handlePreferenceTreeClick(preference: Preference): Boolean {
if (preference.key != preferenceKey) return false
val confirmDeletion by mContext.settingsGlobalBoolean(
name = KEY_CONFIRM_SIM_DELETION,
defaultValue = mContext.resources
.getBoolean(R.bool.config_sim_deletion_confirmation_default_on),
)
if (confirmDeletion) {
WifiDppUtils.showLockScreen(mContext) { deleteSim() }
} else {
deleteSim()
}
return true
}
private fun deleteSim() {
SubscriptionUtil.startDeleteEuiccSubscriptionDialogActivity(mContext, subscriptionId)
// result handled in MobileNetworkSettings
}
}

View File

@@ -240,8 +240,7 @@ public class MobileNetworkSettings extends AbstractMobileNetworkSettings impleme
use(MmsMessagePreferenceController.class).init(mSubId);
use(AutoDataSwitchPreferenceController.class).init(mSubId);
use(DisabledSubscriptionController.class).init(mSubId);
use(DeleteSimProfilePreferenceController.class).init(mSubId, this,
REQUEST_CODE_DELETE_SUBSCRIPTION);
use(DeleteSimProfilePreferenceController.class).init(mSubId);
use(DisableSimFooterPreferenceController.class).init(mSubId);
use(NrDisabledInDsdsFooterPreferenceController.class).init(mSubId);