Check MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED

To know whether MMS is always allowed, should check
isMobileDataPolicyEnabled(MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED)
instead of isDataEnabledForApn(ApnSetting.TYPE_MMS).

Fix: 303759893
Test: manual - toggle "MMS messages" switch
Test: unit test
Change-Id: Id0417e569ff4e3dbac1ecff9327b4a964125c122
This commit is contained in:
Chaohui Wang
2023-10-26 16:10:36 +08:00
parent 43306392ea
commit 8a5bba1f22
4 changed files with 229 additions and 255 deletions

View File

@@ -1,113 +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.os.Handler;
import android.os.Looper;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telephony.data.ApnSetting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.network.MobileDataContentObserver;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
/**
* Preference controller for "MMS messages"
*/
public class MmsMessagePreferenceController extends TelephonyTogglePreferenceController implements
LifecycleObserver, OnStart, OnStop {
private TelephonyManager mTelephonyManager;
private MobileDataContentObserver mMobileDataContentObserver;
private PreferenceScreen mScreen;
private Preference mPreference;
public MmsMessagePreferenceController(Context context, String key) {
super(context, key);
mMobileDataContentObserver = new MobileDataContentObserver(
new Handler(Looper.getMainLooper()));
mMobileDataContentObserver.setOnMobileDataChangedListener(() -> refreshPreference());
}
@Override
public int getAvailabilityStatus(int subId) {
final TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class)
.createForSubscriptionId(subId);
return (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
&& !telephonyManager.isDataEnabled()
&& telephonyManager.isApnMetered(ApnSetting.TYPE_MMS))
? AVAILABLE
: CONDITIONALLY_UNAVAILABLE;
}
@Override
public void onStart() {
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
mMobileDataContentObserver.register(mContext, mSubId);
updateState(mPreference);
}
}
@Override
public void onStop() {
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
mMobileDataContentObserver.unRegister(mContext);
}
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mScreen = screen;
mPreference = screen.findPreference(getPreferenceKey());
}
public void init(int subId) {
mSubId = subId;
mTelephonyManager = mContext.getSystemService(TelephonyManager.class)
.createForSubscriptionId(mSubId);
}
@Override
public boolean setChecked(boolean isChecked) {
if (mTelephonyManager == null) {
return false;
}
mTelephonyManager.setMobileDataPolicyEnabled(
TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED, isChecked);
return true;
}
@Override
public boolean isChecked() {
return mTelephonyManager != null && mTelephonyManager.isDataEnabledForApn(
ApnSetting.TYPE_MMS);
}
private void refreshPreference() {
if (mScreen != null) {
super.displayPreference(mScreen);
}
}
}

View File

@@ -0,0 +1,87 @@
/*
* 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.os.Handler
import android.os.Looper
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import android.telephony.data.ApnSetting
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.preference.PreferenceScreen
import com.android.settings.network.MobileDataContentObserver
/**
* Preference controller for "MMS messages"
*/
class MmsMessagePreferenceController(context: Context, key: String) :
TelephonyTogglePreferenceController(context, key), DefaultLifecycleObserver {
private lateinit var telephonyManager: TelephonyManager
private var preferenceScreen: PreferenceScreen? = null
private val mobileDataContentObserver =
MobileDataContentObserver(Handler(Looper.getMainLooper())).apply {
setOnMobileDataChangedListener {
preferenceScreen?.let { super.displayPreference(it) }
}
}
fun init(subId: Int) {
mSubId = subId
telephonyManager = mContext.getSystemService(TelephonyManager::class.java)!!
.createForSubscriptionId(subId)
}
override fun getAvailabilityStatus(subId: Int) =
if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID &&
!telephonyManager.isDataEnabled &&
telephonyManager.isApnMetered(ApnSetting.TYPE_MMS)
) AVAILABLE else CONDITIONALLY_UNAVAILABLE
override fun onStart(owner: LifecycleOwner) {
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
mobileDataContentObserver.register(mContext, mSubId)
}
}
override fun onStop(owner: LifecycleOwner) {
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
mobileDataContentObserver.unRegister(mContext)
}
}
override fun displayPreference(screen: PreferenceScreen) {
super.displayPreference(screen)
preferenceScreen = screen
}
override fun isChecked(): Boolean = telephonyManager.isMobileDataPolicyEnabled(
TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED
)
override fun setChecked(isChecked: Boolean): Boolean {
telephonyManager.setMobileDataPolicyEnabled(
TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED,
isChecked,
)
return true
}
}